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 sniff: change nested dirname() calls to use $levels parameter #3

Open
jrfnl opened this issue Jul 16, 2018 · 0 comments
Open

New sniff: change nested dirname() calls to use $levels parameter #3

jrfnl opened this issue Jul 16, 2018 · 0 comments

Comments

@jrfnl
Copy link
Member

jrfnl commented Jul 16, 2018

Sniff basics -
Fixable for PHP: 7.0+
Sniff type: Modernize
Fixer type: Safe

Short description

PHP 7 introduced a $levels parameter for the dirname() function. Using this parameter is more efficient than having several nested calls to the dirname() function.

Related PHPCompatibility sniff(s):

  • NewFunctionParameters

PHP manual references:

Example code:

Detect the following code pattern(s):

dirname( dirname( $file ) );
dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
dirname(dirname(dirname(__DIR__)));
dirname( dirname( dirname( dirname( __DIR__, 2 ) ) ) );

And fix these to:

dirname( $file, 2 );
dirname( __FILE__, 4 );
dirname(__DIR__, 3);
dirname( __DIR__, 5 );

Notes for implementation of the sniff:

  • Spacing within the function call should not be changed by the sniff.
  • The sniff should check for the second parameter and if it already exists, adjust it, rather than add the parameter.

Loosely related to issue #2

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

No branches or pull requests

1 participant