PHPCompatibility / PHPModernizer

External PHPCS standard with auto-fixers to modernize legacy codebases
GNU Lesser General Public License v3.0
10 stars 0 forks source link

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

Open jrfnl opened 6 years ago

jrfnl commented 6 years ago
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):

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:

Loosely related to issue #2