Skip to content

Commit ea714d2

Browse files
committed
feat: add NoDALFilterByID rule with tests
1 parent 1f137a0 commit ea714d2

31 files changed

Lines changed: 171 additions & 10 deletions

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
->setRules([
66
'@PER-CS2.0' => true,
77
'@PER-CS2.0:risky' => true,
8+
'declare_strict_types' => true,
89
])
910
->setFinder(PhpCsFixer\Finder::create()
1011
->in(__DIR__)

src/Rule/ClassExtendUsesAbstractClassWhenExisting.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Shopware\PhpStan\Rule;
46

57
use PhpParser\Node;

src/Rule/DisallowDefaultContextCreation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Shopware\PhpStan\Rule;
46

57
use PhpParser\Node;

src/Rule/ForbidGlobBraceRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Shopware\PhpStan\Rule;
46

57
use PhpParser\Node;

src/Rule/InternalClassExtendsRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Shopware\PhpStan\Rule;
46

57
use PhpParser\Node;

src/Rule/MethodBecomesAbstractRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Shopware\PhpStan\Rule;
46

57
use PhpParser\Node;

src/Rule/NoDALFilterByID.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\PhpStan\Rule;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\New_;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Rules\Rule;
11+
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
12+
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
13+
use PHPStan\Rules\RuleErrorBuilder;
14+
15+
/**
16+
* @implements Rule<New_>
17+
*/
18+
class NoDALFilterByID implements Rule
19+
{
20+
public function getNodeType(): string
21+
{
22+
return New_::class;
23+
}
24+
25+
public function processNode(Node $node, Scope $scope): array
26+
{
27+
if (!$node->class instanceof Node\Name) {
28+
return [];
29+
}
30+
31+
$className = $node->class->toString();
32+
33+
if (!in_array($className, [EqualsFilter::class, EqualsAnyFilter::class], true)) {
34+
return [];
35+
}
36+
37+
if (empty($node->args)) {
38+
return [];
39+
}
40+
41+
if (!$node->args[0] instanceof Node\Arg) {
42+
return [];
43+
}
44+
45+
$firstArg = $node->args[0]->value;
46+
47+
if (!$firstArg instanceof Node\Scalar\String_) {
48+
return [];
49+
}
50+
51+
if (strtolower($firstArg->value) === 'id') {
52+
return [
53+
RuleErrorBuilder::message('Using "id" directly in EqualsFilter or EqualsAnyFilter is forbidden. Pass the ids directly to the constructor of Criteria or use setIds instead')
54+
->line($node->getLine())
55+
->identifier('shopware.dal.filterById')
56+
->build(),
57+
];
58+
}
59+
60+
return [];
61+
}
62+
}

src/Rule/NoUserEntityGetStoreTokenRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Shopware\PhpStan\Rule;
46

57
use PhpParser\Node;

src/Rule/ScheduledTaskTooLowIntervalRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Shopware\PhpStan\Rule;
46

57
use PhpParser\Node;

src/Rule/SetForeignKeyRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Shopware\PhpStan\Rule;
46

57
use PhpParser\Node;

0 commit comments

Comments
 (0)