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

Handle calling magic instantiation methods from within instance methods of the Enum #147

Merged
merged 3 commits into from
Jun 7, 2020
Merged

Handle calling magic instantiation methods from within instance methods of the Enum #147

merged 3 commits into from
Jun 7, 2020

Conversation

spawnia
Copy link
Collaborator

@spawnia spawnia commented May 28, 2020

  • Added or updated tests
  • [ ] Added or updated the README.md
  • Detailed changes in the CHANGELOG.md unreleased section

Related Issue/Intent

Here is a actual code we use that did not work as expected:

use BenSampo\Enum\Enum;

/**
 * @method static Workgroup ROUTINE()
 * @method static Workgroup MLL5K()
 * @method static Workgroup MLL5Kv2()
 */
final class Workgroup extends Enum
{
    protected const ROUTINE = 'Routine';
    protected const MLL5K = 'MLL5K';
    protected const MLL5Kv2 = 'MLL5Kv2';

    /**
     * @return array<self>
     */
    public function allPrioritized(): array
    {
        switch ($this->value) {
            case self::MLL5K:
                return [
                    self::MLL5K(),
                    self::ROUTINE(),
                    self::MLL5Kv2(),
                ];
            case self::MLL5Kv2:
                return [
                    self::MLL5Kv2(),
                    self::ROUTINE(),
                    self::MLL5K(),
                ];
            default:
                return [
                    self::ROUTINE(),
                    self::MLL5K(),
                    self::MLL5Kv2(),
                ];
        }
    }
}

Calling $workgroup->allPrioritized() fails with an error message like this:

BadMethodCallException: Method App\Enums\Workgroup::ROUTINE does not exist.

Even though the call to self::ROUTINE() looks like a static method call, PHP actually treats it as dynamic and calls __call rather than __callStatic.

Changes

Allow such magic instantiation calls from within an enum.

While it is not typical to use the magic instantiation dynamically, it may happen
incidentally when calling the instantiation in an instance method of itself.
Even when using the static::KEY() syntax, PHP still interprets this is a call to
an instance method when it happens inside of an instance method of the same class.

Breaking changes

None, i took particular care to ensure that dynamic macro's stil work.

spawnia and others added 3 commits May 28, 2020 14:22
…ds of the Enum

While it is not typical to use the magic instantiation dynamically, it may happen
incidentally when calling the instantiation in an instance method of itself.
Even when using the `static::KEY()` syntax, PHP still interprets this is a call to
an instance method when it happens inside of an instance method of the same class.
@BenSampo BenSampo merged commit bcc3916 into BenSampo:master Jun 7, 2020
atymic pushed a commit to atymic/laravel-enum that referenced this pull request Oct 11, 2020
…ds of the Enum (BenSampo#147)

* Handle calling magic instantiation methods from within instance methods of the Enum

While it is not typical to use the magic instantiation dynamically, it may happen
incidentally when calling the instantiation in an instance method of itself.
Even when using the `static::KEY()` syntax, PHP still interprets this is a call to
an instance method when it happens inside of an instance method of the same class.

* Add PR link

Co-authored-by: Ben Sampson <[email protected]>
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.

2 participants