Skip to content

Commit 3146735

Browse files
committed
Generic/LowerCaseType: fix potential PHP notice
The `Generic.PHP.LowerCaseType` sniff calls the `File::getMemberProperties()` method to get information about potential properties. That method throws an exception when the `T_VARIABLE` token passed is not a property, but will create an `Internal.ParseError.InterfaceHasMemberVar` warning and return an empty array when the `T_VARIABLE` passed is an _illegal_ property, i.e. a property in a context in which it is not allowed (interface/enum). As things were, the sniff did not take a potential return value of an empty array into account, which could result in an `Undefined array key "type"` PHP notice. Fixed now. Includes unit test.
1 parent b0d171b commit 3146735

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public function process(File $phpcsFile, $stackPtr)
9797
return;
9898
}
9999

100+
if (empty($props) === true) {
101+
// Parse error - property in interface or enum. Ignore.
102+
return;
103+
}
104+
100105
// Strip off potential nullable indication.
101106
$type = ltrim($props['type'], '?');
102107

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,8 @@ function intersectionReturnTypes ($var): \Package\ClassName&\Package\Other_Class
9292

9393
$arrow = fn (int $a, string $b, bool $c, array $d, Foo\Bar $e) : int => $a * $b;
9494
$arrow = fn (Int $a, String $b, BOOL $c, Array $d, Foo\Bar $e) : Float => $a * $b;
95+
96+
// Intentional error, should be ignored by the sniff.
97+
interface PropertiesNotAllowed {
98+
public $notAllowed;
99+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,8 @@ function intersectionReturnTypes ($var): \Package\ClassName&\Package\Other_Class
9292

9393
$arrow = fn (int $a, string $b, bool $c, array $d, Foo\Bar $e) : int => $a * $b;
9494
$arrow = fn (int $a, string $b, bool $c, array $d, Foo\Bar $e) : float => $a * $b;
95+
96+
// Intentional error, should be ignored by the sniff.
97+
interface PropertiesNotAllowed {
98+
public $notAllowed;
99+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public function getErrorList()
8282
*/
8383
public function getWarningList()
8484
{
85-
return [];
85+
// Warning from getMemberProperties() about parse error.
86+
return [98 => 1];
8687

8788
}//end getWarningList()
8889

0 commit comments

Comments
 (0)