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 3 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
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check that InstalledVersions class exists.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, we should even catch OutOfBoundsException in case dbal is not installed: https://getcomposer.org/doc/07-runtime.md#knowing-the-version-of-package-x

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh damn, and the check should be about 'doctrine/collections' not 'doctrine/dbal'.

I fixed it and added a class_exists check and the try/catch of OutOfBoundsException.

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) {}

}