-
-
Notifications
You must be signed in to change notification settings - Fork 75
Improve handling of disable/enable/ignore directives #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anomiex
wants to merge
4
commits into
PHPCSStandards:4.x
Choose a base branch
from
anomiex:fix/ignore-list-handling
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 <[email protected]> | ||
* @copyright 2023 Brad Jorsch | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Util; | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class IgnoreList | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
|
||
/** | ||
* 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 | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
private $data = [ '.default' => false ]; | ||
|
||
|
||
/** | ||
* Get an instance set to ignore nothing. | ||
* | ||
* @return static | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public static function ignoringNone() | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return new self(); | ||
|
||
}//end ignoringNone() | ||
|
||
|
||
/** | ||
* Get an instance set to ignore everything. | ||
* | ||
* @return static | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public static function ignoringAll() | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$ret = new self(); | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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) | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$data = $this->data; | ||
$ret = $data['.default']; | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
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() | ||
|
||
|
||
/** | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Check if the list is empty. | ||
* | ||
* @return bool | ||
*/ | ||
public function isEmpty() | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$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() | ||
anomiex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.