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

WP/AlternativeFunctions: implement PHPCSUtils and support modern PHP #2169

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Dec 20, 2022

WP/AlternativeFunctions: order lists alphabetically

Follow up on #2108

Orders function name lists alphabetically. The group ordering, while not in alphabetic order, is left as-is for now.

This commit does not add or delete any functions from the lists.

WP/AlternativeFunctions: add missing @since tags

... for properties which were added to the sniff after the initial conception.

WP/AlternativeFunctions: change property visibility

... from protected to private for properties which are only intended to be used by the sniff itself anyway.

WP/AlternativeFunctions: remove stray blank lines at top of test file

WP/AlternativeFunctions: move parse_url related tests down

WP/AlternativeFunctions: bug fix - special casing for parse_url was not applied

The wp_parse_url() alternative to parse_url() was introduced in WP 4.4.0, but didn't have support for the $component parameter until WP 4.7.0.

For that reason, special casing was put in place in PR #1369 (WPCS 1.0.0) to examine whether the $component parameter was passed to parse_url() or not and to only advise the switch to wp_parse_url():

  1. If the $component parameter was not used and the minimum_wp_version was 4.4 or higher.
  2. If the $component parameter was used and the minimum_wp_version was 4.7 or higher.

Unfortunately, the special casing was looking for the wrong function name match, so has never worked.

The additional tests now added demonstrate the bug.

This commit, combined with the previously pulled version_compare() related fix from #2149, fixes the bug.


Includes a follow up to #2121

The "final" // phpcs:set annotations in the test case file should always reset to the default value for the property. The default value which was being used, however, was out of date.

Fixed now.

WP/AlternativeFunctions: add support for PHP 8.0+ named parameters [1] / strip_tags()

  1. Adjusted the way it is determined whether the PHP native function can be replaced with the WP version of the same using the updated PHPCSUtils 1.0.0-alpha4 PassedParameters functionality.
  2. Verified the parameter name used is in line with the name as per the PHP 8.0 release.
    PHP itself renamed a lot of parameters in PHP 8.0. As named parameters did not exist before PHP 8.0, the parameter name as per PHP 8.0 (or above) is the only relevant name.
    Ref: https://www.php.net/manual/en/function.strip-tags

Includes additional unit tests.

WP/AlternativeFunctions: add support for PHP 8.0+ named parameters [2] / parse_url()

  1. Adjusted the way it is determined whether the PHP native function can be replaced with the WP version of the same using the updated PHPCSUtils 1.0.0-alpha4 PassedParameters functionality.
  2. Verified the parameter name used is in line with the name as per the PHP 8.0 release.
    PHP itself renamed a lot of parameters in PHP 8.0. As named parameters did not exist before PHP 8.0, the parameter name as per PHP 8.0 (or above) is the only relevant name.
    Ref: https://www.php.net/manual/en/function.parse-url

Includes additional unit tests.

WP/AlternativeFunctions: add support for PHP 8.0+ named parameters [3] / file_get_contents()

  1. Adjusted the way it is determined whether the PHP native function can be replaced with the WP version of the same using the updated PHPCSUtils 1.0.0-alpha4 PassedParameters functionality.
  2. Verified the parameter names used are in line with the names as per the PHP 8.0 release.
    PHP itself renamed a lot of parameters in PHP 8.0. As named parameters did not exist before PHP 8.0, the parameter name as per PHP 8.0 (or above) is the only relevant name.
    Ref: https://www.php.net/manual/en/function.file-get-contents

Includes additional unit tests.

WP/AlternativeFunctions: minor code readability improvements

WP/AlternativeFunctions: add support for PHP 8.0+ named parameters [4] / readfile/fopen/file_put_contents()

  1. Adjusted the way it is determined whether the PHP native function can be replaced with the WP version of the same using the updated PHPCSUtils 1.0.0-alpha4 PassedParameters functionality.
  2. Verified the parameter names used are in line with the names as per the PHP 8.0 release.
    PHP itself renamed a lot of parameters in PHP 8.0. As named parameters did not exist before PHP 8.0, the parameter name as per PHP 8.0 (or above) is the only relevant name.
    Refs:

Includes additional unit tests.

WP/AlternativeFunctions: prevent some false positives / file_get_contents()

The 'raw' key in the parameter arrays returned from the PassedParameters class contains - as per the name - the raw contents of the parameter.

Since PHPCSUtils 1.0.0-alpha4, the return array also contain a 'clean' index, which contains the contents of the parameter cleaned of comments.

By switching to using that key, some false negatives and false positives get fixed.

Includes unit tests demonstrating the issue and safeguarding the fix.

WP/AlternativeFunctions: minor code tweak / readfile/fopen/file_put_contents()

The 'raw' key in the parameter arrays returned from the PassedParameters class contains - as per the name - the raw contents of the parameter.

Since PHPCSUtils 1.0.0-alpha4, the return array also contain a 'clean' index, which contains the contents of the parameter cleaned of comments.

