diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b1984a1a9..02ff99ddde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php index 28ecb0215f..daa6f0f2e9 100644 --- a/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php @@ -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'], '?'); diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc index 98d2aa4b56..56393c0b24 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc @@ -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; +} diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed index 9f17f0f7b1..c1055c726b 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed @@ -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; +} diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php index 51c42943bc..29d15c59a6 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php +++ b/src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php @@ -83,7 +83,8 @@ public function getErrorList() */ public function getWarningList() { - return []; + // Warning from getMemberProperties() about parse error. + return [100 => 1]; }//end getWarningList()