Skip to content
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

Added new named constructor to create enum from mixed #135

Merged
merged 1 commit into from
Feb 12, 2021

Conversation

michaelpetri
Copy link
Contributor

The current implementation of Enum requires to pass a valid enum value to class constructor. With this new named constructor we can just pass any value to enum and get an instance or unexpected value exception. In the end it's like the class constructor except for it makes psalm happy.

@michaelpetri michaelpetri force-pushed the feature/add-create-from-mixed branch 2 times, most recently from d33c58e to 9343c3f Compare February 10, 2021 07:38
src/Enum.php Outdated Show resolved Hide resolved
src/Enum.php Outdated Show resolved Hide resolved
src/Enum.php Outdated Show resolved Hide resolved
The current implementation of Enum requires to pass a valid enum value to class constructor. With this new named constructor we can just pass any value to enum and get an instance or unexpected value exception.
Copy link
Member

@mnapoli mnapoli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good! I wasn't so keen on adding yet new methods, but this makes sense and will make this library closer to the future PHP 8.1 enums!

Thanks!

@mnapoli mnapoli merged commit 9fcffe3 into myclabs:master Feb 12, 2021
@michaelpetri michaelpetri deleted the feature/add-create-from-mixed branch February 12, 2021 18:26
* @psalm-pure
* @psalm-assert T $value
*/
public static function assertValidValue($value): void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to not extend the API also with this static method and have it private.
I wanted to change how it behaves here: #136 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@drealecs the use-case for this method is to assert on input values from unknown contexts: the API is relatively minimal, and doesn't need to rely on the optimized structure you worked on in #136.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what you say. Yes, it can be useful as a public API as well to just validate without creating the instances.
It would have help a bit in making sure is_array or array_search are not called in constructor more than once when object is created,
is_array is already called twice when from() is used and I was thinking that should also be optimized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this endpoint can be made completely independent from the ctor in the patch doing the perf optimization, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can.

Ocramius added a commit to Ocramius/php-enum-1 that referenced this pull request Mar 11, 2021
…ound to any template

These functions had a `T` template parameter in them, but this `T` is completely decoupled
from the one defined at class-level (and therefore at `Enum` instance level, rather than
statically):

 * `Enum::from()`
 * `Enum::isValid()`
 * `Enum::assertValidValue()`
 * `Enum::assertValidValueReturningKey()` (internal detail)

In practice, this means that myclabs#135 (Added new named constructor to create enum from mixed)
was not a valid approach to infer types for enumerable values, when performed statically.

A new approach will be attempted, but the current one will likely need to be scrapped for
now.

In practice, `@psalm-assert`, `@psalm-assert-if-true` and `@psalm-return static<T>` had no
effect on these methods, due to a design-level issue that wasn't spotted (by myself either)
at review.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants