10000 [TypeInfo] Add `accepts` method by mtarld · Pull Request #59291 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Merged
merged 1 commit into from
Dec 29, 2024
Merged

Conversation

mtarld
Copy link
Contributor
@mtarld mtarld commented Dec 23, 2024
Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

Add Type::accepts($value) method, which returns true whether a given value matches the current type.

@stof
Copy link
Member
stof commented Dec 23, 2024

$type->matches($value) reads exactly the opposite of what the method does (returning whether the value matches the type), so we might need a better name for this method.

@mtarld
Copy link
Contributor Author
mtarld commented Dec 23, 2024

You're effectively right, any ideas for that new name?

@chalasr
Copy link
Member
chalasr commented Dec 23, 2024

matchesTypeOf($value)? satisfiesTypeOf($value)?

@mtarld
Copy link
Contributor Author
mtarld commented Dec 23, 2024

matchesTypeOf looks good to me 👍

@GromNaN
Copy link
Member
GromNaN commented Dec 24, 2024
$type->accepts($value)

This method name exists in PHPUnit to check if values can be compared.

@mtarld
Copy link
Contributor Author
mtarld commented Dec 24, 2024

accept is even better, yes!

@mtarld mtarld force-pushed the feat/type-validate-value branch from 6af57b9 to b41f120 Compare December 24, 2024 08:49
@mtarld mtarld changed the title [TypeInfo] Add matches method [TypeInfo] Add accept method Dec 24, 2024
@stof
Copy link
Member
stof commented Dec 24, 2024

I would name it accepts, so that it reads $type->accepts($value)

@GromNaN GromNaN changed the title [TypeInfo] Add accept method [TypeInfo] Add accepts method Dec 24, 2024
@mtarld mtarld force-pushed the feat/type-validate-value branch 2 times, most recently from a9eef69 to d94f045 Compare December 24, 2024 10:51

$i = 0;

foreach ($value as $k => $v) {
Copy link
Member

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 
    { /* … */ }
}

Copy link
Contributor Author

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?

@mtarld mtarld force-pushed the feat/type-validate-value branch from d94f045 to 547cb46 Compare December 24, 2024 16:03
$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));
Copy link
Member

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.

@mtarld mtarld force-pushed the feat/type-validate-value branch from 1f29214 to 92352f5 Compare December 28, 2024 08:42
@fabpot
Copy link
Member
fabpot commented Dec 29, 2024

Thank you @mtarld.

@fabpot fabpot merged commit 4078cb1 into symfony:7.3 Dec 29, 2024
11 checks passed
@mtarld mtarld deleted the feat/type-validate-value branch December 29, 2024 14:48
@xabbuh xabbuh mentioned this pull request Jan 9, 2025
fabpot added a commit that referenced this pull request Jan 25, 2025
…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
@fabpot fabpot mentioned this pull request May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0