From 7126fad1c7f9afbc48e847f46a4e15681c337dc2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 31 Oct 2023 19:07:33 +0100 Subject: [PATCH 1/2] ErrorSuppressionTest: prevent Internal errors ... about a mismatch in line endings when the code "template" is defined using a heredoc with Linux line endings, while the code snippets being inserted into the code "template" were using line endings matching the OS on which the tests were being run. --- tests/Core/ErrorSuppressionTest.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/Core/ErrorSuppressionTest.php b/tests/Core/ErrorSuppressionTest.php index 7181613c62..5d8cd78edb 100644 --- a/tests/Core/ErrorSuppressionTest.php +++ b/tests/Core/ErrorSuppressionTest.php @@ -550,19 +550,19 @@ public function dataNestedSuppressLine() // Process with line suppression nested within disable/enable suppression. 'disable/enable: slash comment, next line nested single line suppression' => [ - 'before' => '// phpcs:disable'.PHP_EOL.'// phpcs:ignore', + 'before' => '// phpcs:disable'."\n".'// phpcs:ignore', 'after' => '// phpcs:enable', ], 'disable/enable: slash comment, with @, next line nested single line suppression' => [ - 'before' => '// @phpcs:disable'.PHP_EOL.'// @phpcs:ignore', + 'before' => '// @phpcs:disable'."\n".'// @phpcs:ignore', 'after' => '// @phpcs:enable', ], 'disable/enable: hash comment, next line nested single line suppression' => [ - 'before' => '# @phpcs:disable'.PHP_EOL.'# @phpcs:ignore', + 'before' => '# @phpcs:disable'."\n".'# @phpcs:ignore', 'after' => '# @phpcs:enable', ], 'old style: slash comment, next line nested single line suppression' => [ - 'before' => '// @codingStandardsIgnoreStart'.PHP_EOL.'// @codingStandardsIgnoreLine', + 'before' => '// @codingStandardsIgnoreStart'."\n".'// @codingStandardsIgnoreLine', 'after' => '// @codingStandardsIgnoreEnd', ], ]; @@ -734,7 +734,7 @@ public function dataSuppressFile() 'ignoreFile: start of file, hash comment, with @' => ['before' => '# @phpcs:ignoreFile'], 'ignoreFile: start of file, single-line star comment' => ['before' => '/* phpcs:ignoreFile */'], 'ignoreFile: start of file, multi-line star comment' => [ - 'before' => '/*'.PHP_EOL.' phpcs:ignoreFile'.PHP_EOL.' */', + 'before' => '/*'."\n".' phpcs:ignoreFile'."\n".' */', ], 'ignoreFile: start of file, single-line docblock comment' => ['before' => '/** phpcs:ignoreFile */'], @@ -748,7 +748,7 @@ public function dataSuppressFile() 'old style: start of file, slash comment' => ['before' => '// @codingStandardsIgnoreFile'], 'old style: start of file, single-line star comment' => ['before' => '/* @codingStandardsIgnoreFile */'], 'old style: start of file, multi-line star comment' => [ - 'before' => '/*'.PHP_EOL.' @codingStandardsIgnoreFile'.PHP_EOL.' */', + 'before' => '/*'."\n".' @codingStandardsIgnoreFile'."\n".' */', ], 'old style: start of file, single-line docblock comment' => ['before' => '/** @codingStandardsIgnoreFile */'], @@ -829,18 +829,18 @@ public function dataDisableSelected() 'expectedErrors' => 1, ], 'disable: single sniff, docblock' => [ - 'before' => '/**'.PHP_EOL.' * phpcs:disable Generic.Commenting.Todo'.PHP_EOL.' */ ', + 'before' => '/**'."\n".' * phpcs:disable Generic.Commenting.Todo'."\n".' */ ', 'expectedErrors' => 1, ], 'disable: single sniff, docblock, with @' => [ - 'before' => '/**'.PHP_EOL.' * @phpcs:disable Generic.Commenting.Todo'.PHP_EOL.' */ ', + 'before' => '/**'."\n".' * @phpcs:disable Generic.Commenting.Todo'."\n".' */ ', 'expectedErrors' => 1, ], // Multiple sniffs. 'disable: multiple sniffs in one comment' => ['before' => '// phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant'], 'disable: multiple sniff in multiple comments' => [ - 'before' => '// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'// phpcs:disable Generic.PHP.LowerCaseConstant', + 'before' => '// phpcs:disable Generic.Commenting.Todo'."\n".'// phpcs:disable Generic.PHP.LowerCaseConstant', ], // Selectiveness variations. @@ -857,17 +857,17 @@ public function dataDisableSelected() // Wrong category/sniff/code. 'disable: wrong error code and category' => [ - 'before' => '/**'.PHP_EOL.' * phpcs:disable Generic.PHP.LowerCaseConstant.Upper,Generic.Comments'.PHP_EOL.' */ ', + 'before' => '/**'."\n".' * phpcs:disable Generic.PHP.LowerCaseConstant.Upper,Generic.Comments'."\n".' */ ', 'expectedErrors' => 1, 'expectedWarnings' => 1, ], 'disable: wrong category, docblock' => [ - 'before' => '/**'.PHP_EOL.' * phpcs:disable Generic.Files'.PHP_EOL.' */ ', + 'before' => '/**'."\n".' * phpcs:disable Generic.Files'."\n".' */ ', 'expectedErrors' => 1, 'expectedWarnings' => 1, ], 'disable: wrong category, docblock, with @' => [ - 'before' => '/**'.PHP_EOL.' * @phpcs:disable Generic.Files'.PHP_EOL.' */ ', + 'before' => '/**'."\n".' * @phpcs:disable Generic.Files'."\n".' */ ', 'expectedErrors' => 1, 'expectedWarnings' => 1, ], @@ -1124,7 +1124,7 @@ public function dataIgnoreSelected() 'expectedWarnings' => 1, ], 'disable: single sniff; ignore: single sniff' => [ - 'before' => '// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'// phpcs:ignore Generic.PHP.LowerCaseConstant', + 'before' => '// phpcs:disable Generic.Commenting.Todo'."\n".'// phpcs:ignore Generic.PHP.LowerCaseConstant', 'expectedErrors' => 1, 'expectedWarnings' => 0, ], From 0a2d7f1dbddf7a04687d8bcf7c95a54b96aa594b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 31 Oct 2023 18:46:37 +0100 Subject: [PATCH 2/2] File::addMessage(): do not ignore `Internal` errors when scanning selectively When either the `--sniffs=...` CLI parameter is used, or the `--exclude=...` CLI parameter, the `File::addMessage()` method bows out when an error is passed which is not for one of the selected sniffs/is for one of the excluded sniffs. Unfortunately, this "bowing out" did not take `Internal` errors into account, meaning those were now hidden, while those should _always_ be thrown as they generally inform the end-user of something seriously wrong (mixed line endings/no code found etc). Fixed now. Includes updating two test files to allow for seeing internal errors. --- src/Files/File.php | 1 + .../Tests/PHP/DisallowAlternativePHPTagsUnitTest.php | 8 ++++++++ .../Generic/Tests/PHP/DisallowShortOpenTagUnitTest.php | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/src/Files/File.php b/src/Files/File.php index 21cbeb2158..0a442b16ad 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -909,6 +909,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s // Filter out any messages for sniffs that shouldn't have run // due to the use of the --sniffs command line argument. if ($includeAll === false + && $parts[0] !== 'Internal' && ((empty($this->configCache['sniffs']) === false && in_array(strtolower($listenerCode), $this->configCache['sniffs'], true) === false) || (empty($this->configCache['exclude']) === false diff --git a/src/Standards/Generic/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php b/src/Standards/Generic/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php index 953e8ad9b5..93db3508f2 100644 --- a/src/Standards/Generic/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php +++ b/src/Standards/Generic/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php @@ -89,7 +89,15 @@ public function getErrorList($testFile='') public function getWarningList($testFile='') { if ($testFile === 'DisallowAlternativePHPTagsUnitTest.3.inc') { + // Check if the Internal.NoCodeFound error can be expected on line 1. + $option = (bool) ini_get('short_open_tag'); + $line1 = 1; + if ($option === true) { + $line1 = 0; + } + return [ + 1 => $line1, 3 => 1, 4 => 1, 5 => 1, diff --git a/src/Standards/Generic/Tests/PHP/DisallowShortOpenTagUnitTest.php b/src/Standards/Generic/Tests/PHP/DisallowShortOpenTagUnitTest.php index b79572ce6b..469fdd4530 100644 --- a/src/Standards/Generic/Tests/PHP/DisallowShortOpenTagUnitTest.php +++ b/src/Standards/Generic/Tests/PHP/DisallowShortOpenTagUnitTest.php @@ -88,7 +88,14 @@ public function getWarningList($testFile='') case 'DisallowShortOpenTagUnitTest.1.inc': return []; case 'DisallowShortOpenTagUnitTest.3.inc': + // Check if the Internal.NoCodeFound error can be expected on line 1. + $option = (bool) ini_get('short_open_tag'); + $line1 = 1; + if ($option === true) { + $line1 = 0; + } return [ + 1 => $line1, 3 => 1, 6 => 1, 11 => 1,