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 Dec 5, 2023
1 parent 206b3db commit fed93f1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ The file documents changes to the PHP_CodeSniffer project.
- Thanks to Dan Wallis (@fredden) for the patch
- Fixed bug #3816 : PSR12/FileHeader: bug fix - false positives on PHP 8.2+ readonly classes
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3833 : Generic.PHP.LowerCaseType: fixed potential undefined array index notice
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3854 : Fatal error when using Gitblame report in combination with `--basepath` and running from project subdirectory
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3867 : Tokenizer/PHP: union type and intersection type operators were not correctly tokenized for static properties without explicit visibility
Expand Down
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 @@ -98,6 +98,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
7 changes: 6 additions & 1 deletion src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ 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;

$cl = function (False $a, TRUE $b, Null $c): ?True {}
$cl = function (False $a, TRUE $b, Null $c): ?True {};

// Intentional error, should be ignored by the sniff.
interface PropertiesNotAllowed {
public $notAllowed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ 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;

$cl = function (false $a, true $b, null $c): ?true {}
$cl = function (false $a, true $b, null $c): ?true {};

// 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 @@ -83,7 +83,8 @@ public function getErrorList()
*/
public function getWarningList()
{
return [];
// Warning from getMemberProperties() about parse error.
return [100 => 1];

}//end getWarningList()

Expand Down

0 comments on commit fed93f1

Please sign in to comment.