Skip to content

Commit

Permalink
minor #6291 PHP7.4 - remove run time checks (SpacePossum)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

PHP7.4 - remove run time checks

part of #6288

Commits
-------

d46dd35 PHP7.4 - remove run time checks
  • Loading branch information
SpacePossum committed Feb 16, 2022
2 parents d55a8d0 + d46dd35 commit bc05dce
Show file tree
Hide file tree
Showing 20 changed files with 35 additions and 142 deletions.
Expand Up @@ -19,7 +19,6 @@
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerConfiguration\InvalidOptionsForEnvException;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
Expand Down Expand Up @@ -91,10 +90,6 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn
->setAllowedTypes(['bool'])
->setDefault(false)
->setNormalizer(static function (Options $options, $value) {
if (\PHP_VERSION_ID < 70300 && $value) {
throw new InvalidOptionsForEnvException('"after_heredoc" option can only be enabled with PHP 7.3+.');
}

return $value;
})
->getOption(),
Expand Down
8 changes: 0 additions & 8 deletions src/Fixer/ControlStructure/TrailingCommaInMultilineFixer.php
Expand Up @@ -112,10 +112,6 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn
->setAllowedTypes(['bool'])
->setDefault(false)
->setNormalizer(static function (Options $options, $value) {
if (\PHP_VERSION_ID < 70300 && $value) {
throw new InvalidOptionsForEnvException('"after_heredoc" option can only be enabled with PHP 7.3+.');
}

return $value;
})
->getOption(),
Expand All @@ -124,10 +120,6 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn
->setAllowedValues([new AllowedValueSubset([self::ELEMENTS_ARRAYS, self::ELEMENTS_ARGUMENTS, self::ELEMENTS_PARAMETERS])])
->setDefault([self::ELEMENTS_ARRAYS])
->setNormalizer(static function (Options $options, $value) {
if (\PHP_VERSION_ID < 70300 && \in_array(self::ELEMENTS_ARGUMENTS, $value, true)) {
throw new InvalidOptionsForEnvException(sprintf('"%s" option can only be enabled with PHP 7.3+.', self::ELEMENTS_ARGUMENTS));
}

if (\PHP_VERSION_ID < 80000 && \in_array(self::ELEMENTS_PARAMETERS, $value, true)) {
throw new InvalidOptionsForEnvException(sprintf('"%s" option can only be enabled with PHP 8.0+.', self::ELEMENTS_PARAMETERS));
}
Expand Down
30 changes: 13 additions & 17 deletions src/Fixer/ControlStructure/YodaStyleFixer.php
Expand Up @@ -475,24 +475,20 @@ private function isOfLowerPrecedenceAssignment(Token $token): bool

if (null === $tokens) {
$tokens = [
T_AND_EQUAL, // &=
T_CONCAT_EQUAL, // .=
T_DIV_EQUAL, // /=
T_MINUS_EQUAL, // -=
T_MOD_EQUAL, // %=
T_MUL_EQUAL, // *=
T_OR_EQUAL, // |=
T_PLUS_EQUAL, // +=
T_POW_EQUAL, // **=
T_SL_EQUAL, // <<=
T_SR_EQUAL, // >>=
T_XOR_EQUAL, // ^=
T_AND_EQUAL, // &=
T_CONCAT_EQUAL, // .=
T_DIV_EQUAL, // /=
T_MINUS_EQUAL, // -=
T_MOD_EQUAL, // %=
T_MUL_EQUAL, // *=
T_OR_EQUAL, // |=
T_PLUS_EQUAL, // +=
T_POW_EQUAL, // **=
T_SL_EQUAL, // <<=
T_SR_EQUAL, // >>=
T_XOR_EQUAL, // ^=
T_COALESCE_EQUAL, // ??=
];

// @TODO: drop condition when PHP 7.4+ is required
if (\defined('T_COALESCE_EQUAL')) {
$tokens[] = T_COALESCE_EQUAL; // ??=
}
}

return $token->equals('=') || $token->isGivenKind($tokens);
Expand Down
7 changes: 2 additions & 5 deletions src/Fixer/FunctionNotation/FunctionDeclarationFixer.php
Expand Up @@ -57,7 +57,7 @@ final class FunctionDeclarationFixer extends AbstractFixer implements Configurab
*/
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_FUNCTION) || (\PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_FN));
return $tokens->isAnyTokenKindsFound([T_FUNCTION, T_FN]);
}

