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

Universal/ConstructorDestructorReturn: minor efficiency tweak #252

Merged
Merged
Changes from all commits
Commits
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
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