Skip to content

Commit

Permalink
minor: use more spread operator (#7222)
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus authored Aug 11, 2023
1 parent d261c82 commit 671149b
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 107 deletions.
14 changes: 6 additions & 8 deletions src/Fixer/Alias/PowToExponentiationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,11 @@ private function isParenthesisNeeded(Tokens $tokens, int $argumentStartIndex, in
*/
private function getAllowedKinds(): array
{
return array_merge(
[
T_DNUMBER, T_LNUMBER, T_VARIABLE, T_STRING, T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_CAST,
T_INT_CAST, T_INC, T_DEC, T_NS_SEPARATOR, T_WHITESPACE, T_DOUBLE_COLON, T_LINE, T_COMMENT, T_DOC_COMMENT,
CT::T_NAMESPACE_OPERATOR,
],
Token::getObjectOperatorKinds()
);
return [
T_DNUMBER, T_LNUMBER, T_VARIABLE, T_STRING, T_CONSTANT_ENCAPSED_STRING, T_DOUBLE_CAST,
T_INT_CAST, T_INC, T_DEC, T_NS_SEPARATOR, T_WHITESPACE, T_DOUBLE_COLON, T_LINE, T_COMMENT, T_DOC_COMMENT,
CT::T_NAMESPACE_OPERATOR,
...Token::getObjectOperatorKinds(),
];
}
}
38 changes: 18 additions & 20 deletions src/Fixer/Casing/ConstantCaseFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,24 @@ private function isNeighbourAccepted(Tokens $tokens, int $index): bool
static $forbiddenTokens = null;

if (null === $forbiddenTokens) {
$forbiddenTokens = array_merge(
[
T_AS,
T_CLASS,
T_CONST,
T_EXTENDS,
T_IMPLEMENTS,
T_INSTANCEOF,
T_INSTEADOF,
T_INTERFACE,
T_NEW,
T_NS_SEPARATOR,
T_PAAMAYIM_NEKUDOTAYIM,
T_TRAIT,
T_USE,
CT::T_USE_TRAIT,
CT::T_USE_LAMBDA,
],
Token::getObjectOperatorKinds()
);
$forbiddenTokens = [
T_AS,
T_CLASS,
T_CONST,
T_EXTENDS,
T_IMPLEMENTS,
T_INSTANCEOF,
T_INSTEADOF,
T_INTERFACE,
T_NEW,
T_NS_SEPARATOR,
T_PAAMAYIM_NEKUDOTAYIM,
T_TRAIT,
T_USE,
CT::T_USE_TRAIT,
CT::T_USE_LAMBDA,
...Token::getObjectOperatorKinds(),
];
}

$token = $tokens[$index];
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Casing/MagicMethodCasingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __Sleep()

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_STRING) && $tokens->isAnyTokenKindsFound(array_merge([T_FUNCTION, T_DOUBLE_COLON], Token::getObjectOperatorKinds()));
return $tokens->isTokenKindFound(T_STRING) && $tokens->isAnyTokenKindsFound([T_FUNCTION, T_DOUBLE_COLON, ...Token::getObjectOperatorKinds()]);
}

protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
Expand Down
8 changes: 4 additions & 4 deletions src/Fixer/ClassNotation/OrderedTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,10 @@ private function createTypeDeclarationTokens(array $types, string $glue, bool $i

foreach ($types as $i => $type) {
if (\is_array($type)) {
$newTokens = array_merge(
$newTokens,
$this->createTypeDeclarationTokens($type, '&', true)
);
$newTokens = [
...$newTokens,
...$this->createTypeDeclarationTokens($type, '&', true),
];
} elseif (isset($specialTypes[$type])) {
$newTokens[] = new Token($specialTypes[$type]);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/ClassNotation/VisibilityRequiredFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

$expectedKindsGeneric = [T_ABSTRACT, T_FINAL, T_PRIVATE, T_PROTECTED, T_PUBLIC, T_STATIC, T_VAR];
$expectedKindsPropertyKinds = array_merge($expectedKindsGeneric, $propertyTypeDeclarationKinds);
$expectedKindsPropertyKinds = [...$expectedKindsGeneric, ...$propertyTypeDeclarationKinds];

foreach (array_reverse($tokensAnalyzer->getClassyElements(), true) as $index => $element) {
if (!\in_array($element['type'], $this->configuration['elements'], true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function configure(array $configuration): void
$getDefinedConstants = get_defined_constants(true);
unset($getDefinedConstants['user']);
foreach ($getDefinedConstants as $constants) {
$constantsToEscape = array_merge($constantsToEscape, array_keys($constants));
$constantsToEscape = [...$constantsToEscape, ...array_keys($constants)];
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/Fixer/DoctrineAnnotation/DoctrineAnnotationBracesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ public function getDefinition(): FixerDefinitionInterface

protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver(array_merge(
parent::createConfigurationDefinition()->getOptions(),
[
(new FixerOptionBuilder('syntax', 'Whether to add or remove braces.'))
->setAllowedValues(['with_braces', 'without_braces'])
->setDefault('without_braces')
->getOption(),
]
));
return new FixerConfigurationResolver([
...parent::createConfigurationDefinition()->getOptions(),
(new FixerOptionBuilder('syntax', 'Whether to add or remove braces.'))
->setAllowedValues(['with_braces', 'without_braces'])
->setDefault('without_braces')
->getOption(),
]);
}

protected function fixAnnotations(Tokens $doctrineAnnotationTokens): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ public function getDefinition(): FixerDefinitionInterface

protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver(array_merge(
parent::createConfigurationDefinition()->getOptions(),
[
(new FixerOptionBuilder('indent_mixed_lines', 'Whether to indent lines that have content before closing parenthesis.'))
->setAllowedTypes(['bool'])
->setDefault(false)
->getOption(),
]
));
return new FixerConfigurationResolver([
...parent::createConfigurationDefinition()->getOptions(),
(new FixerOptionBuilder('indent_mixed_lines', 'Whether to indent lines that have content before closing parenthesis.'))
->setAllowedTypes(['bool'])
->setDefault(false)
->getOption(),
]);
}

protected function fixAnnotations(Tokens $doctrineAnnotationTokens): void
Expand Down
72 changes: 35 additions & 37 deletions src/Fixer/DoctrineAnnotation/DoctrineAnnotationSpacesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,41 @@ public function getPriority(): int

protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver(array_merge(
parent::createConfigurationDefinition()->getOptions(),
[
(new FixerOptionBuilder('around_parentheses', 'Whether to fix spaces around parentheses.'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('around_commas', 'Whether to fix spaces around commas.'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('before_argument_assignments', 'Whether to add, remove or ignore spaces before argument assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(false)
->getOption(),
(new FixerOptionBuilder('after_argument_assignments', 'Whether to add, remove or ignore spaces after argument assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(false)
->getOption(),
(new FixerOptionBuilder('before_array_assignments_equals', 'Whether to add, remove or ignore spaces before array `=` assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('after_array_assignments_equals', 'Whether to add, remove or ignore spaces after array assignment `=` operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('before_array_assignments_colon', 'Whether to add, remove or ignore spaces before array `:` assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('after_array_assignments_colon', 'Whether to add, remove or ignore spaces after array assignment `:` operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
]
));
return new FixerConfigurationResolver([
...parent::createConfigurationDefinition()->getOptions(),
(new FixerOptionBuilder('around_parentheses', 'Whether to fix spaces around parentheses.'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('around_commas', 'Whether to fix spaces around commas.'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('before_argument_assignments', 'Whether to add, remove or ignore spaces before argument assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(false)
->getOption(),
(new FixerOptionBuilder('after_argument_assignments', 'Whether to add, remove or ignore spaces after argument assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(false)
->getOption(),
(new FixerOptionBuilder('before_array_assignments_equals', 'Whether to add, remove or ignore spaces before array `=` assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('after_array_assignments_equals', 'Whether to add, remove or ignore spaces after array assignment `=` operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('before_array_assignments_colon', 'Whether to add, remove or ignore spaces before array `:` assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('after_array_assignments_colon', 'Whether to add, remove or ignore spaces after array assignment `:` operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
]);
}

protected function fixAnnotations(Tokens $doctrineAnnotationTokens): void
Expand Down
4 changes: 2 additions & 2 deletions src/Fixer/FunctionNotation/SingleLineThrowFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private function isPreviousTokenToClear(Token $token): bool
static $tokens = null;

if (null === $tokens) {
$tokens = array_merge(self::REMOVE_WHITESPACE_AFTER_TOKENS, self::REMOVE_WHITESPACE_AROUND_TOKENS);
$tokens = [...self::REMOVE_WHITESPACE_AFTER_TOKENS, ...self::REMOVE_WHITESPACE_AROUND_TOKENS];
}

return $token->equalsAny($tokens) || $token->isObjectOperator();
Expand All @@ -151,7 +151,7 @@ private function isNextTokenToClear(Token $token): bool
static $tokens = null;

if (null === $tokens) {
$tokens = array_merge(self::REMOVE_WHITESPACE_AROUND_TOKENS, self::REMOVE_WHITESPACE_BEFORE_TOKENS);
$tokens = [...self::REMOVE_WHITESPACE_AROUND_TOKENS, ...self::REMOVE_WHITESPACE_BEFORE_TOKENS];
}

return $token->equalsAny($tokens) || $token->isObjectOperator();
Expand Down
10 changes: 5 additions & 5 deletions src/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ public function getPriority(): int

public function isCandidate(Tokens $tokens): bool
{
$tokenKinds = array_merge(
array_values($this->fixTokenMapContainASingleSpace),
array_values($this->fixTokenMapPrecededByASingleSpace),
array_values($this->fixTokenMapFollowedByASingleSpace),
);
$tokenKinds = [
...array_values($this->fixTokenMapContainASingleSpace),
...array_values($this->fixTokenMapPrecededByASingleSpace),
...array_values($this->fixTokenMapFollowedByASingleSpace),
];

return $tokens->isAnyTokenKindsFound($tokenKinds) && !$tokens->hasAlternativeSyntax();
}
Expand Down
5 changes: 4 additions & 1 deletion src/Tokenizer/Analyzer/FunctionsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public function isGlobalFunctionCall(Tokens $tokens, int $index): bool
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
}

$possibleKind = array_merge([T_DOUBLE_COLON, T_FUNCTION, CT::T_NAMESPACE_OPERATOR, T_NEW, CT::T_RETURN_REF, T_STRING], Token::getObjectOperatorKinds());
$possibleKind = [
T_DOUBLE_COLON, T_FUNCTION, CT::T_NAMESPACE_OPERATOR, T_NEW, CT::T_RETURN_REF, T_STRING,
...Token::getObjectOperatorKinds(),
];

// @TODO: drop condition when PHP 8.0+ is required
if (\defined('T_ATTRIBUTE')) {
Expand Down
8 changes: 4 additions & 4 deletions tests/AutoReview/CiConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public function testTestJobsRunOnEachPhp(): void
$supportedMinPhp = 8;
}

$supportedVersions = array_merge(
$supportedVersions,
self::generateMinorVersionsRange($supportedMinPhp, $supportedMaxPhp)
);
$supportedVersions = [
...$supportedVersions,
...self::generateMinorVersionsRange($supportedMinPhp, $supportedMaxPhp),
];

self::assertTrue(\count($supportedVersions) > 0);

Expand Down
2 changes: 1 addition & 1 deletion tests/DocBlock/TagComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static function provideComparatorTogetherWithDefinedGroupsCases(): iterab

yield [[['param', 'return'], ['author', 'since']], 'author', 'since', true];

yield [array_merge(TagComparator::DEFAULT_GROUPS, [['param', 'return']]), 'link', 'see', true];
yield [[...TagComparator::DEFAULT_GROUPS, ['param', 'return']], 'link', 'see', true];

yield [[['param', 'return']], 'category', 'package', false];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixer/ControlStructure/YodaStyleFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ function foo() {
$assignmentOperators = ['=', '**=', '*=', '|=', '+=', '-=', '^=', '<<=', '>>=', '&=', '.=', '/=', '%=', '??='];
$logicalOperators = ['xor', 'or', 'and', '||', '&&', '??'];

foreach (array_merge($assignmentOperators, $logicalOperators) as $operator) {
foreach ([...$assignmentOperators, ...$logicalOperators] as $operator) {
yield [
sprintf('<?php $a %s 4 === $b ? 2 : 3;', $operator),
sprintf('<?php $a %s $b === 4 ? 2 : 3;', $operator),
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixer/Whitespace/ArrayIndentationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ private static function withLongArraySyntaxCases(array $cases): array
$longSyntaxCases[] = $case;
}

return array_merge($cases, $longSyntaxCases);
return [...$cases, ...$longSyntaxCases];
}

private static function toLongArraySyntax(string $php): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ public static function provideFixWithMultipleConfigStatementsCases(): iterable
'while',
];

$allStatements = array_merge($statementsWithoutCaseOrDefault, ['case', 'default']);
$allStatements = [...$statementsWithoutCaseOrDefault, 'case', 'default'];

yield [
$statementsWithoutCaseOrDefault,
Expand Down
2 changes: 1 addition & 1 deletion tests/Test/AbstractTransformerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function doTest(string $source, array $expectedTokens, array $observed
$tokens,
array_map(
static fn ($kindOrPrototype) => \is_int($kindOrPrototype) ? [$kindOrPrototype] : $kindOrPrototype,
array_unique(array_merge($observedKindsOrPrototypes, $expectedTokens))
array_unique([...$observedKindsOrPrototypes, ...$expectedTokens])
)
),
'Number of expected tokens does not match actual token count.'
Expand Down

0 comments on commit 671149b

Please sign in to comment.