/**
Expand Down Expand Up @@ -123,10 +123,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];

if (
!$token->isGivenKind(T_FUNCTION)
&& (\PHP_VERSION_ID < 70400 || !$token->isGivenKind(T_FN))
) {
if (!$token->isGivenKind([T_FUNCTION, T_FN])) {
continue;
}

Expand Down
11 changes: 2 additions & 9 deletions src/Fixer/FunctionNotation/FunctionTypehintSpaceFixer.php
Expand Up @@ -46,11 +46,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function isCandidate(Tokens $tokens): bool
{
if (\PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_FN)) {
return true;
}

return $tokens->isTokenKindFound(T_FUNCTION);
return $tokens->isAnyTokenKindsFound([T_FUNCTION, T_FN]);
}

/**
Expand All @@ -63,10 +59,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];

if (
!$token->isGivenKind(T_FUNCTION)
&& (\PHP_VERSION_ID < 70400 || !$token->isGivenKind(T_FN))
) {
if (!$token->isGivenKind([T_FUNCTION, T_FN])) {
continue;
}

Expand Down
11 changes: 1 addition & 10 deletions src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php
Expand Up @@ -20,7 +20,6 @@
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerConfiguration\InvalidOptionsForEnvException;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
Expand Down Expand Up @@ -136,11 +135,7 @@ public function getPriority(): int
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$expectedTokens = [T_LIST, T_FUNCTION, CT::T_USE_LAMBDA];

if (\PHP_VERSION_ID >= 70400) {
$expectedTokens[] = T_FN;
}
$expectedTokens = [T_LIST, T_FUNCTION, CT::T_USE_LAMBDA, T_FN];

for ($index = $tokens->count() - 1; $index > 0; --$index) {
$token = $tokens[$index];
Expand Down Expand Up @@ -191,10 +186,6 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn
->setAllowedTypes(['bool'])
->setDefault(false)
->setNormalizer(static function (Options $options, $value) {
if (\PHP_VERSION_ID < 70300 && $value) {
throw new InvalidOptionsForEnvException('"after_heredoc" option can only be enabled with PHP 7.3+.');
}

return $value;
})
->getOption(),
Expand Down
Expand Up @@ -62,11 +62,7 @@ public function getPriority(): int
*/
public function isCandidate(Tokens $tokens): bool
{
if (\PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_FN)) {
return true;
}

return $tokens->isTokenKindFound(T_FUNCTION);
return $tokens->isAnyTokenKindsFound([T_FUNCTION, T_FN]);
}

/**
Expand All @@ -82,10 +78,7 @@ public function isRisky(): bool
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$functionKinds = [T_FUNCTION];
if (\defined('T_FN')) {
$functionKinds[] = T_FN;
}
$functionKinds = [T_FUNCTION, T_FN];

for ($i = 0, $l = $tokens->count(); $i < $l; ++$i) {
if (!$tokens[$i]->isGivenKind($functionKinds)) {
Expand Down
Expand Up @@ -58,15 +58,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function isCandidate(Tokens $tokens): bool
{
if (!$tokens->isTokenKindFound(T_VARIABLE)) {
return false;
}

if (\PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_FN)) {
return true;
}

return $tokens->isTokenKindFound(T_FUNCTION);
return $tokens->isTokenKindFound(T_VARIABLE) && $tokens->isAnyTokenKindsFound([T_FUNCTION, T_FN]);
}

/**
Expand Down Expand Up @@ -98,11 +90,7 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$functionsAnalyzer = new FunctionsAnalyzer();
$tokenKinds = [T_FUNCTION];

if (\PHP_VERSION_ID >= 70400) {
$tokenKinds[] = T_FN;
}
$tokenKinds = [T_FUNCTION, T_FN];

for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/FunctionNotation/PhpdocToPropertyTypeFixer.php
Expand Up @@ -76,7 +76,7 @@ class Foo {
*/
public function isCandidate(Tokens $tokens): bool
{
return \PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_DOC_COMMENT);
return $tokens->isTokenKindFound(T_DOC_COMMENT);
}

