diff --git a/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php b/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php index 01d6115..d199019 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.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.2.inc b/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.2.inc new file mode 100644 index 0000000..a969052 --- /dev/null +++ b/Universal/Tests/CodeAnalysis/ConstructorDestructorReturnUnitTest.2.inc @@ -0,0 +1,22 @@ + => */ - 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, + ]; + + 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 []; + } } /** * 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, + ]; + + 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 []; + } } }