From 6a25c9d1be3ac547b36e89fb029013017e8a4347 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Thu, 30 May 2024 16:30:06 +0200 Subject: [PATCH] QueryBuilderDqlRule: mention DQL for BranchingQueryBuilderType --- src/Rules/Doctrine/ORM/QueryBuilderDqlRule.php | 11 ++++++++--- .../Doctrine/ORM/QueryBuilderDqlRuleSlowTest.php | 4 ++++ tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Rules/Doctrine/ORM/QueryBuilderDqlRule.php b/src/Rules/Doctrine/ORM/QueryBuilderDqlRule.php index 0cc99224..8596810d 100644 --- a/src/Rules/Doctrine/ORM/QueryBuilderDqlRule.php +++ b/src/Rules/Doctrine/ORM/QueryBuilderDqlRule.php @@ -118,10 +118,15 @@ public function processNode(Node $node, Scope $scope): array $message .= sprintf("\nDQL: %s", $dql->getValue()); } + $builder = RuleErrorBuilder::message($message) + ->identifier('doctrine.dql'); + + if (count($dqls) > 1) { + $builder->addTip('Detected from DQL branch: ' . $dql->getValue()); + } + // Use message as index to prevent duplicate - $messages[$message] = RuleErrorBuilder::message($message) - ->identifier('doctrine.dql') - ->build(); + $messages[$message] = $builder->build(); } catch (AssertionError $e) { continue; } diff --git a/tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleSlowTest.php b/tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleSlowTest.php index 4c59b7de..adad87ee 100644 --- a/tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleSlowTest.php +++ b/tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleSlowTest.php @@ -112,18 +112,22 @@ public function testRuleBranches(): void [ 'QueryBuilder: [Semantical Error] line 0, col 58 near \'p.id = 1\': Error: \'p\' is not defined.', 59, + 'Detected from DQL branch: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE p.id = 1', ], [ 'QueryBuilder: [Semantical Error] line 0, col 93 near \'t.id = 1\': Error: \'t\' is not defined.', 90, + 'Detected from DQL branch: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e INNER JOIN e.parent p WHERE p.id = 1 AND t.id = 1', ], [ 'QueryBuilder: [Semantical Error] line 0, col 95 near \'foo = 1\': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named foo', 107, + 'Detected from DQL branch: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e INNER JOIN e.parent p WHERE p.id = 1 AND e.foo = 1', ], [ 'QueryBuilder: [Semantical Error] line 0, col 93 near \'t.id = 1\': Error: \'t\' is not defined.', 107, + 'Detected from DQL branch: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e INNER JOIN e.parent p WHERE p.id = 1 AND t.id = 1', ], ]; $this->analyse([__DIR__ . '/data/query-builder-branches-dql.php'], $errors); diff --git a/tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php b/tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php index 7b04762e..ec3dafb7 100644 --- a/tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php +++ b/tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php @@ -113,18 +113,22 @@ public function testRuleBranches(): void [ 'QueryBuilder: [Semantical Error] line 0, col 58 near \'p.id = 1\': Error: \'p\' is not defined.', 59, + 'Detected from DQL branch: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE p.id = 1', ], [ 'QueryBuilder: [Semantical Error] line 0, col 93 near \'t.id = 1\': Error: \'t\' is not defined.', 90, + 'Detected from DQL branch: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e INNER JOIN e.parent p WHERE p.id = 1 AND t.id = 1', ], [ 'QueryBuilder: [Semantical Error] line 0, col 95 near \'foo = 1\': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named foo', 107, + 'Detected from DQL branch: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e INNER JOIN e.parent p WHERE p.id = 1 AND e.foo = 1', ], [ 'QueryBuilder: [Semantical Error] line 0, col 93 near \'t.id = 1\': Error: \'t\' is not defined.', 107, + 'Detected from DQL branch: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e INNER JOIN e.parent p WHERE p.id = 1 AND t.id = 1', ], ]; $this->analyse([__DIR__ . '/data/query-builder-branches-dql.php'], $errors); @@ -151,6 +155,7 @@ public function testBranchingPerformance(): void [ 'QueryBuilder: [Semantical Error] line 0, col 58 near \'p.id = 1 AND\': Error: \'p\' is not defined.', 121, + 'Detected from DQL branch: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE p.id = 1 AND p.id = 3 AND p.id = 5 AND p.id = 7 AND p.id = 9 AND p.id = 11 AND p.id = 13 AND p.id = 15 AND p.id = 15 AND p.id = 15 AND p.id = 15 AND p.id = 15 AND p.id = 15 AND p.id = 15 AND p.id = 15 AND p.id = 15 AND p.id = 15 AND p.id = 15', ], ]); }