Skip to content

Commit

Permalink
Generic/LowerCaseType: fix potential PHP notice
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Nov 11, 2023
1 parent b0d171b commit 3146735
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

if (empty($props) === true) {
// Parse error - property in interface or enum. Ignore.
return;
}

// Strip off potential nullable indication.
$type = ltrim($props['type'], '?');

Expand Down
5 changes: 5 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,8 @@ function intersectionReturnTypes ($var): \Package\ClassName&\Package\Other_Class

$arrow = fn (int $a, string $b, bool $c, array $d, Foo\Bar $e) : int => $a * $b;
$arrow = fn (Int $a, String $b, BOOL $c, Array $d, Foo\Bar $e) : Float => $a * $b;

// Intentional error, should be ignored by the sniff.
interface PropertiesNotAllowed {
public $notAllowed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,8 @@ function intersectionReturnTypes ($var): \Package\ClassName&\Package\Other_Class

$arrow = fn (int $a, string $b, bool $c, array $d, Foo\Bar $e) : int => $a * $b;
$arrow = fn (int $a, string $b, bool $c, array $d, Foo\Bar $e) : float => $a * $b;

// Intentional error, should be ignored by the sniff.
interface PropertiesNotAllowed {
public $notAllowed;
}
3 changes: 2 additions & 1 deletion src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public function getErrorList()
*/
public function getWarningList()
{
return [];
// Warning from getMemberProperties() about parse error.
return [98 => 1];

}//end getWarningList()

Expand Down

0 comments on commit 3146735

Please sign in to comment.