Skip to content

Commit 1f6560c

Browse files
authored
Merge pull request #321 from garak/improve-code
improve code
2 parents 3a72377 + 98aa98b commit 1f6560c

File tree

25 files changed

+80
-122
lines changed

25 files changed

+80
-122
lines changed

.github/workflows/build.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
name: build
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
tags:
8+
- '!**'
9+
pull_request:
10+
branches:
11+
- '**'
412

513
jobs:
614
phpstan:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"symfony/property-access": "to allow sorting arrays"
5252
},
5353
"conflict": {
54-
"doctrine/dbal": "<2.10"
54+
"doctrine/dbal": "<3.1"
5555
},
5656
"extra": {
5757
"branch-alias": {

src/Knp/Component/Pager/Event/Subscriber/Filtration/Doctrine/ORM/Query/WhereWalker.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function walkSelectStatement(SelectStatement $AST): void
4949
$queriedValue = $query->getHint(self::HINT_PAGINATOR_FILTER_VALUE);
5050
$columns = $query->getHint(self::HINT_PAGINATOR_FILTER_COLUMNS);
5151
$filterCaseInsensitive = $query->getHint(self::HINT_PAGINATOR_FILTER_CASE_INSENSITIVE);
52-
$components = $this->_getQueryComponents();
52+
$components = $this->getQueryComponents();
5353
$filterExpressions = [];
5454
$expressions = [];
5555
foreach ($columns as $column) {
@@ -59,17 +59,17 @@ public function walkSelectStatement(SelectStatement $AST): void
5959
if (2 <= count($parts)) {
6060
$alias = trim(reset($parts));
6161
if (!array_key_exists($alias, $components)) {
62-
throw new InvalidValueException("There is no component aliased by [{$alias}] in the given Query");
62+
throw new InvalidValueException("There is no component aliased by [$alias] in the given Query");
6363
}
6464
$meta = $components[$alias];
6565
if (!$meta['metadata']->hasField($field)) {
66-
throw new InvalidValueException("There is no such field [{$field}] in the given Query component, aliased by [$alias]");
66+
throw new InvalidValueException("There is no such field [$field] in the given Query component, aliased by [$alias]");
6767
}
6868
$pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD, $alias, $field);
6969
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;
7070
} else {
7171
if (!array_key_exists($field, $components)) {
72-
throw new InvalidValueException("There is no component field [{$field}] in the given Query");
72+
throw new InvalidValueException("There is no component field [$field] in the given Query");
7373
}
7474
$pathExpression = $components[$field]['resultVariable'];
7575
}
@@ -160,12 +160,13 @@ public function walkSelectStatement(SelectStatement $AST): void
160160
* @param Node[] $filterExpressions
161161
* @return bool
162162
*/
163-
private function expressionContainsFilter(ConditionalExpression $node, $filterExpressions): bool
163+
private function expressionContainsFilter(ConditionalExpression $node, array $filterExpressions): bool
164164
{
165165
foreach ($node->conditionalTerms as $conditionalTerm) {
166166
if ($conditionalTerm instanceof ConditionalTerm && $this->termContainsFilter($conditionalTerm, $filterExpressions)) {
167167
return true;
168-
} elseif ($conditionalTerm instanceof ConditionalPrimary && $this->primaryContainsFilter($conditionalTerm, $filterExpressions)) {
168+
}
169+
if ($conditionalTerm instanceof ConditionalPrimary && $this->primaryContainsFilter($conditionalTerm, $filterExpressions)) {
169170
return true;
170171
}
171172
}
@@ -178,7 +179,7 @@ private function expressionContainsFilter(ConditionalExpression $node, $filterEx
178179
* @param Node[] $filterExpressions
179180
* @return bool
180181
*/
181-
private function termContainsFilter(ConditionalTerm $node, $filterExpressions): bool
182+
private function termContainsFilter(ConditionalTerm $node, array $filterExpressions): bool
182183
{
183184
foreach ($node->conditionalFactors as $conditionalFactor) {
184185
if ($conditionalFactor instanceof ConditionalFactor) {
@@ -200,7 +201,7 @@ private function termContainsFilter(ConditionalTerm $node, $filterExpressions):
200201
* @param Node[] $filterExpressions
201202
* @return bool
202203
*/
203-
private function factorContainsFilter(ConditionalFactor $node, $filterExpressions): bool
204+
private function factorContainsFilter(ConditionalFactor $node, array $filterExpressions): bool
204205
{
205206
if ($node->conditionalPrimary instanceof ConditionalPrimary && $node->not === false) {
206207
return $this->primaryContainsFilter($node->conditionalPrimary, $filterExpressions);
@@ -214,9 +215,9 @@ private function factorContainsFilter(ConditionalFactor $node, $filterExpression
214215
* @param Node[] $filterExpressions
215216
* @return bool
216217
*/
217-
private function primaryContainsFilter(ConditionalPrimary $node, $filterExpressions): bool
218+
private function primaryContainsFilter(ConditionalPrimary $node, array $filterExpressions): bool
218219
{
219-
if ($node->isSimpleConditionalExpression() && ($node->simpleConditionalExpression instanceof LikeExpression || $node->simpleConditionalExpression instanceof ComparisonExpression)) {
220+
if (($node->simpleConditionalExpression instanceof LikeExpression || $node->simpleConditionalExpression instanceof ComparisonExpression) && $node->isSimpleConditionalExpression()) {
220221
return $this->isExpressionInFilterExpressions($node->simpleConditionalExpression, $filterExpressions);
221222
}
222223
if ($node->isConditionalExpression()) {
@@ -231,7 +232,7 @@ private function primaryContainsFilter(ConditionalPrimary $node, $filterExpressi
231232
* @param Node[] $filterExpressions
232233
* @return bool
233234
*/
234-
private function isExpressionInFilterExpressions(Node $node, $filterExpressions): bool
235+
private function isExpressionInFilterExpressions(Node $node, array $filterExpressions): bool
235236
{
236237
foreach ($filterExpressions as $filterExpression) {
237238
if ((string) $filterExpression === (string) $node) {
@@ -242,11 +243,7 @@ private function isExpressionInFilterExpressions(Node $node, $filterExpressions)
242243
return false;
243244
}
244245

245-
/**
246-
* @param ConditionalPrimary|ConditionalExpression $node
247-
* @return ConditionalPrimary
248-
*/
249-
private function createPrimaryFromNode($node): ConditionalPrimary
246+
private function createPrimaryFromNode(ConditionalPrimary|ConditionalExpression $node): ConditionalPrimary
250247
{
251248
if ($node instanceof ConditionalPrimary) {
252249
$conditionalPrimary = $node;

src/Knp/Component/Pager/Event/Subscriber/Filtration/Doctrine/ORM/QuerySubscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public function items(ItemsEvent $event): void
4747
if (str_contains($value, '*')) {
4848
$value = str_replace('*', '%', $value);
4949
}
50-
if (is_string($columns) && false !== strpos($columns, ',')) {
50+
if (is_string($columns) && str_contains($columns, ',')) {
5151
$columns = explode(',', $columns);
5252
}
5353
$columns = (array) $columns;
5454
if (isset($event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
5555
foreach ($columns as $column) {
5656
if (!in_array($column, $event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
57-
throw new InvalidValueException("Cannot filter by: [{$column}] this field is not in allow list");
57+
throw new InvalidValueException("Cannot filter by: [$column] this field is not in allow list");
5858
}
5959
}
6060
}

src/Knp/Component/Pager/Event/Subscriber/Filtration/PropelQuerySubscriber.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ public function items(ItemsEvent $event): void
3131
} else {
3232
return;
3333
}
34-
if (is_string($columns) && false !== strpos($columns, ',')) {
34+
if (is_string($columns) && str_contains($columns, ',')) {
3535
$columns = explode(',', $columns);
3636
}
3737
$columns = (array) $columns;
3838
if (isset($event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
3939
foreach ($columns as $column) {
4040
if (!in_array($column, $event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
41-
throw new InvalidValueException("Cannot filter by: [{$column}] this field is not in allow list");
41+
throw new InvalidValueException("Cannot filter by: [$column] this field is not in allow list");
4242
}
4343
}
4444
}
4545
$value = $this->argumentAccess->get($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
4646
$criteria = \Criteria::EQUAL;
47-
if (false !== strpos($value, '*')) {
47+
if (str_contains($value, '*')) {
4848
$value = str_replace('*', '%', $value);
4949
$criteria = \Criteria::LIKE;
5050
}
5151
foreach ($columns as $column) {
52-
if (false !== strpos($column, '.')) {
52+
if (str_contains($column, '.')) {
5353
$query->where($column.$criteria.'?', $value);
5454
} else {
5555
$query->{'filterBy'.$column}($value, $criteria);

src/Knp/Component/Pager/Event/Subscriber/Paginate/ArraySubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function items(ItemsEvent $event): void
3232
public static function getSubscribedEvents(): array
3333
{
3434
return [
35-
'knp_pager.items' => ['items', -1/* other data arrays should be analized first*/],
35+
'knp_pager.items' => ['items', -1/* other data arrays should be analyzed first*/],
3636
];
3737
}
3838
}

src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/DBALQueryBuilderSubscriber.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function items(ItemsEvent $event): void
3535
->from('(' . $sql . ')', 'dbal_count_tbl')
3636
;
3737

38-
$compat = $qb->execute();
38+
$compat = $qb->executeQuery();
3939
$event->count = method_exists($compat, 'fetchColumn') ? $compat->fetchColumn(0) : $compat->fetchOne();
4040

4141
// if there is results
@@ -48,8 +48,8 @@ public function items(ItemsEvent $event): void
4848
;
4949

5050
$event->items = $qb
51-
->execute()
52-
->fetchAll()
51+
->executeQuery()
52+
->fetchAllAssociative()
5353
;
5454
}
5555

src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/ODM/MongoDB/QuerySubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function items(ItemsEvent $event): void
1818
}
1919
static $reflectionProperty;
2020
if (is_null($reflectionProperty)) {
21-
$reflectionClass = new \ReflectionClass('Doctrine\ODM\MongoDB\Query\Query');
21+
$reflectionClass = new \ReflectionClass(Query::class);
2222
$reflectionProperty = $reflectionClass->getProperty('query');
2323
$reflectionProperty->setAccessible(true);
2424
}

src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/ORM/QuerySubscriber.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ public function items(ItemsEvent $event): void
2020
}
2121
$event->stopPropagation();
2222

23-
$useOutputWalkers = false;
24-
if (isset($event->options['wrap-queries'])) {
25-
$useOutputWalkers = $event->options['wrap-queries'];
26-
}
23+
$useOutputWalkers = $event->options['wrap-queries'] ?? false;
2724

2825
$event->target
2926
->setFirstResult($event->getOffset())

src/Knp/Component/Pager/Event/Subscriber/Paginate/PropelQuerySubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function items(ItemsEvent $event): void
2222
if ($event->options[PaginatorInterface::DISTINCT]) {
2323
$countQuery->distinct();
2424
}
25-
$event->count = intval($countQuery->count());
25+
$event->count = (int) $countQuery->count();
2626
// process items
2727
$result = null;
2828
if ($event->count) {

0 commit comments

Comments
 (0)