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: Fix string index warning #6257

Closed

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Mar 12, 2024

Fix the following warning when using an interactivity bind directive with a short attribute name, e.g. wp-data-bind--id.

Warning: Uninitialized string offset 4 in /var/www/html/wp-includes/interactivity-api/class-wp-interactivity-api.php on line 579

Trac ticket: https://core.trac.wordpress.org/ticket/60758


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 Mar 12, 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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, cbravobernal, swissspidy, gziolo.

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

@sirreal sirreal force-pushed the fix/interactivity-str-index-warning branch from 30b12f4 to 704bd40 Compare March 12, 2024 13:48
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.

@sirreal
Copy link
Member Author

sirreal commented Mar 12, 2024

👋 @c4rl0sbr4v0 @DAreRodz for review.

Copy link
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

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

LGTM

@swissspidy
Copy link
Member

Possible to add tests for this?

@sirreal
Copy link
Member Author

sirreal commented Mar 13, 2024

I tried adding tests and would like to, but I couldn't get them to fail on the warning, that may be a configuration option or my lack of knowledge, but I couldn't find any way of saying "warnings on this test should cause the test to fail."

There is actually a test that has a short attribute and should hit this code path, but it doesn't fail the test suite or seem to generate any warning:

/**
* Tests handling of bindings within nested tags.
*
* @ticket 60356
*
* @covers ::process_directives
*/
public function test_wp_bind_handles_nested_bindings() {
$html = '<div data-wp-bind--id="myPlugin::state.id"><img data-wp-bind--width="myPlugin::state.width"></div>';
list($p) = $this->process_directives( $html );
$this->assertEquals( 'some-id', $p->get_attribute( 'id' ) );
$p->next_tag();
$this->assertEquals( '100', $p->get_attribute( 'width' ) );
}

@gziolo
Copy link
Member

gziolo commented Mar 14, 2024

I can see the error when running tests. I might be valuable to add another test that handles the boolean value:

Screenshot 2024-03-14 at 12 39 34

The same check needs to be added in another line which only will trigger when passing true:

if ( is_bool( $result ) && strlen( $bound_attribute ) > 5 && '-' === $bound_attribute[4] ) {

@sirreal
Copy link
Member Author

sirreal commented Mar 14, 2024

Good catch! I pushed a change to apply the same fix to that warning.

@gziolo
Copy link
Member

gziolo commented Mar 14, 2024

You also need a test like the one I shared to trigger the warning in the second updated line. The value needs to be true.

@sirreal
Copy link
Member Author

sirreal commented Mar 14, 2024

I pushed a test that does error if I revert the changes.

@sirreal
Copy link
Member Author

sirreal commented Mar 14, 2024

I'll address the test failures.

@sirreal
Copy link
Member Author

sirreal commented Mar 14, 2024

I fixed the tests and updated them to use assertSame. We're comparing values of types we expect to match, mostly strings, so assertSame is the appropriate assertion.

This is something to address across the interactivity tests when comparing strings to strings.

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

Excellent, tests pass locally. I no longer see the warnings. I could confirm that unit tests hit both conditions by manually changing the comparisons strlen( $bound_attribute ) > 5 using value 1.

@swissspidy
Copy link
Member

Committed in https://core.trac.wordpress.org/changeset/57835

@swissspidy swissspidy closed this Mar 14, 2024
@sirreal sirreal deleted the fix/interactivity-str-index-warning branch March 14, 2024 15:36
sirreal added a commit to WordPress/gutenberg that referenced this pull request Mar 15, 2024
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.

4 participants