File tree 4 files changed +46
-16
lines changed
src/Type/Doctrine/Collection
tests/Type/Doctrine/Collection
4 files changed +46
-16
lines changed Original file line number Diff line number Diff line change @@ -313,6 +313,6 @@ services:
313
313
314
314
# Doctrine Collection
315
315
-
316
- class : PHPStan\Type\Doctrine\Collection\FirstTypeSpecifyingExtension
316
+ class : PHPStan\Type\Doctrine\Collection\IsEmptyTypeSpecifyingExtension
317
317
tags :
318
318
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
Original file line number Diff line number Diff line change 12
12
use PHPStan \Type \Constant \ConstantBooleanType ;
13
13
use PHPStan \Type \MethodTypeSpecifyingExtension ;
14
14
15
- final class FirstTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
15
+ final class IsEmptyTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
16
16
{
17
17
18
18
private const COLLECTION_CLASS = 'Doctrine\Common\Collections\Collection ' ;
19
19
private const IS_EMPTY_METHOD_NAME = 'isEmpty ' ;
20
20
private const FIRST_METHOD_NAME = 'first ' ;
21
+ private const LAST_METHOD_NAME = 'last ' ;
21
22
22
23
/** @var TypeSpecifier */
23
24
private $ typeSpecifier ;
@@ -47,11 +48,19 @@ public function specifyTypes(
47
48
TypeSpecifierContext $ context
48
49
): SpecifiedTypes
49
50
{
50
- return $ this ->typeSpecifier ->create (
51
+ $ first = $ this ->typeSpecifier ->create (
51
52
new MethodCall ($ node ->var , self ::FIRST_METHOD_NAME ),
52
53
new ConstantBooleanType (false ),
53
54
$ context
54
55
);
56
+
57
+ $ last = $ this ->typeSpecifier ->create (
58
+ new MethodCall ($ node ->var , self ::LAST_METHOD_NAME ),
59
+ new ConstantBooleanType (false ),
60
+ $ context
61
+ );
62
+
63
+ return $ first ->unionWith ($ last );
55
64
}
56
65
57
66
public function setTypeSpecifier (TypeSpecifier $ typeSpecifier ): void
Original file line number Diff line number Diff line change 7
7
/**
8
8
* @extends \PHPStan\Testing\RuleTestCase<VariableTypeReportingRule>
9
9
*/
10
- class FirstTypeSpecifyingExtensionTest extends \PHPStan \Testing \RuleTestCase
10
+ class IsEmptyTypeSpecifyingExtensionTest extends \PHPStan \Testing \RuleTestCase
11
11
{
12
12
13
13
protected function getRule (): Rule
@@ -21,24 +21,36 @@ protected function getRule(): Rule
21
21
protected function getMethodTypeSpecifyingExtensions (): array
22
22
{
23
23
return [
24
- new FirstTypeSpecifyingExtension (),
24
+ new IsEmptyTypeSpecifyingExtension (),
25
25
];
26
26
}
27
27
28
28
public function testExtension (): void
29
29
{
30
30
$ this ->analyse ([__DIR__ . '/data/collection.php ' ], [
31
31
[
32
- 'Variable $entityOrFalse is: MyEntity|false ' ,
32
+ 'Variable $entityOrFalse1 is: MyEntity|false ' ,
33
33
18 ,
34
34
],
35
35
[
36
- 'Variable $false is: false ' ,
37
- 22 ,
36
+ 'Variable $entityOrFalse2 is: MyEntity| false ' ,
37
+ 21 ,
38
38
],
39
39
[
40
- 'Variable $entity is: MyEntity ' ,
41
- 27 ,
40
+ 'Variable $false1 is: false ' ,
41
+ 25 ,
42
+ ],
43
+ [
44
+ 'Variable $false2 is: false ' ,
45
+ 28 ,
46
+ ],
47
+ [
48
+ 'Variable $entity1 is: MyEntity ' ,
49
+ 33 ,
50
+ ],
51
+ [
52
+ 'Variable $entity2 is: MyEntity ' ,
53
+ 36 ,
42
54
],
43
55
]);
44
56
}
Original file line number Diff line number Diff line change @@ -14,15 +14,24 @@ class MyEntity
14
14
*/
15
15
$ collection = new ArrayCollection ();
16
16
17
- $ entityOrFalse = $ collection ->first ();
18
- $ entityOrFalse ;
17
+ $ entityOrFalse1 = $ collection ->first ();
18
+ $ entityOrFalse1 ;
19
+
20
+ $ entityOrFalse2 = $ collection ->last ();
21
+ $ entityOrFalse2 ;
19
22
20
23
if ($ collection ->isEmpty ()) {
21
- $ false = $ collection ->first ();
22
- $ false ;
24
+ $ false1 = $ collection ->first ();
25
+ $ false1 ;
26
+
27
+ $ false2 = $ collection ->last ();
28
+ $ false2 ;
23
29
}
24
30
25
31
if (!$ collection ->isEmpty ()) {
26
- $ entity = $ collection ->first ();
27
- $ entity ;
32
+ $ entity1 = $ collection ->first ();
33
+ $ entity1 ;
34
+
35
+ $ entity2 = $ collection ->last ();
36
+ $ entity2 ;
28
37
}
You can’t perform that action at this time.
0 commit comments