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

Release PHPCSExtra 1.1.1 #269

Merged
merged 25 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
30b8571
Fix typos
szepeviktor Jul 19, 2023
b3b5f01
Merge pull request #258 from szepeviktor/fix-typos
jrfnl Jul 19, 2023
ac8beea
README: fix order of the sniff list
jrfnl Jul 20, 2023
64b81d1
Merge pull request #259 from PHPCSStandards/feature/readme-fix-sniff-…
jrfnl Jul 20, 2023
2fea560
Modernize/Dirname: rename test case file
jrfnl Aug 9, 2023
46af321
Modernize/Dirname: respect a passed `php_version`
jrfnl Aug 9, 2023
8a41c8b
Merge pull request #261 from PHPCSStandards/modernize/dirname-use-php…
jrfnl Aug 9, 2023
b1877b4
CS: alphabetize use statements
jrfnl Aug 16, 2023
ad2b119
Merge pull request #262 from PHPCSStandards/feature/cs-alphabetize-us…
jrfnl Aug 16, 2023
5626417
Modernize/Dirname: set redundant default values
jrfnl Aug 25, 2023
3399726
Universal/DisallowInlineTabs: set redundant default values
jrfnl Aug 25, 2023
780c759
Universal/PrecisionAlignment: set redundant default values
jrfnl Aug 25, 2023
fb7235e
Universal/NoEchoSprintf: bug fix
jrfnl Aug 25, 2023
dbf3d28
Merge pull request #263 from PHPCSStandards/feature/noechosprintf-bugfix
jrfnl Aug 26, 2023
8ffd4d8
Merge pull request #264 from PHPCSStandards/feature/various-sniffs-se…
jrfnl Aug 26, 2023
0444986
Universal/DisallowInlineTabs: increase type safety
jrfnl Aug 25, 2023
7a34f3b
Merge pull request #265 from PHPCSStandards/universal/disallowinlinet…
jrfnl Aug 26, 2023
c1c93e1
Docs: improve test `@return` tags
jrfnl Aug 25, 2023
ed86bb1
Docs: improve `@return` tags for `register()` methods
jrfnl Aug 26, 2023
93dc83b
Docs: various `@var`/`@param`/`@return` tag improvements
jrfnl Aug 25, 2023
431d42b
Merge pull request #266 from PHPCSStandards/feature/docs-improvements
jrfnl Aug 26, 2023
46d4a05
Add PHPStan to QA checks
jrfnl Aug 26, 2023
5f505da
Merge pull request #267 from PHPCSStandards/feature/add-phpstan
jrfnl Aug 26, 2023
37bfe5e
Changelog for PHPCSExtra 1.1.1
jrfnl Aug 26, 2023
d613f06
Merge pull request #268 from PHPCSStandards/feature/changelog-1.1.1
jrfnl Aug 26, 2023
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
.remarkrc export-ignore
.yamllint.yml export-ignore
phpcs.xml.dist export-ignore
phpstan.neon.dist export-ignore
phpunit.xml.dist export-ignore
phpunit-bootstrap.php export-ignore
/.github/ export-ignore
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/basics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ jobs:
- name: Check sniff feature completeness
run: composer check-complete

phpstan:
name: "PHPStan"
runs-on: "ubuntu-latest"

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: none
tools: phpstan

# Install dependencies and handle caching in one go.
# Dependencies need to be installed to make sure the PHPCS and PHPUnit classes are recognized.
# @link https://github.com/marketplace/actions/install-composer-dependencies
- name: Install Composer dependencies
uses: "ramsey/composer-install@v2"
with:
# Bust the cache at least once a month - output format: YYYY-MM.
custom-cache-suffix: $(date -u "+%Y-%m")

- name: Run PHPStan
run: phpstan analyse

remark:
name: 'QA Markdown'
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor/
/phpcs.xml
/phpunit.xml
/.phpunit.result.cache
phpstan.neon
26 changes: 23 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ This projects adheres to [Keep a CHANGELOG](http://keepachangelog.com/) and uses

_Nothing yet._

## [1.1.1] - 2023-08-26

### Changed

#### Modernize

* `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]
If the `php_version` is not set, the sniff will continue to report on all modernization options.

#### Other

* Various documentation improvements. Props in part to [@szepeviktor].
* Improved defensive coding in select places.
* Various housekeeping.

[#261]: https://github.com/PHPCSStandards/PHPCSExtra/pull/261


## [1.1.0] - 2023-07-19

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

[Unreleased]: https://github.com/PHPCSStandards/PHPCSExtra/compare/stable...HEAD
[1.1.1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.1.0...1.1.1
[1.1.0]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.4...1.1.0
[1.0.4]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.3...1.0.4
[1.0.3]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.2...1.0.3
Expand All @@ -498,6 +517,7 @@ This initial alpha release contains the following sniffs:
[1.0.0-alpha3]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha2...1.0.0-alpha3
[1.0.0-alpha2]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha1...1.0.0-alpha2

[@anomiex]: https://github.com/anomiex
[@derickr]: https://github.com/derickr
[@GaryJones]: https://github.com/GaryJones
[@anomiex]: https://github.com/anomiex
[@derickr]: https://github.com/derickr
[@GaryJones]: https://github.com/GaryJones
[@szepeviktor]: https://github.com/szepeviktor
47 changes: 42 additions & 5 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,12 +26,21 @@
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.
*
* @since 1.0.0
*
* @return int|string[]
* @return array<int|string>
*/
public function register()
{
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 @@ -125,6 +150,8 @@ public function process(File $phpcsFile, $stackPtr)
* PHP 5.3+: Detect use of dirname(__FILE__).
*/
if (\strtoupper($pathParam['clean']) === '__FILE__') {
$levelsValue = false;

// Determine if the issue is auto-fixable.
$hasComment = $phpcsFile->findNext(Tokens::$commentTokens, ($opener + 1), $closer);
$fixable = ($hasComment === false);
Expand Down Expand Up @@ -182,6 +209,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 Expand Up @@ -218,7 +250,12 @@ public function process(File $phpcsFile, $stackPtr)
*/

// Step 1: Are there comments ? If so, not auto-fixable as we don't want to remove comments.
$fixable = true;
$fixable = true;
$outerLevelsValue = false;
$innerParameters = [];
$innerLevelsParam = false;
$innerLevelsValue = false;

for ($i = ($opener + 1); $i < $closer; $i++) {
if (isset(Tokens::$commentTokens[$tokens[$i]['code']])) {
$fixable = false;
Expand Down Expand Up @@ -320,9 +357,9 @@ public function process(File $phpcsFile, $stackPtr)
*
* @since 1.0.0
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param array|false $levelsParam The information about the parameter as retrieved
* via PassedParameters::getParameterFromStack().
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param array<string, int|string>|false $levelsParam The information about the parameter as retrieved
* via PassedParameters::getParameterFromStack().
*
* @return int|false Integer levels value or FALSE if the levels value couldn't be determined.
*/
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);
Loading