From f7290e12e5bda7f091e3faefe8f67ae7fe785846 Mon Sep 17 00:00:00 2001 From: Mohamed Alsharaf Date: Mon, 2 Oct 2023 15:49:54 +1300 Subject: [PATCH] Implement property typehint sniff for private static --- .gitignore | 1 + .../Property/TypehintPrivateStaticSniff.php | 85 +++++++++++++++++++ .../TypehintPrivateStaticSniffTest.php | 39 +++++++++ ...ypehintPrivateStaticSniff_Errors.fixed.php | 23 +++++ .../TypehintPrivateStaticSniff_Errors.php | 32 +++++++ .../TypehintPrivateStaticSniff_NoErrors.php | 17 ++++ 6 files changed, 197 insertions(+) create mode 100644 src/Sniffs/Property/TypehintPrivateStaticSniff.php create mode 100644 tests/Sniffs/Property/TypehintPrivateStaticSniffTest.php create mode 100644 tests/Sniffs/Property/data/TypehintPrivateStaticSniff_Errors.fixed.php create mode 100644 tests/Sniffs/Property/data/TypehintPrivateStaticSniff_Errors.php create mode 100644 tests/Sniffs/Property/data/TypehintPrivateStaticSniff_NoErrors.php diff --git a/.gitignore b/.gitignore index 92459d1..411aa11 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .vscode/ composer.lock .phpunit.result.cache +/app diff --git a/src/Sniffs/Property/TypehintPrivateStaticSniff.php b/src/Sniffs/Property/TypehintPrivateStaticSniff.php new file mode 100644 index 0000000..a99738e --- /dev/null +++ b/src/Sniffs/Property/TypehintPrivateStaticSniff.php @@ -0,0 +1,85 @@ +getTokens(); + + $propertyPointer = TokenHelper::findNext($phpcsFile, [T_FUNCTION, T_CONST, T_VARIABLE], $stackPtr + 1); + + if ($propertyPointer === null || $tokens[$propertyPointer]['code'] !== T_VARIABLE) { + return; + } + + if (!PropertyHelper::isProperty($phpcsFile, $propertyPointer)) { + return; + } + + $previousPointer = TokenHelper::findPreviousExcluding( + $phpcsFile, + array_merge(TokenHelper::$ineffectiveTokenCodes, TokenHelper::getTypeHintTokenCodes(), [T_NULLABLE]), + $propertyPointer - 1 + ); + $propertyStartPointer = TokenHelper::findPrevious($phpcsFile, [T_PRIVATE], $propertyPointer - 1); + + // Skip non-static property + if (!($tokens[$previousPointer]['code'] === T_STATIC && $tokens[$propertyStartPointer]['code'] === T_PRIVATE)) { + return; + } + + // Process property type sniff from SlevomatCodingStandard + $sniff = new PropertyTypeHintSniff(); + $sniff->enableNativeTypeHint = true; + $sniff->enableIntersectionTypeHint = true; + $sniff->enableMixedTypeHint = true; + $sniff->enableStandaloneNullTrueFalseTypeHints = true; + $sniff->enableUnionTypeHint = true; + $sniff->process($phpcsFile, $stackPtr); + } + +} diff --git a/tests/Sniffs/Property/TypehintPrivateStaticSniffTest.php b/tests/Sniffs/Property/TypehintPrivateStaticSniffTest.php new file mode 100644 index 0000000..e18013c --- /dev/null +++ b/tests/Sniffs/Property/TypehintPrivateStaticSniffTest.php @@ -0,0 +1,39 @@ +getErrorCount()); + + self::assertSniffError($report, 8, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT); + self::assertSniffError($report, 12, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT); + self::assertSniffError($report, 16, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT); + self::assertSniffError($report, 21, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT); + self::assertSniffError($report, 23, PropertyTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT); + self::assertSniffError($report, 24, PropertyTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT); + self::assertSniffError($report, 25, PropertyTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT); + self::assertNoSniffError($report, 29); + self::assertNoSniffError($report, 31); + + self::assertAllFixedInFile($report); + } +} diff --git a/tests/Sniffs/Property/data/TypehintPrivateStaticSniff_Errors.fixed.php b/tests/Sniffs/Property/data/TypehintPrivateStaticSniff_Errors.fixed.php new file mode 100644 index 0000000..5bd9af7 --- /dev/null +++ b/tests/Sniffs/Property/data/TypehintPrivateStaticSniff_Errors.fixed.php @@ -0,0 +1,23 @@ +