Fix fatal error with statemachine v3.0.0#5
Merged
Conversation
The example has not run since the v3.0.0 upgrade in April: index.php dies with a fatal error before printing a single line. v3.0.0 changed two signatures that the example classes still implement in their 2015 form: Command::__invoke(mixed ...$args): void ConditionInterface::checkCondition(object $subject, ArrayAccess $context): bool PHP rejects the narrower child declarations (contravariance). Authorize::__invoke -> mixed ...$args): void, arguments unpacked Authorize::__toString -> : string both Conditions -> object $subject ... : bool, getName(): string Order::triggerEvent -> ?ArrayAccess (implicit nullable is deprecated since 8.4) Verified on PHP 8.5.4: before: 1 fatal error, no output after: runs through all four orders, 26 state transitions
The example has not run since the v3.0.0 upgrade in April: index.php dies with a fatal error before printing a single line of output. v3.0.0 changed two signatures that the example classes still implement in their 2015 form: Command::__invoke(mixed ...$args): void ConditionInterface::checkCondition(object $subject, ArrayAccess $context): bool PHP rejects the narrower child declarations (contravariance). Authorize::__invoke -> mixed ...$args): void, arguments unpacked Authorize::__toString -> : string both Conditions -> object $subject ... : bool, getName(): string Order::triggerEvent -> ?ArrayAccess (implicit nullable is deprecated since 8.4) The variadic signature is forced by the parent class, so the type check the old signature performed is gone. Authorize now guards for it in the body, the same way ShippingDateGreater14Days already does. Verified on PHP 8.5.4: before: fatal error, no example output after: all four orders run through, 9 state transitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The example has not run since the v3.0.0 upgrade on 8 April:
index.phpdies with a fatal error before printing a single line of output.Why
v3.0.0 changed two signatures:
The example classes still implement their 2015 form. PHP rejects the narrower child declarations (contravariance). The v3.0.0 upgrade (#4) touched
composer.json,composer.lockandphpunit.xml.dist— no PHP file.What changes
Authorize::__invokemixed ...$args): void, arguments unpackedAuthorize::__toString: stringobject $subject … : bool,getName(): stringOrder::triggerEvent?ArrayAccess— implicit nullable is deprecated since 8.4The variadic signature is forced by the parent class, so the type check the old signature used to perform is gone.
Authorizenow guards for it in the body, the same wayShippingDateGreater14Daysalready does — without it, calling it with the wrong type fails later and less clearly (Call to undefined method stdClass::getNumber()instead of a named exception).Verification
Fresh clone, PHP 8.5.4:
The full example runs again, including the deliberate failure at the end (
Current state "payment failed" doesn't have event "returned").Note: running the example still emits 21 deprecations per run coming from the library itself, visible under
error_reporting=E_ALL— Statemachine#29 addresses those.