From d1fdf3200bd8ac8ec7d0ee9317730edfb29fbc61 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 9 Jul 2020 02:34:49 +0200 Subject: [PATCH] PHP 7.4 | Generic/LowerCaseType: add support for typed properties PHP 7.4 introduced typed properties, but this sniff did not examine those yet. Includes unit tests. --- .../Generic/Sniffs/PHP/LowerCaseTypeSniff.php | 37 +++++++++++++++++++ .../Tests/PHP/LowerCaseTypeUnitTest.inc | 20 ++++++++++ .../Tests/PHP/LowerCaseTypeUnitTest.inc.fixed | 20 ++++++++++ .../Tests/PHP/LowerCaseTypeUnitTest.php | 14 +++++++ 4 files changed, 91 insertions(+) diff --git a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php index 55debf4934..a738c30d66 100644 --- a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php @@ -9,6 +9,7 @@ namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP; +use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; @@ -50,6 +51,7 @@ public function register() $tokens = Tokens::$castTokens; $tokens[] = T_FUNCTION; $tokens[] = T_CLOSURE; + $tokens[] = T_VARIABLE; return $tokens; }//end register() @@ -81,6 +83,41 @@ public function process(File $phpcsFile, $stackPtr) return; } + /* + * Check property types. + */ + + if ($tokens[$stackPtr]['code'] === T_VARIABLE) { + try { + $props = $phpcsFile->getMemberProperties($stackPtr); + } catch (RuntimeException $e) { + // Not an OO property. + return; + } + + // Strip off potential nullable indication. + $type = ltrim($props['type'], '?'); + + if ($type !== '') { + $error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s"'; + $errorCode = 'PropertyTypeFound'; + + if (strpos($type, '|') !== false) { + $this->processUnionType( + $phpcsFile, + $props['type_token'], + $props['type_end_token'], + $error, + $errorCode + ); + } else if (isset($this->phpTypes[strtolower($type)]) === true) { + $this->processType($phpcsFile, $props['type_token'], $type, $error, $errorCode); + } + } + + return; + }//end if + /* * Check function return type. */ diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc index 5c3d51c08c..28e696ccc9 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc @@ -53,3 +53,23 @@ function unionParamTypesB (\Package\ClassName | Int | \Package\Other_Class | FAL function unionReturnTypesA ($var): bool|array| /* nullability operator not allowed in union */ NULL {} function unionReturnTypesB ($var): \Package\ClassName | Int | \Package\Other_Class | FALSE {} + +class TypedProperties +{ + protected ClassName $class; + public Int $int; + private ?BOOL $bool; + public Self $self; + protected PaRenT $parent; + private ARRAY $array; + public Float $float; + protected ?STRING $string; + private IterablE $iterable; + public Object $object; + protected Mixed $mixed; + + public Iterable|FALSE|NULL $unionTypeA; + protected SELF|Parent /* comment */ |\Fully\Qualified\ClassName|UnQualifiedClass $unionTypeB; + private ClassName|/*comment*/Float|STRING|False $unionTypeC; + public sTRing | aRRaY | FaLSe $unionTypeD; +} diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed index d9e6489ee7..302c0faf0a 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed @@ -53,3 +53,23 @@ function unionParamTypesB (\Package\ClassName | int | \Package\Other_Class | fal function unionReturnTypesA ($var): bool|array| /* nullability operator not allowed in union */ null {} function unionReturnTypesB ($var): \Package\ClassName | int | \Package\Other_Class | false {} + +class TypedProperties +{ + protected ClassName $class; + public int $int; + private ?bool $bool; + public self $self; + protected parent $parent; + private array $array; + public float $float; + protected ?string $string; + private iterable $iterable; + public object $object; + protected mixed $mixed; + + public iterable|false|null $unionTypeA; + protected self|parent /* comment */ |\Fully\Qualified\ClassName|UnQualifiedClass $unionTypeB; + private ClassName|/*comment*/float|string|false $unionTypeC; + public string | array | false $unionTypeD; +} diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php index ea7946d224..005c8280d9 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php @@ -49,6 +49,20 @@ public function getErrorList() 51 => 2, 53 => 1, 55 => 2, + 60 => 1, + 61 => 1, + 62 => 1, + 63 => 1, + 64 => 1, + 65 => 1, + 66 => 1, + 67 => 1, + 68 => 1, + 69 => 1, + 71 => 3, + 72 => 2, + 73 => 3, + 74 => 3, ]; }//end getErrorList()