Skip to content

Commit 179ca9b

Browse files
VincentLangletondrejmirtes
authored andcommitted
Use conditional loading
1 parent 3819054 commit 179ca9b

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

extension.neon

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ parameters:
3838
- stubs/Persistence/ObjectRepository.stub
3939
- stubs/RepositoryFactory.stub
4040
- stubs/Collections/ArrayCollection.stub
41-
- stubs/Collections/Collection.stub
4241
- stubs/Collections/ReadableCollection.stub
4342
- stubs/Collections/Selectable.stub
4443
- stubs/ORM/AbstractQuery.stub

src/Stubs/Doctrine/StubFilesExtensionLoader.php

+9
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace PHPStan\Stubs\Doctrine;
44

5+
use Composer\InstalledVersions;
56
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
67
use PHPStan\BetterReflection\Reflector\Reflector;
78
use PHPStan\PhpDoc\StubFilesExtension;
89
use function dirname;
10+
use function strpos;
911

1012
class StubFilesExtensionLoader implements StubFilesExtension
1113
{
@@ -59,6 +61,13 @@ public function getFiles(): array
5961
$files[] = $stubsDir . '/ServiceEntityRepository.stub';
6062
}
6163

64+
$collectionVersion = InstalledVersions::getVersion('doctrine/dbal');
65+
if ($collectionVersion !== null && strpos($collectionVersion, '1.') === 0) {
66+
$files[] = $stubsDir . '/Collections/Collection1.stub';
67+
} else {
68+
$files[] = $stubsDir . '/Collections/Collection.stub';
69+
}
70+
6271
return $files;
6372
}
6473

stubs/Collections/Collection.stub

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface Collection extends Countable, IteratorAggregate, ArrayAccess, Readable
2121
*
2222
* @param T $element
2323
*
24-
* @return void
24+
* @return true
2525
*/
2626
public function add($element) {}
2727

stubs/Collections/Collection1.stub

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Doctrine\Common\Collections;
4+
5+
use ArrayAccess;
6+
use Countable;
7+
use IteratorAggregate;
8+
9+
/**
10+
* @template TKey of array-key
11+
* @template T
12+
* @extends IteratorAggregate<TKey, T>
13+
* @extends ArrayAccess<TKey, T>
14+
* @extends ReadableCollection<TKey, T>
15+
*/
16+
interface Collection extends Countable, IteratorAggregate, ArrayAccess, ReadableCollection
17+
{
18+
19+
/**
20+
* @phpstan-impure
21+
*
22+
* @param T $element
23+
*
24+
* @return void
25+
*/
26+
public function add($element) {}
27+
28+
/**
29+
* @phpstan-impure
30+
*
31+
* @param TKey $key
32+
*
33+
* @return T|null
34+
*/
35+
public function remove($key) {}
36+
37+
/**
38+
* @phpstan-impure
39+
*
40+
* @param T $element
41+
*
42+
* @return bool
43+
*/
44+
public function removeElement($element) {}
45+
46+
}

0 commit comments

Comments
 (0)