From 7e51868cd0cab82d3a89cdd87eb7adf6432b9b80 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 23 Nov 2024 13:18:41 +0530 Subject: [PATCH 01/14] Add rule for trailing command in function call. --- CakePHP/ruleset.xml | 1 + docs/README.md | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CakePHP/ruleset.xml b/CakePHP/ruleset.xml index c5eaf3d..95de8a9 100644 --- a/CakePHP/ruleset.xml +++ b/CakePHP/ruleset.xml @@ -219,6 +219,7 @@ + diff --git a/docs/README.md b/docs/README.md index ade369b..b42dfc2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ # CakePHP ruleset -The CakePHP standard contains 144 sniffs +The CakePHP standard contains 147 sniffs CakePHP (20 sniffs) ------------------- @@ -95,9 +95,11 @@ PSR12 (17 sniffs) - PSR12.Properties.ConstantVisibility - PSR12.Traits.UseDeclaration -SlevomatCodingStandard (40 sniffs) +SlevomatCodingStandard (43 sniffs) ---------------------------------- - SlevomatCodingStandard.Arrays.TrailingArrayComma +- SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing +- SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment - SlevomatCodingStandard.Classes.ClassConstantVisibility - SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces - SlevomatCodingStandard.Classes.ModernClassNameReference @@ -114,6 +116,7 @@ SlevomatCodingStandard (40 sniffs) - SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator - SlevomatCodingStandard.Exceptions.DeadCatch - SlevomatCodingStandard.Functions.ArrowFunctionDeclaration +- SlevomatCodingStandard.Functions.RequireTrailingCommaInCall - SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses - SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation - SlevomatCodingStandard.Namespaces.NamespaceDeclaration From 127525805cbffce03293c6ba4c47199ef763026a Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 23 Nov 2024 13:21:55 +0530 Subject: [PATCH 02/14] Update CI workflow --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a98bd5..46a44f0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,11 +11,11 @@ on: jobs: testsuite: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 strategy: fail-fast: false matrix: - php-version: ['8.1', '8.2', '8.3'] + php-version: ['8.1', '8.2', '8.3', '8.4'] dependencies: ['highest'] include: - php-version: '8.1' @@ -40,10 +40,10 @@ jobs: cs-stan: name: Coding Standard & Static Analysis - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 From ae3ab8bd7f3e176808dd2e64f76055f21252dfaa Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 29 Nov 2024 02:37:17 +0100 Subject: [PATCH 03/14] Fix own CS --- CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php | 8 ++++---- CakePHP/Sniffs/Commenting/DocBlockAlignmentSniff.php | 2 +- CakePHP/Sniffs/Commenting/FunctionCommentSniff.php | 10 +++++----- CakePHP/Sniffs/Commenting/TypeHintSniff.php | 10 +++++----- .../Sniffs/Formatting/BlankLineBeforeReturnSniff.php | 2 +- CakePHP/Sniffs/PHP/SingleQuoteSniff.php | 2 +- CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php | 6 +++--- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php b/CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php index 52ea811..0df7c40 100644 --- a/CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php +++ b/CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php @@ -67,7 +67,7 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->addError( 'Chaining methods (@return $this) should not have any return-type-hint.', $startIndex, - 'InvalidSelf' + 'InvalidSelf', ); return; @@ -76,7 +76,7 @@ public function process(File $phpcsFile, $stackPtr) $fix = $phpcsFile->addFixableError( 'Chaining methods (@return $this) should not have any return-type-hint (Remove "self").', $startIndex, - 'InvalidSelf' + 'InvalidSelf', ); if (!$fix) { return; @@ -175,7 +175,7 @@ protected function assertNotThisOrStatic(File $phpCsFile, int $stackPointer): vo $phpCsFile->addError( 'Class name repeated, expected `self` or `$this`.', $classNameIndex, - 'InvalidClass' + 'InvalidClass', ); } } @@ -230,7 +230,7 @@ protected function getClassNameWithNamespace(File $phpCsFile): ?string return ClassHelper::getFullyQualifiedName( $phpCsFile, - $phpCsFile->findPrevious(TokenHelper::$typeKeywordTokenCodes, $lastToken) + $phpCsFile->findPrevious(TokenHelper::$typeKeywordTokenCodes, $lastToken), ); } } diff --git a/CakePHP/Sniffs/Commenting/DocBlockAlignmentSniff.php b/CakePHP/Sniffs/Commenting/DocBlockAlignmentSniff.php index 41e90c1..c04c154 100644 --- a/CakePHP/Sniffs/Commenting/DocBlockAlignmentSniff.php +++ b/CakePHP/Sniffs/Commenting/DocBlockAlignmentSniff.php @@ -61,7 +61,7 @@ public function process(File $phpcsFile, $stackPtr) $commentBorder = $phpcsFile->findNext( [T_DOC_COMMENT_STAR, T_DOC_COMMENT_CLOSE_TAG], $searchToken, - $commentClose + 1 + $commentClose + 1, ); if ($commentBorder !== false) { $tokensToIndent[$commentBorder] = $codeIndentation + 1; diff --git a/CakePHP/Sniffs/Commenting/FunctionCommentSniff.php b/CakePHP/Sniffs/Commenting/FunctionCommentSniff.php index 236d344..ae85a7a 100644 --- a/CakePHP/Sniffs/Commenting/FunctionCommentSniff.php +++ b/CakePHP/Sniffs/Commenting/FunctionCommentSniff.php @@ -59,14 +59,14 @@ public function process(File $phpcsFile, $stackPtr) $docCommentEnd = $phpcsFile->findPrevious( [T_DOC_COMMENT_CLOSE_TAG, T_SEMICOLON, T_CLOSE_CURLY_BRACKET, T_OPEN_CURLY_BRACKET], $stackPtr - 1, - null + null, ); if ($docCommentEnd === false || $tokens[$docCommentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) { $phpcsFile->addError( 'Missing doc comment for function %s()', $stackPtr, 'Missing', - [$phpcsFile->getDeclarationName($stackPtr)] + [$phpcsFile->getDeclarationName($stackPtr)], ); return; @@ -77,14 +77,14 @@ public function process(File $phpcsFile, $stackPtr) $attribute = $phpcsFile->findNext( [T_ATTRIBUTE], $lastEndToken + 1, - $stackPtr + $stackPtr, ); if ($attribute !== false) { if ($tokens[$lastEndToken]['line'] !== $tokens[$attribute]['line'] - 1) { $phpcsFile->addError( 'There must be no blank lines after the function comment or attribute', $lastEndToken, - 'SpacingAfter' + 'SpacingAfter', ); return; @@ -98,7 +98,7 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->addError( 'There must be no blank lines after the function comment or attribute', $lastEndToken, - 'SpacingAfter' + 'SpacingAfter', ); } diff --git a/CakePHP/Sniffs/Commenting/TypeHintSniff.php b/CakePHP/Sniffs/Commenting/TypeHintSniff.php index 9e522a1..3e08854 100644 --- a/CakePHP/Sniffs/Commenting/TypeHintSniff.php +++ b/CakePHP/Sniffs/Commenting/TypeHintSniff.php @@ -123,7 +123,7 @@ public function process(File $phpcsFile, $stackPtr) '%s type hint is not formatted properly, expected "%s"', $tag, 'IncorrectFormat', - [$tokens[$tag]['content'], $sortedTypeHint] + [$tokens[$tag]['content'], $sortedTypeHint], ); if (!$fix) { continue; @@ -135,7 +135,7 @@ public function process(File $phpcsFile, $stackPtr) '%s %s %s', $sortedTypeHint, $valueNode->variableName, - $valueNode->description + $valueNode->description, )); if ($tagComment[-1] === ' ') { // tags above variables in code have a trailing space @@ -147,13 +147,13 @@ public function process(File $phpcsFile, $stackPtr) $sortedTypeHint, $valueNode->isVariadic ? '...' : '', $valueNode->parameterName, - $valueNode->description + $valueNode->description, )); } elseif ($valueNode instanceof ReturnTagValueNode) { $newComment = trim(sprintf( '%s %s', $sortedTypeHint, - $valueNode->description + $valueNode->description, )); } @@ -276,7 +276,7 @@ protected function renderUnionTypes(array $typeNodes): string return preg_replace( ['/ ([\|&]) /', '/<\(/', '/\)>/', '/\), /', '/, \(/'], ['${1}', '<', '>', ', ', ', '], - implode('|', $typeNodes) + implode('|', $typeNodes), ); } diff --git a/CakePHP/Sniffs/Formatting/BlankLineBeforeReturnSniff.php b/CakePHP/Sniffs/Formatting/BlankLineBeforeReturnSniff.php index 7aa7bfd..916da55 100644 --- a/CakePHP/Sniffs/Formatting/BlankLineBeforeReturnSniff.php +++ b/CakePHP/Sniffs/Formatting/BlankLineBeforeReturnSniff.php @@ -72,7 +72,7 @@ public function process(File $phpcsFile, $stackPtr) $fix = $phpcsFile->addFixableError( 'Missing blank line before return statement', $stackPtr, - 'BlankLineBeforeReturn' + 'BlankLineBeforeReturn', ); if ($fix === true) { $phpcsFile->fixer->beginChangeset(); diff --git a/CakePHP/Sniffs/PHP/SingleQuoteSniff.php b/CakePHP/Sniffs/PHP/SingleQuoteSniff.php index de97077..a07660b 100644 --- a/CakePHP/Sniffs/PHP/SingleQuoteSniff.php +++ b/CakePHP/Sniffs/PHP/SingleQuoteSniff.php @@ -56,7 +56,7 @@ public function process(File $phpcsFile, $stackPtr) $fix = $phpcsFile->addFixableError( 'Use single instead of double quotes for simple strings.', $stackPtr, - 'UseSingleQuote' + 'UseSingleQuote', ); if ($fix) { $content = substr($content, 1, -1); diff --git a/CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php b/CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php index c488f28..1228f20 100644 --- a/CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php +++ b/CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php @@ -65,7 +65,7 @@ public function process(File $phpCsFile, $stackPointer) $fix = $phpCsFile->addFixableError( 'Every function/method needs a newline afterwards', $closingParenthesisIndex, - 'Abstract' + 'Abstract', ); if ($fix) { $phpCsFile->fixer->addNewline($semicolonIndex); @@ -108,7 +108,7 @@ protected function assertNewLineAtTheEnd(File $phpCsFile, int $closingBraceIndex $fix = $phpCsFile->addFixableError( 'Every function/method needs a newline afterwards', $closingBraceIndex, - 'Concrete' + 'Concrete', ); if ($fix) { $phpCsFile->fixer->addNewline($closingBraceIndex); @@ -160,7 +160,7 @@ protected function assertNewLineAtTheBeginning(File $phpCsFile, int $stackPointe $fix = $phpCsFile->addFixableError( 'Every function/method needs a newline before', $firstTokenInLineIndex, - 'Concrete' + 'Concrete', ); if ($fix) { $phpCsFile->fixer->addNewline($prevContentIndex); From 5b667d513d3047715dec4eb41104a8509a7c99ca Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 27 Dec 2022 22:49:05 +0100 Subject: [PATCH 04/14] Update for PHP 8.1+ --- .editorconfig | 3 +++ CakePHP/ruleset.xml | 3 +++ composer.json | 1 + phpstan.neon | 9 +++++++++ tests/phpstan_bootstrap.php | 8 ++++++++ 5 files changed, 24 insertions(+) create mode 100644 phpstan.neon create mode 100644 tests/phpstan_bootstrap.php diff --git a/.editorconfig b/.editorconfig index d7ec37b..b88269d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -15,3 +15,6 @@ end_of_line = crlf [*.yml] indent_size = 2 + +[*.neon] +indent_style = tab diff --git a/CakePHP/ruleset.xml b/CakePHP/ruleset.xml index 95de8a9..a80b47c 100644 --- a/CakePHP/ruleset.xml +++ b/CakePHP/ruleset.xml @@ -96,6 +96,8 @@ + + @@ -141,6 +143,7 @@ */tests/* + diff --git a/composer.json b/composer.json index 1347fc2..94b755f 100644 --- a/composer.json +++ b/composer.json @@ -45,6 +45,7 @@ ], "cs-check": "phpcs --colors --parallel=16 -p -s CakePHP/", "cs-fix": "phpcbf --colors --parallel=16 -p CakePHP/", + "stan": "phpstan analyse", "docs": "php docs/generate.php", "explain": "phpcs -e --standard=CakePHP" } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..e82ad2a --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +parameters: + level: 6 + paths: + - CakePHP/Sniffs/ + bootstrapFiles: + - %rootDir%/../../../tests/phpstan_bootstrap.php + ignoreErrors: + + checkMissingIterableValueType: false diff --git a/tests/phpstan_bootstrap.php b/tests/phpstan_bootstrap.php new file mode 100644 index 0000000..3cc86c5 --- /dev/null +++ b/tests/phpstan_bootstrap.php @@ -0,0 +1,8 @@ + Date: Sun, 1 Jan 2023 15:33:14 +0100 Subject: [PATCH 05/14] Update ruleset.xml --- CakePHP/ruleset.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CakePHP/ruleset.xml b/CakePHP/ruleset.xml index a80b47c..fab5836 100644 --- a/CakePHP/ruleset.xml +++ b/CakePHP/ruleset.xml @@ -98,6 +98,22 @@ + + + + + + + + + + + + + + + + From 4670bf15834dfa96135b10629802faaa2e26e029 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 30 Nov 2024 04:42:06 +0100 Subject: [PATCH 06/14] Update docs. --- docs/README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index b42dfc2..7728ccb 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ # CakePHP ruleset -The CakePHP standard contains 147 sniffs +The CakePHP standard contains 152 sniffs CakePHP (20 sniffs) ------------------- @@ -95,7 +95,7 @@ PSR12 (17 sniffs) - PSR12.Properties.ConstantVisibility - PSR12.Traits.UseDeclaration -SlevomatCodingStandard (43 sniffs) +SlevomatCodingStandard (48 sniffs) ---------------------------------- - SlevomatCodingStandard.Arrays.TrailingArrayComma - SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing @@ -116,7 +116,12 @@ SlevomatCodingStandard (43 sniffs) - SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator - SlevomatCodingStandard.Exceptions.DeadCatch - SlevomatCodingStandard.Functions.ArrowFunctionDeclaration +- SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall +- SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse +- SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration - SlevomatCodingStandard.Functions.RequireTrailingCommaInCall +- SlevomatCodingStandard.Functions.RequireTrailingCommaInClosureUse +- SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration - SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses - SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation - SlevomatCodingStandard.Namespaces.NamespaceDeclaration From 009c78e58bd662bf068537d2e0cc51f15804911b Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 30 Nov 2024 05:44:38 +0100 Subject: [PATCH 07/14] Add PHPStan. --- .github/workflows/ci.yml | 3 +++ CakePHP/Sniffs/Commenting/TypeHintSniff.php | 1 + composer.json | 1 + phpstan.neon | 3 +-- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 46a44f0..dd060c2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -57,3 +57,6 @@ jobs: - name: Run PHP CodeSniffer run: vendor/bin/phpcs --report=checkstyle | cs2pr + + - name: Run PHPStan + run: vendor/bin/phpstan diff --git a/CakePHP/Sniffs/Commenting/TypeHintSniff.php b/CakePHP/Sniffs/Commenting/TypeHintSniff.php index 3e08854..ee7604a 100644 --- a/CakePHP/Sniffs/Commenting/TypeHintSniff.php +++ b/CakePHP/Sniffs/Commenting/TypeHintSniff.php @@ -101,6 +101,7 @@ public function process(File $phpcsFile, $stackPtr) continue; } + /** @phpstan-ignore-next-line */ if ($valueNode->type instanceof UnionTypeNode) { $types = $valueNode->type->types; } elseif ($valueNode->type instanceof ArrayTypeNode) { diff --git a/composer.json b/composer.json index 94b755f..78145a8 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "squizlabs/php_codesniffer": "^3.9" }, "require-dev": { + "phpstan/phpstan": "^2.0.3", "phpunit/phpunit": "^9.3.4" }, "autoload": { diff --git a/phpstan.neon b/phpstan.neon index e82ad2a..bf0b10f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,5 +5,4 @@ parameters: bootstrapFiles: - %rootDir%/../../../tests/phpstan_bootstrap.php ignoreErrors: - - checkMissingIterableValueType: false + - identifier: missingType.iterableValue From 1a3cfcfcdbe63ae86c7a677c388659c4ee35cc89 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 30 Nov 2024 05:54:12 +0100 Subject: [PATCH 08/14] Add NamedArgumentSpacing sniff. --- CakePHP/ruleset.xml | 18 +++++++++--------- docs/README.md | 5 +++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/CakePHP/ruleset.xml b/CakePHP/ruleset.xml index fab5836..3372d0d 100644 --- a/CakePHP/ruleset.xml +++ b/CakePHP/ruleset.xml @@ -133,17 +133,17 @@ - - - - - + + + + + - + @@ -232,13 +232,13 @@ - + - + + - diff --git a/docs/README.md b/docs/README.md index 7728ccb..c3d7cf4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ # CakePHP ruleset -The CakePHP standard contains 152 sniffs +The CakePHP standard contains 153 sniffs CakePHP (20 sniffs) ------------------- @@ -95,7 +95,7 @@ PSR12 (17 sniffs) - PSR12.Properties.ConstantVisibility - PSR12.Traits.UseDeclaration -SlevomatCodingStandard (48 sniffs) +SlevomatCodingStandard (49 sniffs) ---------------------------------- - SlevomatCodingStandard.Arrays.TrailingArrayComma - SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing @@ -119,6 +119,7 @@ SlevomatCodingStandard (48 sniffs) - SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall - SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse - SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration +- SlevomatCodingStandard.Functions.NamedArgumentSpacing - SlevomatCodingStandard.Functions.RequireTrailingCommaInCall - SlevomatCodingStandard.Functions.RequireTrailingCommaInClosureUse - SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration From 3c23ac86aa4432d14a4eb0a6ecdb7860d162c82a Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 30 Nov 2024 23:00:15 +0530 Subject: [PATCH 09/14] Use phive to install and run phpstan --- .gitattributes | 4 +++- .github/workflows/ci.yml | 7 +++++-- .phive/phars.xml | 4 ++++ composer.json | 3 +-- phpstan.neon | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 .phive/phars.xml diff --git a/.gitattributes b/.gitattributes index 59e7ce9..33d633c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11,9 +11,11 @@ # Ignore files for distribution archives, generated using `git archive` .editorconfig export-ignore -.git export-ignore +.github export-ignore .gitattributes export-ignore .gitignore export-ignore phpcs.xml export-ignore phpunit.xml.dist export-ignore /CakePHP/Tests export-ignore +phpstan.neon export-ignore +.phive export-ignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dd060c2..c099c7a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,14 +49,17 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: '8.1' - tools: cs2pr + tools: phive, cs2pr coverage: none - name: Composer install uses: ramsey/composer-install@v3 + - name: Install PHP tools with phive. + run: "phive install --trust-gpg-keys 'CF1A108D0E7AE720,51C67305FFC2E5C0,12CE0F1D262429A5'" + - name: Run PHP CodeSniffer run: vendor/bin/phpcs --report=checkstyle | cs2pr - name: Run PHPStan - run: vendor/bin/phpstan + run: tools/phpstan analyse --error-format=github diff --git a/.phive/phars.xml b/.phive/phars.xml new file mode 100644 index 0000000..65df421 --- /dev/null +++ b/.phive/phars.xml @@ -0,0 +1,4 @@ + + + + diff --git a/composer.json b/composer.json index 78145a8..fa3dace 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,6 @@ "squizlabs/php_codesniffer": "^3.9" }, "require-dev": { - "phpstan/phpstan": "^2.0.3", "phpunit/phpunit": "^9.3.4" }, "autoload": { @@ -46,7 +45,7 @@ ], "cs-check": "phpcs --colors --parallel=16 -p -s CakePHP/", "cs-fix": "phpcbf --colors --parallel=16 -p CakePHP/", - "stan": "phpstan analyse", + "stan": "tools/phpstan analyse", "docs": "php docs/generate.php", "explain": "phpcs -e --standard=CakePHP" } diff --git a/phpstan.neon b/phpstan.neon index bf0b10f..dfd111e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,6 +3,6 @@ parameters: paths: - CakePHP/Sniffs/ bootstrapFiles: - - %rootDir%/../../../tests/phpstan_bootstrap.php + - tests/phpstan_bootstrap.php ignoreErrors: - identifier: missingType.iterableValue From bb2e5c0dc391fd80c25870611b736f09f91b0b30 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 30 Nov 2024 18:50:05 +0100 Subject: [PATCH 10/14] PHPStan Level 8 --- CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php | 7 ++++++- CakePHP/Sniffs/Commenting/FunctionCommentSniff.php | 2 +- CakePHP/Sniffs/Commenting/TypeHintSniff.php | 2 +- .../Sniffs/NamingConventions/ValidFunctionNameSniff.php | 4 ++++ CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php | 9 +++++++++ phpstan.neon | 2 +- 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php b/CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php index 0df7c40..03d4ec1 100644 --- a/CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php +++ b/CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php @@ -228,9 +228,14 @@ protected function getClassNameWithNamespace(File $phpCsFile): ?string return null; } + $classPointer = $phpCsFile->findPrevious(TokenHelper::$typeKeywordTokenCodes, $lastToken); + if (!$classPointer) { + return null; + } + return ClassHelper::getFullyQualifiedName( $phpCsFile, - $phpCsFile->findPrevious(TokenHelper::$typeKeywordTokenCodes, $lastToken), + $classPointer, ); } } diff --git a/CakePHP/Sniffs/Commenting/FunctionCommentSniff.php b/CakePHP/Sniffs/Commenting/FunctionCommentSniff.php index ae85a7a..c6a3fa2 100644 --- a/CakePHP/Sniffs/Commenting/FunctionCommentSniff.php +++ b/CakePHP/Sniffs/Commenting/FunctionCommentSniff.php @@ -152,7 +152,7 @@ protected function processThrows(File $phpcsFile, int $stackPtr, int $commentSta if ($tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING) { $matches = []; preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[$tag + 2]['content'], $matches); - $exception = $matches[1]; + $exception = $matches[1] ?? null; } if ($exception === null) { diff --git a/CakePHP/Sniffs/Commenting/TypeHintSniff.php b/CakePHP/Sniffs/Commenting/TypeHintSniff.php index ee7604a..55e15be 100644 --- a/CakePHP/Sniffs/Commenting/TypeHintSniff.php +++ b/CakePHP/Sniffs/Commenting/TypeHintSniff.php @@ -274,7 +274,7 @@ protected function getSortedTypeHint(array $types): string protected function renderUnionTypes(array $typeNodes): string { // Remove parenthesis added by phpstan around union and intersection types - return preg_replace( + return (string)preg_replace( ['/ ([\|&]) /', '/<\(/', '/\)>/', '/\), /', '/, \(/'], ['${1}', '<', '>', ', ', ', '], implode('|', $typeNodes), diff --git a/CakePHP/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/CakePHP/Sniffs/NamingConventions/ValidFunctionNameSniff.php index 17329df..c7dd3c0 100644 --- a/CakePHP/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/CakePHP/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -63,6 +63,10 @@ public function __construct() protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) { $methodName = $phpcsFile->getDeclarationName($stackPtr); + if ($methodName === null) { + return; + } + $className = $phpcsFile->getDeclarationName($currScope); $errorData = [$className . '::' . $methodName]; diff --git a/CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php b/CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php index 1228f20..064f04b 100644 --- a/CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php +++ b/CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php @@ -53,6 +53,9 @@ public function process(File $phpCsFile, $stackPointer) $closingParenthesisIndex = $tokens[$openingParenthesisIndex]['parenthesis_closer']; $semicolonIndex = $phpCsFile->findNext(T_SEMICOLON, $closingParenthesisIndex + 1); + if (!$semicolonIndex) { + return; + } $nextContentIndex = $phpCsFile->findNext(T_WHITESPACE, $semicolonIndex + 1, null, true); @@ -84,6 +87,9 @@ public function process(File $phpCsFile, $stackPointer) } $nextContentIndex = $phpCsFile->findNext(T_WHITESPACE, $closingBraceIndex + 1, null, true); + if (!$nextContentIndex) { + return; + } // Do not mess with the end of the class if ($tokens[$nextContentIndex]['code'] === T_CLOSE_CURLY_BRACKET) { @@ -147,6 +153,9 @@ protected function assertNewLineAtTheBeginning(File $phpCsFile, int $stackPointe } $prevContentIndex = $phpCsFile->findPrevious(T_WHITESPACE, $firstTokenInLineIndex - 1, null, true); + if (!$prevContentIndex) { + return; + } // Do not mess with the start of the class if ($tokens[$prevContentIndex]['code'] === T_OPEN_CURLY_BRACKET) { diff --git a/phpstan.neon b/phpstan.neon index dfd111e..f4fa105 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 6 + level: 8 paths: - CakePHP/Sniffs/ bootstrapFiles: From f767e0af3902c199e7bf38a1277726c0c6371dc5 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Sat, 30 Nov 2024 18:50:58 +0100 Subject: [PATCH 11/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3ed3a28..8b2226f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # CakePHP Code Sniffer -![Build Status](https://github.com/cakephp/cakephp-codesniffer/actions/workflows/ci.yml/badge.svg?branch=master) +![Build Status](https://github.com/cakephp/cakephp-codesniffer/actions/workflows/ci.yml/badge.svg?branch=5.next) [![Total Downloads](https://img.shields.io/packagist/dt/cakephp/cakephp-codesniffer.svg?style=flat-square)](https://packagist.org/packages/cakephp/cakephp-codesniffer) [![Latest Stable Version](https://img.shields.io/packagist/v/cakephp/cakephp-codesniffer.svg?style=flat-square)](https://packagist.org/packages/cakephp/cakephp-codesniffer) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) From 331e4899837d4fd98f82444056eb8b6278717c58 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 30 Nov 2024 22:43:19 +0530 Subject: [PATCH 12/14] Remove unneded ClosureDeclarationSniff. Its checks are already covered by other sniffs. Space is now disallowed after `fn` keyword to match the PER 2.0 standard. --- .../Functions/ClosureDeclarationSniff.php | 52 ------------------- .../Functions/ClosureDeclarationUnitTest.inc | 39 -------------- .../Functions/ClosureDeclarationUnitTest.php | 28 ---------- CakePHP/ruleset.xml | 6 ++- docs/README.md | 5 +- 5 files changed, 7 insertions(+), 123 deletions(-) delete mode 100644 CakePHP/Sniffs/Functions/ClosureDeclarationSniff.php delete mode 100644 CakePHP/Tests/Functions/ClosureDeclarationUnitTest.inc delete mode 100644 CakePHP/Tests/Functions/ClosureDeclarationUnitTest.php diff --git a/CakePHP/Sniffs/Functions/ClosureDeclarationSniff.php b/CakePHP/Sniffs/Functions/ClosureDeclarationSniff.php deleted file mode 100644 index e9dafce..0000000 --- a/CakePHP/Sniffs/Functions/ClosureDeclarationSniff.php +++ /dev/null @@ -1,52 +0,0 @@ -getTokens(); - $spaces = 0; - - if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE) { - $spaces = strlen($tokens[$stackPtr + 1]['content']); - } - - if ($spaces !== 1) { - $keyword = $tokens[$stackPtr]['code'] === T_FN ? 'fn' : 'function'; - $error = "Expected 1 space after closure's $keyword keyword; %s found"; - $data = [$spaces]; - $phpcsFile->addError($error, $stackPtr, 'SpaceAfterFunction', $data); - } - } -} diff --git a/CakePHP/Tests/Functions/ClosureDeclarationUnitTest.inc b/CakePHP/Tests/Functions/ClosureDeclarationUnitTest.inc deleted file mode 100644 index 836703f..0000000 --- a/CakePHP/Tests/Functions/ClosureDeclarationUnitTest.inc +++ /dev/null @@ -1,39 +0,0 @@ - echo 'It Fails'; - $visitor = fn ($expression) => echo 'It works'; - } -} - -$foo = 'bar'; -$bar = 'foo'; - -$zum = function()use ($foo, $bar) { - return $foo; -}; - -$zum = function ()use ($foo, $bar) { - return $foo; -}; diff --git a/CakePHP/Tests/Functions/ClosureDeclarationUnitTest.php b/CakePHP/Tests/Functions/ClosureDeclarationUnitTest.php deleted file mode 100644 index abe1c68..0000000 --- a/CakePHP/Tests/Functions/ClosureDeclarationUnitTest.php +++ /dev/null @@ -1,28 +0,0 @@ - 1, - 25 => 1, - 33 => 1, - ]; - } - - /** - * @inheritDoc - */ - public function getWarningList() - { - return []; - } -} diff --git a/CakePHP/ruleset.xml b/CakePHP/ruleset.xml index 3372d0d..99d6ac2 100644 --- a/CakePHP/ruleset.xml +++ b/CakePHP/ruleset.xml @@ -236,7 +236,11 @@ - + + + + + diff --git a/docs/README.md b/docs/README.md index c3d7cf4..de14c5b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,8 +1,8 @@ # CakePHP ruleset -The CakePHP standard contains 153 sniffs +The CakePHP standard contains 152 sniffs -CakePHP (20 sniffs) +CakePHP (19 sniffs) ------------------- - CakePHP.Classes.ReturnTypeHint - CakePHP.Commenting.DocBlockAlignment @@ -13,7 +13,6 @@ CakePHP (20 sniffs) - CakePHP.ControlStructures.ElseIfDeclaration - CakePHP.ControlStructures.WhileStructures - CakePHP.Formatting.BlankLineBeforeReturn -- CakePHP.Functions.ClosureDeclaration - CakePHP.NamingConventions.ValidFunctionName - CakePHP.NamingConventions.ValidTraitName - CakePHP.PHP.DisallowShortOpenTag From 2118ce6cf2f52f0e887cda9c22e36f4772a28ecb Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 22 Dec 2024 13:24:39 +0530 Subject: [PATCH 13/14] Add couple of new sniffs --- CakePHP/ruleset.xml | 2 ++ docs/README.md | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CakePHP/ruleset.xml b/CakePHP/ruleset.xml index 9bcc274..26f6db0 100644 --- a/CakePHP/ruleset.xml +++ b/CakePHP/ruleset.xml @@ -244,6 +244,8 @@ + + diff --git a/docs/README.md b/docs/README.md index de14c5b..311a217 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ # CakePHP ruleset -The CakePHP standard contains 152 sniffs +The CakePHP standard contains 155 sniffs CakePHP (19 sniffs) ------------------- @@ -94,11 +94,12 @@ PSR12 (17 sniffs) - PSR12.Properties.ConstantVisibility - PSR12.Traits.UseDeclaration -SlevomatCodingStandard (49 sniffs) +SlevomatCodingStandard (52 sniffs) ---------------------------------- - SlevomatCodingStandard.Arrays.TrailingArrayComma - SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing - SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment +- SlevomatCodingStandard.Classes.BackedEnumTypeSpacing - SlevomatCodingStandard.Classes.ClassConstantVisibility - SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces - SlevomatCodingStandard.Classes.ModernClassNameReference @@ -113,6 +114,7 @@ SlevomatCodingStandard (49 sniffs) - SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses - SlevomatCodingStandard.ControlStructures.NewWithParentheses - SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator +- SlevomatCodingStandard.ControlStructures.RequireShortTernaryOperator - SlevomatCodingStandard.Exceptions.DeadCatch - SlevomatCodingStandard.Functions.ArrowFunctionDeclaration - SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall @@ -123,6 +125,7 @@ SlevomatCodingStandard (49 sniffs) - SlevomatCodingStandard.Functions.RequireTrailingCommaInClosureUse - SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration - SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses +- SlevomatCodingStandard.Namespaces.DisallowGroupUse - SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation - SlevomatCodingStandard.Namespaces.NamespaceDeclaration - SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly From 50f6094230111081f9731df82d2a693512b53668 Mon Sep 17 00:00:00 2001 From: Nicos Panayides Date: Wed, 19 Mar 2025 10:31:58 +0200 Subject: [PATCH 14/14] Update to Slevomat 8.16 and phpdoc-parser 2. --- CakePHP/Sniffs/Commenting/TypeHintSniff.php | 10 +++++++--- CakePHP/ruleset.xml | 5 +++-- composer.json | 4 ++-- docs/README.md | 4 ++-- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/CakePHP/Sniffs/Commenting/TypeHintSniff.php b/CakePHP/Sniffs/Commenting/TypeHintSniff.php index f22cb49..da82ecf 100644 --- a/CakePHP/Sniffs/Commenting/TypeHintSniff.php +++ b/CakePHP/Sniffs/Commenting/TypeHintSniff.php @@ -32,6 +32,7 @@ use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; +use PHPStan\PhpDocParser\ParserConfig; /** * Verifies order of types in type hints @@ -294,13 +295,16 @@ protected static function getValueNode(string $tagName, string $tagComment): Php { static $phpDocParser; if (!$phpDocParser) { - $constExprParser = new ConstExprParser(); - $phpDocParser = new PhpDocParser(new TypeParser($constExprParser), $constExprParser); + $config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]); + + $constExprParser = new ConstExprParser($config); + $phpDocParser = new PhpDocParser($config, new TypeParser($config, $constExprParser), $constExprParser); } static $phpDocLexer; if (!$phpDocLexer) { - $phpDocLexer = new Lexer(); + $config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]); + $phpDocLexer = new Lexer($config); } return $phpDocParser->parseTagValue(new TokenIterator($phpDocLexer->tokenize($tagComment)), $tagName); diff --git a/CakePHP/ruleset.xml b/CakePHP/ruleset.xml index 26f6db0..234e573 100644 --- a/CakePHP/ruleset.xml +++ b/CakePHP/ruleset.xml @@ -219,10 +219,11 @@ */tests/* - + - + + diff --git a/composer.json b/composer.json index fa3dace..e7f83ff 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,8 @@ }, "require": { "php": ">=8.1.0", - "phpstan/phpdoc-parser": "^1.4.5", - "slevomat/coding-standard": "^8.15", + "phpstan/phpdoc-parser": "^2.1.0", + "slevomat/coding-standard": "^8.16", "squizlabs/php_codesniffer": "^3.9" }, "require-dev": { diff --git a/docs/README.md b/docs/README.md index 311a217..c1384cc 100644 --- a/docs/README.md +++ b/docs/README.md @@ -138,6 +138,7 @@ SlevomatCodingStandard (52 sniffs) - SlevomatCodingStandard.PHP.UselessParentheses - SlevomatCodingStandard.PHP.UselessSemicolon - SlevomatCodingStandard.TypeHints.DeclareStrictTypes +- SlevomatCodingStandard.TypeHints.DNFTypeHintFormat - SlevomatCodingStandard.TypeHints.LongTypeHints - SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue - SlevomatCodingStandard.TypeHints.ParameterTypeHint @@ -145,7 +146,6 @@ SlevomatCodingStandard (52 sniffs) - SlevomatCodingStandard.TypeHints.PropertyTypeHint - SlevomatCodingStandard.TypeHints.ReturnTypeHint - SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing -- SlevomatCodingStandard.TypeHints.UnionTypeHintFormat - SlevomatCodingStandard.Variables.DuplicateAssignmentToVariable - SlevomatCodingStandard.Variables.UnusedVariable @@ -182,4 +182,4 @@ Squiz (28 sniffs) Zend (1 sniff) -------------- -- Zend.NamingConventions.ValidVariableName \ No newline at end of file +- Zend.NamingConventions.ValidVariableName