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

Interactivity API bug: Fix fatal error when trying to use a stdClass as state-context. #6672

Conversation

cbravobernal
Copy link
Contributor

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.

Copy link

github-actions bot commented May 29, 2024

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 props-bot label.

Unlinked Accounts

The 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:

Props narenin, cbravobernal, jonsurrell, gziolo, czapla.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@sirreal sirreal left a 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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertEquals( 'property', $result );
$this->assertSame( 'property', $result );
Comment on lines 752 to 754
$obj = new stdClass();
$obj->prop = 'property';
$generate_state = function ( $name, $obj = null ) {
Copy link
Member

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, ) {
Copy link
Member

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.

Suggested change
private function evaluate( $directive_value, ) {
private function evaluate( $directive_value ) {
Copy link
Contributor Author

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

@michalczaplinski
Copy link
Contributor

It looks good to me, too! Nice work 👍

Copy link
Member

@sirreal sirreal left a 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,
);
Copy link
Member

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:

Suggested change
);
'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 );
Copy link
Member

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:

Suggested change
$this->assertSame( 'myPlugin-state', $result );
$this->assertSame( 'myPlugin-state', $result );
$result = $this->evaluate( 'state.arrAccess.1' );
$this->assertSame( '1', $result );
Copy link
Contributor Author

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?

Copy link
Member

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]
Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Member

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).

Copy link
Member

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 🙂

Copy link
Contributor Author

@cbravobernal cbravobernal Jun 3, 2024

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

Copy link
Member

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:
Screenshot 2024-06-03 at 14 09 44

@gziolo
Copy link
Member

gziolo commented Jun 3, 2024

@cbravobernal, is this patch ready to land? #6672 (comment) – any actions to take here?

@cbravobernal
Copy link
Contributor Author

@cbravobernal, is this patch ready to land? #6672 (comment) – any actions to take here?

I removed the bool type and added #[\ReturnTypeWillChange] at the beginning of the function like you mentioned to keep consistency with other parts of the code. Let's see the tests.

@cbravobernal
Copy link
Contributor Author

All green so ready to land!

@gziolo
Copy link
Member

gziolo commented Jun 4, 2024

@gziolo gziolo closed this Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants