forked from PHPCSStandards/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve handling of disable/enable/ignore directives
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 PHPCSStandards#111
Showing
5 changed files
with
525 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
/** | ||
* Class to manage a list of sniffs to ignore. | ||
* | ||
* @author Brad Jorsch <brad.jorsch@automattic.com> | ||
* @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 static(); | ||
|
||
}//end ignoringNone() | ||
|
||
|
||
/** | ||
* Get an instance set to ignore everything. | ||
* | ||
* @return static | ||
*/ | ||
public static function ignoringAll() | ||
{ | ||
$ret = new static(); | ||
$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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,253 @@ | ||
<?php | ||
/** | ||
* Tests for the IgnoreList class. | ||
* | ||
* @author Brad Jorsch <brad.jorsch@automattic.com> | ||
* @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 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 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 |