Skip to content

Commit

Permalink
Allow closures to omit parameters
Browse files Browse the repository at this point in the history
Breaking change: minimum Psalm version is now 3.0.15
  • Loading branch information
weirdan committed Mar 13, 2019
1 parent c93c2de commit 8d6a5b5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 52 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"vimeo/psalm": "^3.0.13 || dev-master",
"vimeo/psalm": "^3.0.15 || dev-master",
"doctrine/collections": "^1.0",
"doctrine/orm": "^2.6"
},
Expand Down
10 changes: 5 additions & 5 deletions stubs/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,32 +118,32 @@ public function current();
public function next();

/**
* @param Closure(TKey,TValue):bool $p
* @param Closure(TKey=,TValue=):bool $p
* @return bool
*/
public function exists(Closure $p);

/**
* @param Closure(TValue):bool $p
* @param Closure(TValue=):bool $p
* @return Collection<TKey,TValue>
*/
public function filter(Closure $p);

/**
* @param Closure(TKey,TValue):bool $p
* @param Closure(TKey=,TValue=):bool $p
* @return bool
*/
public function forAll(Closure $p);

/**
* @template T
* @param Closure(TValue):T $func
* @param Closure(TValue=):T $func
* @return Collection<TKey,T>
*/
public function map(Closure $func);

/**
* @param Closure(TValue):bool $p
* @param Closure(TValue=):bool $p
* @return array{0:Collection<TKey,TValue>,1:Collection<TKey,TValue>}
*/
public function partition(Closure $p);
Expand Down
82 changes: 36 additions & 46 deletions tests/acceptance/Collections.feature
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ 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, Closure(int, int):bool provided |
| 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
Expand All @@ -371,14 +371,13 @@ 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, Closure(int, string):int provided |
| 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 some future Psalm that supports this feature "union of closures, see vimeo/psalm#1462"
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -389,8 +388,7 @@ Feature: Collections

@Collection::exists
Scenario: Invoking exists with a closure accepting nothing
Given I have some future Psalm that supports this feature "union of closures, see vimeo/psalm#1462"
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand Down Expand Up @@ -423,8 +421,8 @@ 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, Closure(int):bool provided |
| 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
Expand All @@ -437,8 +435,8 @@ 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, Closure(string):int provided |
| 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

@Collection::filter
Expand All @@ -454,8 +452,7 @@ Feature: Collections

@Collection::filter
Scenario: Invoking filter with a closure accepting nothing
Given I have some future Psalm that supports this feature "union of closures, see vimeo/psalm#1462"
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -474,8 +471,8 @@ 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, Closure(string, string):bool provided |
| Type | Message |
| InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::forAll expects Closure(int=, string=):bool, Closure(string, string):bool provided |
And I see no other errors

@Collection::forAll
Expand All @@ -488,8 +485,8 @@ 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, Closure(int, int):bool provided |
| Type | Message |
| InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::forAll expects Closure(int=, string=):bool, Closure(int, int):bool provided |
And I see no other errors

@Collection::forAll
Expand All @@ -502,14 +499,13 @@ 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, Closure(int, string):int provided |
| Type | Message |
| InvalidScalarArgument | Argument 1 of Doctrine\Common\Collections\Collection::forAll 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 some future Psalm that supports this feature "union of closures, see vimeo/psalm#1462"
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -520,8 +516,7 @@ Feature: Collections

@Collection::forAll
Scenario: Invoking forAll with a closure accepting nothing
Given I have some future Psalm that supports this feature "union of closures, see vimeo/psalm#1462"
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand Down Expand Up @@ -554,14 +549,13 @@ Feature: Collections
"""
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 |
| 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 some future Psalm that supports this feature "union of closures, see vimeo/psalm#1462"
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -572,8 +566,7 @@ Feature: Collections

@Collection::partition
Scenario: Invoking partition with a proper closure
Given I have Psalm newer than "3.0.12" (because of "Psalm bug, see vimeo/psalm#1248")
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -595,8 +588,8 @@ Feature: Collections
"""
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 |
| 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
Expand All @@ -609,14 +602,13 @@ Feature: Collections
"""
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 |
| 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 some future Psalm that supports this feature "union of closures, see vimeo/psalm#1462"
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand Down Expand Up @@ -701,8 +693,7 @@ Feature: Collections

@Collections::ArrayAccess
Scenario: Adding an item to collection like an array
Given I have Psalm newer than "3.0.13" (because of "Psalm bug, see vimeo/psalm#1260")
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -713,8 +704,7 @@ Feature: Collections

@Collections::ArrayAccess
Scenario: Adding an item to collection with array offset access
# This was fine previously, but only because no types were checked in this assignment
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -725,8 +715,7 @@ Feature: Collections

@Collections::ArrayAccess
Scenario: Adding an item of a wrong type with array-like push
Given I have Psalm newer than "3.0.13" (because of "Psalm bug, see vimeo/psalm#1260")
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -736,11 +725,11 @@ Feature: Collections
Then I see these errors
| Type | Message |
| InvalidScalarArgument | Argument 2 of Doctrine\Common\Collections\Collection::offsetSet 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 Psalm newer than "3.0.13" (because of "Psalm bug, see vimeo/psalm#1260")
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -750,11 +739,11 @@ Feature: Collections
Then I see these errors
| Type | Message |
| InvalidScalarArgument | Argument 2 of %::offsetSet 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 Psalm newer than "3.0.13" (because of "Psalm bug, see vimeo/psalm#1260")
And I have the following code
Given I have the following code
"""
/** @var Collection<int,string> */
$c = new ArrayCollection(["a", "b", "c"]);
Expand All @@ -764,3 +753,4 @@ Feature: Collections
Then I see these errors
| Type | Message |
| InvalidScalarArgument | Argument 1 of %::offsetSet expects null\|int, string% provided |
And I see no other errors

0 comments on commit 8d6a5b5

Please sign in to comment.