Skip to content

Commit 8bdd7fd

Browse files
vokuclxmstaab
andauthored
SyntaxErrorInQueryMethodRule: allow abstract classes in "classMethods" (#234)
* SyntaxErrorInQueryMethodRule: allow abstract classes in "classMethods" * fix * added test * fix * fix tests * re-record * cs * fix Co-authored-by: Markus Staab <[email protected]>
1 parent cea4713 commit 8bdd7fd

10 files changed

+167
-28
lines changed

.phpstan-dba.cache

Lines changed: 20 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Rules/SyntaxErrorInPreparedStatementMethodRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function processNode(Node $callLike, Scope $scope): array
6969
foreach ($this->classMethods as $classMethod) {
7070
sscanf($classMethod, '%[^::]::%s', $className, $methodName);
7171

72-
if ($methodName === $methodReflection->getName() && $className === $methodReflection->getDeclaringClass()->getName()) {
72+
if ($methodName === $methodReflection->getName() &&
73+
($methodReflection->getDeclaringClass()->getName() === $className || $methodReflection->getDeclaringClass()->isSubclassOf($className))) {
7374
$unsupportedMethod = false;
7475
break;
7576
}

src/Rules/SyntaxErrorInQueryMethodRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public function processNode(Node $node, Scope $scope): array
5454
foreach ($this->classMethods as $classMethod) {
5555
sscanf($classMethod, '%[^::]::%[^#]#%s', $className, $methodName, $queryArgPosition);
5656

57-
if ($methodName === $methodReflection->getName() && $className === $methodReflection->getDeclaringClass()->getName()) {
57+
if ($methodName === $methodReflection->getName() &&
58+
($methodReflection->getDeclaringClass()->getName() === $className || $methodReflection->getDeclaringClass()->isSubclassOf($className))) {
5859
$unsupportedMethod = false;
5960
break;
6061
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace staabm\PHPStanDba\Tests\Fixture;
4+
5+
class BaseQueryClass
6+
{
7+
public function doQuery(string $query, array $params = [])
8+
{
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace staabm\PHPStanDba\Tests\Fixture;
4+
5+
class MySubClass extends BaseQueryClass
6+
{
7+
// class inherits query method
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace staabm\PHPStanDba\Tests;
4+
5+
use PHPStan\Rules\Rule;
6+
use staabm\PHPStanDba\Rules\SyntaxErrorInPreparedStatementMethodRule;
7+
use Symplify\PHPStanExtensions\Testing\AbstractServiceAwareRuleTestCase;
8+
9+
/**
10+
* @extends AbstractServiceAwareRuleTestCase<SyntaxErrorInPreparedStatementMethodRule>
11+
*/
12+
class SyntaxErrorInPreparedStatementMethodSubclassedRuleTest extends AbstractServiceAwareRuleTestCase
13+
{
14+
protected function getRule(): Rule
15+
{
16+
return $this->getRuleFromConfig(SyntaxErrorInPreparedStatementMethodRule::class, __DIR__.'/config/subclassed-method-rule.neon');
17+
}
18+
19+
public function testSyntaxErrorInQueryRule(): void
20+
{
21+
require_once __DIR__.'/data/syntax-error-in-method-subclassed.php';
22+
23+
$this->analyse([__DIR__.'/data/syntax-error-in-method-subclassed.php'], [
24+
[
25+
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'with syntax error GROUPY by x LIMIT 0' at line 1 (1064).",
26+
12,
27+
],
28+
[
29+
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
30+
18,
31+
],
32+
[
33+
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'FROM ada LIMIT 0' at line 3 (1064).",
34+
20,
35+
],
36+
]);
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace staabm\PHPStanDba\Tests;
4+
5+
use PHPStan\Rules\Rule;
6+
use staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule;
7+
use Symplify\PHPStanExtensions\Testing\AbstractServiceAwareRuleTestCase;
8+
9+
/**
10+
* @extends AbstractServiceAwareRuleTestCase<SyntaxErrorInQueryMethodRule>
11+
*/
12+
class SyntaxErrorInQueryMethodSubclassedRuleTest extends AbstractServiceAwareRuleTestCase
13+
{
14+
protected function getRule(): Rule
15+
{
16+
return $this->getRuleFromConfig(SyntaxErrorInQueryMethodRule::class, __DIR__.'/config/subclassed-method-rule.neon');
17+
}
18+
19+
public function testSyntaxErrorInQueryRule(): void
20+
{
21+
require_once __DIR__.'/data/syntax-error-in-method-subclassed.php';
22+
23+
$this->analyse([__DIR__.'/data/syntax-error-in-method-subclassed.php'], [
24+
[
25+
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'with syntax error GROUPY by x LIMIT 0' at line 1 (1064).",
26+
12,
27+
],
28+
[
29+
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
30+
18,
31+
],
32+
]);
33+
}
34+
}

tests/default/config/.phpunit-phpstan-dba.cache

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
includes:
2+
- ../../../config/stubFiles.neon
3+
- ../../../config/extensions.neon
4+
5+
services:
6+
-
7+
class: staabm\PHPStanDba\Rules\SyntaxErrorInPreparedStatementMethodRule
8+
tags: [phpstan.rules.rule]
9+
arguments:
10+
classMethods:
11+
- 'staabm\PHPStanDba\Tests\Fixture\BaseQueryClass::doQuery'
12+
13+
-
14+
class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule
15+
tags: [phpstan.rules.rule]
16+
arguments:
17+
classMethods:
18+
- 'staabm\PHPStanDba\Tests\Fixture\BaseQueryClass::doQuery#0'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace SyntaxErrorInPreparedStatementMethodSubclassedRuleTest;
4+
5+
use staabm\PHPStanDba\Tests\Fixture\MySubClass;
6+
7+
class HelloWorld
8+
{
9+
public function syntaxError()
10+
{
11+
$sub = new MySubClass();
12+
$sub->doQuery('SELECT with syntax error GROUPY by x');
13+
}
14+
15+
public function preparedError(int $i)
16+
{
17+
$sub = new MySubClass();
18+
$sub->doQuery('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', []);
19+
20+
$sub->doQuery('
21+
SELECT email adaid
22+
WHERE gesperrt = ? AND email LIKE ?
23+
FROM ada
24+
LIMIT 1
25+
', [$i, '%@example.com']);
26+
}
27+
}

0 commit comments

Comments
 (0)