Skip to content

Commit d1fdf32

Browse files
committed
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.
1 parent b4313a7 commit d1fdf32

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
1111

12+
use PHP_CodeSniffer\Exceptions\RuntimeException;
1213
use PHP_CodeSniffer\Files\File;
1314
use PHP_CodeSniffer\Sniffs\Sniff;
1415
use PHP_CodeSniffer\Util\Tokens;
@@ -50,6 +51,7 @@ public function register()
5051
$tokens = Tokens::$castTokens;
5152
$tokens[] = T_FUNCTION;
5253
$tokens[] = T_CLOSURE;
54+
$tokens[] = T_VARIABLE;
5355
return $tokens;
5456

5557
}//end register()
@@ -81,6 +83,41 @@ public function process(File $phpcsFile, $stackPtr)
8183
return;
8284
}
8385

86+
/*
87+
* Check property types.
88+
*/
89+
90+
if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
91+
try {
92+
$props = $phpcsFile->getMemberProperties($stackPtr);
93+
} catch (RuntimeException $e) {
94+
// Not an OO property.
95+
return;
96+
}
97+
98+
// Strip off potential nullable indication.
99+
$type = ltrim($props['type'], '?');
100+
101+
if ($type !== '') {
102+
$error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s"';
103+
$errorCode = 'PropertyTypeFound';
104+
105+
if (strpos($type, '|') !== false) {
106+
$this->processUnionType(
107+
$phpcsFile,
108+
$props['type_token'],
109+
$props['type_end_token'],
110+
$error,
111+
$errorCode
112+
);
113+
} else if (isset($this->phpTypes[strtolower($type)]) === true) {
114+
$this->processType($phpcsFile, $props['type_token'], $type, $error, $errorCode);
115+
}
116+
}
117+
118+
return;
119+
}//end if
120+
84121
/*
85122
* Check function return type.
86123
*/

src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,23 @@ function unionParamTypesB (\Package\ClassName | Int | \Package\Other_Class | FAL
5353
function unionReturnTypesA ($var): bool|array| /* nullability operator not allowed in union */ NULL {}
5454

5555
function unionReturnTypesB ($var): \Package\ClassName | Int | \Package\Other_Class | FALSE {}
56+
57+
class TypedProperties
58+
{
59+
protected ClassName $class;
60+
public Int $int;
61+
private ?BOOL $bool;
62+
public Self $self;
63+
protected PaRenT $parent;
64+
private ARRAY $array;
65+
public Float $float;
66+
protected ?STRING $string;
67+
private IterablE $iterable;
68+
public Object $object;
69+
protected Mixed $mixed;
70+
71+
public Iterable|FALSE|NULL $unionTypeA;
72+
protected SELF|Parent /* comment */ |\Fully\Qualified\ClassName|UnQualifiedClass $unionTypeB;
73+
private ClassName|/*comment*/Float|STRING|False $unionTypeC;
74+
public sTRing | aRRaY | FaLSe $unionTypeD;
75+
}

src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,23 @@ function unionParamTypesB (\Package\ClassName | int | \Package\Other_Class | fal
5353
function unionReturnTypesA ($var): bool|array| /* nullability operator not allowed in union */ null {}
5454

5555
function unionReturnTypesB ($var): \Package\ClassName | int | \Package\Other_Class | false {}
56+
57+
class TypedProperties
58+
{
59+
protected ClassName $class;
60+
public int $int;
61+
private ?bool $bool;
62+
public self $self;
63+
protected parent $parent;
64+
private array $array;
65+
public float $float;
66+
protected ?string $string;
67+
private iterable $iterable;
68+
public object $object;
69+
protected mixed $mixed;
70+
71+
public iterable|false|null $unionTypeA;
72+
protected self|parent /* comment */ |\Fully\Qualified\ClassName|UnQualifiedClass $unionTypeB;
73+
private ClassName|/*comment*/float|string|false $unionTypeC;
74+
public string | array | false $unionTypeD;
75+
}

src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ public function getErrorList()
4949
51 => 2,
5050
53 => 1,
5151
55 => 2,
52+
60 => 1,
53+
61 => 1,
54+
62 => 1,
55+
63 => 1,
56+
64 => 1,
57+
65 => 1,
58+
66 => 1,
59+
67 => 1,
60+
68 => 1,
61+
69 => 1,
62+
71 => 3,
63+
72 => 2,
64+
73 => 3,
65+
74 => 3,
5266
];
5367

5468
}//end getErrorList()

0 commit comments

Comments
 (0)