From 9a0ee0c6764afcfdb2224eb6aa2ff2c1ad453b81 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Thu, 21 Sep 2023 14:58:22 -0400 Subject: [PATCH 1/4] Improve handling of disable/enable/ignore directives MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current method, listing codes to disable and a list of exceptions to that list, still has trouble with some cases. For example, disabling a standard, re-enabling a category within that standard, then ignoring or disabling a sniff within that category cannot be handled. We'd need a list of exceptions to the exceptions, and possibly a list of exceptions to that list too, and figuring out how to keep those lists up to date as new directives are encountered could prove to be confusing. Since the standard→category→sniff→code hierarchy is supposed to be thought of as a tree, let's store the ignore list that way instead. Manipulating the branches of the tree is straightforward no matter what directives are encountered. In this implementation I've favored speed over space: there are cases where we could prune a subtree that would evaluate to "ignore" or "don't ignore" for any possible input, but detecting that doesn't seem worth the time when it's not likely there will be so many enable or disable directives that the wasted space will be a problem. Fixes #111 --- src/Files/File.php | 31 +--- src/Tokenizers/Tokenizer.php | 101 ++++------- src/Util/IgnoreList.php | 168 ++++++++++++++++++ tests/Core/ErrorSuppressionTest.php | 64 +++++++ tests/Core/IgnoreListTest.php | 253 ++++++++++++++++++++++++++++ 5 files changed, 522 insertions(+), 95 deletions(-) create mode 100644 src/Util/IgnoreList.php create mode 100644 tests/Core/IgnoreListTest.php diff --git a/src/Files/File.php b/src/Files/File.php index d9209df67c..473ae5a66c 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -801,7 +801,7 @@ public function addFixableWarning( protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable) { // Check if this line is ignoring all message codes. - if (isset($this->tokenizer->ignoredLines[$line]['.all']) === true) { + if (isset($this->tokenizer->ignoredLines[$line]) === true && $this->tokenizer->ignoredLines[$line]->isAll() === true) { return false; } @@ -835,32 +835,9 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s ]; }//end if - if (isset($this->tokenizer->ignoredLines[$line]) === true) { - // Check if this line is ignoring this specific message. - $ignored = false; - foreach ($checkCodes as $checkCode) { - if (isset($this->tokenizer->ignoredLines[$line][$checkCode]) === true) { - $ignored = true; - break; - } - } - - // If it is ignored, make sure there is no exception in place. - if ($ignored === true - && isset($this->tokenizer->ignoredLines[$line]['.except']) === true - ) { - foreach ($checkCodes as $checkCode) { - if (isset($this->tokenizer->ignoredLines[$line]['.except'][$checkCode]) === true) { - $ignored = false; - break; - } - } - } - - if ($ignored === true) { - return false; - } - }//end if + if (isset($this->tokenizer->ignoredLines[$line]) === true && $this->tokenizer->ignoredLines[$line]->check($sniffCode) === true) { + return false; + } $includeAll = true; if ($this->configCache['cache'] === false diff --git a/src/Tokenizers/Tokenizer.php b/src/Tokenizers/Tokenizer.php index f683f75a3a..301c28adc9 100644 --- a/src/Tokenizers/Tokenizer.php +++ b/src/Tokenizers/Tokenizer.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Exceptions\TokenizerException; use PHP_CodeSniffer\Util\Common; +use PHP_CodeSniffer\Util\IgnoreList; use PHP_CodeSniffer\Util\Tokens; use PHP_CodeSniffer\Util\Writers\StatusWriter; @@ -175,6 +176,7 @@ private function createPositionMap() $lineNumber = 1; $eolLen = strlen($this->eolChar); $ignoring = null; + $ignoreAll = IgnoreList::ignoringAll(); $inTests = defined('PHP_CODESNIFFER_IN_TESTS'); $checkEncoding = false; @@ -349,7 +351,7 @@ private function createPositionMap() if (substr($commentTextLower, 0, 9) === 'phpcs:set') { // Ignore standards for complete lines that change sniff settings. if ($lineHasOtherTokens === false) { - $this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true]; + $this->ignoredLines[$this->tokens[$i]['line']] = $ignoreAll; } // Need to maintain case here, to get the correct sniff code. @@ -372,42 +374,28 @@ private function createPositionMap() } else if (substr($commentTextLower, 0, 13) === 'phpcs:disable') { if ($lineHasOtherContent === false) { // Completely ignore the comment line. - $this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true]; - } - - if ($ignoring === null) { - $ignoring = []; + $this->ignoredLines[$this->tokens[$i]['line']] = $ignoreAll; } $disabledSniffs = []; $additionalText = substr($commentText, 14); if (empty($additionalText) === true) { - $ignoring = ['.all' => true]; + $ignoring = $ignoreAll; } else { + if ($ignoring === null) { + $ignoring = IgnoreList::ignoringNone(); + } else { + $ignoring = clone $ignoring; + } + $parts = explode(',', $additionalText); foreach ($parts as $sniffCode) { $sniffCode = trim($sniffCode); $disabledSniffs[$sniffCode] = true; - $ignoring[$sniffCode] = true; - - // This newly disabled sniff might be disabling an existing - // enabled exception that we are tracking. - if (isset($ignoring['.except']) === true) { - foreach (array_keys($ignoring['.except']) as $ignoredSniffCode) { - if ($ignoredSniffCode === $sniffCode - || strpos($ignoredSniffCode, $sniffCode.'.') === 0 - ) { - unset($ignoring['.except'][$ignoredSniffCode]); - } - } - - if (empty($ignoring['.except']) === true) { - unset($ignoring['.except']); - } - } - }//end foreach - }//end if + $ignoring->set($sniffCode, true); + } + } $this->tokens[$i]['code'] = T_PHPCS_DISABLE; $this->tokens[$i]['type'] = 'T_PHPCS_DISABLE'; @@ -420,49 +408,22 @@ private function createPositionMap() if (empty($additionalText) === true) { $ignoring = null; } else { - $parts = explode(',', $additionalText); + $ignoring = clone $ignoring; + $parts = explode(',', $additionalText); foreach ($parts as $sniffCode) { $sniffCode = trim($sniffCode); $enabledSniffs[$sniffCode] = true; + $ignoring->set($sniffCode, false); + } - // This new enabled sniff might remove previously disabled - // sniffs if it is actually a standard or category of sniffs. - foreach (array_keys($ignoring) as $ignoredSniffCode) { - if ($ignoredSniffCode === $sniffCode - || strpos($ignoredSniffCode, $sniffCode.'.') === 0 - ) { - unset($ignoring[$ignoredSniffCode]); - } - } - - // This new enabled sniff might be able to clear up - // previously enabled sniffs if it is actually a standard or - // category of sniffs. - if (isset($ignoring['.except']) === true) { - foreach (array_keys($ignoring['.except']) as $ignoredSniffCode) { - if ($ignoredSniffCode === $sniffCode - || strpos($ignoredSniffCode, $sniffCode.'.') === 0 - ) { - unset($ignoring['.except'][$ignoredSniffCode]); - } - } - } - }//end foreach - - if (empty($ignoring) === true) { + if ($ignoring->isEmpty() === true) { $ignoring = null; - } else { - if (isset($ignoring['.except']) === true) { - $ignoring['.except'] += $enabledSniffs; - } else { - $ignoring['.except'] = $enabledSniffs; - } } - }//end if + } if ($lineHasOtherContent === false) { // Completely ignore the comment line. - $this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true]; + $this->ignoredLines[$this->tokens[$i]['line']] = $ignoreAll; } else { // The comment is on the same line as the code it is ignoring, // so respect the new ignore rules. @@ -479,11 +440,19 @@ private function createPositionMap() $additionalText = substr($commentText, 13); if (empty($additionalText) === true) { - $ignoreRules = ['.all' => true]; + $ignoreRules = ['.all' => true]; + $lineIgnoring = $ignoreAll; } else { $parts = explode(',', $additionalText); + if ($ignoring === null) { + $lineIgnoring = IgnoreList::ignoringNone(); + } else { + $lineIgnoring = clone $ignoring; + } + foreach ($parts as $sniffCode) { $ignoreRules[trim($sniffCode)] = true; + $lineIgnoring->set($sniffCode, true); } } @@ -491,19 +460,15 @@ private function createPositionMap() $this->tokens[$i]['type'] = 'T_PHPCS_IGNORE'; $this->tokens[$i]['sniffCodes'] = $ignoreRules; - if ($ignoring !== null) { - $ignoreRules += $ignoring; - } - if ($lineHasOtherContent === false) { // Completely ignore the comment line, and set the following // line to include the ignore rules we've set. - $this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true]; - $this->ignoredLines[($this->tokens[$i]['line'] + 1)] = $ignoreRules; + $this->ignoredLines[$this->tokens[$i]['line']] = $ignoreAll; + $this->ignoredLines[($this->tokens[$i]['line'] + 1)] = $lineIgnoring; } else { // The comment is on the same line as the code it is ignoring, // so respect the ignore rules it set. - $this->ignoredLines[$this->tokens[$i]['line']] = $ignoreRules; + $this->ignoredLines[$this->tokens[$i]['line']] = $lineIgnoring; } }//end if }//end if diff --git a/src/Util/IgnoreList.php b/src/Util/IgnoreList.php new file mode 100644 index 0000000000..26ba8f4fbb --- /dev/null +++ b/src/Util/IgnoreList.php @@ -0,0 +1,168 @@ + + * @copyright 2023 Brad Jorsch + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Util; + +class IgnoreList +{ + + /** + * Ignore data. + * + * Data is a tree, standard → category → sniff → code. + * Each level may be a boolean indicating that everything underneath the branch is or is not ignored, or + * may have a `.default' key indicating the default status for any branches not in the tree. + * + * @var array|boolean + */ + private $data = [ '.default' => false ]; + + + /** + * Get an instance set to ignore nothing. + * + * @return static + */ + public static function ignoringNone() + { + return new self(); + + }//end ignoringNone() + + + /** + * Get an instance set to ignore everything. + * + * @return static + */ + public static function ignoringAll() + { + $ret = new self(); + $ret->data['.default'] = true; + return $ret; + + }//end ignoringAll() + + + /** + * Check whether a sniff code is ignored. + * + * @param string $code Partial or complete sniff code. + * + * @return bool + */ + public function check($code) + { + $data = $this->data; + $ret = $data['.default']; + foreach (explode('.', $code) as $part) { + if (isset($data[$part]) === false) { + break; + } + + $data = $data[$part]; + if (is_bool($data) === true) { + $ret = $data; + break; + } + + if (isset($data['.default']) === true) { + $ret = $data['.default']; + } + } + + return $ret; + + }//end check() + + + /** + * Set the ignore status for a sniff. + * + * @param string $code Partial or complete sniff code. + * @param bool $ignore Whether the specified sniff should be ignored. + * + * @return this + */ + public function set($code, $ignore) + { + $data = &$this->data; + $parts = explode('.', $code); + while (count($parts) > 1) { + $part = array_shift($parts); + if (isset($data[$part]) === false) { + $data[$part] = []; + } else if (is_bool($data[$part]) === true) { + $data[$part] = [ '.default' => $data[$part] ]; + } + + $data = &$data[$part]; + } + + $part = array_shift($parts); + $data[$part] = (bool) $ignore; + + return $this; + + }//end set() + + + /** + * Check if the list is empty. + * + * @return bool + */ + public function isEmpty() + { + $arrs = [ $this->data ]; + while ($arrs !== []) { + $arr = array_pop($arrs); + foreach ($arr as $v) { + if ($v === true) { + return false; + } + + if (is_array($v) === true) { + $arrs[] = $v; + } + } + } + + return true; + + }//end isEmpty() + + + /** + * Check if the list ignores everything. + * + * @return bool + */ + public function isAll() + { + $arrs = [ $this->data ]; + while ($arrs !== []) { + $arr = array_pop($arrs); + foreach ($arr as $v) { + if ($v === false) { + return false; + } + + if (is_array($v) === true) { + $arrs[] = $v; + } + } + } + + return true; + + }//end isAll() + + +}//end class diff --git a/tests/Core/ErrorSuppressionTest.php b/tests/Core/ErrorSuppressionTest.php index 7d8c8366b0..a1b8eb4723 100644 --- a/tests/Core/ErrorSuppressionTest.php +++ b/tests/Core/ErrorSuppressionTest.php @@ -993,6 +993,70 @@ public static function dataEnableSelected() 'expectedErrors' => 1, 'expectedWarnings' => 2, ], + 'disable: two sniffs; enable: both sniffs; ignore: one of those sniffs (#3889)' => [ + 'code' => ' + // phpcs:disable Generic.PHP.LowerCaseConstant + // phpcs:disable Generic.Commenting.Todo + //TODO: write some code + $var = TRUE; + // phpcs:enable Generic.Commenting.Todo + // phpcs:enable Generic.PHP.LowerCaseConstant + + $var = FALSE; // phpcs:ignore Generic.PHP.LowerCaseConstant + ', + 'expectedErrors' => 0, + 'expectedWarnings' => 0, + ], + 'disable: two sniffs; enable: one sniff; ignore: enabled sniff' => [ + 'code' => ' + // phpcs:disable Generic.PHP.LowerCaseConstant + // phpcs:disable Generic.Commenting.Todo + //TODO: write some code + $var = TRUE; + // phpcs:enable Generic.PHP.LowerCaseConstant + + $var = FALSE; // phpcs:ignore Generic.PHP.LowerCaseConstant + ', + 'expectedErrors' => 0, + 'expectedWarnings' => 0, + ], + 'disable: two sniffs; enable: one sniff; ignore: category' => [ + 'code' => ' + // phpcs:disable Generic.PHP.LowerCaseConstant + // phpcs:disable Generic.Commenting.Todo + //TODO: write some code + $var = TRUE; + // phpcs:enable Generic.PHP.LowerCaseConstant + + $var = FALSE; // phpcs:ignore Generic.PHP + ', + 'expectedErrors' => 0, + 'expectedWarnings' => 0, + ], + 'disable: two sniffs; enable: category; ignore: sniff in category' => [ + 'code' => ' + // phpcs:disable Generic.PHP.LowerCaseConstant + // phpcs:disable Generic.Commenting.Todo + //TODO: write some code + $var = TRUE; + // phpcs:enable Generic.PHP + + $var = FALSE; // phpcs:ignore Generic.PHP.LowerCaseConstant + ', + 'expectedErrors' => 0, + 'expectedWarnings' => 0, + ], + 'disable: standard; enable: category in standard; disable: sniff in category' => [ + 'code' => ' + // phpcs:disable Generic + // phpcs:enable Generic.PHP + // phpcs:disable Generic.PHP.LowerCaseConstant + //TODO: write some code + $var = TRUE; + ', + 'expectedErrors' => 0, + 'expectedWarnings' => 0, + ], ]; }//end dataEnableSelected() diff --git a/tests/Core/IgnoreListTest.php b/tests/Core/IgnoreListTest.php new file mode 100644 index 0000000000..ea19db1831 --- /dev/null +++ b/tests/Core/IgnoreListTest.php @@ -0,0 +1,253 @@ + + * @copyright 2023 Brad Jorsch + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core; + +use PHP_CodeSniffer\Util\IgnoreList; +use PHPUnit\Framework\TestCase; + +class IgnoreListTest extends TestCase +{ + + + /** + * Test ignoringNone() works. + * + * @covers PHP_CodeSniffer\Util\IgnoreList::ignoringNone + * @return void + */ + public function testIgnoringNoneWorks() + { + $ignoreList = IgnoreList::ignoringNone(); + $this->assertInstanceOf(IgnoreList::class, $ignoreList); + $this->assertFalse($ignoreList->check('Anything')); + + }//end testIgnoringNoneWorks() + + + /** + * Test ignoringAll() works. + * + * @covers PHP_CodeSniffer\Util\IgnoreList::ignoringAll + * @return void + */ + public function testIgnoringAllWorks() + { + $ignoreList = IgnoreList::ignoringAll(); + $this->assertInstanceOf(IgnoreList::class, $ignoreList); + $this->assertTrue($ignoreList->check('Anything')); + + }//end testIgnoringAllWorks() + + + /** + * Test isEmpty() and isAll(). + * + * @param IgnoreList $ignoreList IgnoreList to test. + * @param bool $expectEmpty Expected return value from isEmpty(). + * @param bool $expectAll Expected return value from isAll(). + * + * @return void + * + * @dataProvider dataIsEmptyAndAll + * @covers PHP_CodeSniffer\Util\IgnoreList::isEmpty + * @covers PHP_CodeSniffer\Util\IgnoreList::isAll + */ + public function testIsEmptyAndAll($ignoreList, $expectEmpty, $expectAll) + { + $this->assertSame($expectEmpty, $ignoreList->isEmpty()); + $this->assertSame($expectAll, $ignoreList->isAll()); + + }//end testIsEmptyAndAll() + + + /** + * Data provider. + * + * @see testIsEmptyAndAll() + * + * @return array + */ + public static function dataIsEmptyAndAll() + { + return [ + 'fresh list' => [ + new IgnoreList(), + true, + false, + ], + 'list from ignoringNone' => [ + IgnoreList::ignoringNone(), + true, + false, + ], + 'list from ignoringAll' => [ + IgnoreList::ignoringAll(), + false, + true, + ], + 'list from ignoringNone, something set to false' => [ + IgnoreList::ignoringNone()->set('Foo.Bar', false), + true, + false, + ], + 'list from ignoringNone, something set to true' => [ + IgnoreList::ignoringNone()->set('Foo.Bar', true), + false, + false, + ], + 'list from ignoringAll, something set to false' => [ + IgnoreList::ignoringAll()->set('Foo.Bar', false), + false, + false, + ], + 'list from ignoringAll, something set to true' => [ + IgnoreList::ignoringAll()->set('Foo.Bar', true), + false, + true, + ], + 'list from ignoringNone, something set to true then overridden' => [ + IgnoreList::ignoringNone()->set('Foo.Bar', true)->set('Foo', false), + true, + false, + ], + 'list from ignoringAll, something set to false then overridden' => [ + IgnoreList::ignoringAll()->set('Foo.Bar', false)->set('Foo', true), + false, + true, + ], + ]; + + }//end dataIsEmptyAndAll() + + + /** + * Test check() and set(). + * + * @param array $toSet Associative array of $code => $ignore to pass to set(). + * @param array $toCheck Associative array of $code => $expect to pass to check(). + * + * @return void + * + * @dataProvider dataCheckAndSet + * @covers PHP_CodeSniffer\Util\IgnoreList::check + * @covers PHP_CodeSniffer\Util\IgnoreList::set + */ + public function testCheckAndSet($toSet, $toCheck) + { + $ignoreList = new IgnoreList(); + foreach ($toSet as $code => $ignore) { + $this->assertSame($ignoreList, $ignoreList->set($code, $ignore)); + } + + foreach ($toCheck as $code => $expect) { + $this->assertSame($expect, $ignoreList->check($code)); + } + + }//end testCheckAndSet() + + + /** + * Data provider. + * + * @see testCheckAndSet() + * + * @return array + */ + public static function dataCheckAndSet() + { + return [ + 'set a code' => [ + ['Standard.Category.Sniff.Code' => true], + [ + 'Standard.Category.Sniff.Code' => true, + 'Standard.Category.Sniff.OtherCode' => false, + 'Standard.Category.OtherSniff.Code' => false, + 'Standard.OtherCategory.Sniff.Code' => false, + 'OtherStandard.Category.Sniff.Code' => false, + ], + ], + 'set a sniff' => [ + ['Standard.Category.Sniff' => true], + [ + 'Standard.Category.Sniff.Code' => true, + 'Standard.Category.Sniff.OtherCode' => true, + 'Standard.Category.OtherSniff.Code' => false, + 'Standard.OtherCategory.Sniff.Code' => false, + 'OtherStandard.Category.Sniff.Code' => false, + ], + ], + 'set a category' => [ + ['Standard.Category' => true], + [ + 'Standard.Category.Sniff.Code' => true, + 'Standard.Category.Sniff.OtherCode' => true, + 'Standard.Category.OtherSniff.Code' => true, + 'Standard.OtherCategory.Sniff.Code' => false, + 'OtherStandard.Category.Sniff.Code' => false, + ], + ], + 'set a standard' => [ + ['Standard' => true], + [ + 'Standard.Category.Sniff.Code' => true, + 'Standard.Category.Sniff.OtherCode' => true, + 'Standard.Category.OtherSniff.Code' => true, + 'Standard.OtherCategory.Sniff.Code' => true, + 'OtherStandard.Category.Sniff.Code' => false, + ], + ], + 'set a standard, unignore a sniff in it' => [ + [ + 'Standard' => true, + 'Standard.Category.Sniff' => false, + ], + [ + 'Standard.Category.Sniff.Code' => false, + 'Standard.Category.Sniff.OtherCode' => false, + 'Standard.Category.OtherSniff.Code' => true, + 'Standard.OtherCategory.Sniff.Code' => true, + 'OtherStandard.Category.Sniff.Code' => false, + ], + ], + 'set a standard, unignore a category in it, ignore a sniff in that' => [ + [ + 'Standard' => true, + 'Standard.Category' => false, + 'Standard.Category.Sniff' => true, + ], + [ + 'Standard.Category.Sniff.Code' => true, + 'Standard.Category.Sniff.OtherCode' => true, + 'Standard.Category.OtherSniff.Code' => false, + 'Standard.OtherCategory.Sniff.Code' => true, + 'OtherStandard.Category.Sniff.Code' => false, + ], + ], + 'ignore some sniffs, then override some of those by unignoring the whole category' => [ + [ + 'Standard.Category1.Sniff1' => true, + 'Standard.Category1.Sniff2' => true, + 'Standard.Category2.Sniff1' => true, + 'Standard.Category2.Sniff2' => true, + 'Standard.Category1' => false, + ], + [ + 'Standard.Category1.Sniff1' => false, + 'Standard.Category1.Sniff2' => false, + 'Standard.Category2.Sniff1' => true, + 'Standard.Category2.Sniff2' => true, + ], + ], + ]; + + }//end dataCheckAndSet() + + +}//end class From 5b3d2c8879ed7a276cc7521c53c967898e80463f Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 6 May 2025 10:15:11 -0600 Subject: [PATCH 2/4] Apply suggestions from code review --- src/Files/File.php | 4 +- src/Tokenizers/Tokenizer.php | 20 +-- src/Util/IgnoreList.php | 111 +++++++++----- tests/Core/ErrorSuppressionTest.php | 10 ++ .../IgnoreList/CheckAndSetTest.php} | 140 ++---------------- .../IgnoreList/GetInstanceIgnoringAllTest.php | 37 +++++ .../GetInstanceIgnoringNothingTest.php | 37 +++++ .../IgnoreList/GetNewInstanceFromTest.php | 42 ++++++ .../IgnoresNothingAndEverythingTest.php | 103 +++++++++++++ 9 files changed, 323 insertions(+), 181 deletions(-) rename tests/Core/{IgnoreListTest.php => Util/IgnoreList/CheckAndSetTest.php} (53%) create mode 100644 tests/Core/Util/IgnoreList/GetInstanceIgnoringAllTest.php create mode 100644 tests/Core/Util/IgnoreList/GetInstanceIgnoringNothingTest.php create mode 100644 tests/Core/Util/IgnoreList/GetNewInstanceFromTest.php create mode 100644 tests/Core/Util/IgnoreList/IgnoresNothingAndEverythingTest.php diff --git a/src/Files/File.php b/src/Files/File.php index 473ae5a66c..9f359c39f9 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -801,7 +801,7 @@ public function addFixableWarning( protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable) { // Check if this line is ignoring all message codes. - if (isset($this->tokenizer->ignoredLines[$line]) === true && $this->tokenizer->ignoredLines[$line]->isAll() === true) { + if (isset($this->tokenizer->ignoredLines[$line]) === true && $this->tokenizer->ignoredLines[$line]->ignoresEverything() === true) { return false; } @@ -835,7 +835,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s ]; }//end if - if (isset($this->tokenizer->ignoredLines[$line]) === true && $this->tokenizer->ignoredLines[$line]->check($sniffCode) === true) { + if (isset($this->tokenizer->ignoredLines[$line]) === true && $this->tokenizer->ignoredLines[$line]->isIgnored($sniffCode) === true) { return false; } diff --git a/src/Tokenizers/Tokenizer.php b/src/Tokenizers/Tokenizer.php index 301c28adc9..0498914fdd 100644 --- a/src/Tokenizers/Tokenizer.php +++ b/src/Tokenizers/Tokenizer.php @@ -176,7 +176,7 @@ private function createPositionMap() $lineNumber = 1; $eolLen = strlen($this->eolChar); $ignoring = null; - $ignoreAll = IgnoreList::ignoringAll(); + $ignoreAll = IgnoreList::getInstanceIgnoringAll(); $inTests = defined('PHP_CODESNIFFER_IN_TESTS'); $checkEncoding = false; @@ -383,11 +383,7 @@ private function createPositionMap() if (empty($additionalText) === true) { $ignoring = $ignoreAll; } else { - if ($ignoring === null) { - $ignoring = IgnoreList::ignoringNone(); - } else { - $ignoring = clone $ignoring; - } + $ignoring = IgnoreList::getNewInstanceFrom($ignoring); $parts = explode(',', $additionalText); foreach ($parts as $sniffCode) { @@ -408,7 +404,7 @@ private function createPositionMap() if (empty($additionalText) === true) { $ignoring = null; } else { - $ignoring = clone $ignoring; + $ignoring = IgnoreList::getNewInstanceFrom($ignoring); $parts = explode(',', $additionalText); foreach ($parts as $sniffCode) { $sniffCode = trim($sniffCode); @@ -416,7 +412,7 @@ private function createPositionMap() $ignoring->set($sniffCode, false); } - if ($ignoring->isEmpty() === true) { + if ($ignoring->ignoresNothing() === true) { $ignoring = null; } } @@ -443,12 +439,8 @@ private function createPositionMap() $ignoreRules = ['.all' => true]; $lineIgnoring = $ignoreAll; } else { - $parts = explode(',', $additionalText); - if ($ignoring === null) { - $lineIgnoring = IgnoreList::ignoringNone(); - } else { - $lineIgnoring = clone $ignoring; - } + $parts = explode(',', $additionalText); + $lineIgnoring = IgnoreList::getNewInstanceFrom($ignoring); foreach ($parts as $sniffCode) { $ignoreRules[trim($sniffCode)] = true; diff --git a/src/Util/IgnoreList.php b/src/Util/IgnoreList.php index 26ba8f4fbb..3c42a65199 100644 --- a/src/Util/IgnoreList.php +++ b/src/Util/IgnoreList.php @@ -2,14 +2,23 @@ /** * Class to manage a list of sniffs to ignore. * - * @author Brad Jorsch - * @copyright 2023 Brad Jorsch - * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + * @copyright 2025 PHPCSStandards and contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ namespace PHP_CodeSniffer\Util; -class IgnoreList +/** + * Class to manage a list of sniffs to ignore. + * + * --------------------------------------------------------------------------------------------- + * This class is intended for internal use only and is not part of the public API. + * This also means that it has no promise of backward compatibility. Use at your own risk. + * --------------------------------------------------------------------------------------------- + * + * @internal + */ +final class IgnoreList { /** @@ -19,7 +28,7 @@ class IgnoreList * Each level may be a boolean indicating that everything underneath the branch is or is not ignored, or * may have a `.default' key indicating the default status for any branches not in the tree. * - * @var array|boolean + * @var array>>> */ private $data = [ '.default' => false ]; @@ -27,27 +36,47 @@ class IgnoreList /** * Get an instance set to ignore nothing. * - * @return static + * @return IgnoreList */ - public static function ignoringNone() + public static function getInstanceIgnoringNothing() { return new self(); - }//end ignoringNone() + }//end getInstanceIgnoringNothing() /** * Get an instance set to ignore everything. * - * @return static + * @return IgnoreList + */ + public static function getInstanceIgnoringAll() + { + $instance = new self(); + $instance->data['.default'] = true; + return $instance; + + }//end getInstanceIgnoringAll() + + + /** + * Get a new instance based on an existing instance. + * + * If passed null, creates a new instance that ignores nothing. + * + * @param IgnoreList|null $ignoreList List to clone. + * + * @return IgnoreList */ - public static function ignoringAll() + public static function getNewInstanceFrom(?IgnoreList $ignoreList) { - $ret = new self(); - $ret->data['.default'] = true; - return $ret; + if ($ignoreList === null) { + return self::getInstanceIgnoringNothing(); + } + + return clone $ignoreList; - }//end ignoringAll() + }//end getNewInstanceFrom() /** @@ -57,10 +86,10 @@ public static function ignoringAll() * * @return bool */ - public function check($code) + public function isIgnored($code) { - $data = $this->data; - $ret = $data['.default']; + $data = $this->data; + $returnValue = $data['.default']; foreach (explode('.', $code) as $part) { if (isset($data[$part]) === false) { break; @@ -68,18 +97,18 @@ public function check($code) $data = $data[$part]; if (is_bool($data) === true) { - $ret = $data; + $returnValue = $data; break; } if (isset($data['.default']) === true) { - $ret = $data['.default']; + $returnValue = $data['.default']; } } - return $ret; + return $returnValue; - }//end check() + }//end isIgnored() /** @@ -88,7 +117,7 @@ public function check($code) * @param string $code Partial or complete sniff code. * @param bool $ignore Whether the specified sniff should be ignored. * - * @return this + * @return IgnoreList $this for chaining. */ public function set($code, $ignore) { @@ -114,29 +143,29 @@ public function set($code, $ignore) /** - * Check if the list is empty. + * Check if the list ignores nothing. * * @return bool */ - public function isEmpty() + public function ignoresNothing() { - $arrs = [ $this->data ]; - while ($arrs !== []) { - $arr = array_pop($arrs); - foreach ($arr as $v) { - if ($v === true) { + $arraysToProcess = [ $this->data ]; + while ($arraysToProcess !== []) { + $arrayBeingProcessed = array_pop($arraysToProcess); + foreach ($arrayBeingProcessed as $valueBeingProcessed) { + if ($valueBeingProcessed === true) { return false; } - if (is_array($v) === true) { - $arrs[] = $v; + if (is_array($valueBeingProcessed) === true) { + $arraysToProcess[] = $valueBeingProcessed; } } } return true; - }//end isEmpty() + }//end ignoresNothing() /** @@ -144,25 +173,25 @@ public function isEmpty() * * @return bool */ - public function isAll() + public function ignoresEverything() { - $arrs = [ $this->data ]; - while ($arrs !== []) { - $arr = array_pop($arrs); - foreach ($arr as $v) { - if ($v === false) { + $arraysToProcess = [ $this->data ]; + while ($arraysToProcess !== []) { + $arrayBeingProcessed = array_pop($arraysToProcess); + foreach ($arrayBeingProcessed as $valueBeingProcessed) { + if ($valueBeingProcessed === false) { return false; } - if (is_array($v) === true) { - $arrs[] = $v; + if (is_array($valueBeingProcessed) === true) { + $arraysToProcess[] = $valueBeingProcessed; } } } return true; - }//end isAll() + }//end ignoresEverything() }//end class diff --git a/tests/Core/ErrorSuppressionTest.php b/tests/Core/ErrorSuppressionTest.php index a1b8eb4723..223261fc72 100644 --- a/tests/Core/ErrorSuppressionTest.php +++ b/tests/Core/ErrorSuppressionTest.php @@ -1057,6 +1057,16 @@ public static function dataEnableSelected() 'expectedErrors' => 0, 'expectedWarnings' => 0, ], + 'disable: everything; enable: sniff' => [ + 'code' => ' + // phpcs:disable + // phpcs:enable Generic.PHP.LowerCaseConstant + //TODO: write some code + $var = TRUE; + ', + 'expectedErrors' => 1, + 'expectedWarnings' => 0, + ], ]; }//end dataEnableSelected() diff --git a/tests/Core/IgnoreListTest.php b/tests/Core/Util/IgnoreList/CheckAndSetTest.php similarity index 53% rename from tests/Core/IgnoreListTest.php rename to tests/Core/Util/IgnoreList/CheckAndSetTest.php index ea19db1831..b3d2421405 100644 --- a/tests/Core/IgnoreListTest.php +++ b/tests/Core/Util/IgnoreList/CheckAndSetTest.php @@ -2,152 +2,44 @@ /** * Tests for the IgnoreList class. * - * @author Brad Jorsch - * @copyright 2023 Brad Jorsch - * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + * @copyright 2025 PHPCSStandards and contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ -namespace PHP_CodeSniffer\Tests\Core; +namespace PHP_CodeSniffer\Tests\Core\Util\IgnoreList; use PHP_CodeSniffer\Util\IgnoreList; use PHPUnit\Framework\TestCase; -class IgnoreListTest extends TestCase +/** + * Test isIgnored() and set(). + * + * @covers PHP_CodeSniffer\Util\IgnoreList::isIgnored + * @covers PHP_CodeSniffer\Util\IgnoreList::set + */ +class CheckAndSetTest extends TestCase { /** - * Test ignoringNone() works. - * - * @covers PHP_CodeSniffer\Util\IgnoreList::ignoringNone - * @return void - */ - public function testIgnoringNoneWorks() - { - $ignoreList = IgnoreList::ignoringNone(); - $this->assertInstanceOf(IgnoreList::class, $ignoreList); - $this->assertFalse($ignoreList->check('Anything')); - - }//end testIgnoringNoneWorks() - - - /** - * Test ignoringAll() works. - * - * @covers PHP_CodeSniffer\Util\IgnoreList::ignoringAll - * @return void - */ - public function testIgnoringAllWorks() - { - $ignoreList = IgnoreList::ignoringAll(); - $this->assertInstanceOf(IgnoreList::class, $ignoreList); - $this->assertTrue($ignoreList->check('Anything')); - - }//end testIgnoringAllWorks() - - - /** - * Test isEmpty() and isAll(). - * - * @param IgnoreList $ignoreList IgnoreList to test. - * @param bool $expectEmpty Expected return value from isEmpty(). - * @param bool $expectAll Expected return value from isAll(). - * - * @return void - * - * @dataProvider dataIsEmptyAndAll - * @covers PHP_CodeSniffer\Util\IgnoreList::isEmpty - * @covers PHP_CodeSniffer\Util\IgnoreList::isAll - */ - public function testIsEmptyAndAll($ignoreList, $expectEmpty, $expectAll) - { - $this->assertSame($expectEmpty, $ignoreList->isEmpty()); - $this->assertSame($expectAll, $ignoreList->isAll()); - - }//end testIsEmptyAndAll() - - - /** - * Data provider. - * - * @see testIsEmptyAndAll() - * - * @return array - */ - public static function dataIsEmptyAndAll() - { - return [ - 'fresh list' => [ - new IgnoreList(), - true, - false, - ], - 'list from ignoringNone' => [ - IgnoreList::ignoringNone(), - true, - false, - ], - 'list from ignoringAll' => [ - IgnoreList::ignoringAll(), - false, - true, - ], - 'list from ignoringNone, something set to false' => [ - IgnoreList::ignoringNone()->set('Foo.Bar', false), - true, - false, - ], - 'list from ignoringNone, something set to true' => [ - IgnoreList::ignoringNone()->set('Foo.Bar', true), - false, - false, - ], - 'list from ignoringAll, something set to false' => [ - IgnoreList::ignoringAll()->set('Foo.Bar', false), - false, - false, - ], - 'list from ignoringAll, something set to true' => [ - IgnoreList::ignoringAll()->set('Foo.Bar', true), - false, - true, - ], - 'list from ignoringNone, something set to true then overridden' => [ - IgnoreList::ignoringNone()->set('Foo.Bar', true)->set('Foo', false), - true, - false, - ], - 'list from ignoringAll, something set to false then overridden' => [ - IgnoreList::ignoringAll()->set('Foo.Bar', false)->set('Foo', true), - false, - true, - ], - ]; - - }//end dataIsEmptyAndAll() - - - /** - * Test check() and set(). + * Test isIgnored() and set(). * - * @param array $toSet Associative array of $code => $ignore to pass to set(). - * @param array $toCheck Associative array of $code => $expect to pass to check(). + * @param array $toSet Associative array of $code => $ignore to pass to set(). + * @param array $toCheck Associative array of $code => $expect to pass to isIgnored(). * * @return void * * @dataProvider dataCheckAndSet - * @covers PHP_CodeSniffer\Util\IgnoreList::check - * @covers PHP_CodeSniffer\Util\IgnoreList::set */ public function testCheckAndSet($toSet, $toCheck) { $ignoreList = new IgnoreList(); foreach ($toSet as $code => $ignore) { - $this->assertSame($ignoreList, $ignoreList->set($code, $ignore)); + $this->assertSame($ignoreList, $ignoreList->set($code, $ignore), 'Set method returned $this'); } foreach ($toCheck as $code => $expect) { - $this->assertSame($expect, $ignoreList->check($code)); + $this->assertSame($expect, $ignoreList->isIgnored($code), "$code is ignored"); } }//end testCheckAndSet() @@ -158,7 +50,7 @@ public function testCheckAndSet($toSet, $toCheck) * * @see testCheckAndSet() * - * @return array + * @return array>> */ public static function dataCheckAndSet() { diff --git a/tests/Core/Util/IgnoreList/GetInstanceIgnoringAllTest.php b/tests/Core/Util/IgnoreList/GetInstanceIgnoringAllTest.php new file mode 100644 index 0000000000..9ba935534d --- /dev/null +++ b/tests/Core/Util/IgnoreList/GetInstanceIgnoringAllTest.php @@ -0,0 +1,37 @@ +assertInstanceOf(IgnoreList::class, $ignoreList); + $this->assertTrue($ignoreList->isIgnored('Anything')); + + }//end testGetInstanceIgnoringAllWorks() + + +}//end class diff --git a/tests/Core/Util/IgnoreList/GetInstanceIgnoringNothingTest.php b/tests/Core/Util/IgnoreList/GetInstanceIgnoringNothingTest.php new file mode 100644 index 0000000000..aeefc98181 --- /dev/null +++ b/tests/Core/Util/IgnoreList/GetInstanceIgnoringNothingTest.php @@ -0,0 +1,37 @@ +assertInstanceOf(IgnoreList::class, $ignoreList); + $this->assertFalse($ignoreList->isIgnored('Anything')); + + }//end testGetInstanceIgnoringNothingWorks() + + +}//end class diff --git a/tests/Core/Util/IgnoreList/GetNewInstanceFromTest.php b/tests/Core/Util/IgnoreList/GetNewInstanceFromTest.php new file mode 100644 index 0000000000..d0b20f2578 --- /dev/null +++ b/tests/Core/Util/IgnoreList/GetNewInstanceFromTest.php @@ -0,0 +1,42 @@ +assertTrue($ignoreList->ignoresNothing(), 'Passing null returned an instance ignoring nothing'); + + $ignoreList->set('Foo.Bar', true); + $ignoreList2 = IgnoreList::getNewInstanceFrom($ignoreList); + $this->assertNotSame($ignoreList, $ignoreList2, 'Passing an instance returns a different instance'); + + $this->assertTrue($ignoreList2->isIgnored('Foo.Bar'), 'New instance ignores the same as the old one'); + + }//end testGetNewInstanceFrom() + + +}//end class diff --git a/tests/Core/Util/IgnoreList/IgnoresNothingAndEverythingTest.php b/tests/Core/Util/IgnoreList/IgnoresNothingAndEverythingTest.php new file mode 100644 index 0000000000..64ebef3cd4 --- /dev/null +++ b/tests/Core/Util/IgnoreList/IgnoresNothingAndEverythingTest.php @@ -0,0 +1,103 @@ +assertSame($expectIgnoresNothing, $ignoreList->ignoresNothing(), 'Ignores nothing'); + $this->assertSame($expectIgnoresEverything, $ignoreList->ignoresEverything(), 'Ignores everything'); + + }//end testIgnoresNothingAndEverything() + + + /** + * Data provider. + * + * @see testIgnoresNothingAndEverything() + * + * @return array> + */ + public static function dataIgnoresNothingAndEverything() + { + return [ + 'fresh list' => [ + new IgnoreList(), + true, + false, + ], + 'list from getInstanceIgnoringNothing' => [ + IgnoreList::getInstanceIgnoringNothing(), + true, + false, + ], + 'list from getInstanceIgnoringAll' => [ + IgnoreList::getInstanceIgnoringAll(), + false, + true, + ], + 'list from getInstanceIgnoringNothing, something set to false' => [ + IgnoreList::getInstanceIgnoringNothing()->set('Foo.Bar', false), + true, + false, + ], + 'list from getInstanceIgnoringNothing, something set to true' => [ + IgnoreList::getInstanceIgnoringNothing()->set('Foo.Bar', true), + false, + false, + ], + 'list from getInstanceIgnoringAll, something set to false' => [ + IgnoreList::getInstanceIgnoringAll()->set('Foo.Bar', false), + false, + false, + ], + 'list from getInstanceIgnoringAll, something set to true' => [ + IgnoreList::getInstanceIgnoringAll()->set('Foo.Bar', true), + false, + true, + ], + 'list from getInstanceIgnoringNothing, something set to true then overridden' => [ + IgnoreList::getInstanceIgnoringNothing()->set('Foo.Bar', true)->set('Foo', false), + true, + false, + ], + 'list from getInstanceIgnoringAll, something set to false then overridden' => [ + IgnoreList::getInstanceIgnoringAll()->set('Foo.Bar', false)->set('Foo', true), + false, + true, + ], + ]; + + }//end dataIgnoresNothingAndEverything() + + +}//end class From 0ff1f1188872ac6a546e30f2cf8a107a1161c1ab Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 6 May 2025 11:43:43 -0600 Subject: [PATCH 3/4] Use FQCNs in doc comments --- src/Util/IgnoreList.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Util/IgnoreList.php b/src/Util/IgnoreList.php index 3c42a65199..6fd9881b86 100644 --- a/src/Util/IgnoreList.php +++ b/src/Util/IgnoreList.php @@ -36,7 +36,7 @@ final class IgnoreList /** * Get an instance set to ignore nothing. * - * @return IgnoreList + * @return \PHP_CodeSniffer\Util\IgnoreList */ public static function getInstanceIgnoringNothing() { @@ -48,7 +48,7 @@ public static function getInstanceIgnoringNothing() /** * Get an instance set to ignore everything. * - * @return IgnoreList + * @return \PHP_CodeSniffer\Util\IgnoreList */ public static function getInstanceIgnoringAll() { @@ -64,9 +64,9 @@ public static function getInstanceIgnoringAll() * * If passed null, creates a new instance that ignores nothing. * - * @param IgnoreList|null $ignoreList List to clone. + * @param \PHP_CodeSniffer\Util\IgnoreList|null $ignoreList List to clone. * - * @return IgnoreList + * @return \PHP_CodeSniffer\Util\IgnoreList */ public static function getNewInstanceFrom(?IgnoreList $ignoreList) { @@ -117,7 +117,7 @@ public function isIgnored($code) * @param string $code Partial or complete sniff code. * @param bool $ignore Whether the specified sniff should be ignored. * - * @return IgnoreList $this for chaining. + * @return \PHP_CodeSniffer\Util\IgnoreList $this for chaining. */ public function set($code, $ignore) { From 3e4bcb0c55121834fe8e1ac43d5e59bc174f783c Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 6 May 2025 11:45:13 -0600 Subject: [PATCH 4/4] Missed one arrow alignment --- tests/Core/ErrorSuppressionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Core/ErrorSuppressionTest.php b/tests/Core/ErrorSuppressionTest.php index 223261fc72..793574b83a 100644 --- a/tests/Core/ErrorSuppressionTest.php +++ b/tests/Core/ErrorSuppressionTest.php @@ -1057,7 +1057,7 @@ public static function dataEnableSelected() 'expectedErrors' => 0, 'expectedWarnings' => 0, ], - 'disable: everything; enable: sniff' => [ + 'disable: everything; enable: sniff' => [ 'code' => ' // phpcs:disable // phpcs:enable Generic.PHP.LowerCaseConstant