diff --git a/Modernize/Sniffs/FunctionCalls/DirnameSniff.php b/Modernize/Sniffs/FunctionCalls/DirnameSniff.php index 63e9c680..14dfed1a 100644 --- a/Modernize/Sniffs/FunctionCalls/DirnameSniff.php +++ b/Modernize/Sniffs/FunctionCalls/DirnameSniff.php @@ -13,6 +13,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\BackCompat\Helper; use PHPCSUtils\Tokens\Collections; use PHPCSUtils\Utils\Context; use PHPCSUtils\Utils\PassedParameters; @@ -25,6 +26,15 @@ final class DirnameSniff implements Sniff { + /** + * PHP version as configured or 0 if unknown. + * + * @since 1.1.1 + * + * @var int + */ + private $phpVersion; + /** * Registers the tokens that this sniff wants to listen for. * @@ -50,6 +60,21 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { + if (isset($this->phpVersion) === false || \defined('PHP_CODESNIFFER_IN_TESTS')) { + // Set default value to prevent this code from running every time the sniff is triggered. + $this->phpVersion = 0; + + $phpVersion = Helper::getConfigData('php_version'); + if ($phpVersion !== null) { + $this->phpVersion = (int) $phpVersion; + } + } + + if ($this->phpVersion !== 0 && $this->phpVersion < 50300) { + // PHP version too low, nothing to do. + return; + } + $tokens = $phpcsFile->getTokens(); if (\strtolower($tokens[$stackPtr]['content']) !== 'dirname') { @@ -182,6 +207,11 @@ public function process(File $phpcsFile, $stackPtr) /* * PHP 7.0+: Detect use of nested calls to dirname(). */ + if ($this->phpVersion !== 0 && $this->phpVersion < 70000) { + // No need to check for this issue if the PHP version would not allow for it anyway. + return; + } + if (\preg_match('`^\s*\\\\?dirname\s*\(`i', $pathParam['clean']) !== 1) { return; } diff --git a/Modernize/Tests/FunctionCalls/DirnameUnitTest.inc b/Modernize/Tests/FunctionCalls/DirnameUnitTest.1.inc similarity index 100% rename from Modernize/Tests/FunctionCalls/DirnameUnitTest.inc rename to Modernize/Tests/FunctionCalls/DirnameUnitTest.1.inc diff --git a/Modernize/Tests/FunctionCalls/DirnameUnitTest.inc.fixed b/Modernize/Tests/FunctionCalls/DirnameUnitTest.1.inc.fixed similarity index 100% rename from Modernize/Tests/FunctionCalls/DirnameUnitTest.inc.fixed rename to Modernize/Tests/FunctionCalls/DirnameUnitTest.1.inc.fixed diff --git a/Modernize/Tests/FunctionCalls/DirnameUnitTest.2.inc b/Modernize/Tests/FunctionCalls/DirnameUnitTest.2.inc new file mode 100644 index 00000000..e88858be --- /dev/null +++ b/Modernize/Tests/FunctionCalls/DirnameUnitTest.2.inc @@ -0,0 +1,10 @@ + */ - public function getErrorList() + public function getErrorList($testFile = '') { - return [ - 50 => 1, - 51 => 1, - 53 => 1, - 56 => 1, - 61 => 1, - 62 => 1, - 63 => 1, - 65 => 1, - 66 => 1, - 69 => 1, - 70 => 1, - 73 => 1, - 79 => 3, - 80 => 3, - 82 => 1, - 83 => 2, - 86 => 1, - 94 => 1, - 99 => 1, - 107 => 1, - 108 => 1, - 110 => 2, - 113 => 3, - 114 => 3, - 120 => 4, - 121 => 4, - 124 => 4, - 127 => 1, - 128 => 1, - 130 => 1, - 131 => 1, - ]; + switch ($testFile) { + case 'DirnameUnitTest.1.inc': + return [ + 50 => 1, + 51 => 1, + 53 => 1, + 56 => 1, + 61 => 1, + 62 => 1, + 63 => 1, + 65 => 1, + 66 => 1, + 69 => 1, + 70 => 1, + 73 => 1, + 79 => 3, + 80 => 3, + 82 => 1, + 83 => 2, + 86 => 1, + 94 => 1, + 99 => 1, + 107 => 1, + 108 => 1, + 110 => 2, + 113 => 3, + 114 => 3, + 120 => 4, + 121 => 4, + 124 => 4, + 127 => 1, + 128 => 1, + 130 => 1, + 131 => 1, + ]; + + case 'DirnameUnitTest.3.inc': + return [ + 10 => 1, + ]; + + case 'DirnameUnitTest.4.inc': + return [ + 9 => 1, + 10 => 1, + ]; + + default: + return []; + } } /** diff --git a/README.md b/README.md index c5af005d..22ba5382 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,9 @@ This sniff will detect and auto-fix two typical code modernizations which can be 2. Since PHP 7.0, nested function calls to `dirname()` can be changed to use the `$levels` parameter. Errorcode: `Modernize.FunctionCalls.Dirname.Nested`. +If a [`php_version` configuration option][php_version-config] has been passed to PHPCS using either `--config-set` or `--runtime-set`, it will be respected by the sniff. +In effect, this means that the sniff will only report on modernizations which can be applied for the PHP version as configured. + ### NormalizedArrays