Skip to content

Commit

Permalink
Merge pull request #10306 from nicelocal/rector_5.x
Browse files Browse the repository at this point in the history
Backport rector fixes to 5.x
  • Loading branch information
orklah committed Oct 22, 2023
2 parents 915d801 + 4c656f0 commit c7d7b48
Show file tree
Hide file tree
Showing 38 changed files with 63 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
$removed_foreign_functions
);

uksort($new_local, fn($a, $b) => strtolower($a) <=> strtolower($b));
uksort($new_local, static fn($a, $b) => strtolower($a) <=> strtolower($b));

foreach ($new_local as $name => $data) {
if (!is_array($data)) {
Expand Down
2 changes: 1 addition & 1 deletion examples/plugins/ClassUnqualifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function afterClassLikeExistenceCheck(
$new_candidate_type = implode(
'',
array_map(
fn($f) => $f[0],
static fn($f) => $f[0],
$type_tokens,
),
);
Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
use UnexpectedValueException;

use function array_combine;
use function array_merge;
use function array_pop;
use function array_reverse;
use function array_values;
Expand Down Expand Up @@ -1917,7 +1916,7 @@ public function getCompletionItemsForClassishThing(
);
}

$completion_items = array_merge($completion_items, array_values($pseudo_property_types));
$completion_items = [...$completion_items, ...array_values($pseudo_property_types)];

foreach ($class_storage->declaring_property_ids as $property_name => $declaring_class) {
$property_storage = $this->properties->getStorage(
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ protected function checkTemplateParams(
&& $storage->template_types
&& $storage->template_covariants
&& ($local_offset
= array_search($t->param_name, array_keys($storage->template_types)))
= array_search($t->param_name, array_keys($storage->template_types), true))
!== false
&& !empty($storage->template_covariants[$local_offset])
) {
Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Analyzer/CommentAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Psalm\Type\Union;
use UnexpectedValueException;

use function array_merge;
use function count;
use function is_string;
use function preg_match;
Expand Down Expand Up @@ -400,7 +399,7 @@ public static function splitDocLine(string $return_block): array
$remaining = trim(preg_replace('@^[ \t]*\* *@m', ' ', substr($return_block, $i + 1)));

if ($remaining) {
return array_merge([rtrim($type)], preg_split('/\s+/', $remaining) ?: []);
return [rtrim($type), ...preg_split('/\s+/', $remaining) ?: []];
}

return [$type];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ private static function getExtendedType(
): ?Union {
if ($calling_class === $template_class) {
if (isset($class_template_types[$template_name]) && $calling_type_params) {
$offset = array_search($template_name, array_keys($class_template_types));
$offset = array_search($template_name, array_keys($class_template_types), true);

if ($offset !== false && isset($calling_type_params[$offset])) {
return $calling_type_params[$offset];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ private static function handleTemplatedMixins(
$param_position = array_search(
$mixin->param_name,
$template_type_keys,
true,
);

if ($param_position !== false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public static function castIntAttempt(
// todo: emit error here
}

$valid_types = array_merge($valid_ints, $castable_types);
$valid_types = [...$valid_ints, ...$castable_types];

if (!$valid_types) {
$int_type = Type::getInt();
Expand Down Expand Up @@ -661,7 +661,7 @@ public static function castFloatAttempt(
// todo: emit error here
}

$valid_types = array_merge($valid_floats, $castable_types);
$valid_types = [...$valid_floats, ...$castable_types];

if (!$valid_types) {
$float_type = Type::getFloat();
Expand Down Expand Up @@ -804,10 +804,7 @@ public static function castStringAttempt(
$parent_nodes = array_merge($return_type->parent_nodes, $parent_nodes);
}

$castable_types = array_merge(
$castable_types,
array_values($return_type->getAtomicTypes()),
);
$castable_types = [...$castable_types, ...array_values($return_type->getAtomicTypes())];

continue 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ public static function localizePropertyType(
$position = array_search(
$param_name,
array_keys($property_class_storage->template_types),
true,
);
}

Expand Down Expand Up @@ -1000,7 +1001,7 @@ private static function handleEnumName(
empty($relevant_enum_case_names)
? Type::getNonEmptyString()
: new Union(array_map(
fn(string $name): TString => Type::getAtomicStringFromLiteral($name),
static fn(string $name): TString => Type::getAtomicStringFromLiteral($name),
$relevant_enum_case_names,
)),
);
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ public function checkUnreferencedVars(array $stmts, Context $context): void
}

if ($function_storage) {
$param_index = array_search(substr($var_id, 1), array_keys($function_storage->param_lookup));
$param_index = array_search(substr($var_id, 1), array_keys($function_storage->param_lookup), true);
if ($param_index !== false) {
$param = $function_storage->params[$param_index];

Expand Down
16 changes: 8 additions & 8 deletions src/Psalm/Internal/Codebase/Populator.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,10 +931,10 @@ protected function inheritMethodsFromParent(
if ($parent_storage->is_trait
&& $storage->trait_alias_map
) {
$aliased_method_names = array_merge(
$aliased_method_names,
array_keys($storage->trait_alias_map, $method_name_lc, true),
);
$aliased_method_names = [
...$aliased_method_names,
...array_keys($storage->trait_alias_map, $method_name_lc, true),
];
}

foreach ($aliased_method_names as $aliased_method_name) {
Expand Down Expand Up @@ -1001,10 +1001,10 @@ protected function inheritMethodsFromParent(
if ($parent_storage->is_trait
&& $storage->trait_alias_map
) {
$aliased_method_names = array_merge(
$aliased_method_names,
array_keys($storage->trait_alias_map, $method_name_lc, true),
);
$aliased_method_names = [
...$aliased_method_names,
...array_keys($storage->trait_alias_map, $method_name_lc, true),
];
}

foreach ($aliased_method_names as $aliased_method_name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FunctionDocblockManipulator
private static array $manipulators = [];

/** @var Closure|Function_|ClassMethod|ArrowFunction */
private $stmt;
private FunctionLike $stmt;

private int $docblock_start;

Expand Down Expand Up @@ -415,7 +415,7 @@ private function getDocblock(): string
$modified_docblock = true;
$inferredThrowsClause = array_reduce(
$this->throwsExceptions,
fn(string $throwsClause, string $exception) => $throwsClause === ''
static fn(string $throwsClause, string $exception) => $throwsClause === ''
? $exception
: $throwsClause.'|'.$exception,
'',
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/LanguageServer/LanguageServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function (Message $msg): Generator {

$this->protocolReader->on(
'readMessageGroup',
function (): void {
static function (): void {
//$this->verboseLog('Received message group');
//$this->doAnalysis();
},
Expand Down Expand Up @@ -765,7 +765,7 @@ function (IssueData $issue_data): Diagnostic {
return $diagnostic;
},
array_filter(
array_map(function (IssueData $issue_data) use (&$issue_baseline) {
array_map(static function (IssueData $issue_data) use (&$issue_baseline) {
if (empty($issue_baseline)) {
return $issue_data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$array_arg_types = array_map(null, ...$array_arg_types);
$array_arg_types = array_map(
/** @param non-empty-array<?Union> $sub */
function (array $sub) use ($null) {
static function (array $sub) use ($null) {
$sub = array_map(
fn(?Union $t) => $t ?? $null,
static fn(?Union $t) => $t ?? $null,
$sub,
);
return new Union([new TKeyedArray($sub, null, null, true)]);
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ private static function reconcileNonEmptyCountable(
$existing_var_type->removeType('array');
$existing_var_type->addType($array_atomic_type->setProperties(
array_map(
fn(Union $union) => $union->setPossiblyUndefined(false),
static fn(Union $union) => $union->setPossiblyUndefined(false),
$array_atomic_type->properties,
),
));
Expand Down Expand Up @@ -806,7 +806,7 @@ private static function reconcileExactlyCountable(
$existing_var_type->removeType('array');
$existing_var_type->addType($array_atomic_type->setProperties(
array_map(
fn(Union $union) => $union->setPossiblyUndefined(false),
static fn(Union $union) => $union->setPossiblyUndefined(false),
$array_atomic_type->properties,
),
));
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Type/TemplateInferredTypeReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public static function replace(
)->freeze();
}

$atomic_types = array_merge($types, $new_types);
$atomic_types = [...$types, ...$new_types];
if (!$atomic_types) {
throw new UnexpectedValueException('This array should be full');
}
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,7 @@ public static function getMappedGenericTypeParams(
$old_params_offset = (int) array_search(
$template->param_name,
array_keys($input_class_storage->template_types),
true,
);

$candidate_param_types[] = ($input_type_params[$old_params_offset] ?? Type::getMixed())
Expand Down
7 changes: 5 additions & 2 deletions src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,11 @@ private static function getTypeFromGenericTree(
}
assert(count($parse_tree->children) === 2);

$get_int_range_bound = function (ParseTree $parse_tree, Union $generic_param, string $bound_name): ?int {
$get_int_range_bound = static function (
ParseTree $parse_tree,
Union $generic_param,
string $bound_name
): ?int {
if (!$parse_tree instanceof Value
|| count($generic_param->getAtomicTypes()) > 1
|| (!$generic_param->getSingleAtomic() instanceof TLiteralInt
Expand All @@ -970,7 +974,6 @@ private static function getTypeFromGenericTree(
"Invalid type \"{$generic_param->getId()}\" as int $bound_name boundary",
);
}

$generic_param_atomic = $generic_param->getSingleAtomic();
return $generic_param_atomic instanceof TLiteralInt ? $generic_param_atomic->value : null;
};
Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/IssueBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static function isSuppressed(CodeIssue $e, array $suppressed_issues = [])
return true;
}

$suppressed_issue_position = array_search($issue_type, $suppressed_issues);
$suppressed_issue_position = array_search($issue_type, $suppressed_issues, true);

if ($suppressed_issue_position !== false) {
if (is_int($suppressed_issue_position)) {
Expand All @@ -203,7 +203,7 @@ public static function isSuppressed(CodeIssue $e, array $suppressed_issues = [])
$parent_issue_type = Config::getParentIssueType($issue_type);

if ($parent_issue_type) {
$suppressed_issue_position = array_search($parent_issue_type, $suppressed_issues);
$suppressed_issue_position = array_search($parent_issue_type, $suppressed_issues, true);

if ($suppressed_issue_position !== false) {
if (is_int($suppressed_issue_position)) {
Expand All @@ -216,7 +216,7 @@ public static function isSuppressed(CodeIssue $e, array $suppressed_issues = [])

$suppress_all_position = $config->disable_suppress_all
? false
: array_search('all', $suppressed_issues);
: array_search('all', $suppressed_issues, true);

if ($suppress_all_position !== false) {
if (is_int($suppress_all_position)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class AfterMethodCallAnalysisEvent
/**
* @var MethodCall|StaticCall
*/
private $expr;
private Expr $expr;
private string $method_id;
private string $appearing_method_id;
private string $declaring_method_id;
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Report/ByIssueLevelAndTypeReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private function sortIssuesByLevelAndType(): void
{
usort(
$this->issues_data,
fn(IssueData $left, IssueData $right): int => [$left->error_level > 0, -$left->error_level,
static fn(IssueData $left, IssueData $right): int => [$left->error_level > 0, -$left->error_level,
$left->type, $left->file_path, $left->file_name, $left->line_from]
<=> [$right->error_level > 0, -$right->error_level, $right->type, $right->file_path, $right->file_name,
$right->line_from],
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Report/CountReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function create(): string
$issue_type_counts[$issue_data->type] = 1;
}
}
uksort($issue_type_counts, function (string $a, string $b) use ($issue_type_counts): int {
uksort($issue_type_counts, static function (string $a, string $b) use ($issue_type_counts): int {
$cmp_result = $issue_type_counts[$a] <=> $issue_type_counts[$b];
if ($cmp_result === 0) {
return $a <=> $b;
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Storage/FunctionLikeStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function getHoverMarkdown(): string
$params = count($this->params) > 0 ? "\n" . implode(
",\n",
array_map(
function (FunctionLikeParameter $param): string {
static function (FunctionLikeParameter $param): string {
$realType = $param->type ?: 'mixed';
return " {$realType} \${$param->name}";
},
Expand Down Expand Up @@ -289,7 +289,7 @@ public function getCompletionSignature(): string
$symbol_text = 'function ' . $this->cased_name . '(' . implode(
',',
array_map(
fn(FunctionLikeParameter $param): string => ($param->type ?: 'mixed') . ' $' . $param->name,
static fn(FunctionLikeParameter $param): string => ($param->type ?: 'mixed') . ' $' . $param->name,
$this->params,
),
) . ') : ' . ($this->return_type ?: 'mixed');
Expand Down
4 changes: 1 addition & 3 deletions src/Psalm/Type/Atomic/TClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Psalm\Type\Atomic;
use Psalm\Type\Union;

use function array_merge;

/**
* Represents a closure where we know the return type and params
*
Expand Down Expand Up @@ -131,6 +129,6 @@ public function replaceTemplateTypesWithStandins(

protected function getChildNodeKeys(): array
{
return array_merge(parent::getChildNodeKeys(), $this->getCallableChildNodeKeys());
return [...parent::getChildNodeKeys(), ...$this->getCallableChildNodeKeys()];
}
}
3 changes: 1 addition & 2 deletions src/Psalm/Type/Atomic/TGenericObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Psalm\Type\Atomic;
use Psalm\Type\Union;

use function array_merge;
use function count;
use function implode;
use function strrpos;
Expand Down Expand Up @@ -127,7 +126,7 @@ public function getAssertionString(): string

protected function getChildNodeKeys(): array
{
return array_merge(parent::getChildNodeKeys(), ['type_params']);
return [...parent::getChildNodeKeys(), 'type_params'];
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Psalm/Type/Atomic/TValueOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ private static function getValueTypeForNamedObject(array $cases, TNamedObject $a
}

return new Union(array_map(
function (EnumCaseStorage $case): Atomic {
assert($case->value !== null); // Backed enum must have a value
static function (EnumCaseStorage $case): Atomic {
assert($case->value !== null);
// Backed enum must have a value
return ConstantTypeResolver::getLiteralTypeFromScalarValue($case->value);
},
array_values($cases),
Expand Down

0 comments on commit c7d7b48

Please sign in to comment.