From 58e87fa5e0cb8c76d8dd8ae4423342a47b35d403 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Fri, 29 Jul 2022 18:17:21 -0400 Subject: [PATCH 1/4] Applied ArraySpreadInsteadOfArrayMergeRector (take 2) --- src/Psalm/Codebase.php | 3 +- src/Psalm/Config/Creator.php | 5 +- src/Psalm/ErrorBaseline.php | 17 ++- .../Internal/Algebra/FormulaGenerator.php | 6 +- .../FunctionLike/ReturnTypeCollector.php | 108 +++++++++--------- .../Internal/Analyzer/ProjectAnalyzer.php | 2 +- src/Psalm/Internal/Analyzer/ScopeAnalyzer.php | 83 +++++--------- .../Analyzer/Statements/Block/DoAnalyzer.php | 2 +- .../Block/IfConditionalAnalyzer.php | 2 +- .../Statements/Block/IfElse/ElseAnalyzer.php | 5 +- .../Block/IfElse/ElseIfAnalyzer.php | 20 ++-- .../Statements/Block/IfElse/IfAnalyzer.php | 8 +- .../Statements/Block/IfElseAnalyzer.php | 9 +- .../Statements/Block/LoopAnalyzer.php | 21 ++-- .../Statements/Block/SwitchAnalyzer.php | 5 +- .../Statements/Block/SwitchCaseAnalyzer.php | 14 +-- .../Statements/Block/WhileAnalyzer.php | 5 +- .../InstancePropertyAssignmentAnalyzer.php | 8 +- .../Expression/AssignmentAnalyzer.php | 27 +++-- .../Expression/BinaryOp/AndAnalyzer.php | 8 +- .../Expression/BinaryOp/ConcatAnalyzer.php | 17 ++- .../Expression/BinaryOp/OrAnalyzer.php | 7 +- .../Expression/Call/ArgumentAnalyzer.php | 17 ++- .../Call/ClassTemplateParamCollector.php | 17 ++- .../Expression/Call/FunctionCallAnalyzer.php | 2 +- .../Call/FunctionCallReturnTypeFetcher.php | 2 +- .../Call/Method/AtomicMethodCallAnalyzer.php | 8 +- .../Expression/Call/StaticCallAnalyzer.php | 2 +- .../Statements/Expression/CallAnalyzer.php | 10 +- .../Statements/Expression/CastAnalyzer.php | 2 +- .../Statements/Expression/TernaryAnalyzer.php | 12 +- .../Analyzer/Statements/ReturnAnalyzer.php | 9 +- .../Internal/Analyzer/StatementsAnalyzer.php | 3 +- src/Psalm/Internal/Analyzer/TypeAnalyzer.php | 3 +- src/Psalm/Internal/Cli/Psalter.php | 3 +- src/Psalm/Internal/Codebase/DataFlowGraph.php | 3 +- src/Psalm/Internal/Codebase/Methods.php | 12 +- src/Psalm/Internal/Codebase/Populator.php | 4 +- .../Internal/Codebase/TaintFlowGraph.php | 4 +- .../Internal/Codebase/VariableUseGraph.php | 7 +- .../Internal/Diff/FileStatementsDiffer.php | 21 ++-- .../Diff/NamespaceStatementsDiffer.php | 11 +- src/Psalm/Internal/EventDispatcher.php | 5 +- src/Psalm/Internal/IncludeCollector.php | 3 +- .../Reflector/FunctionLikeNodeScanner.php | 3 +- .../Provider/FileReferenceProvider.php | 37 +++--- .../Generator/ClassLikeStubGenerator.php | 6 +- .../Type/TemplateStandinTypeReplacer.php | 37 +++--- src/Psalm/Internal/Type/TypeExpander.php | 35 +++--- src/Psalm/Internal/Type/TypeParser.php | 3 +- src/Psalm/Type/Union.php | 5 +- tests/Internal/CallMapTest.php | 4 +- 52 files changed, 280 insertions(+), 392 deletions(-) diff --git a/src/Psalm/Codebase.php b/src/Psalm/Codebase.php index 9bab2d6ed40..341c824b26f 100644 --- a/src/Psalm/Codebase.php +++ b/src/Psalm/Codebase.php @@ -67,7 +67,6 @@ use UnexpectedValueException; use function array_combine; -use function array_merge; use function array_pop; use function array_reverse; use function count; @@ -618,7 +617,7 @@ public function findReferencesToClassLike(string $fq_class_name): array $locations = $this->file_reference_provider->getClassLocations($fq_class_name_lc); if (isset($this->use_referencing_locations[$fq_class_name_lc])) { - $locations = array_merge($locations, $this->use_referencing_locations[$fq_class_name_lc]); + $locations = [...$locations, ...$this->use_referencing_locations[$fq_class_name_lc]]; } return $locations; diff --git a/src/Psalm/Config/Creator.php b/src/Psalm/Config/Creator.php index ee8a7c9d059..de415f9ea99 100644 --- a/src/Psalm/Config/Creator.php +++ b/src/Psalm/Config/Creator.php @@ -246,10 +246,7 @@ private static function getPsr4Or0Paths(string $current_dir, array $composer_jso foreach ($paths as $path) { if ($path === '') { - $nodes = array_merge( - $nodes, - self::guessPhpFileDirs($current_dir) - ); + $nodes = [...$nodes, ...self::guessPhpFileDirs($current_dir)]; continue; } diff --git a/src/Psalm/ErrorBaseline.php b/src/Psalm/ErrorBaseline.php index d6fdfa90627..296a5eff0e6 100644 --- a/src/Psalm/ErrorBaseline.php +++ b/src/Psalm/ErrorBaseline.php @@ -249,19 +249,16 @@ private static function writeToFile( $filesNode->setAttribute('psalm-version', PSALM_VERSION); if ($include_php_versions) { - $extensions = array_merge(get_loaded_extensions(), get_loaded_extensions(true)); + $extensions = [...get_loaded_extensions(), ...get_loaded_extensions(true)]; usort($extensions, 'strnatcasecmp'); - $filesNode->setAttribute('php-version', implode(';' . "\n\t", array_merge( - [ - ('php:' . PHP_VERSION), - ], - array_map( - static fn(string $extension): string => $extension . ':' . phpversion($extension), - $extensions - ) - ))); + $filesNode->setAttribute('php-version', implode(';' . "\n\t", [...[ + ('php:' . PHP_VERSION), + ], ...array_map( + static fn(string $extension): string => $extension . ':' . phpversion($extension), + $extensions + )])); } foreach ($groupedIssues as $file => $issueTypes) { diff --git a/src/Psalm/Internal/Algebra/FormulaGenerator.php b/src/Psalm/Internal/Algebra/FormulaGenerator.php index 0f0556febec..5df0a73c84f 100644 --- a/src/Psalm/Internal/Algebra/FormulaGenerator.php +++ b/src/Psalm/Internal/Algebra/FormulaGenerator.php @@ -14,7 +14,6 @@ use Psalm\Node\Expr\VirtualBooleanNot; use Psalm\Storage\Assertion\Truthy; -use function array_merge; use function count; use function spl_object_id; use function substr; @@ -62,10 +61,7 @@ public static function getFormula( $cache ); - return array_merge( - $left_assertions, - $right_assertions - ); + return [...$left_assertions, ...$right_assertions]; } if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr || diff --git a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php index 824d6c97a34..21fa3715577 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php +++ b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php @@ -94,138 +94,138 @@ public static function getReturnTypes( } if ($stmt->expr instanceof PhpParser\Node\Expr\Assign) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, [$stmt->expr->expr], $yield_types ) - ); + ]; } $yield_types = array_merge($yield_types, self::getYieldTypeFromExpression($stmt->expr, $nodes)); } elseif ($stmt instanceof PhpParser\Node\Stmt\If_) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $stmt->stmts, $yield_types ) - ); + ]; foreach ($stmt->elseifs as $elseif) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $elseif->stmts, $yield_types ) - ); + ]; } if ($stmt->else) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $stmt->else->stmts, $yield_types ) - ); + ]; } } elseif ($stmt instanceof PhpParser\Node\Stmt\TryCatch) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $stmt->stmts, $yield_types ) - ); + ]; foreach ($stmt->catches as $catch) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $catch->stmts, $yield_types ) - ); + ]; } if ($stmt->finally) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $stmt->finally->stmts, $yield_types ) - ); + ]; } } elseif ($stmt instanceof PhpParser\Node\Stmt\For_) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $stmt->stmts, $yield_types ) - ); + ]; } elseif ($stmt instanceof PhpParser\Node\Stmt\Foreach_) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $stmt->stmts, $yield_types ) - ); + ]; } elseif ($stmt instanceof PhpParser\Node\Stmt\While_) { $yield_types = array_merge($yield_types, self::getYieldTypeFromExpression($stmt->cond, $nodes)); - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $stmt->stmts, $yield_types ) - ); + ]; } elseif ($stmt instanceof PhpParser\Node\Stmt\Do_) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $stmt->stmts, $yield_types ) - ); + ]; } elseif ($stmt instanceof PhpParser\Node\Stmt\Switch_) { foreach ($stmt->cases as $case) { - $return_types = array_merge( - $return_types, - self::getReturnTypes( + $return_types = [ + ...$return_types, + ...self::getReturnTypes( $codebase, $nodes, $case->stmts, $yield_types ) - ); + ]; } } } @@ -339,10 +339,10 @@ protected static function getYieldTypeFromExpression( } if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) { - return array_merge( - self::getYieldTypeFromExpression($stmt->left, $nodes), - self::getYieldTypeFromExpression($stmt->right, $nodes) - ); + return [ + ...self::getYieldTypeFromExpression($stmt->left, $nodes), + ...self::getYieldTypeFromExpression($stmt->right, $nodes) + ]; } if ($stmt instanceof PhpParser\Node\Expr\Assign) { @@ -361,7 +361,7 @@ protected static function getYieldTypeFromExpression( $yield_types = []; foreach ($stmt->getArgs() as $arg) { - $yield_types = array_merge($yield_types, self::getYieldTypeFromExpression($arg->value, $nodes)); + $yield_types = [...$yield_types, ...self::getYieldTypeFromExpression($arg->value, $nodes)]; } return $yield_types; @@ -372,7 +372,7 @@ protected static function getYieldTypeFromExpression( foreach ($stmt->items as $item) { if ($item instanceof PhpParser\Node\Expr\ArrayItem) { - $yield_types = array_merge($yield_types, self::getYieldTypeFromExpression($item->value, $nodes)); + $yield_types = [...$yield_types, ...self::getYieldTypeFromExpression($item->value, $nodes)]; } } diff --git a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php index 105b32587e2..d25d5ae6d72 100644 --- a/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php @@ -619,7 +619,7 @@ public function check(string $base_dir, bool $is_diff = false): void && $this->project_cache_provider->canDiffFiles() ) { $deleted_files = $this->file_reference_provider->getDeletedReferencedFiles(); - $diff_files = array_merge($deleted_files, $this->getDiffFiles()); + $diff_files = [...$deleted_files, ...$this->getDiffFiles()]; } $this->progress->write($this->generatePHPVersionMessage()); diff --git a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php index 2661cc14f69..a747aacc945 100644 --- a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php @@ -9,7 +9,6 @@ use function array_diff; use function array_filter; use function array_intersect; -use function array_merge; use function array_unique; use function array_values; use function count; @@ -61,13 +60,13 @@ public static function getControlActions( // don't consider a return if the expression never returns (e.g. a throw inside a short closure) if ($stmt_return_type && $stmt_return_type->isNever()) { - return array_values(array_unique(array_merge($control_actions, [self::ACTION_END]))); + return array_values(array_unique([...$control_actions, ...[self::ACTION_END]])); } - return array_values(array_unique(array_merge($control_actions, [self::ACTION_RETURN]))); + return array_values(array_unique([...$control_actions, ...[self::ACTION_RETURN]])); } - return array_values(array_unique(array_merge($control_actions, [self::ACTION_END]))); + return array_values(array_unique([...$control_actions, ...[self::ACTION_END]])); } if ($stmt instanceof PhpParser\Node\Stmt\Expression) { @@ -76,7 +75,7 @@ public static function getControlActions( && ($stmt_expr_type = $nodes->getType($stmt->expr)) && $stmt_expr_type->isNever() ) { - return array_values(array_unique(array_merge($control_actions, [self::ACTION_END]))); + return array_values(array_unique([...$control_actions, ...[self::ACTION_END]])); } continue; @@ -89,13 +88,13 @@ public static function getControlActions( if ($break_types && $count !== null && count($break_types) >= $count) { if ($break_types[count($break_types) - $count] === 'switch') { - return array_merge($control_actions, [self::ACTION_LEAVE_SWITCH]); + return [...$control_actions, ...[self::ACTION_LEAVE_SWITCH]]; } return array_values($control_actions); } - return array_values(array_unique(array_merge($control_actions, [self::ACTION_CONTINUE]))); + return array_values(array_unique([...$control_actions, ...[self::ACTION_CONTINUE]])); } if ($stmt instanceof PhpParser\Node\Stmt\Break_) { @@ -105,13 +104,13 @@ public static function getControlActions( if ($break_types && $count !== null && count($break_types) >= $count) { if ($break_types[count($break_types) - $count] === 'switch') { - return array_merge($control_actions, [self::ACTION_LEAVE_SWITCH]); + return [...$control_actions, ...[self::ACTION_LEAVE_SWITCH]]; } return array_values($control_actions); } - return array_values(array_unique(array_merge($control_actions, [self::ACTION_BREAK]))); + return array_values(array_unique([...$control_actions, ...[self::ACTION_BREAK]])); } if ($stmt instanceof PhpParser\Node\Stmt\If_) { @@ -159,30 +158,23 @@ public static function getControlActions( static fn(string $action): bool => $action === self::ACTION_NONE ); - $all_elseif_actions = array_merge($elseif_control_actions, $all_elseif_actions); + $all_elseif_actions = [...$elseif_control_actions, ...$all_elseif_actions]; } } if ($all_leave) { return array_values( - array_unique( - array_merge( - $control_actions, - $if_statement_actions, - $else_statement_actions, - $all_elseif_actions - ) - ) + array_unique([ + ...$control_actions, + ...$if_statement_actions, + ...$else_statement_actions, + ...$all_elseif_actions + ]) ); } $control_actions = array_filter( - array_merge( - $control_actions, - $if_statement_actions, - $else_statement_actions, - $all_elseif_actions - ), + [...$control_actions, ...$if_statement_actions, ...$else_statement_actions, ...$all_elseif_actions], static fn(string $action): bool => $action !== self::ACTION_NONE ); } @@ -201,7 +193,7 @@ public static function getControlActions( $case_actions = self::getControlActions( $case->stmts, $nodes, - array_merge($break_types, ['switch']), + [...$break_types, ...['switch']], $return_is_exit ); @@ -227,10 +219,7 @@ public static function getControlActions( $has_ended = true; } - $all_case_actions = array_merge( - $all_case_actions, - $case_actions - ); + $all_case_actions = [...$all_case_actions, ...$case_actions]; if (!$case_does_end && !$has_ended) { continue 2; @@ -247,13 +236,10 @@ public static function getControlActions( ); if ($has_default_terminator || $stmt->getAttribute('allMatched', false)) { - return array_values(array_unique(array_merge($control_actions, $all_case_actions))); + return array_values(array_unique([...$control_actions, ...$all_case_actions])); } - $control_actions = array_merge( - $control_actions, - $all_case_actions - ); + $control_actions = [...$control_actions, ...$all_case_actions]; } if ($stmt instanceof PhpParser\Node\Stmt\Do_ @@ -264,12 +250,12 @@ public static function getControlActions( $loop_actions = self::getControlActions( $stmt->stmts, $nodes, - array_merge($break_types, ['loop']), + [...$break_types, ...['loop']], $return_is_exit ); $control_actions = array_filter( - array_merge($control_actions, $loop_actions), + [...$control_actions, ...$loop_actions], static fn(string $action): bool => $action !== self::ACTION_NONE ); @@ -349,25 +335,21 @@ public static function getControlActions( ); if (!$all_leave) { - $control_actions = array_merge($control_actions, $catch_actions); + $control_actions = [...$control_actions, ...$catch_actions]; } else { - $all_catch_actions = array_merge($all_catch_actions, $catch_actions); + $all_catch_actions = [...$all_catch_actions, ...$catch_actions]; } } if ($all_leave && $try_statement_actions !== [self::ACTION_NONE]) { return array_values( array_unique( - array_merge( - $control_actions, - $try_statement_actions, - $all_catch_actions - ) + [...$control_actions, ...$try_statement_actions, ...$all_catch_actions] ) ); } } elseif ($try_leaves) { - return array_values(array_unique(array_merge($control_actions, $try_statement_actions))); + return array_values(array_unique([...$control_actions, ...$try_statement_actions])); } if ($stmt->finally && $stmt->finally->stmts) { @@ -379,18 +361,15 @@ public static function getControlActions( ); if (!in_array(self::ACTION_NONE, $finally_statement_actions, true)) { - return array_merge( - array_filter( - $control_actions, - static fn(string $action): bool => $action !== self::ACTION_NONE - ), - $finally_statement_actions - ); + return [...array_filter( + $control_actions, + static fn(string $action): bool => $action !== self::ACTION_NONE + ), ...$finally_statement_actions]; } } $control_actions = array_filter( - array_merge($control_actions, $try_statement_actions), + [...$control_actions, ...$try_statement_actions], static fn(string $action): bool => $action !== self::ACTION_NONE ); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php index 0056c1b77dd..e55434fa258 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php @@ -116,7 +116,7 @@ static function (Clause $c) use ($mixed_var_ids): bool { $negated_while_types = Algebra::getTruthsFromFormula( Algebra::simplifyCNF( - array_merge($context->clauses, $negated_while_clauses) + [...$context->clauses, ...$negated_while_clauses] ) ); diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfConditionalAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfConditionalAnalyzer.php index 7ab5c32fcae..c64ebaa0d58 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfConditionalAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfConditionalAnalyzer.php @@ -43,7 +43,7 @@ public static function analyze( // used when evaluating elseifs if ($if_scope->negated_clauses) { - $entry_clauses = array_merge($outer_context->clauses, $if_scope->negated_clauses); + $entry_clauses = [...$outer_context->clauses, ...$if_scope->negated_clauses]; $changed_var_ids = []; diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseAnalyzer.php index 136ce7daec8..057bc064745 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseAnalyzer.php @@ -51,10 +51,7 @@ public static function analyze( } $else_context->clauses = Algebra::simplifyCNF( - array_merge( - $else_context->clauses, - $if_scope->negated_clauses - ) + [...$else_context->clauses, ...$if_scope->negated_clauses] ); $else_types = Algebra::getTruthsFromFormula($else_context->clauses); diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseIfAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseIfAnalyzer.php index dfa52aaad5e..1c816846647 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseIfAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseIfAnalyzer.php @@ -136,7 +136,7 @@ public static function analyze( $assigned_in_conditional_var_ids ); - $elseif_context_clauses = array_merge($entry_clauses, $elseif_clauses); + $elseif_context_clauses = [...$entry_clauses, ...$elseif_clauses]; if ($elseif_context->reconciled_expression_clauses) { $reconciled_expression_clauses = $elseif_context->reconciled_expression_clauses; @@ -192,19 +192,16 @@ public static function analyze( } $all_negated_vars = array_unique( - array_merge( - array_keys($negated_elseif_types), - array_keys($if_scope->negated_types) - ) + [...array_keys($negated_elseif_types), ...array_keys($if_scope->negated_types)] ); foreach ($all_negated_vars as $var_id) { if (isset($negated_elseif_types[$var_id])) { if (isset($if_scope->negated_types[$var_id])) { - $if_scope->negated_types[$var_id] = array_merge( - $if_scope->negated_types[$var_id], - $negated_elseif_types[$var_id] - ); + $if_scope->negated_types[$var_id] = [ + ...$if_scope->negated_types[$var_id], + ...$negated_elseif_types[$var_id] + ]; } else { $if_scope->negated_types[$var_id] = $negated_elseif_types[$var_id]; } @@ -416,10 +413,7 @@ public static function analyze( try { $if_scope->negated_clauses = Algebra::simplifyCNF( - array_merge( - $if_scope->negated_clauses, - Algebra::negateFormula($elseif_clauses) - ) + [...$if_scope->negated_clauses, ...Algebra::negateFormula($elseif_clauses)] ); } catch (ComplicatedExpressionException $e) { $if_scope->negated_clauses = []; diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php index d4459c9f5c5..0fc2ffa9845 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php @@ -367,10 +367,10 @@ private static function getDefinitelyEvaluatedOredExpressions(PhpParser\Node\Exp || $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr || $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalXor ) { - return array_merge( - self::getDefinitelyEvaluatedOredExpressions($stmt->left), - self::getDefinitelyEvaluatedOredExpressions($stmt->right) - ); + return [ + ...self::getDefinitelyEvaluatedOredExpressions($stmt->left), + ...self::getDefinitelyEvaluatedOredExpressions($stmt->right) + ]; } return [$stmt]; diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php index 2c314b2f7cf..944512ae98a 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php @@ -171,7 +171,7 @@ public static function analyze( $if_clauses = Algebra::simplifyCNF($if_clauses); - $if_context_clauses = array_merge($entry_clauses, $if_clauses); + $if_context_clauses = [...$entry_clauses, ...$if_clauses]; $if_context->clauses = $entry_clauses ? Algebra::simplifyCNF($if_context_clauses) @@ -219,7 +219,7 @@ public static function analyze( $if_scope->negated_types = Algebra::getTruthsFromFormula( Algebra::simplifyCNF( - array_merge($context->clauses, $if_scope->negated_clauses) + [...$context->clauses, ...$if_scope->negated_clauses] ) ); @@ -406,10 +406,7 @@ public static function analyze( && (count($if_scope->reasonable_clauses) > 1 || !$if_scope->reasonable_clauses[0]->wedge) ) { $context->clauses = Algebra::simplifyCNF( - array_merge( - $if_scope->reasonable_clauses, - $context->clauses - ) + [...$if_scope->reasonable_clauses, ...$context->clauses] ); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php index 6be05572729..d3f428692c8 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php @@ -192,17 +192,14 @@ public static function analyze( $inner_do_context = clone $continue_context; foreach ($pre_conditions as $condition_offset => $pre_condition) { - $always_assigned_before_loop_body_vars = array_merge( - self::applyPreConditionToLoopContext( - $statements_analyzer, - $pre_condition, - $pre_condition_clauses[$condition_offset], - $continue_context, - $loop_parent_context, - $is_do - ), - $always_assigned_before_loop_body_vars - ); + $always_assigned_before_loop_body_vars = [...self::applyPreConditionToLoopContext( + $statements_analyzer, + $pre_condition, + $pre_condition_clauses[$condition_offset], + $continue_context, + $loop_parent_context, + $is_do + ), ...$always_assigned_before_loop_body_vars]; } } @@ -598,7 +595,7 @@ private static function applyPreConditionToLoopContext( $always_assigned_before_loop_body_vars = Context::getNewOrUpdatedVarIds($outer_context, $loop_context); $loop_context->clauses = Algebra::simplifyCNF( - array_merge($outer_context->clauses, $pre_condition_clauses) + [...$outer_context->clauses, ...$pre_condition_clauses] ); $active_while_types = []; diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php index 66f00d34b8c..8f1b1b47107 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php @@ -142,10 +142,7 @@ public static function analyze( if (!$has_default && $switch_scope->negated_clauses && $switch_var_id) { $entry_clauses = Algebra::simplifyCNF( - array_merge( - $original_context->clauses, - $switch_scope->negated_clauses - ) + [...$original_context->clauses, ...$switch_scope->negated_clauses] ); $reconcilable_if_types = Algebra::getTruthsFromFormula($entry_clauses); diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php index 8fea6b8ca3c..cb8991c68d4 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php @@ -356,10 +356,7 @@ public static function analyze( if ($switch_scope->negated_clauses && count($switch_scope->negated_clauses) < 50) { $entry_clauses = Algebra::simplifyCNF( - array_merge( - $original_context->clauses, - $switch_scope->negated_clauses - ) + [...$original_context->clauses, ...$switch_scope->negated_clauses] ); } else { $entry_clauses = $original_context->clauses; @@ -376,9 +373,9 @@ public static function analyze( ); if (count($entry_clauses) + count($case_clauses) < 50) { - $case_context->clauses = Algebra::simplifyCNF(array_merge($entry_clauses, $case_clauses)); + $case_context->clauses = Algebra::simplifyCNF([...$entry_clauses, ...$case_clauses]); } else { - $case_context->clauses = array_merge($entry_clauses, $case_clauses); + $case_context->clauses = [...$entry_clauses, ...$case_clauses]; } } else { $case_context->clauses = $entry_clauses; @@ -459,10 +456,7 @@ public static function analyze( } } - $switch_scope->negated_clauses = array_merge( - $switch_scope->negated_clauses, - $negated_case_clauses - ); + $switch_scope->negated_clauses = [...$switch_scope->negated_clauses, ...$negated_case_clauses]; } $statements_analyzer->analyze($case_stmts, $case_context); diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/WhileAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/WhileAnalyzer.php index 1d8716e7143..6a4e8eab3c8 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/WhileAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/WhileAnalyzer.php @@ -122,10 +122,7 @@ public static function getAndExpressions( PhpParser\Node\Expr $expr ): array { if ($expr instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd) { - return array_merge( - self::getAndExpressions($expr->left), - self::getAndExpressions($expr->right) - ); + return [...self::getAndExpressions($expr->left), ...self::getAndExpressions($expr->right)]; } return [$expr]; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php index 049677a9d36..c70fcacbb79 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php @@ -1411,10 +1411,10 @@ private static function analyzeAtomicAssignment( if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) { foreach ($assignment_value_type->parent_nodes as $parent_node) { - $origin_locations = array_merge( - $origin_locations, - $statements_analyzer->data_flow_graph->getOriginLocations($parent_node) - ); + $origin_locations = [ + ...$origin_locations, + ...$statements_analyzer->data_flow_graph->getOriginLocations($parent_node) + ]; } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index d7dc233d5a6..ccc7369a74b 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -83,7 +83,6 @@ use Psalm\Type\Union; use UnexpectedValueException; -use function array_merge; use function count; use function in_array; use function is_string; @@ -405,10 +404,10 @@ public static function analyze( if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) { foreach ($assign_value_type->parent_nodes as $parent_node) { - $origin_locations = array_merge( - $origin_locations, - $statements_analyzer->data_flow_graph->getOriginLocations($parent_node) - ); + $origin_locations = [ + ...$origin_locations, + ...$statements_analyzer->data_flow_graph->getOriginLocations($parent_node) + ]; } } @@ -610,10 +609,10 @@ public static function analyze( $event = new AddRemoveTaintsEvent($assign_var, $context, $statements_analyzer, $codebase); $added_taints = $codebase->config->eventDispatcher->dispatchAddTaints($event); - $removed_taints = array_merge( - $removed_taints, - $codebase->config->eventDispatcher->dispatchRemoveTaints($event) - ); + $removed_taints = [ + ...$removed_taints, + ...$codebase->config->eventDispatcher->dispatchRemoveTaints($event) + ]; self::taintAssignment( $context->vars_in_scope[$var_id], @@ -1500,10 +1499,10 @@ private static function analyzeDestructuringAssignment( $event = new AddRemoveTaintsEvent($var, $context, $statements_analyzer, $codebase); $added_taints = $codebase->config->eventDispatcher->dispatchAddTaints($event); - $removed_taints = array_merge( - $removed_taints, - $codebase->config->eventDispatcher->dispatchRemoveTaints($event) - ); + $removed_taints = [ + ...$removed_taints, + ...$codebase->config->eventDispatcher->dispatchRemoveTaints($event) + ]; self::taintAssignment( $context->vars_in_scope[$list_var_id], @@ -1754,7 +1753,7 @@ private static function analyzeAssignmentToVariable( $cond_object_id ); - $context->clauses = array_merge($context->clauses, $assignment_clauses); + $context->clauses = [...$context->clauses, ...$assignment_clauses]; } } } else { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/AndAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/AndAnalyzer.php index 4dd5bfb2f87..cad1020fecc 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/AndAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/AndAnalyzer.php @@ -198,14 +198,14 @@ public static function analyze( $context->assigned_var_ids, ); - $if_body_context->reconciled_expression_clauses = array_merge( - $if_body_context->reconciled_expression_clauses, - array_map( + $if_body_context->reconciled_expression_clauses = [ + ...$if_body_context->reconciled_expression_clauses, + ...array_map( /** @return string|int */ static fn(Clause $c) => $c->hash, $partitioned_clauses[1] ) - ); + ]; $if_body_context->vars_possibly_in_scope = array_merge( $if_body_context->vars_possibly_in_scope, diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ConcatAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ConcatAnalyzer.php index d5a0beda4a8..a4f3924df52 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ConcatAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ConcatAnalyzer.php @@ -41,7 +41,6 @@ use Psalm\Type\Union; use UnexpectedValueException; -use function array_merge; use function assert; use function count; use function reset; @@ -91,10 +90,10 @@ public static function analyze( if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) { foreach ($left_type->parent_nodes as $parent_node) { - $origin_locations = array_merge( - $origin_locations, - $statements_analyzer->data_flow_graph->getOriginLocations($parent_node) - ); + $origin_locations = [ + ...$origin_locations, + ...$statements_analyzer->data_flow_graph->getOriginLocations($parent_node) + ]; } } @@ -118,10 +117,10 @@ public static function analyze( if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) { foreach ($right_type->parent_nodes as $parent_node) { - $origin_locations = array_merge( - $origin_locations, - $statements_analyzer->data_flow_graph->getOriginLocations($parent_node) - ); + $origin_locations = [ + ...$origin_locations, + ...$statements_analyzer->data_flow_graph->getOriginLocations($parent_node) + ]; } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/OrAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/OrAnalyzer.php index 203165c9d84..4504a137dcb 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/OrAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/OrAnalyzer.php @@ -186,10 +186,7 @@ public static function analyze( } $clauses_for_right_analysis = Algebra::simplifyCNF( - array_merge( - $context->clauses, - $negated_left_clauses - ) + [...$context->clauses, ...$negated_left_clauses] ); $active_negated_type_assertions = []; @@ -292,7 +289,7 @@ public static function analyze( )[0]; $combined_right_clauses = Algebra::simplifyCNF( - array_merge($clauses_for_right_analysis, $right_clauses) + [...$clauses_for_right_analysis, ...$right_clauses] ); $active_right_type_assertions = []; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php index c410388f8d8..e87585606f5 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php @@ -60,7 +60,6 @@ use Psalm\Type\Atomic\TNamedObject; use Psalm\Type\Union; -use function array_merge; use function count; use function explode; use function in_array; @@ -738,10 +737,10 @@ public static function verifyType( if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) { foreach ($input_type->parent_nodes as $parent_node) { - $origin_locations = array_merge( - $origin_locations, - $statements_analyzer->data_flow_graph->getOriginLocations($parent_node) - ); + $origin_locations = [ + ...$origin_locations, + ...$statements_analyzer->data_flow_graph->getOriginLocations($parent_node) + ]; } } @@ -959,10 +958,10 @@ public static function verifyType( if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) { foreach ($input_type->parent_nodes as $parent_node) { - $origin_locations = array_merge( - $origin_locations, - $statements_analyzer->data_flow_graph->getOriginLocations($parent_node) - ); + $origin_locations = [ + ...$origin_locations, + ...$statements_analyzer->data_flow_graph->getOriginLocations($parent_node) + ]; } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ClassTemplateParamCollector.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ClassTemplateParamCollector.php index 61e286afbb0..ab31e700a52 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ClassTemplateParamCollector.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ClassTemplateParamCollector.php @@ -242,16 +242,13 @@ private static function expandType( || !isset($static_template_types[$type_extends_atomic->param_name])) && isset($e[$type_extends_atomic->defining_class][$type_extends_atomic->param_name]) ) { - $output_type_extends = array_merge( - $output_type_extends, - self::expandType( - $codebase, - $e[$type_extends_atomic->defining_class][$type_extends_atomic->param_name], - $e, - $static_fq_class_name, - $static_template_types - ) - ); + $output_type_extends = [...$output_type_extends, ...self::expandType( + $codebase, + $e[$type_extends_atomic->defining_class][$type_extends_atomic->param_name], + $e, + $static_fq_class_name, + $static_template_types + )]; } elseif ($type_extends_atomic instanceof TClassConstant) { $expanded = TypeExpander::expandAtomic( $codebase, diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php index 2f8bdb1a483..96af6145b5d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php @@ -948,7 +948,7 @@ private static function processAssertFunctionEffects( [] ); - $simplified_clauses = Algebra::simplifyCNF(array_merge($context->clauses, $assert_clauses)); + $simplified_clauses = Algebra::simplifyCNF([...$context->clauses, ...$assert_clauses]); $assert_type_assertions = Algebra::getTruthsFromFormula($simplified_clauses); diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php index afe36e9b8e1..ed207aded41 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php @@ -646,7 +646,7 @@ private static function taintReturnType( $assignment_node, 'conditionally-escaped', $added_taints, - array_merge($removed_taints, $conditionally_removed_taints) + [...$removed_taints, ...$conditionally_removed_taints] ); $stmt_type->parent_nodes[$assignment_node->id] = $assignment_node; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php index 27326139d00..1c9edaf7607 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php @@ -659,10 +659,10 @@ private static function handleInvalidClass( if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) { foreach ($lhs_type->parent_nodes as $parent_node) { - $origin_locations = array_merge( - $origin_locations, - $statements_analyzer->data_flow_graph->getOriginLocations($parent_node) - ); + $origin_locations = [ + ...$origin_locations, + ...$statements_analyzer->data_flow_graph->getOriginLocations($parent_node) + ]; } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php index a7ccea91a39..772ad322c0c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php @@ -344,7 +344,7 @@ public static function taintReturnType( $assignment_node, 'conditionally-escaped', $added_taints, - array_merge($conditionally_removed_taints, $removed_taints) + [...$conditionally_removed_taints, ...$removed_taints] ); $return_type_candidate->parent_nodes[$assignment_node->id] = $assignment_node; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php index e4756e86d05..d624dbc92a7 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php @@ -824,10 +824,10 @@ public static function applyAssertionsToContext( if ($orred_rules) { if (isset($type_assertions[$assertion_var_id])) { - $type_assertions[$assertion_var_id] = array_merge( - $type_assertions[$assertion_var_id], - [$orred_rules] - ); + $type_assertions[$assertion_var_id] = [ + ...$type_assertions[$assertion_var_id], + ...[$orred_rules] + ]; } else { $type_assertions[$assertion_var_id] = [$orred_rules]; } @@ -878,7 +878,7 @@ public static function applyAssertionsToContext( } $simplified_clauses = Algebra::simplifyCNF( - array_merge($context->clauses, $assert_clauses) + [...$context->clauses, ...$assert_clauses] ); $assert_type_assertions = Algebra::getTruthsFromFormula( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php index 16f799609f0..48795d26f34 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php @@ -464,7 +464,7 @@ public static function castStringAttempt( // todo: emit error here } - $valid_types = array_merge($valid_strings, $castable_types); + $valid_types = [...$valid_strings, ...$castable_types]; if (!$valid_types) { $str_type = Type::getString(); diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php index 7be2e617b3e..3a4f70870c8 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php @@ -130,10 +130,7 @@ static function (Clause $c) use ($mixed_var_ids, $cond_object_id): Clause { $if_clauses = Algebra::simplifyCNF($if_clauses); $ternary_context_clauses = $entry_clauses - ? Algebra::simplifyCNF(array_merge( - $entry_clauses, - $if_clauses - )) + ? Algebra::simplifyCNF([...$entry_clauses, ...$if_clauses]) : $if_clauses; if ($if_context->reconciled_expression_clauses) { @@ -175,7 +172,7 @@ static function (Clause $c) use ($mixed_var_ids, $cond_object_id): Clause { $if_scope->negated_types = Algebra::getTruthsFromFormula( Algebra::simplifyCNF( - array_merge($context->clauses, $if_scope->negated_clauses) + [...$context->clauses, ...$if_scope->negated_clauses] ) ); @@ -219,10 +216,7 @@ static function (Clause $c) use ($mixed_var_ids, $cond_object_id): Clause { } $t_else_context->clauses = Algebra::simplifyCNF( - array_merge( - $t_else_context->clauses, - $if_scope->negated_clauses - ) + [...$t_else_context->clauses, ...$if_scope->negated_clauses] ); $changed_var_ids = []; diff --git a/src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php index 6ce6cdb1536..c063431a38f 100644 --- a/src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php @@ -43,7 +43,6 @@ use Psalm\Type\Atomic\TClosure; use Psalm\Type\Union; -use function array_merge; use function count; use function explode; use function reset; @@ -325,10 +324,10 @@ public static function analyze( if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) { foreach ($stmt_type->parent_nodes as $parent_node) { - $origin_locations = array_merge( - $origin_locations, - $statements_analyzer->data_flow_graph->getOriginLocations($parent_node) - ); + $origin_locations = [ + ...$origin_locations, + ...$statements_analyzer->data_flow_graph->getOriginLocations($parent_node) + ]; } } diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index 39722e39639..284fd2fabf4 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -68,7 +68,6 @@ use function array_combine; use function array_keys; use function array_map; -use function array_merge; use function array_search; use function assert; use function count; @@ -389,7 +388,7 @@ private static function analyzeStatement( PREG_SPLIT_NO_EMPTY ); if ($possible_traced_variable_names) { - $traced_variables = array_merge($traced_variables, $possible_traced_variable_names); + $traced_variables = [...$traced_variables, ...$possible_traced_variable_names]; } } } diff --git a/src/Psalm/Internal/Analyzer/TypeAnalyzer.php b/src/Psalm/Internal/Analyzer/TypeAnalyzer.php index 9d1648c5961..05a4548c84e 100644 --- a/src/Psalm/Internal/Analyzer/TypeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/TypeAnalyzer.php @@ -6,7 +6,6 @@ use Psalm\Type\Union; use function array_keys; -use function array_merge; use function array_unique; /** @@ -24,7 +23,7 @@ class TypeAnalyzer */ public static function combineKeyedTypes(array $new_types, array $existing_types): array { - $keys = array_merge(array_keys($new_types), array_keys($existing_types)); + $keys = [...array_keys($new_types), ...array_keys($existing_types)]; $keys = array_unique($keys); $result_types = []; diff --git a/src/Psalm/Internal/Cli/Psalter.php b/src/Psalm/Internal/Cli/Psalter.php index 6b534b3b374..7da3c7498f7 100644 --- a/src/Psalm/Internal/Cli/Psalter.php +++ b/src/Psalm/Internal/Cli/Psalter.php @@ -26,7 +26,6 @@ use function array_filter; use function array_key_exists; use function array_map; -use function array_merge; use function array_shift; use function array_slice; use function chdir; @@ -304,7 +303,7 @@ public static function run(array $argv): void $files_for_codeowners = self::loadCodeownersFiles($desired_codeowners, $codeowner_files); $paths_to_check = is_array($paths_to_check) ? - array_merge($paths_to_check, $files_for_codeowners) : + [...$paths_to_check, ...$files_for_codeowners] : $files_for_codeowners; } diff --git a/src/Psalm/Internal/Codebase/DataFlowGraph.php b/src/Psalm/Internal/Codebase/DataFlowGraph.php index 4bd3f617fa6..5f66a1955cc 100644 --- a/src/Psalm/Internal/Codebase/DataFlowGraph.php +++ b/src/Psalm/Internal/Codebase/DataFlowGraph.php @@ -7,7 +7,6 @@ use function abs; use function array_keys; -use function array_merge; use function array_reverse; use function array_sum; use function count; @@ -156,7 +155,7 @@ public function summarizeEdges(): array $edges = []; foreach ($this->forward_edges as $source => $destinations) { - $edges[] = array_merge([$source], array_keys($destinations)); + $edges[] = [...[$source], ...array_keys($destinations)]; } return $edges; diff --git a/src/Psalm/Internal/Codebase/Methods.php b/src/Psalm/Internal/Codebase/Methods.php index c32c69a3683..ba56971366e 100644 --- a/src/Psalm/Internal/Codebase/Methods.php +++ b/src/Psalm/Internal/Codebase/Methods.php @@ -40,7 +40,6 @@ use Psalm\Type\Union; use UnexpectedValueException; -use function array_merge; use function array_pop; use function array_values; use function assert; @@ -639,13 +638,10 @@ public static function getExtendedTemplatedTypes( foreach ($extended_param->getAtomicTypes() as $extended_atomic_type) { if ($extended_atomic_type instanceof TTemplateParam) { - $extra_added_types = array_merge( - $extra_added_types, - self::getExtendedTemplatedTypes( - $extended_atomic_type, - $extends - ) - ); + $extra_added_types = [...$extra_added_types, ...self::getExtendedTemplatedTypes( + $extended_atomic_type, + $extends + )]; } else { $extra_added_types[] = $extended_atomic_type; } diff --git a/src/Psalm/Internal/Codebase/Populator.php b/src/Psalm/Internal/Codebase/Populator.php index de22e04a5a2..5902fa63dc1 100644 --- a/src/Psalm/Internal/Codebase/Populator.php +++ b/src/Psalm/Internal/Codebase/Populator.php @@ -232,11 +232,11 @@ private function populateClassLikeStorage(ClassLikeStorage $storage, array $depe if (!$storage->is_interface && !$storage->is_trait) { foreach ($storage->methods as $method) { - $method->internal = array_merge($storage->internal, $method->internal); + $method->internal = [...$storage->internal, ...$method->internal]; } foreach ($storage->properties as $property) { - $property->internal = array_merge($storage->internal, $property->internal); + $property->internal = [...$storage->internal, ...$property->internal]; } } diff --git a/src/Psalm/Internal/Codebase/TaintFlowGraph.php b/src/Psalm/Internal/Codebase/TaintFlowGraph.php index 9fa5a4c2462..bebf807215d 100644 --- a/src/Psalm/Internal/Codebase/TaintFlowGraph.php +++ b/src/Psalm/Internal/Codebase/TaintFlowGraph.php @@ -189,7 +189,7 @@ public function getIssueTrace(DataFlowNode $source): array return []; } - return array_merge($this->getIssueTrace($previous_source), [$node]); + return [...$this->getIssueTrace($previous_source), ...[$node]]; } return [$node]; @@ -467,7 +467,7 @@ private function getChildNodes( $new_destination->previous = $generated_source; $new_destination->taints = $new_taints; $new_destination->specialized_calls = $generated_source->specialized_calls; - $new_destination->path_types = array_merge($generated_source->path_types, [$path_type]); + $new_destination->path_types = [...$generated_source->path_types, ...[$path_type]]; $key = $to_id . ' ' . json_encode($new_destination->specialized_calls, JSON_THROW_ON_ERROR) . diff --git a/src/Psalm/Internal/Codebase/VariableUseGraph.php b/src/Psalm/Internal/Codebase/VariableUseGraph.php index d92b6568d06..365f440a572 100644 --- a/src/Psalm/Internal/Codebase/VariableUseGraph.php +++ b/src/Psalm/Internal/Codebase/VariableUseGraph.php @@ -129,10 +129,7 @@ public function getOriginLocations(DataFlowNode $assignment_node): array continue; } - $new_parent_nodes = array_merge( - $new_parent_nodes, - $parent_nodes - ); + $new_parent_nodes = [...$new_parent_nodes, ...$parent_nodes]; } $child_nodes = $new_parent_nodes; @@ -191,7 +188,7 @@ private function getChildNodes( } $new_destination = new DataFlowNode($to_id, $to_id, null); - $new_destination->path_types = array_merge($generated_source->path_types, [$path_type]); + $new_destination->path_types = [...$generated_source->path_types, ...[$path_type]]; $new_child_nodes[$to_id] = $new_destination; } diff --git a/src/Psalm/Internal/Diff/FileStatementsDiffer.php b/src/Psalm/Internal/Diff/FileStatementsDiffer.php index 92620e0305c..cb90a339c51 100644 --- a/src/Psalm/Internal/Diff/FileStatementsDiffer.php +++ b/src/Psalm/Internal/Diff/FileStatementsDiffer.php @@ -4,7 +4,6 @@ use PhpParser; -use function array_merge; use function end; use function get_class; use function substr; @@ -96,11 +95,11 @@ static function ( $b_code ); - $keep = array_merge($keep, $namespace_keep[0]); - $keep_signature = array_merge($keep_signature, $namespace_keep[1]); - $add_or_delete = array_merge($add_or_delete, $namespace_keep[2]); - $diff_map = array_merge($diff_map, $namespace_keep[3]); - $deletion_ranges = array_merge($deletion_ranges, $namespace_keep[4]); + $keep = [...$keep, ...$namespace_keep[0]]; + $keep_signature = [...$keep_signature, ...$namespace_keep[1]]; + $add_or_delete = [...$add_or_delete, ...$namespace_keep[2]]; + $diff_map = [...$diff_map, ...$namespace_keep[3]]; + $deletion_ranges = [...$deletion_ranges, ...$namespace_keep[4]]; } elseif (($diff_elem->old instanceof PhpParser\Node\Stmt\Class_ && $diff_elem->new instanceof PhpParser\Node\Stmt\Class_) || ($diff_elem->old instanceof PhpParser\Node\Stmt\Interface_ @@ -116,11 +115,11 @@ static function ( $b_code ); - $keep = array_merge($keep, $class_keep[0]); - $keep_signature = array_merge($keep_signature, $class_keep[1]); - $add_or_delete = array_merge($add_or_delete, $class_keep[2]); - $diff_map = array_merge($diff_map, $class_keep[3]); - $deletion_ranges = array_merge($deletion_ranges, $class_keep[4]); + $keep = [...$keep, ...$class_keep[0]]; + $keep_signature = [...$keep_signature, ...$class_keep[1]]; + $add_or_delete = [...$add_or_delete, ...$class_keep[2]]; + $diff_map = [...$diff_map, ...$class_keep[3]]; + $deletion_ranges = [...$deletion_ranges, ...$class_keep[4]]; } } elseif ($diff_elem->type === DiffElem::TYPE_REMOVE) { if ($diff_elem->old instanceof PhpParser\Node\Stmt\Use_ diff --git a/src/Psalm/Internal/Diff/NamespaceStatementsDiffer.php b/src/Psalm/Internal/Diff/NamespaceStatementsDiffer.php index 90019904aab..a3788f848e6 100644 --- a/src/Psalm/Internal/Diff/NamespaceStatementsDiffer.php +++ b/src/Psalm/Internal/Diff/NamespaceStatementsDiffer.php @@ -4,7 +4,6 @@ use PhpParser; -use function array_merge; use function end; use function get_class; use function substr; @@ -101,11 +100,11 @@ static function ( $b_code ); - $keep = array_merge($keep, $class_keep[0]); - $keep_signature = array_merge($keep_signature, $class_keep[1]); - $add_or_delete = array_merge($add_or_delete, $class_keep[2]); - $diff_map = array_merge($diff_map, $class_keep[3]); - $deletion_ranges = array_merge($deletion_ranges, $class_keep[4]); + $keep = [...$keep, ...$class_keep[0]]; + $keep_signature = [...$keep_signature, ...$class_keep[1]]; + $add_or_delete = [...$add_or_delete, ...$class_keep[2]]; + $diff_map = [...$diff_map, ...$class_keep[3]]; + $deletion_ranges = [...$deletion_ranges, ...$class_keep[4]]; } } elseif ($diff_elem->type === DiffElem::TYPE_REMOVE) { if ($diff_elem->old instanceof PhpParser\Node\Stmt\Use_ diff --git a/src/Psalm/Internal/EventDispatcher.php b/src/Psalm/Internal/EventDispatcher.php index acf8378f7bc..f8031e3934f 100644 --- a/src/Psalm/Internal/EventDispatcher.php +++ b/src/Psalm/Internal/EventDispatcher.php @@ -39,7 +39,6 @@ use Psalm\Plugin\EventHandler\StringInterpreterInterface; use Psalm\Type\Atomic\TLiteralString; -use function array_merge; use function count; use function is_bool; use function is_subclass_of; @@ -416,7 +415,7 @@ public function dispatchAddTaints(AddRemoveTaintsEvent $event): array $added_taints = []; foreach ($this->add_taints_checks as $handler) { - $added_taints = array_merge($added_taints, $handler::addTaints($event)); + $added_taints = [...$added_taints, ...$handler::addTaints($event)]; } return $added_taints; @@ -430,7 +429,7 @@ public function dispatchRemoveTaints(AddRemoveTaintsEvent $event): array $removed_taints = []; foreach ($this->remove_taints_checks as $handler) { - $removed_taints = array_merge($removed_taints, $handler::removeTaints($event)); + $removed_taints = [...$removed_taints, ...$handler::removeTaints($event)]; } return $removed_taints; diff --git a/src/Psalm/Internal/IncludeCollector.php b/src/Psalm/Internal/IncludeCollector.php index 617b2b72fb9..317f2a3aba5 100644 --- a/src/Psalm/Internal/IncludeCollector.php +++ b/src/Psalm/Internal/IncludeCollector.php @@ -3,7 +3,6 @@ namespace Psalm\Internal; use function array_diff; -use function array_merge; use function array_unique; use function array_values; use function get_included_files; @@ -38,7 +37,7 @@ public function runAndCollect(callable $f) $included = array_diff($after, $before); - $this->included_files = array_values(array_unique(array_merge($this->included_files, $included))); + $this->included_files = array_values(array_unique([...$this->included_files, ...$included])); return $ret; } diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php index 509c4f6907a..5583040e62b 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php @@ -52,7 +52,6 @@ use UnexpectedValueException; use function array_keys; -use function array_merge; use function array_pop; use function array_search; use function count; @@ -463,7 +462,7 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal if ($classlike_storage && !$classlike_storage->is_trait) { - $storage->internal = array_merge($classlike_storage->internal, $storage->internal); + $storage->internal = [...$classlike_storage->internal, ...$storage->internal]; } if ($doc_comment) { diff --git a/src/Psalm/Internal/Provider/FileReferenceProvider.php b/src/Psalm/Internal/Provider/FileReferenceProvider.php index 08636f673ba..65afc1ff2c3 100644 --- a/src/Psalm/Internal/Provider/FileReferenceProvider.php +++ b/src/Psalm/Internal/Provider/FileReferenceProvider.php @@ -384,10 +384,7 @@ private function calculateFilesReferencingFile(Codebase $codebase, string $file) if (isset(self::$nonmethod_references_to_classes[$file_class_lc])) { $new_files = array_keys(self::$nonmethod_references_to_classes[$file_class_lc]); - $referenced_files = array_merge( - $referenced_files, - $new_files - ); + $referenced_files = [...$referenced_files, ...$new_files]; } if (isset(self::$method_references_to_classes[$file_class_lc])) { @@ -421,10 +418,10 @@ private function calculateFilesInheritingFile(Codebase $codebase, string $file): foreach ($file_classes as $file_class_lc => $_) { if (isset(self::$files_inheriting_classes[$file_class_lc])) { - $referenced_files = array_merge( - $referenced_files, - array_keys(self::$files_inheriting_classes[$file_class_lc]) - ); + $referenced_files = [ + ...$referenced_files, + ...array_keys(self::$files_inheriting_classes[$file_class_lc]) + ]; } } @@ -1162,10 +1159,10 @@ public function addClassMethodLocations(array $references): void { foreach ($references as $referenced_member_id => $locations) { if (isset(self::$class_method_locations[$referenced_member_id])) { - self::$class_method_locations[$referenced_member_id] = array_merge( - self::$class_method_locations[$referenced_member_id], - $locations - ); + self::$class_method_locations[$referenced_member_id] = [ + ...self::$class_method_locations[$referenced_member_id], + ...$locations + ]; } else { self::$class_method_locations[$referenced_member_id] = $locations; } @@ -1180,10 +1177,10 @@ public function addClassPropertyLocations(array $references): void { foreach ($references as $referenced_member_id => $locations) { if (isset(self::$class_property_locations[$referenced_member_id])) { - self::$class_property_locations[$referenced_member_id] = array_merge( - self::$class_property_locations[$referenced_member_id], - $locations - ); + self::$class_property_locations[$referenced_member_id] = [ + ...self::$class_property_locations[$referenced_member_id], + ...$locations + ]; } else { self::$class_property_locations[$referenced_member_id] = $locations; } @@ -1198,10 +1195,10 @@ public function addClassLocations(array $references): void { foreach ($references as $referenced_member_id => $locations) { if (isset(self::$class_locations[$referenced_member_id])) { - self::$class_locations[$referenced_member_id] = array_merge( - self::$class_locations[$referenced_member_id], - $locations - ); + self::$class_locations[$referenced_member_id] = [ + ...self::$class_locations[$referenced_member_id], + ...$locations + ]; } else { self::$class_locations[$referenced_member_id] = $locations; } diff --git a/src/Psalm/Internal/Stubs/Generator/ClassLikeStubGenerator.php b/src/Psalm/Internal/Stubs/Generator/ClassLikeStubGenerator.php index d6efd715654..4ea25eabbe6 100644 --- a/src/Psalm/Internal/Stubs/Generator/ClassLikeStubGenerator.php +++ b/src/Psalm/Internal/Stubs/Generator/ClassLikeStubGenerator.php @@ -39,11 +39,7 @@ public static function getClassLikeNode( string $classlike_name ) : PhpParser\Node\Stmt\ClassLike { $subnodes = [ - 'stmts' => array_merge( - self::getConstantNodes($codebase, $storage), - self::getPropertyNodes($storage), - self::getMethodNodes($storage) - ) + 'stmts' => [...self::getConstantNodes($codebase, $storage), ...self::getPropertyNodes($storage), ...self::getMethodNodes($storage)] ]; $docblock = new ParsedDocblock('', []); diff --git a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php index ab770007085..5b3341d5570 100644 --- a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php +++ b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php @@ -102,26 +102,23 @@ public static function replace( $had_template = false; foreach ($original_atomic_types as $key => $atomic_type) { - $atomic_types = array_merge( - $atomic_types, - self::handleAtomicStandin( - $atomic_type, - $key, - $template_result, - $codebase, - $statements_analyzer, - $input_type, - $input_arg_offset, - $calling_class, - $calling_function, - $replace, - $add_lower_bound, - $bound_equality_classlike, - $depth, - count($original_atomic_types) === 1, - $had_template - ) - ); + $atomic_types = [...$atomic_types, ...self::handleAtomicStandin( + $atomic_type, + $key, + $template_result, + $codebase, + $statements_analyzer, + $input_type, + $input_arg_offset, + $calling_class, + $calling_function, + $replace, + $add_lower_bound, + $bound_equality_classlike, + $depth, + count($original_atomic_types) === 1, + $had_template + )]; } if ($replace) { diff --git a/src/Psalm/Internal/Type/TypeExpander.php b/src/Psalm/Internal/Type/TypeExpander.php index 9fb1230a07b..babaf30aa77 100644 --- a/src/Psalm/Internal/Type/TypeExpander.php +++ b/src/Psalm/Internal/Type/TypeExpander.php @@ -98,7 +98,7 @@ public static function expandUnion( $had_split_values = true; } - $new_return_type_parts = array_merge($new_return_type_parts, $parts); + $new_return_type_parts = [...$new_return_type_parts, ...$parts]; } if ($had_split_values) { @@ -254,10 +254,10 @@ public static function expandAtomic( $class_storage = $codebase->classlike_storage_provider->get($return_type->fq_classlike_name); if (strpos($return_type->const_name, '*') !== false) { - $matching_constants = array_merge( - array_keys($class_storage->constants), - array_keys($class_storage->enum_cases) - ); + $matching_constants = [ + ...array_keys($class_storage->constants), + ...array_keys($class_storage->enum_cases) + ]; $const_name_part = substr($return_type->const_name, 0, -1); @@ -349,10 +349,10 @@ public static function expandAtomic( $throw_on_unresolvable_constant, ); - $recursively_fleshed_out_types = array_merge( - $more_recursively_fleshed_out_types, - $recursively_fleshed_out_types - ); + $recursively_fleshed_out_types = [ + ...$more_recursively_fleshed_out_types, + ...$recursively_fleshed_out_types + ]; } return $recursively_fleshed_out_types; @@ -747,10 +747,7 @@ private static function expandConditional( $throw_on_unresolvable_constant, ); - $if_conditional_return_types = array_merge( - $if_conditional_return_types, - $candidate_types - ); + $if_conditional_return_types = [...$if_conditional_return_types, ...$candidate_types]; } $else_conditional_return_types = []; @@ -770,10 +767,7 @@ private static function expandConditional( $throw_on_unresolvable_constant, ); - $else_conditional_return_types = array_merge( - $else_conditional_return_types, - $candidate_types - ); + $else_conditional_return_types = [...$else_conditional_return_types, ...$candidate_types]; } if ($assertion && $return_type->param_name === (string) $return_type->if_type) { @@ -811,10 +805,7 @@ private static function expandConditional( } } - $all_conditional_return_types = array_merge( - $if_conditional_return_types, - $else_conditional_return_types - ); + $all_conditional_return_types = [...$if_conditional_return_types, ...$else_conditional_return_types]; $number_of_types = count($all_conditional_return_types); // we filter TNever that have no bearing on the return type @@ -999,7 +990,7 @@ private static function expandKeyOfValueOf( $expand_templates, $throw_on_unresolvable_constant, ); - $type_atomics = array_merge($type_atomics, $type_param_expanded); + $type_atomics = [...$type_atomics, ...$type_param_expanded]; continue; } diff --git a/src/Psalm/Internal/Type/TypeParser.php b/src/Psalm/Internal/Type/TypeParser.php index 614be8b3c5c..71a2ddd1e58 100644 --- a/src/Psalm/Internal/Type/TypeParser.php +++ b/src/Psalm/Internal/Type/TypeParser.php @@ -71,7 +71,6 @@ use function array_key_first; use function array_keys; use function array_map; -use function array_merge; use function array_pop; use function array_shift; use function array_unique; @@ -493,7 +492,7 @@ public static function getComputedIntsFromMask(array $potential_ints): array } } - $potential_values = array_merge($new_values, $potential_values); + $potential_values = [...$new_values, ...$potential_values]; } array_unshift($potential_values, 0); diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index fb0f7d351c0..9d004d386ce 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -44,7 +44,6 @@ use Psalm\Type\Atomic\TTrue; use function array_filter; -use function array_merge; use function array_unique; use function count; use function get_class; @@ -494,13 +493,13 @@ public function toNamespacedString( } if (count($literal_ints) <= 3 && !$has_non_literal_int) { - $other_types = array_merge($other_types, $literal_ints); + $other_types = [...$other_types, ...$literal_ints]; } else { $other_types[] = 'int'; } if (count($literal_strings) <= 3 && !$has_non_literal_string) { - $other_types = array_merge($other_types, $literal_strings); + $other_types = [...$other_types, ...$literal_strings]; } else { $other_types[] = 'string'; } diff --git a/tests/Internal/CallMapTest.php b/tests/Internal/CallMapTest.php index 02c9fdba55e..162ea0d4ed8 100644 --- a/tests/Internal/CallMapTest.php +++ b/tests/Internal/CallMapTest.php @@ -285,9 +285,9 @@ public function testChangedAndRemovedFunctionsMustExist(array $deltaFiles): arra ); $newFunctions = array_diff($newFunctions, $removedFunctions); - $newFunctions = array_merge($newFunctions, $addedFunctions); + $newFunctions = [...$newFunctions, ...$addedFunctions]; $deletedFunctions = array_diff($deletedFunctions, $addedFunctions); - $deletedFunctions = array_merge($deletedFunctions, $removedFunctions); + $deletedFunctions = [...$deletedFunctions, ...$removedFunctions]; } return $deletedFunctions; } From 238b54abf884042e3739c7ae975767b43188344b Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Fri, 29 Jul 2022 22:00:21 -0400 Subject: [PATCH 2/4] Revert one particular replacement that confuses Psalm --- .../Analyzer/Statements/Expression/CallAnalyzer.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php index d624dbc92a7..ade1205332c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php @@ -824,10 +824,10 @@ public static function applyAssertionsToContext( if ($orred_rules) { if (isset($type_assertions[$assertion_var_id])) { - $type_assertions[$assertion_var_id] = [ - ...$type_assertions[$assertion_var_id], - ...[$orred_rules] - ]; + $type_assertions[$assertion_var_id] = array_merge( + $type_assertions[$assertion_var_id], + [$orred_rules] + ); } else { $type_assertions[$assertion_var_id] = [$orred_rules]; } From 7444ea8e2dccc206376ef6207fb48c88ad3d033f Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Fri, 29 Jul 2022 23:01:49 -0400 Subject: [PATCH 3/4] Simplify some methods to keep Psalm happy --- .../InstancePropertyAssignmentAnalyzer.php | 58 ++++--- .../Expression/AssignmentAnalyzer.php | 146 +++++++++++------- 2 files changed, 130 insertions(+), 74 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php index c70fcacbb79..cbbf381a410 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/InstancePropertyAssignmentAnalyzer.php @@ -1254,26 +1254,13 @@ private static function analyzeAtomicAssignment( false ); - if ($codebase->properties_to_rename) { - $declaring_property_id = strtolower($declaring_property_class) . '::$' . $prop_name; - - foreach ($codebase->properties_to_rename as $original_property_id => $new_property_name) { - if ($declaring_property_id === $original_property_id) { - $file_manipulations = [ - new FileManipulation( - (int)$stmt->name->getAttribute('startFilePos'), - (int)$stmt->name->getAttribute('endFilePos') + 1, - $new_property_name - ) - ]; - - FileManipulationBuffer::add( - $statements_analyzer->getFilePath(), - $file_manipulations - ); - } - } - } + self::handlePropertyRenames( + $codebase, + $declaring_property_class, + $prop_name, + $stmt, + $statements_analyzer->getFilePath() + ); $declaring_class_storage = $codebase->classlike_storage_provider->get($declaring_property_class); @@ -1446,6 +1433,37 @@ private static function analyzeAtomicAssignment( ); } + private static function handlePropertyRenames( + Codebase $codebase, + string $declaring_property_class, + string $prop_name, + PropertyFetch $stmt, + string $file_path + ): void { + if (!$codebase->properties_to_rename) { + return; + } + + $declaring_property_id = strtolower($declaring_property_class) . '::$' . $prop_name; + + foreach ($codebase->properties_to_rename as $original_property_id => $new_property_name) { + if ($declaring_property_id === $original_property_id) { + $file_manipulations = [ + new FileManipulation( + (int)$stmt->name->getAttribute('startFilePos'), + (int)$stmt->name->getAttribute('endFilePos') + 1, + $new_property_name + ) + ]; + + FileManipulationBuffer::add( + $file_path, + $file_manipulations + ); + } + } + } + public static function getExpandedPropertyType( Codebase $codebase, string $fq_class_name, diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index ccc7369a74b..a6962744ce9 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -70,6 +70,7 @@ use Psalm\Node\Expr\VirtualAssign; use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent; use Psalm\Storage\Assertion\Falsy; +use Psalm\Storage\FileStorage; use Psalm\Type; use Psalm\Type\Atomic\TArray; use Psalm\Type\Atomic\TFalse; @@ -491,71 +492,21 @@ public static function analyze( ); } - if ($assign_var instanceof PhpParser\Node\Expr\Variable) { - self::analyzeAssignmentToVariable( - $statements_analyzer, - $codebase, + if (self::analyzeAssignment( $assign_var, - $assign_value, - $assign_value_type, - $var_id, - $context - ); - } elseif ($assign_var instanceof PhpParser\Node\Expr\List_ - || $assign_var instanceof PhpParser\Node\Expr\Array_ - ) { - self::analyzeDestructuringAssignment( $statements_analyzer, $codebase, - $assign_var, $assign_value, $assign_value_type, + $var_id, $context, $doc_comment, $extended_var_id, $var_comments, $removed_taints - ); - } elseif ($assign_var instanceof PhpParser\Node\Expr\ArrayDimFetch) { - ArrayAssignmentAnalyzer::analyze( - $statements_analyzer, - $assign_var, - $context, - $assign_value, - $assign_value_type - ); - } elseif ($assign_var instanceof PhpParser\Node\Expr\PropertyFetch) { - self::analyzePropertyAssignment( - $statements_analyzer, - $codebase, - $assign_var, - $context, - $assign_value, - $assign_value_type, - $var_id - ); - } elseif ($assign_var instanceof PhpParser\Node\Expr\StaticPropertyFetch && - $assign_var->class instanceof PhpParser\Node\Name + ) === false ) { - if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_var, $context) === false) { - return false; - } - - if ($context->check_classes) { - if (StaticPropertyAssignmentAnalyzer::analyze( - $statements_analyzer, - $assign_var, - $assign_value, - $assign_value_type, - $context - ) === false) { - return false; - } - } - - if ($var_id) { - $context->vars_possibly_in_scope[$var_id] = true; - } + return false; } if ($var_id && isset($context->vars_in_scope[$var_id])) { @@ -652,6 +603,93 @@ public static function analyze( return $assign_value_type; } + /** + * @param list $var_comments + * @param list $removed_taints + * @return null|false + */ + private static function analyzeAssignment( + \PhpParser\Node\Expr $assign_var, + StatementsAnalyzer $statements_analyzer, + Codebase $codebase, + ?\PhpParser\Node\Expr $assign_value, + Union $assign_value_type, + ?string $var_id, + Context $context, + ?\PhpParser\Comment\Doc $doc_comment, + ?string $extended_var_id, + array $var_comments, + array $removed_taints + ): ?bool { + if ($assign_var instanceof PhpParser\Node\Expr\Variable) { + self::analyzeAssignmentToVariable( + $statements_analyzer, + $codebase, + $assign_var, + $assign_value, + $assign_value_type, + $var_id, + $context + ); + } elseif ($assign_var instanceof PhpParser\Node\Expr\List_ + || $assign_var instanceof PhpParser\Node\Expr\Array_ + ) { + self::analyzeDestructuringAssignment( + $statements_analyzer, + $codebase, + $assign_var, + $assign_value, + $assign_value_type, + $context, + $doc_comment, + $extended_var_id, + $var_comments, + $removed_taints + ); + } elseif ($assign_var instanceof PhpParser\Node\Expr\ArrayDimFetch) { + ArrayAssignmentAnalyzer::analyze( + $statements_analyzer, + $assign_var, + $context, + $assign_value, + $assign_value_type + ); + } elseif ($assign_var instanceof PhpParser\Node\Expr\PropertyFetch) { + self::analyzePropertyAssignment( + $statements_analyzer, + $codebase, + $assign_var, + $context, + $assign_value, + $assign_value_type, + $var_id + ); + } elseif ($assign_var instanceof PhpParser\Node\Expr\StaticPropertyFetch && + $assign_var->class instanceof PhpParser\Node\Name + ) { + if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_var, $context) === false) { + return false; + } + + if ($context->check_classes) { + if (StaticPropertyAssignmentAnalyzer::analyze( + $statements_analyzer, + $assign_var, + $assign_value, + $assign_value_type, + $context + ) === false) { + return false; + } + } + + if ($var_id) { + $context->vars_possibly_in_scope[$var_id] = true; + } + } + return null; + } + public static function assignTypeFromVarDocblock( StatementsAnalyzer $statements_analyzer, PhpParser\Node $stmt, From c9e6b540500ec4173e90f8a8e618099aa9596d99 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Fri, 29 Jul 2022 23:08:11 -0400 Subject: [PATCH 4/4] CS fix --- .../Expression/AssignmentAnalyzer.php | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index a6962744ce9..c787b2443c9 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -3,6 +3,8 @@ namespace Psalm\Internal\Analyzer\Statements\Expression; use PhpParser; +use PhpParser\Comment\Doc; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\PropertyFetch; use Psalm\CodeLocation; @@ -70,7 +72,6 @@ use Psalm\Node\Expr\VirtualAssign; use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent; use Psalm\Storage\Assertion\Falsy; -use Psalm\Storage\FileStorage; use Psalm\Type; use Psalm\Type\Atomic\TArray; use Psalm\Type\Atomic\TFalse; @@ -493,18 +494,18 @@ public static function analyze( } if (self::analyzeAssignment( - $assign_var, - $statements_analyzer, - $codebase, - $assign_value, - $assign_value_type, - $var_id, - $context, - $doc_comment, - $extended_var_id, - $var_comments, - $removed_taints - ) === false + $assign_var, + $statements_analyzer, + $codebase, + $assign_value, + $assign_value_type, + $var_id, + $context, + $doc_comment, + $extended_var_id, + $var_comments, + $removed_taints + ) === false ) { return false; } @@ -609,14 +610,14 @@ public static function analyze( * @return null|false */ private static function analyzeAssignment( - \PhpParser\Node\Expr $assign_var, + Expr $assign_var, StatementsAnalyzer $statements_analyzer, Codebase $codebase, - ?\PhpParser\Node\Expr $assign_value, + ?Expr $assign_value, Union $assign_value_type, ?string $var_id, Context $context, - ?\PhpParser\Comment\Doc $doc_comment, + ?Doc $doc_comment, ?string $extended_var_id, array $var_comments, array $removed_taints