-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TypeInfo] Add accepts
method
#59291
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
Conversation
|
You're effectively right, any ideas for that new name? |
|
|
$type->accepts($value) This method name exists in PHPUnit to check if values can be compared. |
|
6af57b9
to
b41f120
Compare
I would name it |
a9eef69
to
d94f045
Compare
|
||
$i = 0; | ||
|
||
foreach ($value as $k => $v) { |
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.
The value will be iterated. Is it possible to analyze whether $value
is an instance of a class with generic type declaration and check type compatibility?
/** @template-implements ArrayAccess<string, string> */
class StringCollection extends ArrayAccess
{
public function offsetGet(string $key): string
{ /* … */ }
}
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.
Unfortunately, I have dig and I wasn't able to find any way to get all keys/values of an ArrayAccess
class (but maybe you have a way in mind?).
This however leads us to a question. How do we behave in that case? Should we accept or reject the value?
d94f045
to
547cb46
Compare
976274d
to
1f29214
Compare
$this->assertFalse((new BuiltinType(TypeIdentifier::RESOURCE))->accepts('string')); | ||
$this->assertTrue((new BuiltinType(TypeIdentifier::RESOURCE))->accepts(fopen('php://temp', 'r'))); | ||
|
||
$this->assertFalse((new BuiltinType(TypeIdentifier::STRING))->accepts(123)); |
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.
Noted we are strict and don't apply type casts.
1f29214
to
92352f5
Compare
Thank you @mtarld. |
…as array (mtarld) This PR was merged into the 7.3 branch. Discussion ---------- [TypeInfo] Deprecate `CollectionType` as list and not as array | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | no | Deprecations? | yes | Issues | | License | MIT As mentioned in #59291 (comment), creating a list with a type that is not an `array` is not possible. The `CollectionType` should reflect that, that's why this PR deprecates the creation of such types. Commits ------- 50ad14d [TypeInfo] Deprecate creation of `CollectionType` as list and not as array
Add
Type::accepts($value)
method, which returnstrue
whether a given value matches the current type.