From 52212f0ed1ff81938729079c8b3702b7f28d24ed Mon Sep 17 00:00:00 2001 From: Alexander Kim Date: Wed, 30 Nov 2022 15:36:41 -0500 Subject: [PATCH 1/9] feat: add support for psalm 5 This is only a change to `composer.json` to see if CI passes. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dbfbb10..807ee4a 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "require": { "php": "^7.2 || ^8", "composer/semver": "^1.4 || ^2.0 || ^3.0", - "vimeo/psalm": "^4.28" + "vimeo/psalm": "^4.28|^5.0" }, "conflict": { "doctrine/collections": "<1.8", From 69dba006167db7a4eaa0163daf2cfaf4a1a6432c Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 30 Nov 2022 18:48:36 -0400 Subject: [PATCH 2/9] Update the test for Psalm 5 --- tests/acceptance/QueryBuilder.feature | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/acceptance/QueryBuilder.feature b/tests/acceptance/QueryBuilder.feature index 44a06f6..c66d717 100644 --- a/tests/acceptance/QueryBuilder.feature +++ b/tests/acceptance/QueryBuilder.feature @@ -97,16 +97,30 @@ Feature: QueryBuilder | ImplicitToStringCast | Argument 1 of Doctrine\ORM\QueryBuilder::select expects Doctrine\ORM\Query\Expr\Func\|array\|null\|string, but Doctrine\ORM\Query\Expr\Andx provided with a __toString method | @QueryBuilder - Scenario: QueryBuilder::select() rejects wrong non-stringable arguments + Scenario: QueryBuilder::select() rejects wrong non-stringable arguments [Psalm 4] Given I have the following code """ builder()->select(2.2)->distinct(); """ + And I have Psalm older than "5.0.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\ORM\QueryBuilder::select expects Doctrine\ORM\Query\Expr\Func\|array\|null\|string, but float(2.2) provided | + @QueryBuilder + Scenario: QueryBuilder::select() rejects wrong non-stringable arguments [Psalm 5] + Given I have the following code + """ + builder()->select(2.2)->distinct(); + """ + And I have Psalm newer than "4.99.0" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\ORM\QueryBuilder::select expects Doctrine\ORM\Query\Expr\Func\|array\|null\|string, but float(2.2) provided | + + @QueryBuilder Scenario: QueryBuilder ::where(), ::orWhere() and ::andWhere accept Expr\Comparison Given I have the following code From b33a102675d66a02686d3476cc85e64ae793c733 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 30 Nov 2022 18:49:04 -0400 Subject: [PATCH 3/9] Allow plugin --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 807ee4a..f3584bd 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,8 @@ "config": { "sort-packages": true, "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true + "dealerdirect/phpcodesniffer-composer-installer": true, + "composer/package-versions-deprecated": true } }, "extra": { From 756a918c5888ec7ce041ce52fe2addebd9244fd0 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 30 Nov 2022 18:49:39 -0400 Subject: [PATCH 4/9] Fix wrong assertion --- tests/acceptance/Collections.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/acceptance/Collections.feature b/tests/acceptance/Collections.feature index 02a38fe..62a9c98 100644 --- a/tests/acceptance/Collections.feature +++ b/tests/acceptance/Collections.feature @@ -863,4 +863,4 @@ Feature: Collections class MyCollection extends ArrayCollection {} """ When I run Psalm - Then I see no error + Then I see no errors From a019b7ccd37a7dbb814d9f9d2f7a0026172a443e Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 30 Nov 2022 19:34:27 -0400 Subject: [PATCH 5/9] Update Collections tests for Psalm 5 --- tests/acceptance/Collections.feature | 350 +++++++++++++++++++++++++-- 1 file changed, 327 insertions(+), 23 deletions(-) diff --git a/tests/acceptance/Collections.feature b/tests/acceptance/Collections.feature index 62a9c98..0fc3255 100644 --- a/tests/acceptance/Collections.feature +++ b/tests/acceptance/Collections.feature @@ -41,19 +41,36 @@ Feature: Collections @Collection::add - Scenario: Adding an item of invalid type to the collection + Scenario: Adding an item of invalid type to the collection [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->add(1); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::add expects string, but 1 provided | And I see no other errors + @Collection::add + Scenario: Adding an item of invalid type to the collection [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->add(1); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::add expects string, but 1 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 @@ -66,19 +83,35 @@ Feature: Collections Then I see no errors @Collection::contains - Scenario: Checking if collection contains an element of invalid type + Scenario: Checking if collection contains an element of invalid type [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->contains(1); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::contains expects string, but 1 provided | And I see no other errors + @Collection::contains + Scenario: Checking if collection contains an element of invalid type [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->contains(1); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::contains expects string, but 1 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 @@ -91,19 +124,35 @@ Feature: Collections Then I see no errors @Collection::remove - Scenario: Removing element with an invalid key type from the collection + Scenario: Removing element with an invalid key type from the collection [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->remove("string key"); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::remove expects int, but "string key" provided | And I see no other errors + @Collection::remove + Scenario: Removing element with an invalid key type from the collection [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->remove("string key"); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::remove expects int, but 'string key' 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 @@ -119,19 +168,35 @@ Feature: Collections And I see no other errors @Collection::removeElement - Scenario: Removing element of an invalid type from the collection + Scenario: Removing element of an invalid type from the collection [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->removeElement(1); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::removeElement expects string, but 1 provided | And I see no other errors + @Collection::removeElement + Scenario: Removing element of an invalid type from the collection [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->removeElement(1); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::removeElement expects string, but 1 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 @@ -147,19 +212,35 @@ Feature: Collections And I see no other errors @Collection::containsKey - Scenario: Checking if collection contains a key with invalid key type + Scenario: Checking if collection contains a key with invalid key type [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->containsKey("string key"); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::containsKey expects int, but "string key" provided | And I see no other errors + @Collection::containsKey + Scenario: Checking if collection contains a key with invalid key type [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->containsKey("string key"); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::containsKey expects int, but 'string key' 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 @@ -172,19 +253,35 @@ Feature: Collections Then I see no errors @Collection::get - Scenario: Getting an element from the collection using invalid key type + Scenario: Getting an element from the collection using invalid key type [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->get("string key"); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::get expects int, but "string key" provided | And I see no other errors + @Collection::get + Scenario: Getting an element from the collection using invalid key type [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->get("string key"); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::get expects int, but 'string key' 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 @@ -209,7 +306,7 @@ Feature: Collections """ When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | Argument 1 of atan expects float, but list provided | And I see no other errors @@ -223,18 +320,19 @@ Feature: Collections """ When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | Argument 1 of atan expects float, but list provided | And I see no other errors @Collection::set - Scenario: Setting collection entry with invalid key + Scenario: Setting collection entry with invalid key [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->set("string key", "d"); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | @@ -242,19 +340,50 @@ Feature: Collections And I see no other errors @Collection::set - Scenario: Setting collection entry with invalid value + Scenario: Setting collection entry with invalid key [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->set("string key", "d"); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::set expects int, but 'string key' provided | + And I see no other errors + + @Collection::set + Scenario: Setting collection entry with invalid value [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->set(1, 1); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 2 of Doctrine\Common\Collections\Collection::set expects string, but 1 provided | And I see no other errors + @Collection::set + Scenario: Setting collection entry with invalid value [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->set(1, 1); + """ + And I have Psalm older than "5.0" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 2 of Doctrine\Common\Collections\Collection::set expects string, but 1 provided | + And I see no other errors + @Collection::toArray Scenario: Getting array of collection contents Given I have the following code @@ -450,19 +579,35 @@ Feature: Collections And I see no other errors @Collection::exists - Scenario: Invoking exists with a closure having a wrong return type + Scenario: Invoking exists with a closure having a wrong return type [Psalm 4] 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); }); """ + And I have Psalm older than "5.0" (because of "changed issue type") 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, but (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | And I see no other errors + @Collection::exists + Scenario: Invoking exists with a closure having a wrong return type [Psalm 5] + 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); }); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::exists expects Closure\(int=, string=\):bool, but (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | + And I see no other errors + @Collection::exists Scenario: Invoking exists with a closure accepting just keys Given I have the following code @@ -514,19 +659,35 @@ Feature: Collections And I see no other errors @Collection::filter - Scenario: Invoking filter with a closure having wrong return type + Scenario: Invoking filter with a closure having wrong return type [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->filter(function(string $_p): int { return rand(0,1); }); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::filter expects Closure\(string=\):bool, but (impure-)?Closure\(string\):int(<0, 1>)? provided/ | And I see no other errors + @Collection::filter + Scenario: Invoking filter with a closure having wrong return type [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->filter(function(string $_p): int { return rand(0,1); }); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::filter expects Closure\(string=\):bool, but (impure-)?Closure\(string\):int(<0, 1>)? provided/ | + And I see no other errors + # TODO: find out if this is applicable # Might need to be fixed upstream as well # @Collection::filter @@ -580,19 +741,35 @@ Feature: Collections And I see no other errors @Collection::forAll - Scenario: Invoking forAll with a closure having wrong return type + Scenario: Invoking forAll with a closure having wrong return type [Psalm 4] 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); }); """ + And I have Psalm older than "5.0" (because of "changed issue type") 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, but (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | And I see no other errors + @Collection::forAll + Scenario: Invoking forAll with a closure having wrong return type [Psalm 5] + 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); }); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::forAll expects Closure\(int=, string=\):bool, but (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | + And I see no other errors + @Collection::forAll Scenario: Invoking forAll with a closure accepting just keys Given I have the following code @@ -683,19 +860,35 @@ Feature: Collections And I see no other errors @Collection::partition - Scenario: Invoking partition with a closure having wrong return type + Scenario: Invoking partition with a closure having wrong return type [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->partition(function(int $_p): int { return rand(0,1); }); """ + And I have Psalm older than "5.0" (because of "changed issue type") 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, but (impure-)?Closure\(int\):int(<0, 1>)? provided/ | And I see no other errors + @Collection::partition + Scenario: Invoking partition with a closure having wrong return type [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->partition(function(int $_p): int { return rand(0,1); }); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::partition expects Closure\(int=, string=\):bool, but (impure-)?Closure\(int\):int(<0, 1>)? provided/ | + And I see no other errors + @Collection::partition Scenario: Invoking partition with a closure accepting nothing Given I have the following code @@ -708,19 +901,36 @@ Feature: Collections Then I see no errors @Collection::indexOf - Scenario: Invoking indexOf with a wrong type + Scenario: Invoking indexOf with a wrong type [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->indexOf(1); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::indexOf expects string, but 1 provided | And I see no other errors + @Collection::indexOf + Scenario: Invoking indexOf with a wrong type [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->indexOf(1); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::indexOf expects string, but 1 provided | + And I see no other errors + + @Collection::indexOf Scenario: Invoking indexOf with a proper type Given I have the following code @@ -736,13 +946,14 @@ Feature: Collections And I see no other errors @Collection::slice - Scenario: Invoking slice with a wrong start offset + Scenario: Invoking slice with a wrong start offset [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->slice("string key"); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | @@ -750,19 +961,50 @@ Feature: Collections And I see no other errors @Collection::slice - Scenario: Invoking slice with a wrong length + Scenario: Invoking slice with a wrong start offset [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->slice("string key"); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::slice expects int, but 'string key' provided | + And I see no other errors + + @Collection::slice + Scenario: Invoking slice with a wrong length [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c->slice(1, "zzzz"); """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 2 of Doctrine\Common\Collections\Collection::slice expects int\|null, but "zzzz" provided | And I see no other errors + @Collection::slice + Scenario: Invoking slice with a wrong length [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c->slice(1, "zzzz"); + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 2 of Doctrine\Common\Collections\Collection::slice expects int\|null, but 'zzzz' provided | + And I see no other errors + @Collection::slice Scenario: Invoking slice with correct param types Given I have the following code @@ -815,13 +1057,14 @@ Feature: Collections Then I see no errors @Collections::ArrayAccess - Scenario: Adding an item of a wrong type with array-like push + Scenario: Adding an item of a wrong type with array-like push [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c[] = 1.1; """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | @@ -829,13 +1072,30 @@ Feature: Collections And I see no other errors @Collections::ArrayAccess - Scenario: Adding an item using wrong type with array offset access + Scenario: Adding an item of a wrong type with array-like push [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c[] = 1.1; + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 2 of Doctrine\Common\Collections\Collection::offsetSet expects string, but float(1.1) provided | + And I see no other errors + + + @Collections::ArrayAccess + Scenario: Adding an item using wrong type with array offset access [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c[10] = 1.1; """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | @@ -843,24 +1103,68 @@ Feature: Collections And I see no other errors @Collections::ArrayAccess - Scenario: Adding an item using wrong key type with array offset access + Scenario: Adding an item using wrong type with array offset access [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c[10] = 1.1; + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 2 of Doctrine\Common\Collections\Collection::offsetSet expects string, but float(1.1) provided | + And I see no other errors + + @Collections::ArrayAccess + Scenario: Adding an item using wrong key type with array offset access [Psalm 4] Given I have the following code """ /** @var Collection */ $c = new ArrayCollection(["a", "b", "c"]); $c["10"] = "aaa"; """ + And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::offsetSet expects int\|null, but "10" provided | And I see no other errors + @Collections::ArrayAccess + Scenario: Adding an item using wrong key type with array offset access [Psalm 5] + Given I have the following code + """ + /** @var Collection */ + $c = new ArrayCollection(["a", "b", "c"]); + $c["10"] = "aaa"; + """ + And I have Psalm newer than "4.99" (because of "changed issue type") + When I run Psalm + Then I see these errors + | Type | Message | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::offsetGet expects int\|null, but '10' provided | + | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::offsetSet expects int\|null, but '10' provided | + And I see no other errors + + @Collections::ArrayCollection + Scenario: Extending ArrayCollection gives no error [Psalm 4] + Given I have the following code + """ + class MyCollection extends ArrayCollection {} + """ + And I have Psalm older than "5.0" (because of "template parameter requirements") + When I run Psalm + Then I see no errors + @Collections::ArrayCollection - Scenario: Extending ArrayCollection gives no error + Scenario: Extending ArrayCollection gives no error [Psalm 5] Given I have the following code """ + /** @template-extends ArrayCollection */ class MyCollection extends ArrayCollection {} """ + And I have Psalm newer than "4.99" (because of "template parameter requirements") When I run Psalm Then I see no errors From ff203bce1ed2fbd00bba747061b8c2917486c31d Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 30 Nov 2022 19:56:52 -0400 Subject: [PATCH 6/9] Align table markers --- tests/acceptance/Collections.feature | 56 ++++++++++++++-------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/tests/acceptance/Collections.feature b/tests/acceptance/Collections.feature index 0fc3255..08f7e20 100644 --- a/tests/acceptance/Collections.feature +++ b/tests/acceptance/Collections.feature @@ -51,7 +51,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::add expects string, but 1 provided | And I see no other errors @@ -66,7 +66,7 @@ Feature: Collections And I have Psalm newer than "4.99" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::add expects string, but 1 provided | And I see no other errors @@ -93,7 +93,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::contains expects string, but 1 provided | And I see no other errors @@ -108,7 +108,7 @@ Feature: Collections And I have Psalm newer than "4.99" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::contains expects string, but 1 provided | And I see no other errors @@ -134,7 +134,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::remove expects int, but "string key" provided | And I see no other errors @@ -149,7 +149,7 @@ Feature: Collections And I have Psalm newer than "4.99" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::remove expects int, but 'string key' provided | And I see no other errors @@ -178,7 +178,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::removeElement expects string, but 1 provided | And I see no other errors @@ -193,7 +193,7 @@ Feature: Collections And I have Psalm newer than "4.99" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | Argument 1 of Doctrine\Common\Collections\Collection::removeElement expects string, but 1 provided | And I see no other errors @@ -335,7 +335,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::set expects int, but "string key" provided | And I see no other errors @@ -365,7 +365,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 2 of Doctrine\Common\Collections\Collection::set expects string, but 1 provided | And I see no other errors @@ -424,7 +424,7 @@ Feature: Collections """ When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of atan expects float, but string provided | And I see no other errors @@ -440,7 +440,7 @@ Feature: Collections """ When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of atan expects float, but string provided | And I see no other errors @@ -457,7 +457,7 @@ Feature: Collections """ When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of atan expects float, but string provided | And I see no other errors @@ -472,7 +472,7 @@ Feature: Collections """ When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of atan expects float, but string provided | And I see no other errors @@ -502,7 +502,7 @@ Feature: Collections """ When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of atan expects float, but string provided | And I see no other errors @@ -518,7 +518,7 @@ Feature: Collections """ When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of atan expects float, but string provided | And I see no other errors @@ -589,7 +589,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::exists expects Closure\(int=, string=\):bool, but (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | And I see no other errors @@ -604,7 +604,7 @@ Feature: Collections And I have Psalm newer than "4.99" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::exists expects Closure\(int=, string=\):bool, but (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | And I see no other errors @@ -669,7 +669,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::filter expects Closure\(string=\):bool, but (impure-)?Closure\(string\):int(<0, 1>)? provided/ | And I see no other errors @@ -684,7 +684,7 @@ Feature: Collections And I have Psalm newer than "4.99" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::filter expects Closure\(string=\):bool, but (impure-)?Closure\(string\):int(<0, 1>)? provided/ | And I see no other errors @@ -751,7 +751,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::forAll expects Closure\(int=, string=\):bool, but (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | And I see no other errors @@ -766,7 +766,7 @@ Feature: Collections And I have Psalm newer than "4.99" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::forAll expects Closure\(int=, string=\):bool, but (impure-)?Closure\(int, string\):int(<0, 1>)? provided/ | And I see no other errors @@ -870,7 +870,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::partition expects Closure\(int=, string=\):bool, but (impure-)?Closure\(int\):int(<0, 1>)? provided/ | And I see no other errors @@ -885,7 +885,7 @@ Feature: Collections And I have Psalm newer than "4.99" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidArgument | /Argument 1 of Doctrine\\Common\\Collections\\Collection::partition expects Closure\(int=, string=\):bool, but (impure-)?Closure\(int\):int(<0, 1>)? provided/ | And I see no other errors @@ -911,7 +911,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::indexOf expects string, but 1 provided | And I see no other errors @@ -956,7 +956,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::slice expects int, but "string key" provided | And I see no other errors @@ -986,7 +986,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 2 of Doctrine\Common\Collections\Collection::slice expects int\|null, but "zzzz" provided | And I see no other errors @@ -1128,7 +1128,7 @@ Feature: Collections And I have Psalm older than "5.0" (because of "changed issue type") When I run Psalm Then I see these errors - | Type | Message | + | Type | Message | | InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::offsetSet expects int\|null, but "10" provided | And I see no other errors From fe3859b1eee6389287b4eb21de70156554f31b7d Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 30 Nov 2022 19:57:06 -0400 Subject: [PATCH 7/9] Use type_params instead of `getChildNodes()` Psalm 5 dropped `getChildNodes()` --- src/Provider/ReturnTypeProvider/CollectionFirstAndLast.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Provider/ReturnTypeProvider/CollectionFirstAndLast.php b/src/Provider/ReturnTypeProvider/CollectionFirstAndLast.php index 0e556e3..eb00d79 100644 --- a/src/Provider/ReturnTypeProvider/CollectionFirstAndLast.php +++ b/src/Provider/ReturnTypeProvider/CollectionFirstAndLast.php @@ -93,7 +93,7 @@ public static function getMethodReturnType(MethodReturnTypeProviderEvent $event) return null; } - $childNode = $type->getChildNodes(); + $childNode = $type->type_params; if (! isset($childNode[1]) || ! $childNode[1] instanceof Type\Union) { return null; } From feadcb2d6f890cb08db8e7fc937a55b0522fe397 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 30 Nov 2022 20:05:01 -0400 Subject: [PATCH 8/9] Fixed typo in the version guard --- tests/acceptance/Collections.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/acceptance/Collections.feature b/tests/acceptance/Collections.feature index 08f7e20..e95526b 100644 --- a/tests/acceptance/Collections.feature +++ b/tests/acceptance/Collections.feature @@ -377,7 +377,7 @@ Feature: Collections $c = new ArrayCollection(["a", "b", "c"]); $c->set(1, 1); """ - And I have Psalm older than "5.0" (because of "changed issue type") + And I have Psalm newer than "4.99" (because of "changed issue type") When I run Psalm Then I see these errors | Type | Message | From c8e301ecd17d94987bb05255444e85c0a7d9e64a Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 30 Nov 2022 21:02:26 -0400 Subject: [PATCH 9/9] Ignore irrelevant CS issues --- phpcs.xml.dist | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 1b4a7a0..8256af4 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -12,6 +12,7 @@ stubs tests */tests/_run/* + */tests/_support/_generated/* @@ -30,6 +31,8 @@ + +