While this doesn't make a difference functionally (no false positives/negatives), it may improve performance a tiny bit as there is less text for the is_local_data_stream() method to handle.

jrfnl added 13 commits December 20, 2022 12:02
Orders function name lists alphabetically. The group ordering, while not in alphabetic order, is left as-is for now.

This commit does not add or delete any functions from the lists.
... for properties which were added to the sniff after the initial conception.
... from `protected` to `private` for properties which are only intended to be used by the sniff itself anyway.
…ot applied

The `wp_parse_url()` alternative to `parse_url()` was introduced in WP 4.4.0, but didn't have support for the `$component` parameter until WP 4.7.0.

For that reason, special casing was put in place in PR 1369 (WPCS 1.0.0) to examine whether the `$component` parameter was passed to `parse_url()` or not and to only advise the switch to `wp_parse_url()`:
1. If the `$component` parameter was _not_ used and the `minimum_wp_version` was `4.4` or higher.
2. If the `$component` parameter **_was_** used and the `minimum_wp_version` was `4.7` or higher.

Unfortunately, the special casing was looking for the wrong function name match, so has never worked.

The additional tests now added demonstrate the bug.

This PR, combined with the previously pulled `version_compare()` related fix from 2149, fix the bug.

---

Includes a follow up to 2121

The "final" `// phpcs:set` annotations in the test case file should always reset to the default value for the property. The default value which was being used, however, was out of date.

Fixed now.
…] / strip_tags()

1. Adjusted the way it is determined whether the PHP native function can be replaced with the WP version of the same using the updated PHPCSUtils 1.0.0-alpha4 `PassedParameters` functionality.
2. Verified the parameter name used is in line with the name as per the PHP 8.0 release.
    PHP itself renamed a lot of parameters in PHP 8.0. As named parameters did not exist before PHP 8.0, the parameter name as per PHP 8.0 (or above) is the only relevant name.
    Ref: https://www.php.net/manual/en/function.strip-tags

Includes additional unit tests.
…] / parse_url()

1. Adjusted the way it is determined whether the PHP native function can be replaced with the WP version of the same using the updated PHPCSUtils 1.0.0-alpha4 `PassedParameters` functionality.
2. Verified the parameter name used is in line with the name as per the PHP 8.0 release.
    PHP itself renamed a lot of parameters in PHP 8.0. As named parameters did not exist before PHP 8.0, the parameter name as per PHP 8.0 (or above) is the only relevant name.
    Ref: https://www.php.net/manual/en/function.parse-url

Includes additional unit tests.
…] / file_get_contents()

1. Adjusted the way it is determined whether the PHP native function can be replaced with the WP version of the same using the updated PHPCSUtils 1.0.0-alpha4 `PassedParameters` functionality.
2. Verified the parameter names used are in line with the names as per the PHP 8.0 release.
    PHP itself renamed a lot of parameters in PHP 8.0. As named parameters did not exist before PHP 8.0, the parameter name as per PHP 8.0 (or above) is the only relevant name.
    Ref: https://www.php.net/manual/en/function.file-get-contents

Includes additional unit tests.
…] / readfile/fopen/file_put_contents()

1. Adjusted the way it is determined whether the PHP native function can be replaced with the WP version of the same using the updated PHPCSUtils 1.0.0-alpha4 `PassedParameters` functionality.
2. Verified the parameter names used are in line with the names as per the PHP 8.0 release.
    PHP itself renamed a lot of parameters in PHP 8.0. As named parameters did not exist before PHP 8.0, the parameter name as per PHP 8.0 (or above) is the only relevant name.
    Refs:
    * https://www.php.net/manual/en/function.readfile
    * https://www.php.net/manual/en/function.fopen
    * https://www.php.net/manual/en/function.file-put-contents

Includes additional unit tests.
…ents()

The `'raw'` key in the parameter arrays returned from the `PassedParameters` class contains - as per the name - the _raw_ contents of the parameter.

Since PHPCSUtils 1.0.0-alpha4, the return array also contain a `'clean'` index, which contains the contents of the parameter cleaned of comments.

By switching to using that key, some false negatives and false positives get fixed.

Includes unit tests demonstrating the issue and safeguarding the fix.
…ontents()

The `'raw'` key in the parameter arrays returned from the `PassedParameters` class contains - as per the name - the _raw_ contents of the parameter.

Since PHPCSUtils 1.0.0-alpha4, the return array also contain a `'clean'` index, which contains the contents of the parameter cleaned of comments.

While this doesn't make a difference functionally (no false positives/negatives), it may improve performance a tiny bit as there is less text for the `is_local_data_stream()` method to handle.
Copy link
Member

@dingo-d dingo-d left a comment

Choose a reason for hiding this comment

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

👍🏼

Copy link
Member

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

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

@GaryJones GaryJones merged commit ad3c73b into develop Dec 21, 2022
@GaryJones GaryJones deleted the feature/alternativefunctions-phpcsutils-and-modern-php branch December 21, 2022 14:17
@jrfnl jrfnl mentioned this pull request Jan 7, 2023
19 tasks
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.

3 participants