diff --git a/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php b/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php index d199019e..ce66d4d1 100644 --- a/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php +++ b/Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php @@ -31,6 +31,15 @@ final class ConstructorDestructorReturnSniff implements Sniff { + /** + * PHP version as configured or 0 if unknown. + * + * @since 1.1.0 + * + * @var int + */ + private $phpVersion; + /** * Registers the tokens that this sniff wants to listen for. * @@ -56,6 +65,16 @@ 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; + } + } + $scopePtr = Scopes::validDirectScope($phpcsFile, $stackPtr, Tokens::$ooScopeTokens); if ($scopePtr === false) { // Not an OO method. @@ -69,7 +88,7 @@ public function process(File $phpcsFile, $stackPtr) $functionType = \sprintf('A "%s()" magic method', $functionNameLC); } else { // 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) { + if ($this->phpVersion >= 80000) { return; }