Skip to content

Commit 613f592

Browse files
committed
Improved compatibility with PHPStan 1.0
1 parent 10d556a commit 613f592

File tree

5 files changed

+150
-86
lines changed

5 files changed

+150
-86
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Doctrine\ORM;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
8+
9+
/**
10+
* @extends RuleTestCase<QueryBuilderDqlRule>
11+
*/
12+
class QueryBuilderDqlRuleSlowTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return new QueryBuilderDqlRule(new ObjectMetadataResolver($this->createReflectionProvider(), __DIR__ . '/entity-manager.php', null), true);
18+
}
19+
20+
public function testRule(): void
21+
{
22+
$this->analyse([__DIR__ . '/data/query-builder-dql.php'], [
23+
[
24+
"QueryBuilder: [Syntax Error] line 0, col 66: Error: Expected end of string, got ')'\nDQL: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE e.id = 1)",
25+
31,
26+
],
27+
[
28+
"QueryBuilder: [Syntax Error] line 0, col 68: Error: Expected end of string, got ')'\nDQL: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE e.id = :id)",
29+
43,
30+
],
31+
[
32+
"QueryBuilder: [Syntax Error] line 0, col 68: Error: Expected end of string, got ')'\nDQL: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE e.id = :id)",
33+
55,
34+
],
35+
[
36+
'QueryBuilder: [Semantical Error] line 0, col 60 near \'transient = \': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named transient',
37+
62,
38+
],
39+
[
40+
'QueryBuilder: [Semantical Error] line 0, col 14 near \'Foo e\': Error: Class \'Foo\' is not defined.',
41+
71,
42+
],
43+
[
44+
'Could not analyse QueryBuilder with unknown beginning.',
45+
89,
46+
],
47+
[
48+
'Could not analyse QueryBuilder with dynamic arguments.',
49+
99,
50+
],
51+
[
52+
'QueryBuilder: [Semantical Error] line 0, col 60 near \'transient = \': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named transient',
53+
107,
54+
],
55+
[
56+
"QueryBuilder: [Syntax Error] line 0, col 82: Error: Expected end of string, got ')'\nDQL: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE e.id = 1 ORDER BY e.name) ASC",
57+
129,
58+
],
59+
[
60+
'QueryBuilder: [Semantical Error] line 0, col 78 near \'name ASC\': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named name',
61+
139,
62+
],
63+
[
64+
'QueryBuilder: [Semantical Error] line 0, col 78 near \'name ASC\': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named name',
65+
160,
66+
],
67+
[
68+
'QueryBuilder: [Semantical Error] line 0, col 60 near \'transient = 1\': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named transient',
69+
170,
70+
],
71+
[
72+
'QueryBuilder: [Semantical Error] line 0, col 72 near \'nickname LIKE\': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named nickname',
73+
194,
74+
],
75+
[
76+
'QueryBuilder: [Semantical Error] line 0, col 72 near \'nickname IS \': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named nickname',
77+
206,
78+
],
79+
[
80+
"QueryBuilder: [Syntax Error] line 0, col 80: Error: Expected =, <, <=, <>, >, >=, !=, got ')'\nDQL: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE e.id = 1 OR e.nickname) IS NULL",
81+
218,
82+
],
83+
[
84+
'QueryBuilder: [Semantical Error] line 0, col 60 near \'transient = \': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named transient',
85+
234,
86+
],
87+
[
88+
'QueryBuilder: [Semantical Error] line 0, col 60 near \'nonexistent =\': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named nonexistent',
89+
251,
90+
],
91+
[
92+
"QueryBuilder: [Syntax Error] line 0, col -1: Error: Expected =, <, <=, <>, >, >=, !=, got end of string.\nDQL: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE foo",
93+
281,
94+
],
95+
]);
96+
}
97+
98+
public function testRuleBranches(): void
99+
{
100+
$errors = [
101+
[
102+
'QueryBuilder: [Semantical Error] line 0, col 58 near \'p.id = 1\': Error: \'p\' is not defined.',
103+
31,
104+
],
105+
[
106+
'QueryBuilder: [Semantical Error] line 0, col 58 near \'p.id = 1\': Error: \'p\' is not defined.',
107+
45,
108+
],
109+
[
110+
'QueryBuilder: [Semantical Error] line 0, col 58 near \'p.id = 1\': Error: \'p\' is not defined.',
111+
59,
112+
],
113+
/*[
114+
'QueryBuilder: [Semantical Error] line 0, col 93 near \'t.id = 1\': Error: \'t\' is not defined.',
115+
90,
116+
],*/
117+
[
118+
'QueryBuilder: [Semantical Error] line 0, col 95 near \'foo = 1\': Error: Class PHPStan\Rules\Doctrine\ORM\MyEntity has no field or association named foo',
119+
107,
120+
],
121+
];
122+
$this->analyse([__DIR__ . '/data/query-builder-branches-dql.php'], $errors);
123+
}
124+
125+
public static function getAdditionalConfigFiles(): array
126+
{
127+
return [
128+
__DIR__ . '/../../../../extension.neon',
129+
__DIR__ . '/entity-manager.neon',
130+
__DIR__ . '/slow.neon',
131+
];
132+
}
133+
134+
}

tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,23 @@
22

33
namespace PHPStan\Rules\Doctrine\ORM;
44

5-
use Doctrine\ORM\Query\Expr;
6-
use Doctrine\ORM\Query\Expr\Base;
7-
use Doctrine\ORM\Query\Expr\OrderBy;
8-
use PHPStan\DependencyInjection\Container;
9-
use PHPStan\DependencyInjection\ContainerFactory;
105
use PHPStan\Rules\Rule;
116
use PHPStan\Testing\RuleTestCase;
12-
use PHPStan\Type\Doctrine\ArgumentsProcessor;
137
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
14-
use PHPStan\Type\Doctrine\Query\QueryGetDqlDynamicReturnTypeExtension;
15-
use PHPStan\Type\Doctrine\QueryBuilder\CreateQueryBuilderDynamicReturnTypeExtension;
16-
use PHPStan\Type\Doctrine\QueryBuilder\Expr\ExpressionBuilderDynamicReturnTypeExtension;
17-
use PHPStan\Type\Doctrine\QueryBuilder\Expr\NewExprDynamicReturnTypeExtension;
18-
use PHPStan\Type\Doctrine\QueryBuilder\QueryBuilderGetQueryDynamicReturnTypeExtension;
19-
use PHPStan\Type\Doctrine\QueryBuilder\QueryBuilderMethodDynamicReturnTypeExtension;
20-
use PHPStan\Type\Doctrine\QueryBuilder\QueryBuilderTypeSpecifyingExtension;
218

