-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iterable pseudo-type #1941
Merged
Merged
Iterable pseudo-type #1941
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
72692e9
Add iterable pseudo-type
trowski 0e221cb
Add is_iterable() function
trowski 340a000
Add iterable to zend_get_type_by_const()
trowski bf6a659
Fix default argument check
trowski 8146c47
Fix error message
trowski bea9df5
Add iterable tests
trowski 4da3e77
Covariance on inheriting classes with iterable
trowski c2266c3
Fix generator test; add covariance tests
trowski cf0290c
Fix abort too early
trowski 277d89c
Limit covariance to array and Traversable
trowski 0dc772c
Add iterable type to opcache
trowski 583386d
Swap type check order
trowski d9a9cf8
Merge branch 'master' into iterable
trowski File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--TEST-- | ||
iterable type#001 | ||
--FILE-- | ||
<?php | ||
|
||
function test(iterable $iterable) { | ||
var_dump($iterable); | ||
} | ||
|
||
function gen() { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
}; | ||
|
||
test([1, 2, 3]); | ||
test(gen()); | ||
test(new ArrayIterator([1, 2, 3])); | ||
|
||
try { | ||
test(1); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(); | ||
} | ||
|
||
--EXPECTF-- | ||
array(3) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(2) | ||
[2]=> | ||
int(3) | ||
} | ||
object(Generator)#1 (0) { | ||
} | ||
object(ArrayIterator)#1 (1) { | ||
["storage":"ArrayIterator":private]=> | ||
array(3) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(2) | ||
[2]=> | ||
int(3) | ||
} | ||
} | ||
Argument 1 passed to test() must be iterable, integer given, called in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
iterable type#002 - Default values | ||
--FILE-- | ||
<?php | ||
|
||
function foo(iterable $iterable = null) { | ||
// Null should be allowed as a default value | ||
} | ||
|
||
function bar(iterable $iterable = []) { | ||
// Array should be allowed as a default value | ||
} | ||
|
||
function baz(iterable $iterable = 1) { | ||
// No other values should be allowed as defaults | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
|
||
Fatal error: Default value for parameters with iterable type can only be an array or NULL in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--TEST-- | ||
iterable type#003 - Return types | ||
--FILE-- | ||
<?php | ||
|
||
function foo(): iterable { | ||
return []; | ||
} | ||
function bar(): iterable { | ||
return (function () { yield; })(); | ||
} | ||
|
||
function baz(): iterable { | ||
return 1; | ||
} | ||
|
||
var_dump(foo()); | ||
var_dump(bar()); | ||
|
||
try { | ||
baz(); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
array(0) { | ||
} | ||
object(Generator)#2 (0) { | ||
} | ||
Return value of baz() must be iterable, integer returned |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
iterable type#004 - Parameter covariance | ||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
function testArray(array $array) {} | ||
|
||
function testTraversable(Traversable $traversable) {} | ||
|
||
function testScalar(int $int) {} | ||
} | ||
|
||
class Bar extends Foo { | ||
function testArray(iterable $iterable) {} | ||
|
||
function testTraversable(iterable $iterable) {} | ||
|
||
function testScalar(iterable $iterable) {} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
|
||
Warning: Declaration of Bar::testScalar(iterable $iterable) should be compatible with Foo::testScalar(int $int) in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--TEST-- | ||
iterable type#005 - Return type covariance | ||
--FILE-- | ||
<?php | ||
|
||
class Test { | ||
function method(): iterable { | ||
return []; | ||
} | ||
} | ||
|
||
class TestArray extends Test { | ||
function method(): array { | ||
return []; | ||
} | ||
} | ||
|
||
class TestTraversable extends Test { | ||
function method(): Traversable { | ||
return new ArrayIterator([]); | ||
} | ||
} | ||
|
||
class TestScalar extends Test { | ||
function method(): int { | ||
return 1; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
|
||
Fatal error: Declaration of TestScalar::method(): int must be compatible with Test::method(): iterable in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need these special compatibility rules? why not just check for IS_ITERABLE?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allows a degree of co/contravariance in extending/implementing classes. Parameter types of
array
orTraversable
can be broadened toiterable
or return types declaringiterable
can be restricted to eitherarray
orTraversable
.