From 48cff2b406ced29db0eb06bf858ab1e1a3b7be65 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 19 Apr 2025 06:55:03 +0200 Subject: [PATCH 1/9] Modernize: Generic/UnusedFunctionParameter: use class constant for constant array --- .../UnusedFunctionParameterSniff.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php b/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php index 73fff3fbe8..a3788c411a 100644 --- a/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php +++ b/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php @@ -23,22 +23,15 @@ class UnusedFunctionParameterSniff implements Sniff { - /** - * The list of class type hints which will be ignored. - * - * @var array - */ - public $ignoreTypeHints = []; - /** * A list of all PHP magic methods with fixed method signatures. * * Note: `__construct()` and `__invoke()` are excluded on purpose * as their method signature is not fixed. * - * @var array + * @var array */ - private $magicMethods = [ + private const MAGIC_METHODS = [ '__destruct' => true, '__call' => true, '__callstatic' => true, @@ -56,6 +49,13 @@ class UnusedFunctionParameterSniff implements Sniff '__debuginfo' => true, ]; + /** + * The list of class type hints which will be ignored. + * + * @var array + */ + public $ignoreTypeHints = []; + /** * Returns an array of tokens this test wants to listen for. @@ -101,7 +101,7 @@ public function process(File $phpcsFile, $stackPtr) // Check for magic methods and ignore these as the method signature cannot be changed. $methodName = $phpcsFile->getDeclarationName($stackPtr); $methodNameLc = strtolower($methodName); - if (isset($this->magicMethods[$methodNameLc]) === true) { + if (isset(self::MAGIC_METHODS[$methodNameLc]) === true) { return; } From 1cdc2c251709596b9998bb2a378987bbf0606ccc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 19 Apr 2025 06:55:36 +0200 Subject: [PATCH 2/9] Modernize: Generic/UselessOverridingMethod: use class constant for constant array --- .../Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php b/src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php index f2b71835a1..092b23d6a7 100644 --- a/src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php +++ b/src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php @@ -33,7 +33,7 @@ class UselessOverridingMethodSniff implements Sniff * * @var array Keys are the token constants, value is irrelevant. */ - private $validOOScopes = [ + private const VALID_OO_SCOPES = [ T_CLASS => true, T_ANON_CLASS => true, T_TRAIT => true, @@ -74,8 +74,8 @@ public function process(File $phpcsFile, $stackPtr) $conditions = $token['conditions']; $lastCondition = end($conditions); - // Skip functions that are not a method part of a class, anon class or trait. - if (isset($this->validOOScopes[$lastCondition]) === false) { + // Skip functions that are not a method, i.e. part of a class, anon class or trait. + if (isset(self::VALID_OO_SCOPES[$lastCondition]) === false) { return; } From b698f0cf573d1d1f7d149fed2c6b964b4f213bd6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 20 Apr 2025 06:53:18 +0200 Subject: [PATCH 3/9] Modernize: Generic/CyclomaticComplexity: use class constant for constant array --- .../Metrics/CyclomaticComplexitySniff.php | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php b/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php index a6b17d1315..9bce43c189 100644 --- a/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php +++ b/src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php @@ -20,6 +20,27 @@ class CyclomaticComplexitySniff implements Sniff { + /** + * Predicate nodes for PHP. + * + * @var array + */ + private const PREDICATE_NODES = [ + T_CASE => true, + T_DEFAULT => true, + T_CATCH => true, + T_IF => true, + T_FOR => true, + T_FOREACH => true, + T_WHILE => true, + T_ELSEIF => true, + T_INLINE_THEN => true, + T_COALESCE => true, + T_COALESCE_EQUAL => true, + T_MATCH_ARROW => true, + T_NULLSAFE_OBJECT_OPERATOR => true, + ]; + /** * A complexity higher than this value will throw a warning. * @@ -69,28 +90,11 @@ public function process(File $phpcsFile, $stackPtr) $start = $tokens[$stackPtr]['scope_opener']; $end = $tokens[$stackPtr]['scope_closer']; - // Predicate nodes for PHP. - $find = [ - T_CASE => true, - T_DEFAULT => true, - T_CATCH => true, - T_IF => true, - T_FOR => true, - T_FOREACH => true, - T_WHILE => true, - T_ELSEIF => true, - T_INLINE_THEN => true, - T_COALESCE => true, - T_COALESCE_EQUAL => true, - T_MATCH_ARROW => true, - T_NULLSAFE_OBJECT_OPERATOR => true, - ]; - $complexity = 1; // Iterate from start to end and count predicate nodes. for ($i = ($start + 1); $i < $end; $i++) { - if (isset($find[$tokens[$i]['code']]) === true) { + if (isset(self::PREDICATE_NODES[$tokens[$i]['code']]) === true) { $complexity++; } } From 0e18ef744f72121df7d73f84bcd673bfd16ee4cb Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 19 Apr 2025 18:31:18 +0200 Subject: [PATCH 4/9] Modernize: Generic/LowerCaseConstant: use class constant for constant array --- .../Sniffs/PHP/LowerCaseConstantSniff.php | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php b/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php index aa451e2c77..b5b773fa65 100644 --- a/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php @@ -19,9 +19,9 @@ class LowerCaseConstantSniff implements Sniff /** * The tokens this sniff is targetting. * - * @var array + * @var array */ - private $targets = [ + private const TARGET_TOKENS = [ T_TRUE => T_TRUE, T_FALSE => T_FALSE, T_NULL => T_NULL, @@ -32,23 +32,19 @@ class LowerCaseConstantSniff implements Sniff * * @var array */ - private $propertyTypeTokens = [ + private const PROPERTY_TYPE_TOKENS = (Tokens::NAME_TOKENS + [ T_CALLABLE => T_CALLABLE, T_SELF => T_SELF, T_PARENT => T_PARENT, T_FALSE => T_FALSE, T_TRUE => T_TRUE, T_NULL => T_NULL, - T_STRING => T_STRING, - T_NAME_QUALIFIED => T_NAME_QUALIFIED, - T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED, - T_NAME_RELATIVE => T_NAME_RELATIVE, T_TYPE_UNION => T_TYPE_UNION, T_TYPE_INTERSECTION => T_TYPE_INTERSECTION, T_TYPE_OPEN_PARENTHESIS => T_TYPE_OPEN_PARENTHESIS, T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS, T_NULLABLE => T_NULLABLE, - ]; + ]); /** @@ -58,7 +54,7 @@ class LowerCaseConstantSniff implements Sniff */ public function register() { - $targets = $this->targets; + $targets = self::TARGET_TOKENS; // Register scope modifiers to filter out property type declarations. $targets += Tokens::SCOPE_MODIFIERS; @@ -130,7 +126,7 @@ public function process(File $phpcsFile, $stackPtr) || $tokens[$stackPtr]['code'] === T_FINAL || $tokens[$stackPtr]['code'] === T_ABSTRACT ) { - $skipOver = (Tokens::EMPTY_TOKENS + $this->propertyTypeTokens); + $skipOver = (Tokens::EMPTY_TOKENS + self::PROPERTY_TYPE_TOKENS); $skipTo = $phpcsFile->findNext($skipOver, ($stackPtr + 1), null, true); if ($skipTo !== false) { return $skipTo; @@ -161,7 +157,7 @@ public function process(File $phpcsFile, $stackPtr) } // Do a quick check if any of the targets exist in the declaration. - $found = $phpcsFile->findNext($this->targets, $tokens[$stackPtr]['parenthesis_opener'], $end); + $found = $phpcsFile->findNext(self::TARGET_TOKENS, $tokens[$stackPtr]['parenthesis_opener'], $end); if ($found === false) { // Skip forward, no need to examine these tokens again. return $end; @@ -180,7 +176,7 @@ public function process(File $phpcsFile, $stackPtr) } for ($i = $param['default_token']; $i < $paramEnd; $i++) { - if (isset($this->targets[$tokens[$i]['code']]) === true) { + if (isset(self::TARGET_TOKENS[$tokens[$i]['code']]) === true) { $this->processConstant($phpcsFile, $i); } } From e749e959e9b92e54ea673d7a4cebe3556518e61e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 19 Apr 2025 06:59:02 +0200 Subject: [PATCH 5/9] Modernize: Generic/LowerCaseType: use class constant for constant array --- .../Generic/Sniffs/PHP/LowerCaseTypeSniff.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php index beb5227e6e..5da9e6a7af 100644 --- a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php @@ -20,9 +20,9 @@ class LowerCaseTypeSniff implements Sniff /** * Native types supported by PHP. * - * @var array + * @var array */ - private $phpTypes = [ + private const PHP_TYPES = [ 'self' => true, 'parent' => true, 'array' => true, @@ -146,7 +146,7 @@ public function process(File $phpcsFile, $stackPtr) ); } else { $type = $tokens[$startOfType]['content']; - if (isset($this->phpTypes[strtolower($type)]) === true) { + if (isset(self::PHP_TYPES[strtolower($type)]) === true) { $this->processType($phpcsFile, $startOfType, $type, $error, $errorCode); } } @@ -182,7 +182,7 @@ public function process(File $phpcsFile, $stackPtr) $error, $errorCode ); - } else if (isset($this->phpTypes[strtolower($type)]) === true) { + } else if (isset(self::PHP_TYPES[strtolower($type)]) === true) { $this->processType($phpcsFile, $props['type_token'], $type, $error, $errorCode); } } @@ -213,7 +213,7 @@ public function process(File $phpcsFile, $stackPtr) $error, $errorCode ); - } else if (isset($this->phpTypes[strtolower($returnType)]) === true) { + } else if (isset(self::PHP_TYPES[strtolower($returnType)]) === true) { $this->processType($phpcsFile, $props['return_type_token'], $returnType, $error, $errorCode); } } @@ -244,7 +244,7 @@ public function process(File $phpcsFile, $stackPtr) $error, $errorCode ); - } else if (isset($this->phpTypes[strtolower($typeHint)]) === true) { + } else if (isset(self::PHP_TYPES[strtolower($typeHint)]) === true) { $this->processType($phpcsFile, $param['type_hint_token'], $typeHint, $error, $errorCode); } } @@ -285,7 +285,7 @@ protected function processUnionType(File $phpcsFile, $typeDeclStart, $typeDeclEn ) { if ($typeTokenCount === 1 && $type !== '' - && isset($this->phpTypes[strtolower($type)]) === true + && isset(self::PHP_TYPES[strtolower($type)]) === true ) { $this->processType($phpcsFile, $typeStart, $type, $error, $errorCode); } @@ -309,7 +309,7 @@ protected function processUnionType(File $phpcsFile, $typeDeclStart, $typeDeclEn // Handle type at end of type string. if ($typeTokenCount === 1 && $type !== '' - && isset($this->phpTypes[strtolower($type)]) === true + && isset(self::PHP_TYPES[strtolower($type)]) === true ) { $this->processType($phpcsFile, $typeStart, $type, $error, $errorCode); } From 4ca7517e6fb74ced014323cadbed1e02fabaa67b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 20 Apr 2025 05:56:02 +0200 Subject: [PATCH 6/9] Modernize: Generic/UnnecessaryHeredoc: use class constant for constant array --- .../Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php b/src/Standards/Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php index aa81dc2ec5..e6c04dbfd4 100644 --- a/src/Standards/Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php +++ b/src/Standards/Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php @@ -20,7 +20,7 @@ class UnnecessaryHeredocSniff implements Sniff * * @var array */ - private $escapeChars = [ + private const ESCAPE_CHARS = [ // Octal sequences. '\0', '\1', @@ -111,7 +111,7 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->recordMetric($stackPtr, 'Heredoc contains interpolation or expression', 'no'); // Check for escape sequences which aren't supported in nowdocs. - foreach ($this->escapeChars as $testChar) { + foreach (self::ESCAPE_CHARS as $testChar) { if (strpos($body, $testChar) !== false) { return; } From c9590ea4ec8e10004f4961047abbc5e4b8350d5f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 7 Apr 2025 03:09:09 +0200 Subject: [PATCH 7/9] Modernize: Generic/ArbitraryParenthesesSpacing: use class constant for constant array --- .../ArbitraryParenthesesSpacingSniff.php | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/Standards/Generic/Sniffs/WhiteSpace/ArbitraryParenthesesSpacingSniff.php b/src/Standards/Generic/Sniffs/WhiteSpace/ArbitraryParenthesesSpacingSniff.php index 0980c4795f..1e03894d36 100644 --- a/src/Standards/Generic/Sniffs/WhiteSpace/ArbitraryParenthesesSpacingSniff.php +++ b/src/Standards/Generic/Sniffs/WhiteSpace/ArbitraryParenthesesSpacingSniff.php @@ -19,6 +19,24 @@ class ArbitraryParenthesesSpacingSniff implements Sniff { + /** + * Tokens which when they precede an open parenthesis indicate + * that this is a type of structure this sniff should ignore. + * + * @var array + */ + private const IGNORE_TOKENS = (Tokens::FUNCTION_NAME_TOKENS + [ + T_VARIABLE => T_VARIABLE, + T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS, + T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET, + T_CLOSE_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET, + T_CLOSE_SHORT_ARRAY => T_CLOSE_SHORT_ARRAY, + T_THROW => T_THROW, + T_YIELD => T_YIELD, + T_YIELD_FROM => T_YIELD_FROM, + T_CLONE => T_CLONE, + ]); + /** * The number of spaces desired on the inside of the parentheses. * @@ -33,14 +51,6 @@ class ArbitraryParenthesesSpacingSniff implements Sniff */ public $ignoreNewlines = false; - /** - * Tokens which when they precede an open parenthesis indicate - * that this is a type of structure this sniff should ignore. - * - * @var array - */ - private $ignoreTokens = []; - /** * Returns an array of tokens this test wants to listen for. @@ -49,19 +59,6 @@ class ArbitraryParenthesesSpacingSniff implements Sniff */ public function register() { - $this->ignoreTokens = Tokens::FUNCTION_NAME_TOKENS; - - $this->ignoreTokens[T_VARIABLE] = T_VARIABLE; - $this->ignoreTokens[T_CLOSE_PARENTHESIS] = T_CLOSE_PARENTHESIS; - $this->ignoreTokens[T_CLOSE_CURLY_BRACKET] = T_CLOSE_CURLY_BRACKET; - $this->ignoreTokens[T_CLOSE_SQUARE_BRACKET] = T_CLOSE_SQUARE_BRACKET; - $this->ignoreTokens[T_CLOSE_SHORT_ARRAY] = T_CLOSE_SHORT_ARRAY; - - $this->ignoreTokens[T_THROW] = T_THROW; - $this->ignoreTokens[T_YIELD] = T_YIELD; - $this->ignoreTokens[T_YIELD_FROM] = T_YIELD_FROM; - $this->ignoreTokens[T_CLONE] = T_CLONE; - return [ T_OPEN_PARENTHESIS, T_CLOSE_PARENTHESIS, @@ -101,7 +98,7 @@ public function process(File $phpcsFile, $stackPtr) $preOpener = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($opener - 1), null, true); if ($preOpener !== false - && isset($this->ignoreTokens[$tokens[$preOpener]['code']]) === true + && isset(self::IGNORE_TOKENS[$tokens[$preOpener]['code']]) === true && ($tokens[$preOpener]['code'] !== T_CLOSE_CURLY_BRACKET || isset($tokens[$preOpener]['scope_condition']) === false ) ) { From b1840505f61368b71a735593fb7e5ed7a4dce282 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 21 Apr 2025 07:41:32 +0200 Subject: [PATCH 8/9] Modernize: Generic/DisallowSpaceIndent: use class constant for constant array --- .../WhiteSpace/DisallowSpaceIndentSniff.php | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Standards/Generic/Sniffs/WhiteSpace/DisallowSpaceIndentSniff.php b/src/Standards/Generic/Sniffs/WhiteSpace/DisallowSpaceIndentSniff.php index 79d3469d40..e6fbfab040 100644 --- a/src/Standards/Generic/Sniffs/WhiteSpace/DisallowSpaceIndentSniff.php +++ b/src/Standards/Generic/Sniffs/WhiteSpace/DisallowSpaceIndentSniff.php @@ -15,6 +15,20 @@ class DisallowSpaceIndentSniff implements Sniff { + /** + * Tokens which can include indentation. + * + * @var array + */ + private const TOKENS_CONTAINING_INDENT = [ + T_WHITESPACE => true, + T_INLINE_HTML => true, + T_DOC_COMMENT_WHITESPACE => true, + T_COMMENT => true, + T_END_HEREDOC => true, + T_END_NOWDOC => true, + ]; + /** * The --tab-width CLI value that is being used. * @@ -62,20 +76,11 @@ public function process(File $phpcsFile, $stackPtr) } } - $checkTokens = [ - T_WHITESPACE => true, - T_INLINE_HTML => true, - T_DOC_COMMENT_WHITESPACE => true, - T_COMMENT => true, - T_END_HEREDOC => true, - T_END_NOWDOC => true, - ]; - $eolLen = strlen($phpcsFile->eolChar); $tokens = $phpcsFile->getTokens(); for ($i = 0; $i < $phpcsFile->numTokens; $i++) { - if ($tokens[$i]['column'] !== 1 || isset($checkTokens[$tokens[$i]['code']]) === false) { + if ($tokens[$i]['column'] !== 1 || isset(self::TOKENS_CONTAINING_INDENT[$tokens[$i]['code']]) === false) { continue; } From f8f929606629341d67d75fc59b887571ab42e3eb Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 21 Apr 2025 07:41:43 +0200 Subject: [PATCH 9/9] Modernize: Generic/DisallowTabIndent: use class constant for constant array --- .../WhiteSpace/DisallowTabIndentSniff.php | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php b/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php index ba431adee6..9409322ee5 100644 --- a/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php +++ b/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php @@ -15,6 +15,22 @@ class DisallowTabIndentSniff implements Sniff { + /** + * Tokens which can include indentation. + * + * @var array + */ + private const TOKENS_CONTAINING_INDENT = [ + T_WHITESPACE => true, + T_INLINE_HTML => true, + T_DOC_COMMENT_WHITESPACE => true, + T_DOC_COMMENT_STRING => true, + T_COMMENT => true, + T_END_HEREDOC => true, + T_END_NOWDOC => true, + T_YIELD_FROM => true, + ]; + /** * The --tab-width CLI value that is being used. * @@ -58,20 +74,10 @@ public function process(File $phpcsFile, $stackPtr) } } - $tokens = $phpcsFile->getTokens(); - $checkTokens = [ - T_WHITESPACE => true, - T_INLINE_HTML => true, - T_DOC_COMMENT_WHITESPACE => true, - T_DOC_COMMENT_STRING => true, - T_COMMENT => true, - T_END_HEREDOC => true, - T_END_NOWDOC => true, - T_YIELD_FROM => true, - ]; + $tokens = $phpcsFile->getTokens(); for ($i = 0; $i < $phpcsFile->numTokens; $i++) { - if (isset($checkTokens[$tokens[$i]['code']]) === false) { + if (isset(self::TOKENS_CONTAINING_INDENT[$tokens[$i]['code']]) === false) { continue; }