/**
Expand Down
11 changes: 2 additions & 9 deletions src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php
Expand Up @@ -110,11 +110,7 @@ public function create($prototype) {
*/
public function isCandidate(Tokens $tokens): bool
{
if (\PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_FN)) {
return true;
}

return $tokens->isTokenKindFound(T_FUNCTION);
return $tokens->isAnyTokenKindsFound([T_FUNCTION, T_FN]);
}

/**
Expand Down Expand Up @@ -143,10 +139,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

for ($index = $tokens->count() - 1; 0 < $index; --$index) {
if (
!$tokens[$index]->isGivenKind(T_FUNCTION)
&& (\PHP_VERSION_ID < 70400 || !$tokens[$index]->isGivenKind(T_FN))
) {
if (!$tokens[$index]->isGivenKind([T_FUNCTION, T_FN])) {
continue;
}

Expand Down
12 changes: 2 additions & 10 deletions src/Fixer/FunctionNotation/StaticLambdaFixer.php
Expand Up @@ -43,11 +43,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function isCandidate(Tokens $tokens): bool
{
if (\PHP_VERSION_ID >= 70400 && $tokens->isTokenKindFound(T_FN)) {
return true;
}

return $tokens->isTokenKindFound(T_FUNCTION);
return $tokens->isAnyTokenKindsFound([T_FUNCTION, T_FN]);
}

/**
Expand All @@ -64,11 +60,7 @@ public function isRisky(): bool
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$analyzer = new TokensAnalyzer($tokens);
$expectedFunctionKinds = [T_FUNCTION];

if (\PHP_VERSION_ID >= 70400) {
$expectedFunctionKinds[] = T_FN;
}
$expectedFunctionKinds = [T_FUNCTION, T_FN];

for ($index = $tokens->count() - 4; $index > 0; --$index) {
if (!$tokens[$index]->isGivenKind($expectedFunctionKinds) || !$analyzer->isLambda($index)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/FunctionNotation/UseArrowFunctionsFixer.php
Expand Up @@ -59,7 +59,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function isCandidate(Tokens $tokens): bool
{
return \PHP_VERSION_ID >= 70400 && $tokens->isAllTokenKindsFound([T_FUNCTION, T_RETURN]);
return $tokens->isAllTokenKindsFound([T_FUNCTION, T_RETURN]);
}

/**
Expand Down
Expand Up @@ -58,7 +58,7 @@ public function getPriority(): int
*/
public function isCandidate(Tokens $tokens): bool
{
return \defined('T_COALESCE_EQUAL') && $tokens->isTokenKindFound(T_COALESCE);
return $tokens->isTokenKindFound(T_COALESCE);
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Fixer/Operator/BinaryOperatorSpacesFixer.php
Expand Up @@ -468,11 +468,6 @@ private function resolveOperatorsFromConfig(): array
}
}

// @TODO: drop condition when PHP 7.4+ is required
if (!\defined('T_COALESCE_EQUAL')) {
unset($operators['??=']);
}

