From 833c7b65bdd02a8359199f42a941b74aee18a3f1 Mon Sep 17 00:00:00 2001 From: Michael Moll Date: Mon, 18 Aug 2025 18:39:44 +0200 Subject: [PATCH] Update ruleset for modern (PHP 8+) code --- .../Arrays/ArrayDoubleArrowAlignmentSniff.php | 1 - MO4/Sniffs/Arrays/MultiLineArraySniff.php | 1 - .../Commenting/PropertyCommentSniff.php | 2 - .../AlphabeticalUseStatementsSniff.php | 9 +--- .../UnnecessaryNamespaceUsageSniff.php | 20 +++----- .../VariableInDoubleQuotedStringSniff.php | 3 -- .../WhiteSpace/ConstantSpacingSniff.php | 1 - .../WhiteSpace/MultipleEmptyLinesSniff.php | 2 +- MO4/Tests/AbstractMo4SniffUnitTest.php | 4 +- MO4/ruleset.xml | 49 ++++++++++++++----- composer.json | 2 +- integrationtests/testfile.php | 10 ++-- 12 files changed, 55 insertions(+), 49 deletions(-) diff --git a/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php b/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php index dd0ea4a3..ff3fd311 100644 --- a/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php +++ b/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php @@ -68,7 +68,6 @@ public function register(): array * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * - * @return void */ #[\Override] public function process(File $phpcsFile, $stackPtr): void diff --git a/MO4/Sniffs/Arrays/MultiLineArraySniff.php b/MO4/Sniffs/Arrays/MultiLineArraySniff.php index b83d1e7e..f40cb9ca 100644 --- a/MO4/Sniffs/Arrays/MultiLineArraySniff.php +++ b/MO4/Sniffs/Arrays/MultiLineArraySniff.php @@ -65,7 +65,6 @@ public function register(): array * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * - * @return void */ #[\Override] public function process(File $phpcsFile, $stackPtr): void diff --git a/MO4/Sniffs/Commenting/PropertyCommentSniff.php b/MO4/Sniffs/Commenting/PropertyCommentSniff.php index fde822be..d5be5c7a 100644 --- a/MO4/Sniffs/Commenting/PropertyCommentSniff.php +++ b/MO4/Sniffs/Commenting/PropertyCommentSniff.php @@ -73,7 +73,6 @@ public function __construct() * opened the scope that this test is * listening for. * - * @return void * * @throws RuntimeException */ @@ -244,7 +243,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop * @param int $stackPtr The position in the stack where this * token was found. * - * @return void */ #[\Override] protected function processTokenOutsideScope(File $phpcsFile, $stackPtr): void diff --git a/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php b/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php index bf096562..fb17981e 100644 --- a/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php +++ b/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php @@ -87,7 +87,6 @@ class AlphabeticalUseStatementsSniff extends UseDeclarationSniff * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * - * @return void */ #[\Override] public function process(File $phpcsFile, $stackPtr): void @@ -172,7 +171,7 @@ public function process(File $phpcsFile, $stackPtr): void * @param File $phpcsFile PHP CS File * @param int $stackPtr pointer * - * @return array|false + * @return array{startPtr: int, content: string}|false */ private function getUseImport(File $phpcsFile, int $stackPtr) { @@ -208,7 +207,6 @@ private function getUseImport(File $phpcsFile, int $stackPtr) * @param File $phpcsFile PHP CS File * @param int $stackPtr pointer * - * @return string */ private function getUseStatementAsString(File $phpcsFile, int $stackPtr): string { @@ -231,7 +229,6 @@ private function getUseStatementAsString(File $phpcsFile, int $stackPtr): string * @param File $phpcsFile PHP CS File * @param int $stackPtr pointer * - * @return bool */ private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool { @@ -265,7 +262,6 @@ private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool * @param File $phpcsFile PHP CS file * @param int $stackPtr pointer * - * @return void */ private function fixerClearLine(File $phpcsFile, int $stackPtr): void { @@ -289,7 +285,6 @@ private function fixerClearLine(File $phpcsFile, int $stackPtr): void * @param int $stackPtr pointer * @param string $import import string requiring new position * - * @return int */ private function findNewDestination(File $phpcsFile, int $stackPtr, string $import): int { @@ -332,7 +327,6 @@ private function findNewDestination(File $phpcsFile, int $stackPtr, string $impo * @param string $a first namespace string * @param string $b second namespace string * - * @return int */ private function compareString(string $a, string $b): int { @@ -357,7 +351,6 @@ private function compareString(string $a, string $b): int * @param string $a first namespace string * @param string $b second namespace string * - * @return int */ private function dictionaryCompare(string $a, string $b): int { diff --git a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php index 28620a2a..5b1043ea 100644 --- a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php +++ b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php @@ -74,7 +74,6 @@ public function register(): array * file's token stack where the token * was found. * - * @return void * * @throws RuntimeException */ @@ -212,7 +211,7 @@ public function process(File $phpcsFile, $stackPtr): void * @param int $start start pointer * @param int $end end pointer * - * @return array + * @return array */ protected function getUseStatements(File $phpcsFile, int $start, int $end): array { @@ -281,7 +280,6 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra * @param int $start start pointer * @param int $end end pointer * - * @return string */ protected function getNamespace(File $phpcsFile, int $start, int $end): string { @@ -315,7 +313,6 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string * * @param string $className class name * - * @return string */ private function getFullyQualifiedClassName(string $className): string { @@ -325,15 +322,14 @@ private function getFullyQualifiedClassName(string $className): string /** * Check if short hand is possible. * - * @param File $phpcsFile PHP CS File - * @param array $useStatements array with class use statements - * @param string $className class name - * @param string $namespace name space - * @param int $startPtr start token pointer - * @param int $endPtr end token pointer - * @param bool $isDocBlock true if fixing doc block + * @param File $phpcsFile PHP CS File + * @param array $useStatements array with class use statements + * @param string $className class name + * @param string $namespace name space + * @param int $startPtr start token pointer + * @param int $endPtr end token pointer + * @param bool $isDocBlock true if fixing doc block * - * @return void */ private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr, int $endPtr, bool $isDocBlock = false): void { diff --git a/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php b/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php index 10c7ed8c..f359e61d 100644 --- a/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php +++ b/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php @@ -59,7 +59,6 @@ public function register(): array * file's token stack where the token * was found. * - * @return void */ #[\Override] public function process(File $phpcsFile, $stackPtr): void @@ -130,7 +129,6 @@ public function process(File $phpcsFile, $stackPtr): void * @param int $pos position * @param string $var variable * - * @return string */ private function surroundVariableWithBraces(string $content, int $pos, string $var): string { @@ -147,7 +145,6 @@ private function surroundVariableWithBraces(string $content, int $pos, string $v * @param string $correctVariable correct variable * @param File $phpCsFile PHP_CodeSniffer File object * - * @return void */ private function fixPhpCsFile(int $stackPtr, string $correctVariable, File $phpCsFile): void { diff --git a/MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php b/MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php index 0b0dc95a..5f0ac1ad 100644 --- a/MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php +++ b/MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php @@ -63,7 +63,6 @@ public function register(): array * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * - * @return void */ #[\Override] public function process(File $phpcsFile, $stackPtr): void diff --git a/MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php b/MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php index fb7cfc5c..9cf499c8 100644 --- a/MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php +++ b/MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php @@ -70,7 +70,7 @@ public function process(File $phpcsFile, $stackPtr) $next = $stackPtr + 1; while (isset($tokens[$next + 1]) && - $tokens[$next]['code'] === T_WHITESPACE && + T_WHITESPACE === $tokens[$next]['code'] && $tokens[$next]['line'] !== $tokens[$next + 1]['line'] ) { $next++; diff --git a/MO4/Tests/AbstractMo4SniffUnitTest.php b/MO4/Tests/AbstractMo4SniffUnitTest.php index 875462a7..a9f5ae23 100644 --- a/MO4/Tests/AbstractMo4SniffUnitTest.php +++ b/MO4/Tests/AbstractMo4SniffUnitTest.php @@ -98,8 +98,8 @@ protected function getWarningList(string $testFile = ''): array * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * - * @param string $testFile test file - * @param array $list record for test file + * @param string $testFile test file + * @param array> $list record for test file * * @return array * diff --git a/MO4/ruleset.xml b/MO4/ruleset.xml index 6935241a..699771bf 100644 --- a/MO4/ruleset.xml +++ b/MO4/ruleset.xml @@ -14,6 +14,10 @@ + + + + @@ -131,8 +135,16 @@ + + + + + + + + @@ -159,6 +171,10 @@ + + + + @@ -166,6 +182,10 @@ + + + + @@ -229,19 +249,15 @@ - - - - - - - + - 0 + 9 - 0 + 9 + + @@ -251,10 +267,21 @@ - 0 + 9 - 0 + 9 + + + + + + + + + + + diff --git a/composer.json b/composer.json index c59be4e3..9240e2ae 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "php": "^8.1", "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0", "escapestudios/symfony2-coding-standard": "^3.16.0", - "slevomat/coding-standard": "^8.14", + "slevomat/coding-standard": "^8.20", "squizlabs/php_codesniffer": "^3.8.0" }, "require-dev": { diff --git a/integrationtests/testfile.php b/integrationtests/testfile.php index 543cbabf..9f4c2494 100644 --- a/integrationtests/testfile.php +++ b/integrationtests/testfile.php @@ -39,8 +39,6 @@ public function __construct(string $dummy) } /** - * @return string - * * @deprecated */ public function someDeprecatedMethod(): string @@ -53,14 +51,14 @@ public function someDeprecatedMethod(): string /** * Transforms the input given as first argument. * - * @param bool|string $dummy Some argument description - * @param array $options An options collection to be used within the transformation + * @param bool|string $dummy Some argument description + * @param array $options An options collection to be used within the transformation * * @return string|null The transformed input * * @throws \RuntimeException When an invalid option is provided */ - private function transformText($dummy, array $options = []): ?string + private function transformText(bool|string $dummy, array $options = []): ?string { /** @var array $defaultOptions */ $defaultOptions = [ @@ -104,7 +102,7 @@ private function transformText($dummy, array $options = []): ?string * * @return bool|void The resultant check if $theSwitch isn't false, void otherwise */ - private function reverseBoolean($value = null, bool $theSwitch = false) + private function reverseBoolean(mixed $value = null, bool $theSwitch = false) { if (!$theSwitch) { return;