From 2d90233e88e4c32005daf86cff198659f39b3982 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 8 Feb 2024 13:20:38 +0000 Subject: [PATCH 1/2] PSR12/DeclareStatement - rename test files --- ...est.inc => DeclareStatementUnitTest.1.inc} | 0 ...d => DeclareStatementUnitTest.1.inc.fixed} | 0 .../Tests/Files/DeclareStatementUnitTest.php | 59 +++++++++++-------- 3 files changed, 33 insertions(+), 26 deletions(-) rename src/Standards/PSR12/Tests/Files/{DeclareStatementUnitTest.inc => DeclareStatementUnitTest.1.inc} (100%) rename src/Standards/PSR12/Tests/Files/{DeclareStatementUnitTest.inc.fixed => DeclareStatementUnitTest.1.inc.fixed} (100%) diff --git a/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.inc b/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.1.inc similarity index 100% rename from src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.inc rename to src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.1.inc diff --git a/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.inc.fixed b/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.inc.fixed rename to src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.1.inc.fixed diff --git a/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.php b/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.php index 3fbf8760f0..84d31447d1 100644 --- a/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.php +++ b/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.php @@ -26,35 +26,42 @@ final class DeclareStatementUnitTest extends AbstractSniffUnitTest * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * + * @param string $testFile The name of the file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 2 => 1, - 3 => 1, - 4 => 1, - 5 => 2, - 6 => 1, - 7 => 1, - 9 => 2, - 10 => 1, - 11 => 3, - 12 => 2, - 13 => 1, - 14 => 2, - 16 => 3, - 19 => 3, - 22 => 1, - 24 => 1, - 26 => 3, - 28 => 3, - 34 => 2, - 43 => 1, - 46 => 1, - 47 => 1, - 49 => 1, - ]; + switch ($testFile) { + case 'DeclareStatementUnitTest.1.inc': + return [ + 2 => 1, + 3 => 1, + 4 => 1, + 5 => 2, + 6 => 1, + 7 => 1, + 9 => 2, + 10 => 1, + 11 => 3, + 12 => 2, + 13 => 1, + 14 => 2, + 16 => 3, + 19 => 3, + 22 => 1, + 24 => 1, + 26 => 3, + 28 => 3, + 34 => 2, + 43 => 1, + 46 => 1, + 47 => 1, + 49 => 1, + ]; + default: + return []; + }//end switch }//end getErrorList() From 7f5c88a3742fae2fb1f6217fe34b44b8410988d4 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 8 Feb 2024 13:21:36 +0000 Subject: [PATCH 2/2] PSR12/DeclareStatement - handle parse error --- src/Standards/PSR12/Sniffs/Files/DeclareStatementSniff.php | 6 ++++++ .../PSR12/Tests/Files/DeclareStatementUnitTest.2.inc | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.2.inc diff --git a/src/Standards/PSR12/Sniffs/Files/DeclareStatementSniff.php b/src/Standards/PSR12/Sniffs/Files/DeclareStatementSniff.php index b59d6c5cdb..d9615f9a4e 100644 --- a/src/Standards/PSR12/Sniffs/Files/DeclareStatementSniff.php +++ b/src/Standards/PSR12/Sniffs/Files/DeclareStatementSniff.php @@ -110,6 +110,12 @@ public function process(File $phpcsFile, $stackPtr) // There should be no space between equal sign and directive value. $value = $phpcsFile->findNext(T_WHITESPACE, ($equals + 1), null, true); + + if ($value === false) { + // Live coding / parse error. + return; + } + if ($equals !== false) { if ($tokens[($equals + 1)]['type'] !== 'T_LNUMBER') { $error = 'Expected no space between equal sign and the directive value in a declare statement'; diff --git a/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.2.inc b/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.2.inc new file mode 100644 index 0000000000..01c3a016db --- /dev/null +++ b/src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.2.inc @@ -0,0 +1,3 @@ +