Skip to content

Commit 98bcdba

Browse files
authored
Merge pull request #269 from PHPCSStandards/develop
Release PHPCSExtra 1.1.1
2 parents 61a9be9 + d613f06 commit 98bcdba

File tree

122 files changed

+528
-287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+528
-287
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
.remarkrc export-ignore
1313
.yamllint.yml export-ignore
1414
phpcs.xml.dist export-ignore
15+
phpstan.neon.dist export-ignore
1516
phpunit.xml.dist export-ignore
1617
phpunit-bootstrap.php export-ignore
1718
/.github/ export-ignore

.github/workflows/basics.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ jobs:
9595
- name: Check sniff feature completeness
9696
run: composer check-complete
9797

98+
phpstan:
99+
name: "PHPStan"
100+
runs-on: "ubuntu-latest"
101+
102+
steps:
103+
- name: Checkout code
104+
uses: actions/checkout@v3
105+
106+
- name: Install PHP
107+
uses: shivammathur/setup-php@v2
108+
with:
109+
php-version: '7.4'
110+
coverage: none
111+
tools: phpstan
112+
113+
# Install dependencies and handle caching in one go.
114+
# Dependencies need to be installed to make sure the PHPCS and PHPUnit classes are recognized.
115+
# @link https://github.com/marketplace/actions/install-composer-dependencies
116+
- name: Install Composer dependencies
117+
uses: "ramsey/composer-install@v2"
118+
with:
119+
# Bust the cache at least once a month - output format: YYYY-MM.
120+
custom-cache-suffix: $(date -u "+%Y-%m")
121+
122+
- name: Run PHPStan
123+
run: phpstan analyse
124+
98125
remark:
99126
name: 'QA Markdown'
100127
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ vendor/
55
/phpcs.xml
66
/phpunit.xml
77
/.phpunit.result.cache
8+
phpstan.neon

