From 2fea560cb157a82bb8c338493f94b8870ce36216 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 9 Aug 2023 03:10:03 +0200 Subject: [PATCH 1/2] Modernize/Dirname: rename test case file ... in anticipation of additional test case files being added. --- ...nameUnitTest.inc => DirnameUnitTest.1.inc} | 0 ....inc.fixed => DirnameUnitTest.1.inc.fixed} | 0 .../Tests/FunctionCalls/DirnameUnitTest.php | 76 ++++++++++--------- 3 files changed, 42 insertions(+), 34 deletions(-) rename Modernize/Tests/FunctionCalls/{DirnameUnitTest.inc => DirnameUnitTest.1.inc} (100%) rename Modernize/Tests/FunctionCalls/{DirnameUnitTest.inc.fixed => DirnameUnitTest.1.inc.fixed} (100%) 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.php b/Modernize/Tests/FunctionCalls/DirnameUnitTest.php index eaa73dba..ccac2dbc 100644 --- a/Modernize/Tests/FunctionCalls/DirnameUnitTest.php +++ b/Modernize/Tests/FunctionCalls/DirnameUnitTest.php @@ -28,43 +28,51 @@ final class DirnameUnitTest extends AbstractSniffUnitTest * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * + * @param string $testFile The name of the file being tested. + * * @return array */ - 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, + ]; + + default: + return []; + } } /** From 46af321caca99a781cb005c2ef783a428c3f8bf5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 9 Aug 2023 03:36:53 +0200 Subject: [PATCH 2/2] Modernize/Dirname: respect a passed `php_version` If no `php_version` is passed, the sniff will behave as before. However, is a `php_version` is passed, the sniff will now selectively stay silent depending on the configured PHP and whether the proposed fixes could be applied. Includes additional tests. Includes adding information about the support of the `php_version` config setting to the README docs. --- .../Sniffs/FunctionCalls/DirnameSniff.php | 30 ++++++++++++++ .../Tests/FunctionCalls/DirnameUnitTest.2.inc | 10 +++++ .../FunctionCalls/DirnameUnitTest.2.inc.fixed | 10 +++++ .../Tests/FunctionCalls/DirnameUnitTest.3.inc | 11 +++++ .../FunctionCalls/DirnameUnitTest.3.inc.fixed | 11 +++++ .../Tests/FunctionCalls/DirnameUnitTest.4.inc | 10 +++++ .../FunctionCalls/DirnameUnitTest.4.inc.fixed | 10 +++++ .../Tests/FunctionCalls/DirnameUnitTest.5.inc | 7 ++++ .../Tests/FunctionCalls/DirnameUnitTest.php | 41 +++++++++++++++++++ README.md | 3 ++ 10 files changed, 143 insertions(+) create mode 100644 Modernize/Tests/FunctionCalls/DirnameUnitTest.2.inc create mode 100644 Modernize/Tests/FunctionCalls/DirnameUnitTest.2.inc.fixed create mode 100644 Modernize/Tests/FunctionCalls/DirnameUnitTest.3.inc create mode 100644 Modernize/Tests/FunctionCalls/DirnameUnitTest.3.inc.fixed create mode 100644 Modernize/Tests/FunctionCalls/DirnameUnitTest.4.inc create mode 100644 Modernize/Tests/FunctionCalls/DirnameUnitTest.4.inc.fixed create mode 100644 Modernize/Tests/FunctionCalls/DirnameUnitTest.5.inc 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.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 @@ + 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