-
-
Notifications
You must be signed in to change notification settings - Fork 183
Add Forbidden Classes sniff #966
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
277 changes: 277 additions & 0 deletions
277
SlevomatCodingStandard/Sniffs/PHP/ForbiddenClassesSniff.php
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,277 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\PHP; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use SlevomatCodingStandard\Helpers\NamespaceHelper; | ||
use SlevomatCodingStandard\Helpers\ReferencedNameHelper; | ||
use SlevomatCodingStandard\Helpers\TokenHelper; | ||
use SlevomatCodingStandard\Helpers\UseStatementHelper; | ||
use function array_key_exists; | ||
use function array_merge; | ||
use function array_pop; | ||
use function count; | ||
use function in_array; | ||
use function is_array; | ||
use function min; | ||
use function sprintf; | ||
use function strlen; | ||
use function strtolower; | ||
use const PHP_INT_MAX; | ||
use const T_COMMA; | ||
use const T_DOUBLE_COLON; | ||
use const T_EXTENDS; | ||
use const T_IMPLEMENTS; | ||
use const T_NEW; | ||
use const T_OPEN_CURLY_BRACKET; | ||
use const T_SEMICOLON; | ||
use const T_USE; | ||
|
||
class ForbiddenClassesSniff implements Sniff | ||
{ | ||
|
||
public const CODE_FORBIDDEN_CLASS = 'ForbiddenClass'; | ||
public const CODE_FORBIDDEN_PARENT_CLASS = 'ForbiddenParentClass'; | ||
public const CODE_FORBIDDEN_INTERFACE = 'ForbiddenInterface'; | ||
public const CODE_FORBIDDEN_TRAIT = 'ForbiddenTrait'; | ||
|
||
/** @var array<string, (string|null)> */ | ||
public $forbiddenClasses = []; | ||
|
||
/** @var array<string, (string|null)> */ | ||
public $forbiddenExtends = []; | ||
|
||
/** @var array<string, (string|null)> */ | ||
public $forbiddenInterfaces = []; | ||
|
||
/** @var array<string, (string|null)> */ | ||
public $forbiddenTraits = []; | ||
|
||
/** @var array<string> */ | ||
private static $keywordReferences = ['self', 'parent', 'static']; | ||
|
||
/** | ||
* @return array<int, (int|string)> | ||
*/ | ||
public function register(): array | ||
{ | ||
$searchTokens = []; | ||
|
||
if (count($this->forbiddenClasses) > 0) { | ||
$this->forbiddenClasses = self::normalizeInputOption($this->forbiddenClasses); | ||
$searchTokens[] = T_NEW; | ||
$searchTokens[] = T_DOUBLE_COLON; | ||
} | ||
|
||
if (count($this->forbiddenExtends) > 0) { | ||
$this->forbiddenExtends = self::normalizeInputOption($this->forbiddenExtends); | ||
$searchTokens[] = T_EXTENDS; | ||
} | ||
|
||
if (count($this->forbiddenInterfaces) > 0) { | ||
$this->forbiddenInterfaces = self::normalizeInputOption($this->forbiddenInterfaces); | ||
$searchTokens[] = T_IMPLEMENTS; | ||
} | ||
|
||
if (count($this->forbiddenTraits) > 0) { | ||
$this->forbiddenTraits = self::normalizeInputOption($this->forbiddenTraits); | ||
$searchTokens[] = T_USE; | ||
} | ||
|
||
return $searchTokens; | ||
} | ||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
* @param File $phpcsFile | ||
* @param int $tokenPointer | ||
*/ | ||
public function process(File $phpcsFile, $tokenPointer): void | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
$token = $tokens[$tokenPointer]; | ||
$nameTokens = array_merge(TokenHelper::$nameTokenCodes, TokenHelper::$ineffectiveTokenCodes); | ||
|
||
if ( | ||
$token['code'] === T_IMPLEMENTS | ||
|| ($token['code'] === T_USE && UseStatementHelper::isTraitUse($phpcsFile, $tokenPointer)) | ||
) { | ||
$endTokenPointer = TokenHelper::findNext( | ||
$phpcsFile, | ||
[T_SEMICOLON, T_OPEN_CURLY_BRACKET], | ||
$tokenPointer | ||
); | ||
$references = $this->getAllReferences($phpcsFile, $tokenPointer, $endTokenPointer); | ||
|
||
if ($token['code'] === T_IMPLEMENTS) { | ||
$this->checkReferences($phpcsFile, $tokenPointer, $references, $this->forbiddenInterfaces); | ||
} else { | ||
// Fixer does not work when traits contains aliases | ||
$this->checkReferences( | ||
$phpcsFile, | ||
$tokenPointer, | ||
$references, | ||
$this->forbiddenTraits, | ||
$tokens[$endTokenPointer]['code'] !== T_OPEN_CURLY_BRACKET | ||
); | ||
} | ||
} elseif (in_array($token['code'], [T_NEW, T_EXTENDS], true)) { | ||
$endTokenPointer = TokenHelper::findNextExcluding($phpcsFile, $nameTokens, $tokenPointer + 1); | ||
$references = $this->getAllReferences($phpcsFile, $tokenPointer, $endTokenPointer); | ||
|
||
$this->checkReferences( | ||
$phpcsFile, | ||
$tokenPointer, | ||
$references, | ||
$token['code'] === T_NEW ? $this->forbiddenClasses : $this->forbiddenExtends | ||
); | ||
} elseif ($token['code'] === T_DOUBLE_COLON && !$this->isTraitsConflictResolutionToken($token)) { | ||
$startTokenPointer = TokenHelper::findPreviousExcluding($phpcsFile, $nameTokens, $tokenPointer - 1); | ||
$references = $this->getAllReferences($phpcsFile, $startTokenPointer, $tokenPointer); | ||
|
||
$this->checkReferences($phpcsFile, $tokenPointer, $references, $this->forbiddenClasses); | ||
} | ||
} | ||
|
||
/** | ||
* @param File $phpcsFile | ||
* @param int $tokenPointer | ||
* @param array{fullyQualifiedName: string, startPointer: int|null, endPointer: int|null}[] $references | ||
* @param array<string, (string|null)> $forbiddenNames | ||
* @param bool $isFixable | ||
*/ | ||
private function checkReferences( | ||
File $phpcsFile, | ||
int $tokenPointer, | ||
array $references, | ||
array $forbiddenNames, | ||
bool $isFixable = true | ||
): void | ||
{ | ||
$token = $phpcsFile->getTokens()[$tokenPointer]; | ||
$details = [ | ||
T_NEW => ['class', self::CODE_FORBIDDEN_CLASS], | ||
T_DOUBLE_COLON => ['class', self::CODE_FORBIDDEN_CLASS], | ||
T_EXTENDS => ['as a parent class', self::CODE_FORBIDDEN_PARENT_CLASS], | ||
T_IMPLEMENTS => ['interface', self::CODE_FORBIDDEN_INTERFACE], | ||
T_USE => ['trait', self::CODE_FORBIDDEN_TRAIT], | ||
]; | ||
|
||
foreach ($references as $reference) { | ||
if (!array_key_exists($reference['fullyQualifiedName'], $forbiddenNames)) { | ||
continue; | ||
} | ||
|
||
$alternative = $forbiddenNames[$reference['fullyQualifiedName']]; | ||
[$nameType, $code] = $details[$token['code']]; | ||
|
||
if ($alternative === null) { | ||
$phpcsFile->addError( | ||
sprintf('Usage of %s %s is forbidden.', $reference['fullyQualifiedName'], $nameType), | ||
$reference['startPointer'], | ||
$code | ||
); | ||
} elseif (!$isFixable) { | ||
$phpcsFile->addError( | ||
sprintf( | ||
'Usage of %s %s is forbidden, use %s instead.', | ||
$reference['fullyQualifiedName'], | ||
$nameType, | ||
$alternative | ||
), | ||
$reference['startPointer'], | ||
$code | ||
); | ||
} else { | ||
$fix = $phpcsFile->addFixableError( | ||
sprintf( | ||
'Usage of %s %s is forbidden, use %s instead.', | ||
$reference['fullyQualifiedName'], | ||
$nameType, | ||
$alternative | ||
), | ||
$reference['startPointer'], | ||
$code | ||
); | ||
if (!$fix) { | ||
continue; | ||
} | ||
|
||
$phpcsFile->fixer->beginChangeset(); | ||
$phpcsFile->fixer->replaceToken($reference['startPointer'], $alternative); | ||
for ($i = $reference['startPointer'] + 1; $i <= $reference['endPointer']; $i++) { | ||
$phpcsFile->fixer->replaceToken($i, ''); | ||
} | ||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string, array<int, int|string>|int|string> $token | ||
* @return bool | ||
*/ | ||
private function isTraitsConflictResolutionToken(array $token): bool | ||
{ | ||
return is_array($token['conditions']) && array_pop($token['conditions']) === T_USE; | ||
} | ||
|
||
/** | ||
* @param File $phpcsFile | ||
* @param int $startPointer | ||
* @param int $endPointer | ||
* @return array{fullyQualifiedName: string, startPointer: int|null, endPointer: int|null}[] | ||
*/ | ||
private function getAllReferences(File $phpcsFile, int $startPointer, int $endPointer): array | ||
{ | ||
// Always ignore first token | ||
$startPointer++; | ||
$references = []; | ||
|
||
while ($startPointer < $endPointer) { | ||
$nextComma = TokenHelper::findNext($phpcsFile, [T_COMMA], $startPointer + 1); | ||
$nextSeparator = min($endPointer, $nextComma ?? PHP_INT_MAX); | ||
$reference = ReferencedNameHelper::getReferenceName($phpcsFile, $startPointer, $nextSeparator - 1); | ||
|
||
if ( | ||
strlen($reference) !== 0 | ||
&& !in_array(strtolower($reference), self::$keywordReferences, true) | ||
) { | ||
$references[] = [ | ||
'fullyQualifiedName' => NamespaceHelper::resolveClassName($phpcsFile, $reference, $startPointer), | ||
'startPointer' => TokenHelper::findNextEffective($phpcsFile, $startPointer, $endPointer), | ||
'endPointer' => TokenHelper::findPreviousEffective($phpcsFile, $nextSeparator - 1, $startPointer), | ||
]; | ||
} | ||
|
||
$startPointer = $nextSeparator + 1; | ||
} | ||
|
||
return $references; | ||
} | ||
|
||
/** | ||
* @param array<string, (string|null)> $option | ||
* @return array<string, (string|null)> | ||
*/ | ||
private static function normalizeInputOption(array $option): array | ||
{ | ||
$forbiddenClasses = []; | ||
foreach ($option as $forbiddenClass => $alternative) { | ||
$forbiddenClasses[self::normalizeClassName($forbiddenClass)] = self::normalizeClassName($alternative); | ||
} | ||
|
||
return $forbiddenClasses; | ||
} | ||
|
||
private static function normalizeClassName(?string $typeName): ?string | ||
{ | ||
if ($typeName === null || strlen($typeName) === 0 || strtolower($typeName) === 'null') { | ||
return null; | ||
} | ||
|
||
return NamespaceHelper::getFullyQualifiedTypeName($typeName); | ||
} | ||
|
||
} |
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
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.