File tree 4 files changed +58
-4
lines changed
src/Type/Doctrine/Collection
tests/DoctrineIntegration
4 files changed +58
-4
lines changed Original file line number Diff line number Diff line change @@ -370,6 +370,15 @@ services:
370
370
# Doctrine Collection
371
371
-
372
372
class : PHPStan\Type\Doctrine\Collection\IsEmptyTypeSpecifyingExtension
373
+ arguments :
374
+ collectionClass : Doctrine\Common\Collections\Collection
375
+ tags :
376
+ - phpstan.typeSpecifier.methodTypeSpecifyingExtension
377
+
378
+ -
379
+ class : PHPStan\Type\Doctrine\Collection\IsEmptyTypeSpecifyingExtension
380
+ arguments :
381
+ collectionClass : Doctrine\Common\Collections\ReadableCollection
373
382
tags :
374
383
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
375
384
Original file line number Diff line number Diff line change 15
15
final class IsEmptyTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
16
16
{
17
17
18
- private const COLLECTION_CLASS = 'Doctrine\Common\Collections\Collection ' ;
19
18
private const IS_EMPTY_METHOD_NAME = 'isEmpty ' ;
20
19
private const FIRST_METHOD_NAME = 'first ' ;
21
20
private const LAST_METHOD_NAME = 'last ' ;
22
21
23
22
/** @var TypeSpecifier */
24
23
private $ typeSpecifier ;
25
24
25
+ /** @var class-string */
26
+ private $ collectionClass ;
27
+
28
+ /**
29
+ * @param class-string $collectionClass
30
+ */
31
+ public function __construct (string $ collectionClass )
32
+ {
33
+ $ this ->collectionClass = $ collectionClass ;
34
+ }
35
+
26
36
public function getClass (): string
27
37
{
28
- return self :: COLLECTION_CLASS ;
38
+ return $ this -> collectionClass ;
29
39
}
30
40
31
41
public function isMethodSupported (
@@ -35,8 +45,8 @@ public function isMethodSupported(
35
45
): bool
36
46
{
37
47
return (
38
- $ methodReflection ->getDeclaringClass ()->getName () === self :: COLLECTION_CLASS
39
- || $ methodReflection ->getDeclaringClass ()->isSubclassOf (self :: COLLECTION_CLASS )
48
+ $ methodReflection ->getDeclaringClass ()->getName () === $ this -> collectionClass
49
+ || $ methodReflection ->getDeclaringClass ()->isSubclassOf ($ this -> collectionClass )
40
50
)
41
51
&& $ methodReflection ->getName () === self ::IS_EMPTY_METHOD_NAME ;
42
52
}
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ class TypeInferenceTest extends TypeInferenceTestCase
13
13
public function dataFileAsserts (): iterable
14
14
{
15
15
yield from $ this ->gatherAssertTypes (__DIR__ . '/data/getRepository.php ' );
16
+ yield from $ this ->gatherAssertTypes (__DIR__ . '/data/isEmpty.php ' );
16
17
}
17
18
18
19
/**
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Bug375 ;
4
+
5
+ use Doctrine \Common \Collections \ArrayCollection ;
6
+ use Doctrine \Common \Collections \Collection ;
7
+ use function PHPStan \Testing \assertType ;
8
+
9
+ class Foo
10
+ {
11
+
12
+ /** @var Collection<int, Bar> */
13
+ private Collection $ shippingOptions ;
14
+
15
+ public function __construct ()
16
+ {
17
+ $ this ->shippingOptions = new ArrayCollection ();
18
+ }
19
+
20
+ public function doFoo (): void
21
+ {
22
+ if ($ this ->shippingOptions ->isEmpty ()) {
23
+ return ;
24
+ }
25
+
26
+ assertType (Bar::class, $ this ->shippingOptions ->first ());
27
+ }
28
+
29
+ }
30
+
31
+ class Bar
32
+ {
33
+
34
+ }
You can’t perform that action at this time.
0 commit comments