Skip to content

Commit

Permalink
Use conditional loading
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored and ondrejmirtes committed Apr 17, 2024
1 parent 3819054 commit 179ca9b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
1 change: 0 additions & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ parameters:
- stubs/Persistence/ObjectRepository.stub
- stubs/RepositoryFactory.stub
- stubs/Collections/ArrayCollection.stub
- stubs/Collections/Collection.stub
- stubs/Collections/ReadableCollection.stub
- stubs/Collections/Selectable.stub
- stubs/ORM/AbstractQuery.stub
Expand Down
9 changes: 9 additions & 0 deletions src/Stubs/Doctrine/StubFilesExtensionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace PHPStan\Stubs\Doctrine;

use Composer\InstalledVersions;
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\PhpDoc\StubFilesExtension;
use function dirname;
use function strpos;

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

$collectionVersion = InstalledVersions::getVersion('doctrine/dbal');
if ($collectionVersion !== null && strpos($collectionVersion, '1.') === 0) {
$files[] = $stubsDir . '/Collections/Collection1.stub';
} else {
$files[] = $stubsDir . '/Collections/Collection.stub';
}

return $files;
}

Expand Down
2 changes: 1 addition & 1 deletion stubs/Collections/Collection.stub
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface Collection extends Countable, IteratorAggregate, ArrayAccess, Readable
*
* @param T $element
*
* @return void
* @return true
*/
public function add($element) {}

Expand Down
46 changes: 46 additions & 0 deletions stubs/Collections/Collection1.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Doctrine\Common\Collections;

use ArrayAccess;
use Countable;
use IteratorAggregate;

/**
* @template TKey of array-key
* @template T
* @extends IteratorAggregate<TKey, T>
* @extends ArrayAccess<TKey, T>
* @extends ReadableCollection<TKey, T>
*/
interface Collection extends Countable, IteratorAggregate, ArrayAccess, ReadableCollection
{

/**
* @phpstan-impure
*
* @param T $element
*
* @return void
*/
public function add($element) {}

/**
* @phpstan-impure
*
* @param TKey $key
*
* @return T|null
*/
public function remove($key) {}

/**
* @phpstan-impure
*
* @param T $element
*
* @return bool
*/
public function removeElement($element) {}

}

0 comments on commit 179ca9b

Please sign in to comment.