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

✨ New Modernize.FunctionCalls.Dirname sniff #172

Merged
merged 2 commits into from
Dec 2, 2022

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Dec 2, 2022

Introduce new Modernize standard

Includes updating CI and other dev related files to take the new standard into account.

P.S.: By rights, this should be a separate standard in the PHPCompatibility organisation, but that's for later. For now, I just want to make sure this sniff becomes available.

✨ New Modernize.FunctionCalls.Dirname sniff

This new sniff will detect and, when possible, auto-fix two typical code modernizations which can be made related to the dirname() function:

  1. Since PHP 5.3, calls to dirname(__FILE__) can be replaced by __DIR__.
    Errorcode: Modernize.FunctionCalls.Dirname.FileConstant.
  2. Since PHP 7.0, nested function calls to dirname() can be changed to use the new $levels parameter.
    Errorcode: Modernize.FunctionCalls.Dirname.Nested.

Depending on supported PHP versions of an application, the complete sniff can be enabled and/or just one of the error codes could be enabled.

Regarding the fixers:

The sniff - and more importantly, the fixer - take fully qualified function calls, named parameters, as well as trailing comma's in function calls into account.

  • If a comment is found in any of the code which would be removed by the fixer, the issues will still be flagged, but not be marked as fixable.
  • If the value of a pre-existing $levels parameter cannot be determined, the issues will still be flagged, but not be marked as fixable.

Also take note that the fixer does not make any presumptions about the code style of the project. It is recommended to run the code style fixers together with or after this fixer, so those can adjust the code style to the style applicable in the project.

Includes fixer.
Includes unit tests.
Includes documentation.

Notes/Future scope:

  • The sniff is not yet compatible with PHPCS 4.x.
  • When PHPCSUtils releases its own abstract for sniffing function calls, this sniff should switch to it.

Refs:

Includes updating CI and other dev related files to take the new standard into account.
Copy link
Contributor

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

Found just one whitespace issue.

Modernize/Sniffs/FunctionCalls/DirnameSniff.php Outdated Show resolved Hide resolved
This new sniff will detect and, when possible, auto-fix two typical code modernizations which can be made related to the `dirname()` function:
1. Since PHP 5.3, calls to `dirname(__FILE__)` can be replaced by `__DIR__`.
    Errorcode: `Modernize.FunctionCalls.Dirname.FileConstant`.
2. Since PHP 7.0, nested function calls to `dirname()` can be changed to use the new `$levels` parameter.
    Errorcode: `Modernize.FunctionCalls.Dirname.Nested`.

Depending on supported PHP versions of an application, the complete sniff can be enabled and/or just one of the error codes could be enabled.

**Regarding the fixers:**

The sniff - and more importantly, the fixer - take fully qualified function calls, named parameters, as well as trailing comma's in function calls into account.

* If a comment is found in any of the code which would be removed by the fixer, the issues will still be flagged, but not be marked as fixable.
* If the value of a pre-existing `$levels` parameter cannot be determined, the issues will still be flagged, but not be marked as fixable.

Also take note that the fixer does not make any presumptions about the code style of the project. It is recommended to run the code style fixers together with or after this fixer, so those can adjust the code style to the style applicable in the project.

Includes fixer.
Includes unit tests.
Includes documentation.

Notes/Future scope:
* The sniff is not yet compatible with PHPCS 4.x.
* When PHPCSUtils releases its own abstract for sniffing function calls, this sniff should switch to it.

Refs:
* https://www.php.net/manual/en/function.dirname.php
* PHP 5.3: https://php-legacy-docs.zend.com/manual/php5/en/migration53.global-constants
* PHp 7.0: https://www.php.net/manual/en/migration70.changed-functions.php#migration70.changed-functions.core
    Note: the parameter was originally called `$depth`, but has since been renamed to `$levels`.
@jrfnl jrfnl force-pushed the universal/new-modernize-dirname-file-sniff branch from bd72ba2 to 1d3943d Compare December 2, 2022 12:07
@jrfnl jrfnl merged commit 7cafca7 into develop Dec 2, 2022
@jrfnl jrfnl deleted the universal/new-modernize-dirname-file-sniff branch December 2, 2022 12:17
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.

None yet

2 participants