Skip to content

Conversation

@rodrigoprimo
Copy link
Contributor

Description

This PR fixes a bug in Squiz.PHP.EmbeddedPhp that causes a false positive when handling short open tags and was reported in #588.

The content of a PHP long open tag token is <?php (note the space after the tag). The content of a PHP short open tag is <? (no space after the tag). The sniff did not account correctly for this difference when checking the expected number of spaces after a short open tag, resulting in false positives and incorrect fixes.

Suggested changelog entry

Squiz.PHP.EmbeddedPhp false positive when checking spaces after a PHP short open tag.

Related issues/external references

Fixes #588

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.

The content of a PHP long open tag token is `<?php ` (note
the space after the tag). The content of a PHP short open tag is
`<?` (no space after the tag). The sniff did not account correctly for
this difference when checking the expected number of spaces after a
short open tag, resulting in false positives and incorrect fixes.

For example, the code below:

```
<? echo 'one space after short open tag'; ?>
```

Resulted in the error (there is just one space after the opening tag and
not two, as stated in the error):

```
Expected 1 space after opening PHP tag; 2 found (Squiz.PHP.EmbeddedPhp.SpacingAfterOpen)
```

And the incorrect fix:

```
<?echo 'without space after short open tag'; ?>
```

This commit fixes this problem by changing the sniff code to consider
that only long open tags contain a space after the tag in the `content`
key of the token array.
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

Thanks @rodrigoprimo!

Approved on the principle of the patch + the tests (which have been validated).

Just two nitpicks and then I can merge it.

@rodrigoprimo
Copy link
Contributor Author

Thanks for reviewing this PR, @jrfnl. I applied your suggestions and it is now ready for a final check.

@jrfnl
Copy link
Member

jrfnl commented Aug 13, 2024

Closing as merged manually in 0ed5577.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Squiz.PHP.EmbeddedPhp false positive when using PHP short open tag

2 participants