-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Interactivity API bug: Fix fatal error when trying to use a stdClass as state-context. #6672
Interactivity API bug: Fix fatal error when trying to use a stdClass as state-context. #6672
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @narendraie. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
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.
This looks good in general, thanks both!
@@ -785,6 +788,9 @@ public function test_evaluate_value() { | |||
|
|||
$result = $this->evaluate( 'otherPlugin::context.key' ); | |||
$this->assertEquals( 'otherPlugin-context', $result ); | |||
|
|||
$result = $this->evaluate( 'state.obj.prop' ); | |||
$this->assertEquals( 'property', $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.
$this->assertEquals( 'property', $result ); | |
$this->assertSame( 'property', $result ); |
$obj = new stdClass(); | ||
$obj->prop = 'property'; | ||
$generate_state = function ( $name, $obj = null ) { |
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.
It seems like this could be simplified and follow the same pattern as the rest of the state and statically set $obj->property = $name
instead of taking an $obj
parameter.
@@ -748,14 +748,17 @@ public function test_process_directives_does_not_change_inner_html_in_math() { | |||
* @param string $directive_value The directive attribute value to evaluate. | |||
* @return mixed The result of the evaluate method. | |||
*/ | |||
private function evaluate( $directive_value ) { | |||
$generate_state = function ( $name ) { | |||
private function evaluate( $directive_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.
Trailing comma may be a problem in some supported PHP versions.
private function evaluate( $directive_value, ) { | |
private function evaluate( $directive_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.
it is being indeed, thanks! I was getting crazy
It looks good to me, too! Nice work 👍 |
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.
Shall we test ArrayAccess
too?
return array( | ||
'key' => $name, | ||
'nested' => array( 'key' => $name . '-nested' ), | ||
'obj' => $obj, | ||
); |
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.
We can also test array access:
); | |
'arrAccess' => new class() implements ArrayAccess { | |
public function offsetExists($offset): bool { return true; } | |
public function offsetGet($offset): mixed { return $offset; } | |
public function offsetSet($offset, $value): void {} | |
public function offsetUnset($offset): void {} | |
}, | |
); |
@@ -785,6 +789,9 @@ 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 ); |
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.
This is interesting, I don't know what result we'll get here. I assume it's a string 1
. I'm not sure how the path is parsed and if it accounts for numeric access or coercion happens so that numeric string paths access arrays:
$this->assertSame( 'myPlugin-state', $result ); | |
$this->assertSame( 'myPlugin-state', $result ); | |
$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.
In this case, state.arrAccess.1
, 1 is treated first a string. You can operate, in example
public function offsetGet( $offset ): mixed {
return 2 * $offset; }
And will return 2 as a number.
What would be the best approach here?
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 think it's fine as it. It's just interesting that "1"
is used to access numeric arrays.
'arrAccess' => new class() implements ArrayAccess { | ||
public function offsetExists( $offset ): bool { | ||
return true; } | ||
#[\ReturnTypeWillChange] |
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 bool
type 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:
public function offsetExists( $offset ): bool { return true; }
#[\ReturnTypeWillChange]
public function offsetGet( $offset ) { return $offset; }
public function offsetSet( $offset, $value ): void {}
public function offsetUnset( $offset ): void {}
Can you omit the
bool
type hint as the return value?
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 🙂
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
bool
type hint as the return value?
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 notice
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.
@cbravobernal, is this patch ready to land? #6672 (comment) – any actions to take here? |
I removed the bool type and added |
All green so ready to land! |
Committed with https://core.trac.wordpress.org/changeset/58320. |
Trac ticket: https://core.trac.wordpress.org/ticket/61039
Updates #6581 with an unit test.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.