return $operators;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Whitespace/HeredocIndentationFixer.php
Expand Up @@ -88,7 +88,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function isCandidate(Tokens $tokens): bool
{
return \PHP_VERSION_ID >= 70300 && $tokens->isTokenKindFound(T_START_HEREDOC);
return $tokens->isTokenKindFound(T_START_HEREDOC);
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/Linter/TokenizerLinter.php
Expand Up @@ -27,16 +27,6 @@
*/
final class TokenizerLinter implements LinterInterface
{
public function __construct()
{
if (
// @TODO: drop condition when PHP 7.3+ is required
false === class_exists(\CompileError::class)
) {
throw new UnavailableLinterException('Cannot use tokenizer as linter.');
}
}

/**
* {@inheritdoc}
*/
Expand Down
6 changes: 1 addition & 5 deletions src/Tokenizer/AbstractTypeTransformer.php
Expand Up @@ -62,11 +62,7 @@ protected function doProcess(Tokens $tokens, int $index, $originalToken): void
return;
}

$functionKinds = [[T_FUNCTION]];
if (\defined('T_FN')) {
$functionKinds[] = [T_FN];
}

$functionKinds = [[T_FUNCTION], [T_FN]];
$functionIndex = $tokens->getPrevTokenOfKind($prevIndex, $functionKinds);

if (null === $functionIndex) {
Expand Down
11 changes: 2 additions & 9 deletions src/Tokenizer/TokensAnalyzer.php
Expand Up @@ -280,10 +280,7 @@ public function isAnonymousClass(int $index): bool
*/
public function isLambda(int $index): bool
{
if (
!$this->tokens[$index]->isGivenKind(T_FUNCTION)
&& (\PHP_VERSION_ID < 70400 || !$this->tokens[$index]->isGivenKind(T_FN))
) {
if (!$this->tokens[$index]->isGivenKind([T_FUNCTION, T_FN])) {
throw new \LogicException(sprintf('No T_FUNCTION or T_FN at given index %d, got "%s".', $index, $this->tokens[$index]->getName()));
}

Expand Down Expand Up @@ -563,12 +560,8 @@ public function isBinaryOperator(int $index): bool
T_XOR_EQUAL => true, // ^=
T_SPACESHIP => true, // <=>
T_COALESCE => true, // ??
T_COALESCE_EQUAL => true, // ??=
];

// @TODO: drop condition when PHP 7.4+ is required
if (\defined('T_COALESCE_EQUAL')) {
$arrayOperators[T_COALESCE_EQUAL] = true; // ??=
}
}

$tokens = $this->tokens;
Expand Down
7 changes: 1 addition & 6 deletions src/Tokenizer/Transformer/ReturnRefTransformer.php
Expand Up @@ -41,12 +41,7 @@ public function getRequiredPhpVersionId(): int
*/
public function process(Tokens $tokens, Token $token, int $index): void
{
$prevKinds = [T_FUNCTION];
if (\PHP_VERSION_ID >= 70400) {
$prevKinds[] = T_FN;
}

if ($token->equals('&') && $tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind($prevKinds)) {
if ($token->equals('&') && $tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind([T_FUNCTION, T_FN])) {
$tokens[$index] = new Token([CT::T_RETURN_REF, '&']);
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/Tokenizer/Transformer/TypeColonTransformer.php
Expand Up @@ -71,13 +71,7 @@ public function process(Tokens $tokens, Token $token, int $index): void
$prevToken = $tokens[$prevIndex];
}

$prevKinds = [T_FUNCTION, CT::T_RETURN_REF, CT::T_USE_LAMBDA];

if (\defined('T_FN')) { // @TODO: drop condition when PHP 7.4+ is required
$prevKinds[] = T_FN;
}

if ($prevToken->isGivenKind($prevKinds)) {
if ($prevToken->isGivenKind([T_FUNCTION, CT::T_RETURN_REF, CT::T_USE_LAMBDA, T_FN])) {
$tokens[$index] = new Token([CT::T_TYPE_COLON, ':']);
}
}
Expand Down

0 comments on commit bc05dce

Please sign in to comment.