Skip to content

Commit

Permalink
Merge pull request #252 from PHPCSStandards/universal/constructordest…
Browse files Browse the repository at this point in the history
…ructorreturn-minor-efficiency-tweak

Universal/ConstructorDestructorReturn: minor efficiency tweak
  • Loading branch information
jrfnl committed Jul 15, 2023
2 parents d6935ac + 30189ea commit 474e767
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -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;
}

Expand Down

0 comments on commit 474e767

Please sign in to comment.