Skip to content

Commit

Permalink
QueryBuilderDqlRule: mention DQL for BranchingQueryBuilderType
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed May 30, 2024
1 parent f6081cf commit 6a25c9d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Rules/Doctrine/ORM/QueryBuilderDqlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleSlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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',
],
]);
}
Expand Down

0 comments on commit 6a25c9d

Please sign in to comment.