CHANGELOG.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ This projects adheres to [Keep a CHANGELOG](http://keepachangelog.com/) and uses
1414

1515
_Nothing yet._
1616

17+
## [1.1.1] - 2023-08-26
18+
19+
### Changed
20+
21+
#### Modernize
22+
23+
* `Modernize.FunctionCalls.Dirname`: the sniff will now respect a potentially set [`php_version` configuration option][php_version-config] and only report on modernizations which are possible on the configured `php_version`. [#261]
24+
If the `php_version` is not set, the sniff will continue to report on all modernization options.
25+
26+
#### Other
27+
28+
* Various documentation improvements. Props in part to [@szepeviktor].
29+
* Improved defensive coding in select places.
30+
* Various housekeeping.
31+
32+
[#261]: https://github.com/PHPCSStandards/PHPCSExtra/pull/261
33+
34+
1735
## [1.1.0] - 2023-07-19
1836

1937
### Added
@@ -488,6 +506,7 @@ This initial alpha release contains the following sniffs:
488506
[php_version-config]: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-php-version
489507

490508
[Unreleased]: https://github.com/PHPCSStandards/PHPCSExtra/compare/stable...HEAD
509+
[1.1.1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.1.0...1.1.1
491510
[1.1.0]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.4...1.1.0
492511
[1.0.4]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.3...1.0.4
493512
[1.0.3]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.2...1.0.3
@@ -498,6 +517,7 @@ This initial alpha release contains the following sniffs:
498517
[1.0.0-alpha3]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha2...1.0.0-alpha3
499518
[1.0.0-alpha2]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha1...1.0.0-alpha2
500519

501-
[@anomiex]: https://github.com/anomiex
502-
[@derickr]: https://github.com/derickr
503-
[@GaryJones]: https://github.com/GaryJones
520+
[@anomiex]: https://github.com/anomiex
521+
[@derickr]: https://github.com/derickr
522+
[@GaryJones]: https://github.com/GaryJones
523+
[@szepeviktor]: https://github.com/szepeviktor

Modernize/Sniffs/FunctionCalls/DirnameSniff.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHP_CodeSniffer\Files\File;
1414
use PHP_CodeSniffer\Sniffs\Sniff;
1515
use PHP_CodeSniffer\Util\Tokens;
16+
use PHPCSUtils\BackCompat\Helper;
1617
use PHPCSUtils\Tokens\Collections;
1718
use PHPCSUtils\Utils\Context;
1819
use PHPCSUtils\Utils\PassedParameters;
@@ -25,12 +26,21 @@
2526
final class DirnameSniff implements Sniff
2627
{
2728

29+
/**
30+
* PHP version as configured or 0 if unknown.
31+
*
32+
* @since 1.1.1
33+
*
34+
* @var int
35+
*/
36+
private $phpVersion;
37+
2838
/**
2939
* Registers the tokens that this sniff wants to listen for.
3040
*
3141
* @since 1.0.0
3242
*
33-
* @return int|string[]
43+
* @return array<int|string>
3444
*/
3545
public function register()
3646
{
@@ -50,6 +60,21 @@ public function register()
5060
*/
5161
public function process(File $phpcsFile, $stackPtr)
5262
{
63+
if (isset($this->phpVersion) === false || \defined('PHP_CODESNIFFER_IN_TESTS')) {
64+
// Set default value to prevent this code from running every time the sniff is triggered.
65+
$this->phpVersion = 0;
66+
67+
$phpVersion = Helper::getConfigData('php_version');
68+
if ($phpVersion !== null) {
69+
$this->phpVersion = (int) $phpVersion;
70+
}
71+
}
72+
73+
if ($this->phpVersion !== 0 && $this->phpVersion < 50300) {
74+
// PHP version too low, nothing to do.
75+
return;
76+
}
77+
5378
$tokens = $phpcsFile->getTokens();
5479

5580
if (\strtolower($tokens[$stackPtr]['content']) !== 'dirname') {
@@ -125,6 +150,8 @@ public function process(File $phpcsFile, $stackPtr)
125150
* PHP 5.3+: Detect use of dirname(__FILE__).
126151
*/
127152
if (\strtoupper($pathParam['clean']) === '__FILE__') {
153+
$levelsValue = false;
154+
128155
// Determine if the issue is auto-fixable.
129156
$hasComment = $phpcsFile->findNext(Tokens::$commentTokens, ($opener + 1), $closer);
130157
$fixable = ($hasComment === false);
@@ -182,6 +209,11 @@ public function process(File $phpcsFile, $stackPtr)
182209
/*
183210
* PHP 7.0+: Detect use of nested calls to dirname().
184211
*/
212+
if ($this->phpVersion !== 0 && $this->phpVersion < 70000) {
213+
// No need to check for this issue if the PHP version would not allow for it anyway.
214+
return;
215+
}
216+
185217
if (\preg_match('`^\s*\\\\?dirname\s*\(`i', $pathParam['clean']) !== 1) {
186218
return;
187219
}
@@ -218,7 +250,12 @@ public function process(File $phpcsFile, $stackPtr)
218250
*/
219251

220252
// Step 1: Are there comments ? If so, not auto-fixable as we don't want to remove comments.
221-
$fixable = true;
253+
$fixable = true;
254+
$outerLevelsValue = false;
255+
$innerParameters = [];
256+
$innerLevelsParam = false;
257+
$innerLevelsValue = false;
258+
222259
for ($i = ($opener + 1); $i < $closer; $i++) {
223260
if (isset(Tokens::$commentTokens[$tokens[$i]['code']])) {
224261
$fixable = false;
@@ -320,9 +357,9 @@ public function process(File $phpcsFile, $stackPtr)
320357
*
321358
* @since 1.0.0
322359
*
323-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
324-
* @param array|false $levelsParam The information about the parameter as retrieved
325-
* via PassedParameters::getParameterFromStack().
360+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
361+
* @param array<string, int|string>|false $levelsParam The information about the parameter as retrieved
362+
* via PassedParameters::getParameterFromStack().
326363
*
327364
* @return int|false Integer levels value or FALSE if the levels value couldn't be determined.
328365
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
/*
4+
* This test file is run with php_version set to a value of 50000.
5+
*
6+
* Neither of the fixes the sniff offers will be applied as the PHP version is too low.
7+
*/
8+
9+
$path = dirname(__FILE__);
10+
$path = dirname(dirname(__DIR__));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
/*
4+
* This test file is run with php_version set to a value of 50000.
5+
*
6+
* Neither of the fixes the sniff offers will be applied as the PHP version is too low.
7+
*/
8+
9+
$path = dirname(__FILE__);
10+
$path = dirname(dirname(__DIR__));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
/*
4+
* This test file is run with php_version set to a value of 50300.
5+
*
6+
* The `FileConstant` error code should trigger, but the `Nested` error code
7+
* should be ignored as the PHP version is too low.
8+
*/
9+
10+
$path = dirname(__FILE__);
11+
$path = dirname(dirname(__DIR__));

0 commit comments

Comments
 (0)