-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Interactivity API bug: Fix fatal error when trying to use a stdClass as state-context. #6672
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
Changes from all commits
a205fce
08b0696
ab5bc6b
318fdcc
bb8ab0b
916d741
e032c84
a1a3130
5d6223f
659f124
dc96757
0b6d529
94ebb6e
e0855f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -750,16 +750,30 @@ public function test_process_directives_does_not_change_inner_html_in_math() { | |||||||||||||||||
| */ | ||||||||||||||||||
| private function evaluate( $directive_value ) { | ||||||||||||||||||
| $generate_state = function ( $name ) { | ||||||||||||||||||
| $obj = new stdClass(); | ||||||||||||||||||
| $obj->prop = $name; | ||||||||||||||||||
| return array( | ||||||||||||||||||
| 'key' => $name, | ||||||||||||||||||
| 'nested' => array( 'key' => $name . '-nested' ), | ||||||||||||||||||
| 'key' => $name, | ||||||||||||||||||
| 'nested' => array( 'key' => $name . '-nested' ), | ||||||||||||||||||
| 'obj' => $obj, | ||||||||||||||||||
| 'arrAccess' => new class() implements ArrayAccess { | ||||||||||||||||||
| #[\ReturnTypeWillChange] | ||||||||||||||||||
| public function offsetExists( $offset ) { | ||||||||||||||||||
| return true; } | ||||||||||||||||||
|
|
||||||||||||||||||
| public function offsetGet( $offset ): string { | ||||||||||||||||||
| return $offset; } | ||||||||||||||||||
| public function offsetSet( $offset, $value ): void {} | ||||||||||||||||||
| public function offsetUnset( $offset ): void {} | ||||||||||||||||||
| }, | ||||||||||||||||||
| ); | ||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also test array access:
Suggested change
|
||||||||||||||||||
| }; | ||||||||||||||||||
| $this->interactivity->state( 'myPlugin', $generate_state( 'myPlugin-state' ) ); | ||||||||||||||||||
| $this->interactivity->state( 'otherPlugin', $generate_state( 'otherPlugin-state' ) ); | ||||||||||||||||||
| $context = array( | ||||||||||||||||||
| 'myPlugin' => $generate_state( 'myPlugin-context' ), | ||||||||||||||||||
| 'otherPlugin' => $generate_state( 'otherPlugin-context' ), | ||||||||||||||||||
| 'obj' => new stdClass(), | ||||||||||||||||||
| ); | ||||||||||||||||||
| $evaluate = new ReflectionMethod( $this->interactivity, 'evaluate' ); | ||||||||||||||||||
| $evaluate->setAccessible( true ); | ||||||||||||||||||
|
|
@@ -785,6 +799,12 @@ public function test_evaluate_value() { | |||||||||||||||||
|
|
||||||||||||||||||
| $result = $this->evaluate( 'otherPlugin::context.key' ); | ||||||||||||||||||
| $this->assertEquals( 'otherPlugin-context', $result ); | ||||||||||||||||||
|
|
||||||||||||||||||
| $result = $this->evaluate( 'state.obj.prop' ); | ||||||||||||||||||
| $this->assertSame( 'myPlugin-state', $result ); | ||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is interesting, I don't know what result we'll get here. I assume it's a string
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, And will return 2 as a number. What would be the best approach here?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine as it. It's just interesting that |
||||||||||||||||||
|
|
||||||||||||||||||
| $result = $this->evaluate( 'state.arrAccess.1' ); | ||||||||||||||||||
| $this->assertSame( '1', $result ); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sirreal that test implies adding a this line in order to be compatible with PHP 7.2
It seems that the mixed typing is only available in PHP 8 and above.
Should we keep this test then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you omit the
booltype hint as the return value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this for the class body and it works (without errors, deprecations, or warnings) on php 8.3 and 7.2, let's use this:
That triggers a deprecation notice in recent versions (8.3).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to keep the test, if we support the behavior a test is a good thing 🙂
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, it will trigger:
Fatal error: During inheritance of ArrayAccess: Uncaught Return type of ArrayAccess@anonymous::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the noticeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what I meant based on the usage in other places:
