From b24dcfd4815eed489800e97c0b605932800aabec Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 31 Oct 2021 18:59:51 +0100 Subject: [PATCH] Add psalm-mutation-free annotation (#99) * Add psalm annotation * Add test --- composer.json | 2 +- src/Plugin.php | 9 +------ stubs/AbstractLazyCollection.phpstub | 17 +++++++++++++ stubs/ArrayCollection.phpstub | 37 +++------------------------- stubs/PersistentCollection.phpstub | 18 ++++++++++++++ tests/acceptance/Collections.feature | 17 ++++++++++--- 6 files changed, 53 insertions(+), 47 deletions(-) create mode 100644 stubs/AbstractLazyCollection.phpstub create mode 100644 stubs/PersistentCollection.phpstub diff --git a/composer.json b/composer.json index 0126ff8..0912e86 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "vimeo/psalm": "^4.9 || dev-master" }, "conflict": { - "doctrine/collections": "<1.0", + "doctrine/collections": "<1.6", "doctrine/orm": "<2.6" }, "require-dev": { diff --git a/src/Plugin.php b/src/Plugin.php index dcd0a2a..23c2ed1 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -11,7 +11,6 @@ use Weirdan\DoctrinePsalmPlugin\Provider\ReturnTypeProvider\CollectionFirstAndLast; use function array_merge; -use function array_search; use function class_exists; use function explode; use function glob; @@ -35,16 +34,10 @@ public function __invoke(RegistrationInterface $psalm, ?SimpleXMLElement $config /** @return string[] */ private function getStubFiles(): array { - $files = array_merge( + return array_merge( glob(__DIR__ . '/../stubs/*.phpstub') ?: [], glob(__DIR__ . '/../stubs/DBAL/*.phpstub') ?: [] ); - - if ($this->hasPackageOfVersion('doctrine/collections', '>= 1.6.0')) { - unset($files[array_search(__DIR__ . '/../stubs/ArrayCollection.phpstub', $files, true)]); - } - - return $files; } /** @return string[] */ diff --git a/stubs/AbstractLazyCollection.phpstub b/stubs/AbstractLazyCollection.phpstub new file mode 100644 index 0000000..486836e --- /dev/null +++ b/stubs/AbstractLazyCollection.phpstub @@ -0,0 +1,17 @@ + + */ +class AbstractLazyCollection implements Collection +{ + /** + * @return bool + * @psalm-mutation-free + */ + public function isEmpty(); +} diff --git a/stubs/ArrayCollection.phpstub b/stubs/ArrayCollection.phpstub index 6e85c66..50f2649 100644 --- a/stubs/ArrayCollection.phpstub +++ b/stubs/ArrayCollection.phpstub @@ -12,40 +12,9 @@ use Closure; */ class ArrayCollection implements Collection, Selectable { - /** @param array $elements */ - public function __construct(array $elements = []) {} - - /** - * @param array $elements - * @return static - */ - protected function createFrom(array $elements) {} - - /** @param TKey $offset */ - public function offsetExists($offset) {} - - /** @param TKey $offset */ - public function offsetGet($offset) {} - - /** @param TKey $offset */ - public function offsetUnset($offset) {} - - /** - * @param TKey|null $offset - * @param T $value - */ - public function offsetSet($offset, $value) {} - /** - * @template U - * @param Closure(T=):U $func - * @return static + * @return bool + * @psalm-mutation-free */ - public function map(Closure $func) {} - - /** @psalm-return static */ - public function filter(Closure $p) {} - - /** @return string */ - public function __toString() {} + public function isEmpty(); } diff --git a/stubs/PersistentCollection.phpstub b/stubs/PersistentCollection.phpstub new file mode 100644 index 0000000..e512a6f --- /dev/null +++ b/stubs/PersistentCollection.phpstub @@ -0,0 +1,18 @@ + + * @template-implements Selectable + */ +class PersistentCollection implements Collection, Selectable +{ + /** + * @return bool + * @psalm-mutation-free + */ + public function isEmpty(); +} diff --git a/tests/acceptance/Collections.feature b/tests/acceptance/Collections.feature index 853a35b..f91ad9d 100644 --- a/tests/acceptance/Collections.feature +++ b/tests/acceptance/Collections.feature @@ -395,7 +395,7 @@ Feature: Collections When I run Psalm Then I see these errors | Type | Message | - | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::exists expects Closure\(int=, string=\):bool, (impure-)?Closure\(int, string\):int provided/ | + | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::exists expects Closure\(int=, string=\):bool, (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | And I see no other errors @Collection::exists @@ -459,7 +459,7 @@ Feature: Collections When I run Psalm Then I see these errors | Type | Message | - | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::filter expects Closure\(string=\):bool, (impure-)?Closure\(string\):int provided/ | + | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::filter expects Closure\(string=\):bool, (impure-)?Closure\(string\):int(<0, 1>)? provided/ | And I see no other errors # TODO: find out if this is applicable @@ -525,7 +525,7 @@ Feature: Collections When I run Psalm Then I see these errors | Type | Message | - | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::forAll expects Closure\(int=, string=\):bool, (impure-)?Closure\(int, string\):int provided/ | + | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::forAll expects Closure\(int=, string=\):bool, (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | And I see no other errors @Collection::forAll @@ -628,7 +628,7 @@ Feature: Collections When I run Psalm Then I see these errors | Type | Message | - | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::partition expects Closure\(int=, string=\):bool, (impure-)?Closure\(int\):int provided/ | + | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::partition expects Closure\(int=, string=\):bool, (impure-)?Closure\(int\):int(<0, 1>)? provided/ | And I see no other errors @Collection::partition @@ -790,3 +790,12 @@ Feature: Collections | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::offsetSet expects int\|null, "10" provided | And I see no other errors + + @Collections::ArrayCollection + Scenario: Extending ArrayCollection gives no error + Given I have the following code + """ + class MyCollection extends ArrayCollection {} + """ + When I run Psalm + Then I see no error