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 type-safe API that allows validating a given value against a given class-string<Enum> #143

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Commits on Mar 11, 2021

  1. Better type tests around instantiation and validation scenarios

    These added tests verify that:
    
     * `Enum::assert*` methods can be used in a pure form
     * `Enum::from*` methods can be used in a pure form
     * `Enum#__construct()` properly binds the instance type of the enumerable with the input value
     * `Enum::assert*` methods currently **CANNOT** bind the input to an enumerable potential value (BUG)
     * `Enum::from*` methods currently **CANNOT** bind the input to an enumerable potential value (BUG)
    
    In addition to that, `psalm.xml` has been made a bit stricter:
    
     * `resolveFromConfigFile` was removed (we were using the default value)
     * `findUnusedPsalmSuppress` was added, allowing us to find whether intentional suppressions are now
        invalid. This allows us to verify negative scenarios, in which we **expect** a type error to appear
     * `restrictReturnTypes` better refines templated types, requiring inputs/outputs to match for those too
    Ocramius committed Mar 11, 2021
    Configuration menu
    Copy the full SHA
    96598da View commit details
    Browse the repository at this point in the history
  2. Removed invalid T template parameter from methods where it is not b…

    …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.
    Ocramius committed Mar 11, 2021
    Configuration menu
    Copy the full SHA
    ec76a45 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    6262a50 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    74b9e22 View commit details
    Browse the repository at this point in the history
  5. Added new type-safe API that allows validating a given value against …

    …a given `class-string<Enum>`
    
    This patch introduces two new methods:
    
     * `Enum::isValidEnumValue()`
     * `Enum::assertValidEnumValue()`
    
    The `Enum::isValidEnumValue()` is still wonky due to vimeo/psalm#5372, but
    overall, this allows for matching against other `Enum` sub-types, by validating that a given `$value`
    is effectively a subtype of `T` within an `Enum<T>`.
    
    The previous approach did **not** work, because static method types are not coupled with the class they
    are put on (they are effectively namespaced functions, and not much else).
    
    In here, we also:
     * completed test coverage for `Enum::from()`
     * pinned `vimeo/psalm:4.6.2`, mostly for build stability
     * added coverage mapping for `phpunit.xml`
    Ocramius committed Mar 11, 2021
    Configuration menu
    Copy the full SHA
    be511e4 View commit details
    Browse the repository at this point in the history