clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Exceptions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\CatchHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function in_array;
use const T_CATCH;
class DeadCatchSniff implements Sniff
{
public const CODE_CATCH_AFTER_THROWABLE_CATCH = 'CatchAfterThrowableCatch';
/**
* @return array<int, (int|string)>
*/
public function register(): array
{
return [
T_CATCH,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $catchPointer
*/
public function process(File $phpcsFile, $catchPointer): void
{
$tokens = $phpcsFile->getTokens();
$catchToken = $tokens[$catchPointer];
$caughtTypes = CatchHelper::findCaughtTypesInCatch($phpcsFile, $catchToken);
if (!in_array('\\Throwable', $caughtTypes, true)) {
return;
}
$nextCatchPointer = TokenHelper::findNextEffective($phpcsFile, $catchToken['scope_closer'] + 1);
while ($nextCatchPointer !== null) {
$nextCatchToken = $tokens[$nextCatchPointer];
if ($nextCatchToken['code'] !== T_CATCH) {
break;
}
$phpcsFile->addError('Unreachable catch block.', $nextCatchPointer, self::CODE_CATCH_AFTER_THROWABLE_CATCH);
$nextCatchPointer = TokenHelper::findNextEffective($phpcsFile, $nextCatchToken['scope_closer'] + 1);
}
}
}

View File

@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Exceptions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\TokenHelper;
use const T_CATCH;
use const T_VARIABLE;
class DisallowNonCapturingCatchSniff implements Sniff
{
public const CODE_DISALLOWED_NON_CAPTURING_CATCH = 'DisallowedNonCapturingCatch';
/**
* @return array<int, (int|string)>
*/
public function register(): array
{
return [
T_CATCH,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $catchPointer
*/
public function process(File $phpcsFile, $catchPointer): void
{
$tokens = $phpcsFile->getTokens();
$variablePointer = TokenHelper::findNext(
$phpcsFile,
T_VARIABLE,
$tokens[$catchPointer]['parenthesis_opener'],
$tokens[$catchPointer]['parenthesis_closer']
);
if ($variablePointer === null) {
$phpcsFile->addError('Use of non-capturing catch is disallowed.', $catchPointer, self::CODE_DISALLOWED_NON_CAPTURING_CATCH);
}
}
}

View File

@@ -0,0 +1,139 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Exceptions;
use Exception;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\CatchHelper;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\NamespaceHelper;
use SlevomatCodingStandard\Helpers\ReferencedNameHelper;
use SlevomatCodingStandard\Helpers\SuppressHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use Throwable;
use function array_key_exists;
use function array_merge;
use function in_array;
use function sprintf;
use const T_BITWISE_OR;
use const T_CATCH;
use const T_EXTENDS;
use const T_FUNCTION;
use const T_INSTANCEOF;
use const T_NEW;
use const T_OPEN_PARENTHESIS;
use const T_OPEN_TAG;
class ReferenceThrowableOnlySniff implements Sniff
{
public const CODE_REFERENCED_GENERAL_EXCEPTION = 'ReferencedGeneralException';
private const NAME = 'SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly';
/**
* @return array<int, (int|string)>
*/
public function register(): array
{
return [
T_OPEN_TAG,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $openTagPointer
*/
public function process(File $phpcsFile, $openTagPointer): void
{
if (TokenHelper::findPrevious($phpcsFile, T_OPEN_TAG, $openTagPointer - 1) !== null) {
return;
}
$tokens = $phpcsFile->getTokens();
$message = sprintf('Referencing general \%s; use \%s instead.', Exception::class, Throwable::class);
$referencedNames = ReferencedNameHelper::getAllReferencedNames($phpcsFile, $openTagPointer);
foreach ($referencedNames as $referencedName) {
$resolvedName = NamespaceHelper::resolveClassName(
$phpcsFile,
$referencedName->getNameAsReferencedInFile(),
$referencedName->getStartPointer()
);
if ($resolvedName !== '\\Exception') {
continue;
}
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $referencedName->getStartPointer() - 1);
if (in_array($tokens[$previousPointer]['code'], [T_EXTENDS, T_NEW, T_INSTANCEOF], true)) {
// Allow \Exception in extends and instantiating it
continue;
}
if ($tokens[$previousPointer]['code'] === T_BITWISE_OR) {
$previousPointer = TokenHelper::findPreviousExcluding(
$phpcsFile,
array_merge(TokenHelper::$ineffectiveTokenCodes, TokenHelper::getNameTokenCodes(), [T_BITWISE_OR]),
$previousPointer - 1
);
}
if ($tokens[$previousPointer]['code'] === T_OPEN_PARENTHESIS) {
/** @var int $openParenthesisOpenerPointer */
$openParenthesisOpenerPointer = TokenHelper::findPreviousEffective($phpcsFile, $previousPointer - 1);
if ($tokens[$openParenthesisOpenerPointer]['code'] === T_CATCH) {
if ($this->searchForThrowableInNextCatches($phpcsFile, $openParenthesisOpenerPointer)) {
continue;
}
} elseif (
array_key_exists('parenthesis_owner', $tokens[$previousPointer])
&& $tokens[$tokens[$previousPointer]['parenthesis_owner']]['code'] === T_FUNCTION
&& $tokens[$previousPointer]['parenthesis_closer'] > $referencedName->getStartPointer()
&& SuppressHelper::isSniffSuppressed(
$phpcsFile,
$openParenthesisOpenerPointer,
sprintf('%s.%s', self::NAME, self::CODE_REFERENCED_GENERAL_EXCEPTION)
)
) {
continue;
}
}
$fix = $phpcsFile->addFixableError(
$message,
$referencedName->getStartPointer(),
self::CODE_REFERENCED_GENERAL_EXCEPTION
);
if (!$fix) {
continue;
}
$phpcsFile->fixer->beginChangeset();
FixerHelper::change($phpcsFile, $referencedName->getStartPointer(), $referencedName->getEndPointer(), '\Throwable');
$phpcsFile->fixer->endChangeset();
}
}
private function searchForThrowableInNextCatches(File $phpcsFile, int $catchPointer): bool
{
$tokens = $phpcsFile->getTokens();
$nextCatchPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$catchPointer]['scope_closer'] + 1);
while ($nextCatchPointer !== null) {
$nextCatchToken = $tokens[$nextCatchPointer];
if ($nextCatchToken['code'] !== T_CATCH) {
break;
}
$caughtTypes = CatchHelper::findCaughtTypesInCatch($phpcsFile, $nextCatchToken);
if (in_array('\\Throwable', $caughtTypes, true)) {
return true;
}
$nextCatchPointer = TokenHelper::findNextEffective($phpcsFile, $nextCatchToken['scope_closer'] + 1);
}
return false;
}
}

View File

@@ -0,0 +1,170 @@
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Sniffs\Exceptions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\CatchHelper;
use SlevomatCodingStandard\Helpers\FixerHelper;
use SlevomatCodingStandard\Helpers\ParameterHelper;
use SlevomatCodingStandard\Helpers\ScopeHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use SlevomatCodingStandard\Helpers\VariableHelper;
use function array_reverse;
use function count;
use function in_array;
use const T_CATCH;
use const T_DOUBLE_QUOTED_STRING;
use const T_FINALLY;
use const T_HEREDOC;
use const T_VARIABLE;
use const T_WHITESPACE;
class RequireNonCapturingCatchSniff implements Sniff
{
public const CODE_NON_CAPTURING_CATCH_REQUIRED = 'NonCapturingCatchRequired';
/** @var bool|null */
public $enable = null;
/**
* @return array<int, (int|string)>
*/
public function register(): array
{
return [
T_CATCH,
];
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @param int $catchPointer
*/
public function process(File $phpcsFile, $catchPointer): void
{
$this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 80000);
if (!$this->enable) {
return;
}
$tokens = $phpcsFile->getTokens();
$variablePointer = TokenHelper::findNext(
$phpcsFile,
T_VARIABLE,
$tokens[$catchPointer]['parenthesis_opener'],
$tokens[$catchPointer]['parenthesis_closer']
);
if ($variablePointer === null) {
return;
}
$variableName = $tokens[$variablePointer]['content'];
if ($this->isVariableUsedInCodePart(
$phpcsFile,
$tokens[$catchPointer]['scope_opener'],
$tokens[$catchPointer]['scope_closer'],
$variableName
)) {
return;
}
$tryEndPointer = CatchHelper::getTryEndPointer($phpcsFile, $catchPointer);
$possibleFinallyPointer = $tokens[$tryEndPointer]['scope_condition'];
if (
$tokens[$possibleFinallyPointer]['code'] === T_FINALLY
&& $this->isVariableUsedInCodePart(
$phpcsFile,
$tokens[$possibleFinallyPointer]['scope_opener'],
$tokens[$possibleFinallyPointer]['scope_closer'],
$variableName
)
) {
return;
}
$nextScopeEnd = count($tokens) - 1;
foreach (array_reverse($tokens[$tryEndPointer]['conditions'], true) as $conditionPointer => $conditionCode) {
if (in_array($conditionCode, TokenHelper::$functionTokenCodes, true)) {
$nextScopeEnd = $tokens[$conditionPointer]['scope_closer'];
break;
}
}
if ($this->isVariableUsedInCodePart($phpcsFile, $tryEndPointer, $nextScopeEnd, $variableName)) {
return;
}
$fix = $phpcsFile->addFixableError('Non-capturing catch is required.', $catchPointer, self::CODE_NON_CAPTURING_CATCH_REQUIRED);
if (!$fix) {
return;
}
$pointerBeforeVariable = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1);
$fixEndPointer = TokenHelper::findNextContent(
$phpcsFile,
T_WHITESPACE,
$phpcsFile->eolChar,
$variablePointer + 1,
$tokens[$catchPointer]['parenthesis_closer']
);
if ($fixEndPointer === null) {
$fixEndPointer = $tokens[$catchPointer]['parenthesis_closer'];
}
$phpcsFile->fixer->beginChangeset();
FixerHelper::removeBetween($phpcsFile, $pointerBeforeVariable, $fixEndPointer);
$phpcsFile->fixer->endChangeset();
}
private function isVariableUsedInCodePart(File $phpcsFile, int $codeStartPointer, int $codeEndPointer, string $variableName): bool
{
$tokens = $phpcsFile->getTokens();
$firstPointerInCode = $codeStartPointer + 1;
for ($i = $firstPointerInCode; $i <= $codeEndPointer; $i++) {
if ($tokens[$i]['code'] === T_VARIABLE) {
if ($tokens[$i]['content'] !== $variableName) {
continue;
}
if (ParameterHelper::isParameter($phpcsFile, $i)) {
continue;
}
if (!ScopeHelper::isInSameScope($phpcsFile, $firstPointerInCode, $i)) {
continue;
}
$catchPointer = TokenHelper::findPrevious($phpcsFile, T_CATCH, $i - 1, $firstPointerInCode);
if ($catchPointer === null) {
return true;
}
if ($tokens[$catchPointer]['parenthesis_closer'] < $i) {
return true;
}
} elseif (
in_array($tokens[$i]['code'], [T_DOUBLE_QUOTED_STRING, T_HEREDOC], true)
&& VariableHelper::isUsedInScopeInString($phpcsFile, $variableName, $i)
) {
return true;
}
}
return false;
}
}