Skip to content

Commit

Permalink
More rector fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Oct 21, 2023
1 parent cddf6a9 commit 3b66272
Show file tree
Hide file tree
Showing 30 changed files with 40 additions and 40 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
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
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 @@ -180,7 +180,7 @@ public static function analyze(

if ($literal_concat) {
// Bypass opcache bug: https://github.com/php/php-src/issues/10635
(function (int $_): void {
(static function (int $_) : void {
})($combinations);
if (count($result_type_parts) === 0) {
throw new AssertionError("The number of parts cannot be 0!");
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 @@ -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
Original file line number Diff line number Diff line change
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
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
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ 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 +970,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
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
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
2 changes: 1 addition & 1 deletion tests/AsyncTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function assertArrayKeysAreStrings(array $array, string $message =
*/
public static function assertArrayKeysAreZeroOrString(array $array, string $message = ''): void
{
$isZeroOrString = /** @param mixed $key */ fn($key): bool => $key === 0 || is_string($key);
$isZeroOrString = /** @param mixed $key */ static fn($key): bool => $key === 0 || is_string($key);
$validKeys = array_filter($array, $isZeroOrString, ARRAY_FILTER_USE_KEY);
self::assertTrue(count($array) === count($validKeys), $message);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/CodebaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
? (string)$stmt->extends->getAttribute('resolvedName')
: '';
$storage->custom_metadata['implements'] = array_map(
fn(Name $aspect): string => (string)$aspect->getAttribute('resolvedName'),
static fn(Name $aspect): string => (string)$aspect->getAttribute('resolvedName'),
$stmt->implements,
);
$storage->custom_metadata['a'] = 'b';
Expand Down
2 changes: 1 addition & 1 deletion tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ public function testAllPossibleIssues(): void
* @param string $issue_name
* @return string
*/
fn($issue_name): string => '<' . $issue_name . ' errorLevel="suppress" />' . "\n",
static fn($issue_name): string => '<' . $issue_name . ' errorLevel="suppress" />' . "\n",
IssueHandler::getAllIssueTypes(),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ public static function getFunctionStorage(DynamicFunctionStorageProviderEvent $e
$custom_array_map_storage->return_type = self::createReturnType($all_expected_callables);
$custom_array_map_storage->params = [
...array_map(
function (TCallable $expected, int $offset) {
static function (TCallable $expected, int $offset) {
$t = new Union([$expected]);
$param = new FunctionLikeParameter('fn' . $offset, false, $t, $t);
$param->is_optional = false;

return $param;
},
$all_expected_callables,
Expand Down
2 changes: 1 addition & 1 deletion tests/DocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public function testShortcodesAreUnique(): void

$duplicate_shortcodes = array_filter(
$all_shortcodes,
fn($issues): bool => count($issues) > 1
static fn($issues): bool => count($issues) > 1
);

$this->assertEquals(
Expand Down
4 changes: 2 additions & 2 deletions tests/FileDiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testCode(
* @param array{0: int, 1: int, 2: int, 3: int} $arr
* @return array{0: int, 1: int}
*/
fn(array $arr): array => [$arr[2], $arr[3]],
static fn(array $arr): array => [$arr[2], $arr[3]],
$diff[3],
);

Expand Down Expand Up @@ -133,7 +133,7 @@ public function testPartialAstDiff(
* @param array{0: int, 1: int, 2: int, 3: int} $arr
* @return array{0: int, 1: int}
*/
fn(array $arr): array => [$arr[2], $arr[3]],
static fn(array $arr): array => [$arr[2], $arr[3]],
$diff[3],
);

Expand Down
2 changes: 1 addition & 1 deletion tests/TaintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,7 @@ public function multipleTaintIssuesAreDetected(string $code, array $expectedIssu
$this->analyzeFile($filePath, new Context(), false);

$actualIssueTypes = array_map(
fn(IssueData $issue): string => $issue->type . '{ ' . trim($issue->snippet) . ' }',
static fn(IssueData $issue): string => $issue->type . '{ ' . trim($issue->snippet) . ' }',
IssueBuffer::getIssuesDataForFile($filePath),
);
self::assertSame($expectedIssuesTypes, $actualIssueTypes);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public static function assertArrayKeysAreStrings(array $array, string $message =

public static function assertArrayKeysAreZeroOrString(array $array, string $message = ''): void
{
$isZeroOrString = /** @param mixed $key */ fn($key): bool => $key === 0 || is_string($key);
$isZeroOrString = /** @param mixed $key */ static fn($key): bool => $key === 0 || is_string($key);
$validKeys = array_filter($array, $isZeroOrString, ARRAY_FILTER_USE_KEY);
self::assertTrue(count($array) === count($validKeys), $message);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TypeComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getAllBasicTypes(): array
$basic_types['list{123}'] = true;

return array_map(
fn($type) => [$type],
static fn($type) => [$type],
array_keys($basic_types),
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/DestructiveAutoloader/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$GLOBALS[$key] = new Exception;
}

spl_autoload_register(function() {
spl_autoload_register(static function () {
// and destroy vars again
// this will run during scanning (?)
foreach ($GLOBALS as $key => $_) {
Expand Down
4 changes: 1 addition & 3 deletions tests/fixtures/SuicidalAutoloader/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use React\Promise\PromiseInterface as ReactPromise;
use Composer\InstalledVersions;

spl_autoload_register(function (string $className) {
spl_autoload_register(static function (string $className) {
$knownBadClasses = [
ReactPromise::class, // amphp/amp
ResourceBundle::class, // symfony/polyfill-php73
Expand All @@ -25,11 +25,9 @@
'Symfony\Component\String\s',
'Symfony\Component\Translation\t',
];

if (in_array($className, $knownBadClasses)) {
return;
}

$ex = new RuntimeException('Attempted to load ' . $className);
echo $ex->__toString() . "\n\n" . $ex->getTraceAsString() . "\n\n";
exit(70);
Expand Down

0 comments on commit 3b66272

Please sign in to comment.