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

Modernize/Dirname: respect a passed php_version #261

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
117 changes: 83 additions & 34 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,49 +23,97 @@
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.
*
* 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<int, int>
*/
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 [];
}
}

/**
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