Skip to content

Generic/LowerCaseType: fix potential PHP notice #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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