From f1995c57cd8d796cd4363d444c6fa4c4ee78aa1e Mon Sep 17 00:00:00 2001 From: Michael Moll Date: Mon, 16 Dec 2024 21:29:04 +0100 Subject: [PATCH 1/7] raise minimum PHPStan version --- composer.json | 4 ++-- phpstan.neon | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 475b6a22..7fa6848b 100644 --- a/composer.json +++ b/composer.json @@ -32,8 +32,8 @@ "require-dev": { "ergebnis/composer-normalize": ">=2.19 <2.30", "phan/phan": "^5.4.2", - "phpstan/phpstan": "^1.0", - "phpstan/phpstan-strict-rules": "^1.0", + "phpstan/phpstan": "^1.12", + "phpstan/phpstan-strict-rules": "^1.6", "phpunit/phpunit": "^7.5.20 || ^8.5.36 || ^9.6.15", "psalm/plugin-phpunit": "^0.18", "vimeo/psalm": "^4.30" diff --git a/phpstan.neon b/phpstan.neon index 603b51ee..a42fc6b5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,6 +5,7 @@ parameters: paths: - %rootDir%/../../../MO4 - %rootDir%/../../../tests - checkMissingIterableValueType: false + ignoreErrors: + - identifier: missingType.iterableValue includes: - vendor/phpstan/phpstan-strict-rules/rules.neon From 0a95eed05c8e304d12e5bfa3f75b2dd086e94c7a Mon Sep 17 00:00:00 2001 From: Michael Moll Date: Mon, 16 Dec 2024 21:31:32 +0100 Subject: [PATCH 2/7] Update psalm to 6.x and minimum PHP to 8.1 --- .github/workflows/ci.yml | 7 ++----- MO4/Library/PregLibrary.php | 1 - MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php | 2 ++ MO4/Sniffs/Arrays/MultiLineArraySniff.php | 2 ++ MO4/Sniffs/Commenting/PropertyCommentSniff.php | 2 ++ MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php | 5 ++++- MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php | 3 +++ MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php | 2 ++ MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php | 2 ++ MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php | 3 +++ composer.json | 8 ++++---- 11 files changed, 26 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4e11d3aa..459f1a7b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,13 +40,10 @@ jobs: matrix: os: [ubuntu-latest] php_version: - - 7.2 - - 7.3 - - 7.4 - - 8.0 - 8.1 - 8.2 - 8.3 + - 8.4 dependencies_level: - --prefer-lowest - "" @@ -93,7 +90,7 @@ jobs: run: vendor/bin/phpstan analyse --no-progress - name: Run psalm if: ${{ matrix.os != 'windows-latest' }} - run: vendor/bin/psalm --show-info=true + run: vendor/bin/psalm - name: Run phan if: ${{ matrix.os != 'windows-latest' }} run: vendor/bin/phan diff --git a/MO4/Library/PregLibrary.php b/MO4/Library/PregLibrary.php index b694108a..b41efcd9 100644 --- a/MO4/Library/PregLibrary.php +++ b/MO4/Library/PregLibrary.php @@ -43,7 +43,6 @@ public static function MO4PregSplit(string $pattern, string $subject, int $limit { $pregSplitResult = \preg_split($pattern, $subject, $limit, $flags); - // @phan-suppress-next-line PhanTypeComparisonToArray if (false === $pregSplitResult) { throw new RuntimeException('Unexpected Error in MO4 Coding Standard.'); } diff --git a/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php b/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php index d4ee76b0..50f17c2f 100644 --- a/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php +++ b/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php @@ -30,6 +30,8 @@ * @license http://spdx.org/licenses/MIT MIT License * * @link https://github.com/mayflower/mo4-coding-standard + * + * @psalm-api */ class ArrayDoubleArrowAlignmentSniff implements Sniff { diff --git a/MO4/Sniffs/Arrays/MultiLineArraySniff.php b/MO4/Sniffs/Arrays/MultiLineArraySniff.php index 596f9dc9..6ed29d4a 100644 --- a/MO4/Sniffs/Arrays/MultiLineArraySniff.php +++ b/MO4/Sniffs/Arrays/MultiLineArraySniff.php @@ -27,6 +27,8 @@ * @license http://spdx.org/licenses/MIT MIT License * * @link https://github.com/mayflower/mo4-coding-standard + * + * @psalm-api */ class MultiLineArraySniff implements Sniff { diff --git a/MO4/Sniffs/Commenting/PropertyCommentSniff.php b/MO4/Sniffs/Commenting/PropertyCommentSniff.php index 2a6e0ac5..95dd875b 100644 --- a/MO4/Sniffs/Commenting/PropertyCommentSniff.php +++ b/MO4/Sniffs/Commenting/PropertyCommentSniff.php @@ -32,6 +32,8 @@ * @license http://spdx.org/licenses/MIT MIT License * * @link https://github.com/mayflower/mo4-coding-standard + * + * @psalm-api */ class PropertyCommentSniff extends AbstractScopeSniff { diff --git a/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php b/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php index 86f00e2f..a7a6c009 100644 --- a/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php +++ b/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php @@ -33,6 +33,8 @@ * @license http://spdx.org/licenses/MIT MIT License * * @link https://github.com/mayflower/mo4-coding-standard + * + * @psalm-api */ class AlphabeticalUseStatementsSniff extends UseDeclarationSniff { @@ -292,7 +294,8 @@ private function findNewDestination(File $phpcsFile, int $stackPtr, string $impo { $tokens = $phpcsFile->getTokens(); - $line = $tokens[$stackPtr]['line']; + $line = $tokens[$stackPtr]['line']; + /** @var int|bool $prevLine */ $prevLine = false; $prevPtr = $stackPtr; diff --git a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php index 28860e98..7313616b 100644 --- a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php +++ b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php @@ -34,6 +34,8 @@ * @license http://spdx.org/licenses/MIT MIT License * * @link https://github.com/mayflower/mo4-coding-standard + * + * @psalm-api */ class UnnecessaryNamespaceUsageSniff implements Sniff { @@ -381,6 +383,7 @@ private function checkShorthandPossible(File $phpcsFile, array $useStatements, s if (true === $isDocBlock) { $tokens = $phpcsFile->getTokens(); $oldContent = $tokens[$startPtr]['content']; + /** @var string $newContent */ $newContent = \str_replace($className, $replacement, $oldContent); $phpcsFile->fixer->replaceToken($startPtr, $newContent); } else { diff --git a/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php b/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php index 9cb14753..b535d30b 100644 --- a/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php +++ b/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php @@ -29,6 +29,8 @@ * @license http://spdx.org/licenses/MIT MIT License * * @link https://github.com/mayflower/mo4-coding-standard + * + * @psalm-api */ class VariableInDoubleQuotedStringSniff implements Sniff { diff --git a/MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php b/MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php index 7784f6b6..ed614655 100644 --- a/MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php +++ b/MO4/Sniffs/WhiteSpace/ConstantSpacingSniff.php @@ -27,6 +27,8 @@ * @license http://spdx.org/licenses/MIT MIT License * * @link https://github.com/mayflower/mo4-coding-standard + * + * @psalm-api */ class ConstantSpacingSniff implements Sniff { diff --git a/MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php b/MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php index e52853cd..093e22ca 100644 --- a/MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php +++ b/MO4/Sniffs/WhiteSpace/MultipleEmptyLinesSniff.php @@ -13,6 +13,9 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +/** + * @psalm-api + */ class MultipleEmptyLinesSniff implements Sniff { /** diff --git a/composer.json b/composer.json index 7fa6848b..0bb7051d 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "source": "https://github.com/mayflower/mo4-coding-standard" }, "require": { - "php": "~7.2 || ~8.0", + "php": "~8.1", "dealerdirect/phpcodesniffer-composer-installer": "~0.7 || ~1.0", "escapestudios/symfony2-coding-standard": "^3.10.0", "slevomat/coding-standard": "^8.14", @@ -34,9 +34,9 @@ "phan/phan": "^5.4.2", "phpstan/phpstan": "^1.12", "phpstan/phpstan-strict-rules": "^1.6", - "phpunit/phpunit": "^7.5.20 || ^8.5.36 || ^9.6.15", - "psalm/plugin-phpunit": "^0.18", - "vimeo/psalm": "^4.30" + "phpunit/phpunit": "^8.5.36 || ^9.6.15", + "psalm/plugin-phpunit": "^0.19", + "vimeo/psalm": "^6.0.0" }, "config": { "allow-plugins": { From d0a3772ca5ead42fa3f436c16e3ac58d11060b79 Mon Sep 17 00:00:00 2001 From: Michael Moll Date: Sun, 26 Jan 2025 15:40:33 +0100 Subject: [PATCH 3/7] Update composer-normalize --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 0bb7051d..91375587 100644 --- a/composer.json +++ b/composer.json @@ -23,14 +23,14 @@ "source": "https://github.com/mayflower/mo4-coding-standard" }, "require": { - "php": "~8.1", - "dealerdirect/phpcodesniffer-composer-installer": "~0.7 || ~1.0", + "php": "^8.1", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || ^1.0", "escapestudios/symfony2-coding-standard": "^3.10.0", "slevomat/coding-standard": "^8.14", "squizlabs/php_codesniffer": "^3.8.0" }, "require-dev": { - "ergebnis/composer-normalize": ">=2.19 <2.30", + "ergebnis/composer-normalize": "^2.45", "phan/phan": "^5.4.2", "phpstan/phpstan": "^1.12", "phpstan/phpstan-strict-rules": "^1.6", From be479f985e3aefba166f2e92b08b0d1fd74cf796 Mon Sep 17 00:00:00 2001 From: Michael Moll Date: Sun, 26 Jan 2025 15:44:47 +0100 Subject: [PATCH 4/7] Fix --prefer-lowest CI checks --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 91375587..ba4a0cdd 100644 --- a/composer.json +++ b/composer.json @@ -31,11 +31,13 @@ }, "require-dev": { "ergebnis/composer-normalize": "^2.45", - "phan/phan": "^5.4.2", + "phan/phan": "^5.4.5", "phpstan/phpstan": "^1.12", "phpstan/phpstan-strict-rules": "^1.6", "phpunit/phpunit": "^8.5.36 || ^9.6.15", "psalm/plugin-phpunit": "^0.19", + "sabre/event": "^5.1.6", + "symfony/filesystem": ">= 5.4.45", "vimeo/psalm": "^6.0.0" }, "config": { From f1004ef23450fcd0a6d1d746dc9a2fe0b66342ed Mon Sep 17 00:00:00 2001 From: Michael Moll Date: Sun, 26 Jan 2025 17:37:34 +0100 Subject: [PATCH 5/7] Use PHP 8 language contructs --- MO4/Sniffs/Commenting/PropertyCommentSniff.php | 4 ++-- .../AlphabeticalUseStatementsSniff.php | 18 +++++++----------- .../UnnecessaryNamespaceUsageSniff.php | 2 +- .../VariableInDoubleQuotedStringSniff.php | 2 +- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/MO4/Sniffs/Commenting/PropertyCommentSniff.php b/MO4/Sniffs/Commenting/PropertyCommentSniff.php index 95dd875b..7696481b 100644 --- a/MO4/Sniffs/Commenting/PropertyCommentSniff.php +++ b/MO4/Sniffs/Commenting/PropertyCommentSniff.php @@ -111,8 +111,8 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $stackPtr, 'NoDocBlockAllowed' ); - } elseif (0 !== \strncmp($tokens[$postComment]['content'], '//', 2) - && '*/' !== \substr($tokens[$postComment]['content'], -2) + } elseif (!\str_starts_with($tokens[$postComment]['content'], '//') + && !\str_ends_with($tokens[$postComment]['content'], '*/') ) { $phpcsFile->addError( 'no multiline comments after declarations allowed', diff --git a/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php b/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php index a7a6c009..58f47ade 100644 --- a/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php +++ b/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php @@ -335,17 +335,13 @@ private function findNewDestination(File $phpcsFile, int $stackPtr, string $impo */ private function compareString(string $a, string $b): int { - switch ($this->order) { - case 'string': - return \strcmp($a, $b); - case 'string-locale': - return \strcoll($a, $b); - case 'string-case-insensitive': - return \strcasecmp($a, $b); - default: - // Default is 'dictionary'. - return $this->dictionaryCompare($a, $b); - } + return match ($this->order) { + 'string' => \strcmp($a, $b), + 'string-locale' => \strcoll($a, $b), + 'string-case-insensitive' => \strcasecmp($a, $b), + // Default is 'dictionary'. + default => $this->dictionaryCompare($a, $b), + }; } /** diff --git a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php index 7313616b..8d0297d1 100644 --- a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php +++ b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php @@ -359,7 +359,7 @@ private function checkShorthandPossible(File $phpcsFile, array $useStatements, s ); $replaceClassName = true; - } elseif ('' !== $namespace && 0 === \strpos($fullClassName, $namespace)) { + } elseif ('' !== $namespace && \str_starts_with($fullClassName, $namespace)) { $replacement = \substr($fullClassName, \strlen($namespace)); $data = [ diff --git a/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php b/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php index b535d30b..fc5007ac 100644 --- a/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php +++ b/MO4/Sniffs/Strings/VariableInDoubleQuotedStringSniff.php @@ -78,7 +78,7 @@ public function process(File $phpcsFile, $stackPtr): void } if (\strpos(\substr($content, 0, $pos), '{') > 0 - && false === \strpos(\substr($content, 0, $pos), '}') + && !\str_contains(\substr($content, 0, $pos), '}') ) { continue; } From c9e599faba557b0dc64adc86a5284b179d0cbf79 Mon Sep 17 00:00:00 2001 From: Michael Moll Date: Sun, 26 Jan 2025 17:42:10 +0100 Subject: [PATCH 6/7] Optimize boolean conditions --- MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php | 4 ++-- MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php | 2 +- MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php b/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php index 50f17c2f..1ccf49cb 100644 --- a/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php +++ b/MO4/Sniffs/Arrays/ArrayDoubleArrowAlignmentSniff.php @@ -98,7 +98,7 @@ public function process(File $phpcsFile, $stackPtr): void $previous = $tokens[($i - 1)]; // Skip nested arrays. - if (true === \in_array($current['code'], $this->arrayTokens, true)) { + if (\in_array($current['code'], $this->arrayTokens, true)) { $i = T_ARRAY === $current['code'] ? ($current['parenthesis_closer'] + 1) : ($current['bracket_closer'] + 1); continue; @@ -147,7 +147,7 @@ public function process(File $phpcsFile, $stackPtr): void $j = ($i - 1); while (($j >= 0) && ($tokens[$j]['line'] === $current['line'])) { - if (false === \in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::$emptyTokens, true)) { + if (!\in_array($tokens[$j]['code'], PHP_CodeSniffer_Tokens::$emptyTokens, true)) { $hasKeyInLine = true; } diff --git a/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php b/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php index 58f47ade..43fe70ee 100644 --- a/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php +++ b/MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php @@ -91,7 +91,7 @@ class AlphabeticalUseStatementsSniff extends UseDeclarationSniff */ public function process(File $phpcsFile, $stackPtr): void { - if (false === \in_array($this->order, self::SUPPORTED_ORDERING_METHODS, true)) { + if (!\in_array($this->order, self::SUPPORTED_ORDERING_METHODS, true)) { $error = \sprintf( "'%s' is not a valid order function for %s! Pick one of: %s", $this->order, diff --git a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php index 8d0297d1..c755491d 100644 --- a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php +++ b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php @@ -127,7 +127,7 @@ public function process(File $phpcsFile, $stackPtr): void foreach ($tokens[$nsSep]['comment_tags'] as $tag) { $content = $tokens[$tag]['content']; - if (false === \array_key_exists($content, $docCommentTags)) { + if (!\array_key_exists($content, $docCommentTags)) { continue; } @@ -181,7 +181,7 @@ public function process(File $phpcsFile, $stackPtr): void // phpcs:enable foreach ($typeTokens as $typeToken) { - if (true === \in_array($typeToken, $useStatements, true)) { + if (\in_array($typeToken, $useStatements, true)) { continue; } @@ -343,7 +343,7 @@ private function checkShorthandPossible(File $phpcsFile, array $useStatements, s $fullClassName = $this->getFullyQualifiedClassName($className); - if (true === \array_key_exists($fullClassName, $useStatements)) { + if (\array_key_exists($fullClassName, $useStatements)) { $replacement = $useStatements[$fullClassName]; $data = [ From 3a6f726ef7665657819fd2076ef28c082326206a Mon Sep 17 00:00:00 2001 From: Michael Moll Date: Sun, 26 Jan 2025 19:02:11 +0100 Subject: [PATCH 7/7] Raise minimum PHPUnit version --- composer.json | 2 +- phpunit.xml.dist | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index ba4a0cdd..c00fb83b 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "phan/phan": "^5.4.5", "phpstan/phpstan": "^1.12", "phpstan/phpstan-strict-rules": "^1.6", - "phpunit/phpunit": "^8.5.36 || ^9.6.15", + "phpunit/phpunit": "^9.6.15", "psalm/plugin-phpunit": "^0.19", "sabre/event": "^5.1.6", "symfony/filesystem": ">= 5.4.45", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f1c788d3..6f0aabe5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,7 @@ vendor/squizlabs/php_codesniffer/tests/Standards/AllSniffs.php - - + + ./MO4/Sniffs - - + +