From 7063c47d64c9140a676f7b869597b48048fcc8fc Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Fri, 10 Feb 2023 15:37:30 +0100 Subject: [PATCH] Fixing doc type annotation sniffs to correctly ignore attributes --- .woodpecker/code-style.yml | 2 +- .woodpecker/test.yml | 2 +- composer.json | 3 +- .../Annotations/ReturnIsNotVoidSniff.php | 17 ++------- .../VarIsLastTagOnPropertySniff.php | 21 +++++------ .../Commenting/VariableCommentSniff.php | 21 +++++------ src/DigiComp/Sniffs/MemberVarUtility.php | 35 +++++++++++++++++++ src/DigiComp/ruleset.xml | 2 +- .../VarIsLastTagOnPropertySniffTest.php | 2 +- .../Sniffs/Annotations/data/PropertyOrder.php | 14 +++++++- .../Commenting/VariableCommentSniffTest.php | 1 + .../Sniffs/Commenting/data/comments.php | 13 +++++++ 12 files changed, 92 insertions(+), 41 deletions(-) create mode 100644 src/DigiComp/Sniffs/MemberVarUtility.php diff --git a/.woodpecker/code-style.yml b/.woodpecker/code-style.yml index f8925dd..0cf4e24 100644 --- a/.woodpecker/code-style.yml +++ b/.woodpecker/code-style.yml @@ -1,6 +1,6 @@ pipeline: code-style: - image: thecodingmachine/php:7.4-v4-cli + image: thecodingmachine/php:8.1-v4-cli commands: - "sudo -s chown -R docker:docker ." - "composer config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true" diff --git a/.woodpecker/test.yml b/.woodpecker/test.yml index 693a223..7d5f300 100644 --- a/.woodpecker/test.yml +++ b/.woodpecker/test.yml @@ -1,6 +1,6 @@ pipeline: functional-tests: - image: thecodingmachine/php:7.4-v4-cli + image: thecodingmachine/php:8.1-v4-cli commands: - "sudo -s chown -R docker:docker ." - "composer install" diff --git a/composer.json b/composer.json index 5038fc8..c454e3f 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "squizlabs/php_codesniffer": "^3.6.0" }, "require-dev": { - "phpunit/phpunit": "^8.0" + "phpunit/phpunit": "^8.0", + "php": ">=8.1" }, "scripts": { "phpunit": "php vendor/bin/phpunit", diff --git a/src/DigiComp/Sniffs/Annotations/ReturnIsNotVoidSniff.php b/src/DigiComp/Sniffs/Annotations/ReturnIsNotVoidSniff.php index fae06b1..7ee141b 100644 --- a/src/DigiComp/Sniffs/Annotations/ReturnIsNotVoidSniff.php +++ b/src/DigiComp/Sniffs/Annotations/ReturnIsNotVoidSniff.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace DigiComp\PhpCodesniffer\DigiComp\Sniffs\Annotations; +use DigiComp\PhpCodesniffer\DigiComp\Sniffs\MemberVarUtility; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; @@ -21,21 +22,7 @@ class ReturnIsNotVoidSniff implements Sniff $ignore = Tokens::$methodPrefixes; $ignore[\T_WHITESPACE] = \T_WHITESPACE; - for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) { - if (isset($ignore[$tokens[$commentEnd]['code']]) === true) { - continue; - } - - if ( - $tokens[$commentEnd]['code'] === \T_ATTRIBUTE_END - && isset($tokens[$commentEnd]['attribute_opener']) === true - ) { - $commentEnd = $tokens[$commentEnd]['attribute_opener']; - continue; - } - - break; - } + $commentEnd = MemberVarUtility::findCommentEndWithoutAttributes($tokens, $ignore, $stackPtr); if ($tokens[$commentEnd]['code'] === \T_COMMENT) { // Inline comments might just be closing comments for diff --git a/src/DigiComp/Sniffs/Annotations/VarIsLastTagOnPropertySniff.php b/src/DigiComp/Sniffs/Annotations/VarIsLastTagOnPropertySniff.php index 1066317..2fdfb24 100644 --- a/src/DigiComp/Sniffs/Annotations/VarIsLastTagOnPropertySniff.php +++ b/src/DigiComp/Sniffs/Annotations/VarIsLastTagOnPropertySniff.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace DigiComp\PhpCodesniffer\DigiComp\Sniffs\Annotations; +use DigiComp\PhpCodesniffer\DigiComp\Sniffs\MemberVarUtility; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; @@ -39,18 +40,18 @@ class VarIsLastTagOnPropertySniff extends AbstractVariableSniff } $tokens = $phpcsFile->getTokens(); $ignore = [ - \T_PUBLIC, - \T_PRIVATE, - \T_PROTECTED, - \T_VAR, - \T_STATIC, - \T_WHITESPACE, - \T_STRING, - \T_NS_SEPARATOR, - \T_NULLABLE, + \T_PUBLIC => \T_PUBLIC, + \T_PRIVATE => \T_PRIVATE, + \T_PROTECTED => \T_PROTECTED, + \T_VAR => \T_VAR, + \T_STATIC => \T_STATIC, + \T_WHITESPACE => \T_WHITESPACE, + \T_STRING => \T_STRING, + \T_NS_SEPARATOR => \T_NS_SEPARATOR, + \T_NULLABLE => \T_NULLABLE, ]; - $commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true); + $commentEnd = MemberVarUtility::findCommentEndWithoutAttributes($tokens, $ignore, $stackPtr); if ( $commentEnd === false || ($tokens[$commentEnd]['code'] !== \T_DOC_COMMENT_CLOSE_TAG diff --git a/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php b/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php index 53f7620..6f2e0ed 100644 --- a/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php +++ b/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php @@ -2,6 +2,7 @@ namespace DigiComp\PhpCodesniffer\DigiComp\Sniffs\Commenting; +use DigiComp\PhpCodesniffer\DigiComp\Sniffs\MemberVarUtility; use PHP_CodeSniffer\Files\File; class VariableCommentSniff extends \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\VariableCommentSniff @@ -10,18 +11,18 @@ class VariableCommentSniff extends \PHP_CodeSniffer\Standards\Squiz\Sniffs\Comme { $tokens = $phpcsFile->getTokens(); $ignore = [ - \T_PUBLIC, - \T_PRIVATE, - \T_PROTECTED, - \T_VAR, - \T_STATIC, - \T_WHITESPACE, - \T_STRING, - \T_NS_SEPARATOR, - \T_NULLABLE, + \T_PUBLIC => \T_PUBLIC, + \T_PRIVATE => \T_PRIVATE, + \T_PROTECTED => \T_PROTECTED, + \T_VAR => \T_VAR, + \T_STATIC => \T_STATIC, + \T_WHITESPACE => \T_WHITESPACE, + \T_STRING => \T_STRING, + \T_NS_SEPARATOR => \T_NS_SEPARATOR, + \T_NULLABLE => \T_NULLABLE, ]; - $commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true); + $commentEnd = MemberVarUtility::findCommentEndWithoutAttributes($tokens, $ignore, $stackPtr); // this error is copied from above, as it comes with a needed early return if ( $commentEnd === false diff --git a/src/DigiComp/Sniffs/MemberVarUtility.php b/src/DigiComp/Sniffs/MemberVarUtility.php new file mode 100644 index 0000000..db0667a --- /dev/null +++ b/src/DigiComp/Sniffs/MemberVarUtility.php @@ -0,0 +1,35 @@ += 0; $commentEnd--) { + if (isset($ignore[$tokens[$commentEnd]['code']]) === true) { + continue; + } + + if ( + $tokens[$commentEnd]['code'] === \T_ATTRIBUTE_END + && isset($tokens[$commentEnd]['attribute_opener']) === true + ) { + $commentEnd = $tokens[$commentEnd]['attribute_opener']; + continue; + } + + break; + } + return $commentEnd; + } +} diff --git a/src/DigiComp/ruleset.xml b/src/DigiComp/ruleset.xml index 0582294..1ffe4a2 100644 --- a/src/DigiComp/ruleset.xml +++ b/src/DigiComp/ruleset.xml @@ -17,7 +17,7 @@ - + warning 4 diff --git a/tests/DigiComp/Sniffs/Annotations/VarIsLastTagOnPropertySniffTest.php b/tests/DigiComp/Sniffs/Annotations/VarIsLastTagOnPropertySniffTest.php index f45f737..9d6c68b 100644 --- a/tests/DigiComp/Sniffs/Annotations/VarIsLastTagOnPropertySniffTest.php +++ b/tests/DigiComp/Sniffs/Annotations/VarIsLastTagOnPropertySniffTest.php @@ -9,7 +9,7 @@ class VarIsLastTagOnPropertySniffTest extends TestCase public function testErrors(): void { $report = self::checkFile(__DIR__ . '/data/PropertyOrder.php'); - self::assertSame(2, $report->getErrorCount()); + self::assertSame(3, $report->getErrorCount()); } public function testIgnoreVarTag(): void diff --git a/tests/DigiComp/Sniffs/Annotations/data/PropertyOrder.php b/tests/DigiComp/Sniffs/Annotations/data/PropertyOrder.php index 124ab59..d7fc358 100644 --- a/tests/DigiComp/Sniffs/Annotations/data/PropertyOrder.php +++ b/tests/DigiComp/Sniffs/Annotations/data/PropertyOrder.php @@ -2,6 +2,11 @@ namespace Inwebs\PhpCodesniffer\Sniffs\Annotations\data; +#[\Attribute(\Attribute::TARGET_PROPERTY)] +final class OwnAttribute +{ +} + class PropertyOrder { /** @@ -28,7 +33,7 @@ class PropertyOrder * Text * * @Flow\Inject - * @var string + * @var string a very long description * @ORM\Column(stuff={ * "hallo" * }) @@ -40,4 +45,11 @@ class PropertyOrder * @var \stdClass mega! */ protected $valid; + + /** + * @var \stdClass + * @Flow\Validate + */ + #[OwnAttribute] + protected $inValidWithAttribute; } diff --git a/tests/DigiComp/Sniffs/Commenting/VariableCommentSniffTest.php b/tests/DigiComp/Sniffs/Commenting/VariableCommentSniffTest.php index b153d4c..45660f5 100644 --- a/tests/DigiComp/Sniffs/Commenting/VariableCommentSniffTest.php +++ b/tests/DigiComp/Sniffs/Commenting/VariableCommentSniffTest.php @@ -10,5 +10,6 @@ class VariableCommentSniffTest extends TestCase { $report = self::checkFile(__DIR__ . '/data/comments.php'); self::assertCount(1, $report->getWarnings()[20][6]); + self::assertCount(2, $report->getErrors()); } } diff --git a/tests/DigiComp/Sniffs/Commenting/data/comments.php b/tests/DigiComp/Sniffs/Commenting/data/comments.php index 7ddd7fa..3011138 100644 --- a/tests/DigiComp/Sniffs/Commenting/data/comments.php +++ b/tests/DigiComp/Sniffs/Commenting/data/comments.php @@ -24,3 +24,16 @@ class C extends A { class D extends a { protected $var; } + +#[\Attribute(\Attribute::TARGET_PROPERTY)] +final class OwnAttribute +{ +} + +class E extends A { + /** + * @var A + */ + #[\OwnAttribute] + protected $myVar; +}