diff --git a/stubs/Collections.php b/stubs/Collections.php deleted file mode 100644 index e626e67..0000000 --- a/stubs/Collections.php +++ /dev/null @@ -1,175 +0,0 @@ - - * @template-extends ArrayAccess - */ -interface Collection extends Countable, IteratorAggregate, ArrayAccess -{ - - /** - * @param ?TKey $offset - * @param TValue $value - * @return void - */ - public function offsetSet($offset, $value); - - /** - * @param TValue $element - * @return true - */ - public function add($element); - - /** - * @return void - */ - public function clear(); - - /** - * @param TValue $element - * @return bool - */ - public function contains($element); - - /** - * @return bool - */ - public function isEmpty(); - - /** - * @param TKey $key - * @return TValue|null - */ - public function remove($key); - - /** - * @param TValue $element - * @return bool - */ - public function removeElement($element); - - /** - * @param TKey $key - * @return bool - */ - public function containsKey($key); - - /** - * @param TKey $key - * @return TValue|null - */ - public function get($key); - - /** - * @return TKey[] - */ - public function getKeys(); - - /** - * @return TValue[] - */ - public function getValues(); - - /** - * @param TKey $key - * @param TValue $value - * @return void - */ - public function set($key, $value); - - /** - * @return array - */ - public function toArray(); - - /** - * @return TValue|false - */ - public function first(); - - /** - * @return TValue|false - */ - public function last(); - - /** - * @return TKey|null - */ - public function key(); - - /** - * @return TValue|false - */ - public function current(); - - /** - * @return TValue|false - */ - public function next(); - - /** - * @param Closure(TKey=,TValue=):bool $p - * @return bool - */ - public function exists(Closure $p); - - /** - * @param Closure(TValue=):bool $p - * @return Collection - */ - public function filter(Closure $p); - - /** - * @param Closure(TKey=,TValue=):bool $p - * @return bool - */ - public function forAll(Closure $p); - - /** - * @template T - * @param Closure(TValue=):T $func - * @return Collection - */ - public function map(Closure $func); - - /** - * @param Closure(TValue=):bool $p - * @return array{0:Collection,1:Collection} - */ - public function partition(Closure $p); - - /** - * @param TValue $element - * @return false|TKey - */ - public function indexOf($element); - - /** - * @param TKey $offset - * @param int|null $length - * @return array - */ - public function slice($offset, $length = null); -} - -/** - * @template TKey as array-key - * @template TValue - */ -interface Selectable -{ - /** - * @return Collection - */ - public function matching(Criteria $criteria); -} diff --git a/tests/acceptance/Collections.feature b/tests/acceptance/Collections.feature deleted file mode 100644 index e40e359..0000000 --- a/tests/acceptance/Collections.feature +++ /dev/null @@ -1,758 +0,0 @@ -Feature: Collections - In order to use Doctrine Collections safely - As a Psalm user - I need Psalm to typecheck collections - - Background: - Given I have the following config - """ - - - - - - - - - - """ - And I have the following code preamble - """ - */ - $c = new ArrayCollection; - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidTemplateParam | Extended template param TKey expects type array-key, type object given | - And I see no other errors - - @Collection::instantiation - Scenario: Instantiating a collection with compatible key type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection; - """ - When I run Psalm - Then I see no errors - - - @Collection::add - Scenario: Adding an item of invalid type to the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->add(1); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::add expects string, int% provided | - And I see no other errors - - @Collection::add - Scenario: Adding an item of a valid type to the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->add("d"); - """ - When I run Psalm - Then I see no errors - - @Collection::contains - Scenario: Checking if collection contains an element of invalid type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->contains(1); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::contains expects string, int% provided | - And I see no other errors - - @Collection::contains - Scenario: Checking if collection contains an element of a valid type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->contains("a"); - """ - When I run Psalm - Then I see no errors - - @Collection::remove - Scenario: Removing element with an invalid key type from the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->remove("string key"); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::remove expects int, string% provided | - And I see no other errors - - @Collection::remove - Scenario: Removing element with a valid key type from the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->remove(1)); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of atan expects float, null\|string provided | - And I see no other errors - - @Collection::removeElement - Scenario: Removing element of an invalid type from the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->removeElement(1); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::remove%lement expects string, int% provided | - And I see no other errors - - @Collection::removeElement - Scenario: Removing element of a valid type from the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->removeElement("a")); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of atan expects float, bool provided | - And I see no other errors - - @Collection::containsKey - Scenario: Checking if collection contains a key with invalid key type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->containsKey("string key"); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::contains%ey expects int, string% provided | - And I see no other errors - - @Collection::containsKey - Scenario: Checking if collection contains a key with valid key type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->containsKey(1); - """ - When I run Psalm - Then I see no errors - - @Collection::get - Scenario: Getting an element from the collection using invalid key type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->get("string key"); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::get expects int, string% provided | - And I see no other errors - - @Collection::get - Scenario: Getting an element from the collection using a valid key type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->get(1)); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of atan expects float, null\|string provided | - And I see no other errors - - @Collection::getKeys - Scenario: Getting collection keys from the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->getKeys()); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidArgument | Argument 1 of atan expects float, array<%, int> provided | - And I see no other errors - - @Collection::getValues - Scenario: Getting collection values from the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->getValues()); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidArgument | Argument 1 of atan expects float, array<%, string> provided | - And I see no other errors - - @Collection::set - Scenario: Setting collection entry with invalid key - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->set("string key", "d"); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::set expects int, string% provided | - And I see no other errors - - @Collection::set - Scenario: Setting collection entry with invalid value - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->set(1, 1); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 2 of Doctrine\Common\Collections\Collection::set expects string, int% provided | - And I see no other errors - - @Collection::toArray - Scenario: Getting array of collection contents - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->toArray()); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidArgument | Argument 1 of atan expects float, array provided | - And I see no other errors - - @Collection::first - Scenario: Getting first item of the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->first()); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of atan expects float, false\|string provided | - And I see no other errors - - @Collection::last - Scenario: Getting last item of the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->last()); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of atan expects float, false\|string provided | - And I see no other errors - - @Collection::key - Scenario: Getting current key of the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - strlen($c->key()); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of strlen expects string, null\|int provided | - And I see no other errors - - @Collection::current - Scenario: Getting current value of the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->current()); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of atan expects float, false\|string provided | - And I see no other errors - - @Collection::next - Scenario: Getting next value of the collection - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->next()); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of atan expects float, false\|string provided | - And I see no other errors - - @Collection::exists - Scenario: Invoking exists with a closure accepting wrong type of value - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->exists(function(int $_k, int $_v): bool { return (bool) rand(0,1); }); - """ - 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, Closure(int, int):bool provided | - And I see no other errors - - @Collection::exists - Scenario: Invoking exists with a closure having a wrong return type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->exists(function(int $_k, string $_v): int { return rand(0,1); }); - """ - 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, Closure(int, string):int provided | - And I see no other errors - - @Collection::exists - Scenario: Invoking exists with a closure accepting just keys - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->exists(function(int $_k): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see no errors - - @Collection::exists - Scenario: Invoking exists with a closure accepting nothing - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->exists(function(): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see no errors - - @Collection::filter - Scenario: Invoking filter with a valid closure - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->filter(function(string $_p): bool { return (bool) rand(0,1); })); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidArgument | Argument 1 of atan expects float, Doctrine\Common\Collections\Collection provided | - And I see no other errors - - @Collection::filter - Scenario: Invoking filter with a closure having wrong param type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->filter(function(int $_p): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::filter expects Closure(string=):bool, Closure(int):bool provided | - And I see no other errors - - @Collection::filter - Scenario: Invoking filter with a closure having wrong return type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->filter(function(string $_p): int { return rand(0,1); }); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::filter expects Closure(string=):bool, Closure(string):int provided | - And I see no other errors - - # TODO: find out if this is applicable - # Might need to be fixed upstream as well - # @Collection::filter - # Scenario: Invoking filter with a closure accepting keys - # Given I have the following code - # """ - # /** @var Collection */ - # $c = new ArrayCollection(["a", "b", "c"]); - # $c->filter(function(string $_v, int $_k): bool { return (bool) rand(0,1); }); - # """ - # When I run Psalm - # Then I see no errors - - @Collection::filter - Scenario: Invoking filter with a closure accepting nothing - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->filter(function(): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see no errors - - @Collection::forAll - Scenario: Invoking forAll with a closure having wrong first argument type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->forAll(function(string $_k, string $_v): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::for%ll expects Closure(int=, string=):bool, Closure(string, string):bool provided | - And I see no other errors - - @Collection::forAll - Scenario: Invoking forAll with a closure having wrong second argument type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->forAll(function(int $_k, int $_v): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::for%ll expects Closure(int=, string=):bool, Closure(int, int):bool provided | - And I see no other errors - - @Collection::forAll - Scenario: Invoking forAll with a closure having wrong return type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->forAll(function(int $_k, string $_v): int { return rand(0,1); }); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::for%ll expects Closure(int=, string=):bool, Closure(int, string):int provided | - And I see no other errors - - @Collection::forAll - Scenario: Invoking forAll with a closure accepting just keys - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->forAll(function(int $_k): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see no errors - - @Collection::forAll - Scenario: Invoking forAll with a closure accepting nothing - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->forAll(function(): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see no errors - - @Collection::map - Scenario: Invoking map with a proper closure - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->map(function(string $_p): bool { return (bool) rand(0,1); })); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidArgument | Argument 1 of atan expects float, Doctrine\Common\Collections\Collection provided | - And I see no other errors - - @Collection::map - Scenario: Invoking map with a closure having wrong parameter type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->map(function(int $_p): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::map expects Closure(string=):mixed, Closure(int):bool provided | - And I see no other errors - - @Collection::map - Scenario: Invoking map with a closure accepting nothing - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->map(function(): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see no errors - - @Collection::partition - Scenario: Invoking partition with a proper closure - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->partition(function(string $_p): bool { return (bool) rand(0,1); })); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidArgument | Argument 1 of atan expects float, array{0:%Doctrine\Common\Collections\Collection, 1:%Doctrine\Common\Collections\Collection} provided | - And I see no other errors - - @Collection::partition - Scenario: Invoking partition with a closure having wrong parameter type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->partition(function(int $_p): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::partition expects Closure(string=):bool, Closure(int):bool provided | - And I see no other errors - - @Collection::partition - Scenario: Invoking partition with a closure having wrong return type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->partition(function(string $_p): int { return rand(0,1); }); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::partition expects Closure(string=):bool, Closure(string):int provided | - And I see no other errors - - @Collection::partition - Scenario: Invoking partition with a closure accepting nothing - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->partition(function(): bool { return (bool) rand(0,1); }); - """ - When I run Psalm - Then I see no errors - - @Collection::indexOf - Scenario: Invoking indexOf with a wrong type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->indexOf(1); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::index%f expects string, int% provided | - And I see no other errors - - @Collection::indexOf - Scenario: Invoking indexOf with a proper type - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - strlen($c->indexOf("d")); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of strlen expects string, false\|int provided | - And I see no other errors - - @Collection::slice - Scenario: Invoking slice with a wrong start offset - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->slice("string key"); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::slice expects int, string% provided | - And I see no other errors - - @Collection::slice - Scenario: Invoking slice with a wrong length - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c->slice(1, "zzzz"); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 2 of Doctrine\Common\Collections\Collection::slice expects int\|null, string% provided | - And I see no other errors - - @Collection::slice - Scenario: Invoking slice with correct param types - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - atan($c->slice(1, 1)); - atan($c->slice(1, null)); - atan($c->slice(1 )); - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidArgument | Argument 1 of atan expects float, array provided | - | InvalidArgument | Argument 1 of atan expects float, array provided | - | InvalidArgument | Argument 1 of atan expects float, array provided | - And I see no other errors - - @Collections::ArrayAccess - Scenario: Adding an item to collection like an array - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c[] = "d"; - """ - When I run Psalm - Then I see no errors - - @Collections::ArrayAccess - Scenario: Adding an item to collection with array offset access - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c[10] = "d"; - """ - When I run Psalm - Then I see no errors - - @Collections::ArrayAccess - Scenario: Adding an item of a wrong type with array-like push - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c[] = 1.1; - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 2 of Doctrine\Common\Collections\Collection::offset%et expects string, float% provided | - And I see no other errors - - @Collections::ArrayAccess - Scenario: Adding an item using wrong type with array offset access - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c[10] = 1.1; - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 2 of %::offset%et expects string, float% provided | - And I see no other errors - - @Collections::ArrayAccess - Scenario: Adding an item using wrong key type with array offset access - Given I have the following code - """ - /** @var Collection */ - $c = new ArrayCollection(["a", "b", "c"]); - $c["10"] = "aaa"; - """ - When I run Psalm - Then I see these errors - | Type | Message | - | InvalidScalarArgument | Argument 1 of %::offset%et expects null\|int, string% provided | - And I see no other errors