Skip to content

Commit

Permalink
Add psalm-mutation-free annotation (#99)
Browse files Browse the repository at this point in the history
* Add psalm annotation

* Add test
  • Loading branch information
VincentLanglet authored Oct 31, 2021
1 parent a629250 commit b24dcfd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 47 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 1 addition & 8 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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[] */
Expand Down
17 changes: 17 additions & 0 deletions stubs/AbstractLazyCollection.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Doctrine\Common\Collections;

/**
* @template TKey of array-key
* @template T
* @template-implements Collection<TKey,T>
*/
class AbstractLazyCollection implements Collection
{
/**
* @return bool
* @psalm-mutation-free
*/
public function isEmpty();
}
37 changes: 3 additions & 34 deletions stubs/ArrayCollection.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,9 @@ use Closure;
*/
class ArrayCollection implements Collection, Selectable
{
/** @param array<TKey,T> $elements */
public function __construct(array $elements = []) {}

/**
* @param array<TKey,T> $elements
* @return static<TKey,T>
*/
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<TKey, U>
* @return bool
* @psalm-mutation-free
*/
public function map(Closure $func) {}

/** @psalm-return static<TKey,T> */
public function filter(Closure $p) {}

/** @return string */
public function __toString() {}
public function isEmpty();
}
18 changes: 18 additions & 0 deletions stubs/PersistentCollection.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Doctrine\ORM;

/**
* @template TKey of array-key
* @template T
* @template-implements Collection<TKey,T>
* @template-implements Selectable<TKey,T>
*/
class PersistentCollection implements Collection, Selectable
{
/**
* @return bool
* @psalm-mutation-free
*/
public function isEmpty();
}
17 changes: 13 additions & 4 deletions tests/acceptance/Collections.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit b24dcfd

Please sign in to comment.