Skip to content

Commit

Permalink
Modernize/Dirname: respect a passed php_version
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Aug 9, 2023
1 parent 2fea560 commit 46af321
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Modernize/Sniffs/FunctionCalls/DirnameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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') {
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions Modernize/Tests/FunctionCalls/DirnameUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/*
* This test file is run with php_version set to a value of 50000.
*
* Neither of the fixes the sniff offers will be applied as the PHP version is too low.
*/

$path = dirname(__FILE__);
$path = dirname(dirname(__DIR__));
10 changes: 10 additions & 0 deletions Modernize/Tests/FunctionCalls/DirnameUnitTest.2.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/*
* This test file is run with php_version set to a value of 50000.
*
* Neither of the fixes the sniff offers will be applied as the PHP version is too low.
*/

$path = dirname(__FILE__);
$path = dirname(dirname(__DIR__));
11 changes: 11 additions & 0 deletions Modernize/Tests/FunctionCalls/DirnameUnitTest.3.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

/*
* This test file is run with php_version set to a value of 50300.
*
* The `FileConstant` error code should trigger, but the `Nested` error code
* should be ignored as the PHP version is too low.
*/

$path = dirname(__FILE__);
$path = dirname(dirname(__DIR__));
11 changes: 11 additions & 0 deletions Modernize/Tests/FunctionCalls/DirnameUnitTest.3.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

/*
* This test file is run with php_version set to a value of 50300.
*
* The `FileConstant` error code should trigger, but the `Nested` error code
* should be ignored as the PHP version is too low.
*/

$path = __DIR__;
$path = dirname(dirname(__DIR__));
10 changes: 10 additions & 0 deletions Modernize/Tests/FunctionCalls/DirnameUnitTest.4.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/*
* This test file is run with php_version set to a value of 70000.
*
* Both of the fixes the sniff offers will be applied as the PHP version allows for it.
*/

$path = dirname(__FILE__);
$path = dirname(dirname(__DIR__));
10 changes: 10 additions & 0 deletions Modernize/Tests/FunctionCalls/DirnameUnitTest.4.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/*
* This test file is run with php_version set to a value of 70000.
*
* Both of the fixes the sniff offers will be applied as the PHP version allows for it.
*/

$path = __DIR__;
$path = dirname(__DIR__, 2);
7 changes: 7 additions & 0 deletions Modernize/Tests/FunctionCalls/DirnameUnitTest.5.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

/*
* This test file is only here to reset the value of the php_version configuration option.
*/

dirname($path);
41 changes: 41 additions & 0 deletions Modernize/Tests/FunctionCalls/DirnameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSExtra\Modernize\Tests\FunctionCalls;

use PHPCSUtils\BackCompat\Helper;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
Expand All @@ -22,6 +23,35 @@
final class DirnameUnitTest extends AbstractSniffUnitTest
{

/**
* Set CLI values before the file is tested.
*
* @param string $testFile The name of the file being tested.
* @param \PHP_CodeSniffer\Config $config The config data for the test run.
*
* @return void
*/
public function setCliValues($testFile, $config)
{
switch ($testFile) {
case 'DirnameUnitTest.2.inc':
Helper::setConfigData('php_version', '50000', true, $config); // PHP 5.0.
break;

case 'DirnameUnitTest.3.inc':
Helper::setConfigData('php_version', '50300', true, $config); // PHP 5.3.
break;

case 'DirnameUnitTest.4.inc':
Helper::setConfigData('php_version', '70000', true, $config); // PHP 7.0.
break;

default:
Helper::setConfigData('php_version', null, true, $config); // No PHP version set.
break;
}
}

/**
* Returns the lines where errors should occur.
*
Expand Down Expand Up @@ -70,6 +100,17 @@ public function getErrorList($testFile = '')
131 => 1,
];

case 'DirnameUnitTest.3.inc':
return [
10 => 1,
];

case 'DirnameUnitTest.4.inc':
return [
9 => 1,
10 => 1,
];

default:
return [];
}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 46af321

Please sign in to comment.