229
/**
2310
* @extends RuleTestCase<QueryBuilderDqlRule>
2411
*/
2512
class QueryBuilderDqlRuleTest extends RuleTestCase
2613
{
2714

28-
/** @var bool */
29-
private $fasterVersion;
30-
3115
protected function getRule(): Rule
3216
{
3317
return new QueryBuilderDqlRule(new ObjectMetadataResolver($this->createReflectionProvider(), __DIR__ . '/entity-manager.php', null), true);
3418
}
3519

3620
public function testRule(): void
3721
{
38-
$this->fasterVersion = false;
3922
$this->analyse([__DIR__ . '/data/query-builder-dql.php'], [
4023
[
4124
"QueryBuilder: [Syntax Error] line 0, col 66: Error: Expected end of string, got ')'\nDQL: SELECT e FROM PHPStan\Rules\Doctrine\ORM\MyEntity e WHERE e.id = 1)",
@@ -112,21 +95,8 @@ public function testRule(): void
11295
]);
11396
}
11497

115-
/**
116-
* @return bool[][]
117-
*/
118-
public function dataRuleBranches(): array
98+
public function testRuleBranches(): void
11999
{
120-
return [[true], [false]];
121-
}
122-
123-
/**
124-
* @dataProvider dataRuleBranches
125-
* @param bool $fasterVersion
126-
*/
127-
public function testRuleBranches(bool $fasterVersion): void
128-
{
129-
$this->fasterVersion = $fasterVersion;
130100
$errors = [
131101
[
132102
'QueryBuilder: [Semantical Error] line 0, col 58 near \'p.id = 1\': Error: \'p\' is not defined.',
@@ -154,7 +124,6 @@ public function testRuleBranches(bool $fasterVersion): void
154124

155125
public function testBranchingPerformance(): void
156126
{
157-
$this->fasterVersion = true;
158127
$this->analyse([__DIR__ . '/data/query-builder-branches-performance.php'], [
159128
[
160129
'QueryBuilder: [Semantical Error] line 0, col 58 near \'p.id = 1 AND\': Error: \'p\' is not defined.',
@@ -163,53 +132,11 @@ public function testBranchingPerformance(): void
163132
]);
164133
}
165134

166-
/**
167-
* @return \PHPStan\Type\DynamicMethodReturnTypeExtension[]
168-
*/
169-
public function getDynamicMethodReturnTypeExtensions(): array
135+
public static function getAdditionalConfigFiles(): array
170136
{
171-
$objectMetadataResolver = new ObjectMetadataResolver($this->createReflectionProvider(), __DIR__ . '/entity-manager.php', null);
172-
$argumentsProcessor = new ArgumentsProcessor();
173-
174137
return [
175-
new CreateQueryBuilderDynamicReturnTypeExtension(null, $this->fasterVersion),
176-
new QueryBuilderMethodDynamicReturnTypeExtension($this->getContainerWithDoctrineExtensions(), $this->getParser(), null, true),
177-
new QueryBuilderGetQueryDynamicReturnTypeExtension($objectMetadataResolver, $argumentsProcessor, null),
178-
new QueryGetDqlDynamicReturnTypeExtension(),
179-
new ExpressionBuilderDynamicReturnTypeExtension($objectMetadataResolver, $argumentsProcessor),
180-
];
181-
}
182-
183-
private function getContainerWithDoctrineExtensions(): Container
184-
{
185-
$rootDir = __DIR__ . '/../../../../vendor/phpstan/phpstan';
186-
$containerFactory = new ContainerFactory($rootDir);
187-
return $containerFactory->create($rootDir . '/tmp', [
188-
$containerFactory->getConfigDirectory() . '/config.level7.neon',
189138
__DIR__ . '/../../../../extension.neon',
190-
], []);
191-
}
192-
193-
/**
194-
* @return \PHPStan\Type\MethodTypeSpecifyingExtension[]
195-
*/
196-
protected function getMethodTypeSpecifyingExtensions(): array
197-
{
198-
return [
199-
new QueryBuilderTypeSpecifyingExtension(null),
200-
];
201-
}
202-
203-
/**
204-
* @return \PHPStan\Type\DynamicStaticMethodReturnTypeExtension[]
205-
*/
206-
public function getDynamicStaticMethodReturnTypeExtensions(): array
207-
{
208-
$argumentsProcessor = new ArgumentsProcessor();
209-
return [
210-
new NewExprDynamicReturnTypeExtension($argumentsProcessor, OrderBy::class),
211-
new NewExprDynamicReturnTypeExtension($argumentsProcessor, Base::class),
212-
new NewExprDynamicReturnTypeExtension($argumentsProcessor, Expr::class),
139+
__DIR__ . '/entity-manager.neon',
213140
];
214141
}
215142

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
doctrine:
3+
objectManagerLoader: entity-manager.php

tests/Rules/Doctrine/ORM/slow.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
doctrine:
3+
queryBuilderFastAlgorithm: false

tests/Type/Doctrine/Collection/IsEmptyTypeSpecifyingExtensionTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ protected function getRule(): Rule
1515
return new VariableTypeReportingRule();
1616
}
1717

18-
/**
19-
* @return \PHPStan\Type\MethodTypeSpecifyingExtension[]
20-
*/
21-
protected function getMethodTypeSpecifyingExtensions(): array
22-
{
23-
return [
24-
new IsEmptyTypeSpecifyingExtension(),
25-
];
26-
}
27-
2818
public function testExtension(): void
2919
{
3020
$this->analyse([__DIR__ . '/data/collection.php'], [
@@ -55,4 +45,11 @@ public function testExtension(): void
5545
]);
5646
}
5747

48+
public static function getAdditionalConfigFiles(): array
49+
{
50+
return [
51+
__DIR__ . '/../../../../extension.neon',
52+
];
53+
}
54+
5855
}

0 commit comments

Comments
 (0)