From fa57a475a2b772818a8c5631254ee76c2e000180 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 6 Jan 2023 03:07:25 +0100 Subject: [PATCH 1/3] ConstructorDestructorReturn: allow for multiple test files --- ...ConstructorDestructorReturnUnitTest.1.inc} | 0 ...uctorDestructorReturnUnitTest.1.inc.fixed} | 0 .../ConstructorDestructorReturnUnitTest.php | 52 ++++++++++++------- 3 files changed, 34 insertions(+), 18 deletions(-) rename Universal/Tests/CodeAnalysis/{ConstructorDestructorReturnUnitTest.inc => ConstructorDestructorReturnUnitTest.1.inc} (100%) rename Universal/Tests/CodeAnalysis/{ConstructorDestructorReturnUnitTest.inc.fixed => ConstructorDestructorReturnUnitTest.1.inc.fixed} (100%) diff --git a/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.inc b/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.1.inc similarity index 100% rename from Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.inc rename to Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.1.inc diff --git a/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.inc.fixed b/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.1.inc.fixed similarity index 100% rename from Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.inc.fixed rename to Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.1.inc.fixed diff --git a/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.php b/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.php index a6bd3a7e..59eaa86c 100644 --- a/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.php +++ b/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.php @@ -25,34 +25,50 @@ final class ConstructorDestructorReturnUnitTest extends AbstractSniffUnitTest /** * Returns the lines where errors should occur. * + * @param string $testFile The name of the file being tested. + * * @return array => */ - public function getErrorList() + public function getErrorList($testFile = '') { - return [ - 85 => 1, - 89 => 1, - 101 => 1, - 116 => 1, - 118 => 1, - 122 => 1, - 124 => 1, - ]; + switch ($testFile) { + case 'ConstructorDestructorReturnUnitTest.1.inc': + return [ + 85 => 1, + 89 => 1, + 101 => 1, + 116 => 1, + 118 => 1, + 122 => 1, + 124 => 1, + ]; + + default: + return []; + } } /** * Returns the lines where warnings should occur. * + * @param string $testFile The name of the file being tested. + * * @return array => */ - public function getWarningList() + public function getWarningList($testFile = '') { - return [ - 86 => 1, - 90 => 1, - 95 => 1, - 103 => 1, - 107 => 1, - ]; + switch ($testFile) { + case 'ConstructorDestructorReturnUnitTest.1.inc': + return [ + 86 => 1, + 90 => 1, + 95 => 1, + 103 => 1, + 107 => 1, + ]; + + default: + return []; + } } } From eff67eceb902fd186aabb06cc4765e744a31ba72 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 6 Jan 2023 04:38:06 +0100 Subject: [PATCH 2/3] ConstructorDestructorReturn: respect a potentially set `php_version` config value PHP4-style constructors - methods with the same name as the class - are no longer regarded as constructors on PHP 8.0 or higher. This adjusts the sniff to respect a potentially set `php_version` configuration value and will prevent the sniff from reporting on PHP4-style constructors when the `php_version` is set to PHP 8.0 or higher. Includes unit tests. Partially fixes 207 --- .../ConstructorDestructorReturnSniff.php | 8 ++- .../ConstructorDestructorReturnUnitTest.2.inc | 22 +++++++ ...ructorDestructorReturnUnitTest.2.inc.fixed | 22 +++++++ .../ConstructorDestructorReturnUnitTest.3.inc | 22 +++++++ ...ructorDestructorReturnUnitTest.3.inc.fixed | 22 +++++++ .../ConstructorDestructorReturnUnitTest.4.inc | 14 +++++ ...ructorDestructorReturnUnitTest.4.inc.fixed | 14 +++++ .../ConstructorDestructorReturnUnitTest.php | 62 +++++++++++++++++++ 8 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.2.inc create mode 100644 Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.2.inc.fixed create mode 100644 Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.3.inc create mode 100644 Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.3.inc.fixed create mode 100644 Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.4.inc create mode 100644 Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.4.inc.fixed diff --git a/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php b/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php index 01d61158..d199019e 100644 --- a/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php +++ b/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php @@ -14,6 +14,7 @@ use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; use PHPCSUtils\BackCompat\BCFile; +use PHPCSUtils\BackCompat\Helper; use PHPCSUtils\Tokens\Collections; use PHPCSUtils\Utils\FunctionDeclarations; use PHPCSUtils\Utils\GetTokensAsString; @@ -67,7 +68,12 @@ public function process(File $phpcsFile, $stackPtr) if ($functionNameLC === '__construct' || $functionNameLC === '__destruct') { $functionType = \sprintf('A "%s()" magic method', $functionNameLC); } else { - // This may be a PHP 4-style constructor. + // If the PHP version is explicitly set to PHP 8.0 or higher, ignore PHP 4-style constructors. + if ((int) Helper::getConfigData('php_version') >= 80000) { + return; + } + + // This may be a PHP 4-style constructor which should be handled. $OOName = ObjectDeclarations::getName($phpcsFile, $scopePtr); if (empty($OOName) === true) { diff --git a/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.2.inc b/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.2.inc new file mode 100644 index 00000000..a969052e --- /dev/null +++ b/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.2.inc @@ -0,0 +1,22 @@ + 1, ]; + case 'ConstructorDestructorReturnUnitTest.2.inc': + return [ + 10 => 1, + 14 => 1, + ]; + + case 'ConstructorDestructorReturnUnitTest.3.inc': + return [ + 10 => 1, + 14 => 1, + 18 => 1, + ]; + + case 'ConstructorDestructorReturnUnitTest.4.inc': + return [ + 10 => 1, + ]; + default: return []; } @@ -67,6 +111,24 @@ public function getWarningList($testFile = '') 107 => 1, ]; + case 'ConstructorDestructorReturnUnitTest.2.inc': + return [ + 11 => 1, + 15 => 1, + ]; + + case 'ConstructorDestructorReturnUnitTest.3.inc': + return [ + 11 => 1, + 15 => 1, + 20 => 1, + ]; + + case 'ConstructorDestructorReturnUnitTest.4.inc': + return [ + 12 => 1, + ]; + default: return []; } From 18b236151f208fe47e77133a998b4f896514eae8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 6 Jan 2023 23:38:33 +0100 Subject: [PATCH 3/3] Changelog and readme updates for PHPCSExtra 1.0.2 --- CHANGELOG.md | 14 +++++++++++++- README.md | 6 +++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f31386a3..db50ecf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,17 @@ This projects adheres to [Keep a CHANGELOG](http://keepachangelog.com/) and uses _Nothing yet._ +## [1.0.2] - 2023-01-10 + +### Changed + +#### Universal + +* `Universal.CodeAnalysis.ConstructorDestructorReturn`: the sniff will now respect a potentially set [`php_version` configuration option][php_version-config] and only report on PHP4-style constructors when the `php_version` is below `'80000'`. Thanks [@anomiex] for reporting! [#207], [#208] + +[#207]: https://github.com/PHPCSStandards/PHPCSExtra/issues/207 +[#208]: https://github.com/PHPCSStandards/PHPCSExtra/pull/208 + ## [1.0.1] - 2023-01-05 @@ -221,7 +232,6 @@ The upgrade to PHPCSUtils 1.0.0-alpha4 took care of a number of bugs, which pote [php-manual-dirname]: https://www.php.net/function.dirname [php-rfc-negative_array_index]: https://wiki.php.net/rfc/negative_array_index -[php_version-config]: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-php-version [ESLint "no lonely if"]: https://eslint.org/docs/rules/no-lonely-if [PHPCSUtils 1.0.0-alpha4]: https://github.com/PHPCSStandards/PHPCSUtils/releases/tag/1.0.0-alpha4 @@ -385,8 +395,10 @@ This initial alpha release contains the following sniffs: Individual sub-types can be allowed by excluding specific error codes. [Composer PHPCS plugin]: https://github.com/PHPCSStandards/composer-installer +[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.0.2]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.1...1.0.2 [1.0.1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0...1.0.1 [1.0.0]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-rc1...1.0.0 [1.0.0-RC1]: https://github.com/PHPCSStandards/PHPCSExtra/compare/1.0.0-alpha3...1.0.0-rc1 diff --git a/README.md b/README.md index 34f5a2b8..5a43c0f6 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,6 @@ The sniff will make a distinction between keys which will be duplicate in all PH 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 and only report duplicate keys for the configured PHP version. [php-rfc-negative_array_index]: https://wiki.php.net/rfc/negative_array_index -[php_version-config]: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-php-version #### `Universal.Arrays.MixedArrayKeyTypes` :books: @@ -218,6 +217,9 @@ Require a consistent modifier keyword order for class declarations. * Disallows return type declarations on constructor/destructor methods - error code: `ReturnTypeFound`, auto-fixable. * Discourages constructor/destructor methods returning a value - error code: `ReturnValueFound`. +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 PHP4-style constructors if the configured PHP version is less than 8.0. + #### `Universal.CodeAnalysis.ForeachUniqueAssignment` :wrench: :books: Detects `foreach` control structures which use the same variable for both the key as well as the value assignment as this will lead to unexpected - and most likely unintended - behaviour. @@ -478,3 +480,5 @@ This code is released under the GNU Lesser General Public License (LGPLv3). For [phpcs-gh]: https://github.com/squizlabs/PHP_CodeSniffer [phpcsutils-gh]: https://github.com/PHPCSStandards/PHPCSUtils [composer-installer-gh]: https://github.com/PHPCSStandards/composer-installer + +[php_version-config]: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-php-version