Skip to content

Synchronize Collection::add with doctrine/collections #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions src/Stubs/Doctrine/StubFilesExtensionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace PHPStan\Stubs\Doctrine;

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

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

try {
$collectionVersion = class_exists(InstalledVersions::class)
? InstalledVersions::getVersion('doctrine/collections')
: null;
} catch (OutOfBoundsException $e) {
$collectionVersion = null;
}
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 true
* @return void
*/
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 true
*/
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) {}

}
Loading