1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Use array destructuring when possible (#4221)

* list usage

* fix inversion
This commit is contained in:
orklah 2020-09-20 18:55:38 +02:00 committed by Daniil Gentili
parent 69be386072
commit f1aba254be
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
8 changed files with 12 additions and 20 deletions

View File

@ -208,8 +208,7 @@ class ReturnTypeCollector
} }
if ($type instanceof Type\Atomic\TArray) { if ($type instanceof Type\Atomic\TArray) {
$key_type_param = $type->type_params[0]; [$key_type_param, $value_type_param] = $type->type_params;
$value_type_param = $type->type_params[1];
if (!$key_type) { if (!$key_type) {
$key_type = clone $key_type_param; $key_type = clone $key_type_param;

View File

@ -537,8 +537,7 @@ class ForeachAnalyzer
continue; continue;
} }
$value_type_part = $iat->type_params[1]; [$key_type_part, $value_type_part] = $iat->type_params;
$key_type_part = $iat->type_params[0];
if (!$intersection_value_type) { if (!$intersection_value_type) {
$intersection_value_type = $value_type_part; $intersection_value_type = $value_type_part;
@ -835,8 +834,7 @@ class ForeachAnalyzer
$array_atomic_type = $array_atomic_type->getGenericArrayType(); $array_atomic_type = $array_atomic_type->getGenericArrayType();
} }
$key_type_part = $array_atomic_type->type_params[0]; [$key_type_part, $value_type_part] = $array_atomic_type->type_params;
$value_type_part = $array_atomic_type->type_params[1];
} else { } else {
if ($array_atomic_type instanceof Type\Atomic\TNamedObject if ($array_atomic_type instanceof Type\Atomic\TNamedObject
&& $codebase->classExists($array_atomic_type->value) && $codebase->classExists($array_atomic_type->value)

View File

@ -837,8 +837,8 @@ class CallAnalyzer
$union_comparison_result = new \Psalm\Internal\Type\Comparator\TypeComparisonResult(); $union_comparison_result = new \Psalm\Internal\Type\Comparator\TypeComparisonResult();
if (count($template_result->lower_bounds_unintersectable_types) > 1) { if (count($template_result->lower_bounds_unintersectable_types) > 1) {
$upper_bound_type = $template_result->lower_bounds_unintersectable_types[0]; [$upper_bound_type, $lower_bound_type]
$lower_bound_type = $template_result->lower_bounds_unintersectable_types[1]; = $template_result->lower_bounds_unintersectable_types;
} }
if (!UnionTypeComparator::isContainedBy( if (!UnionTypeComparator::isContainedBy(

View File

@ -561,8 +561,7 @@ class ArrayFetchAnalyzer
if (count($key_values) === 1) { if (count($key_values) === 1) {
$from_mixed_array = $type->type_params[1]->isMixed(); $from_mixed_array = $type->type_params[1]->isMixed();
$previous_key_type = $type->type_params[0]; [$previous_key_type, $previous_value_type] = $type->type_params;
$previous_value_type = $type->type_params[1];
// ok, type becomes an TKeyedArray // ok, type becomes an TKeyedArray
$array_type->removeType($type_string); $array_type->removeType($type_string);

View File

@ -37,8 +37,7 @@ class UnusedAssignmentRemover
CodeLocation $original_location CodeLocation $original_location
): void { ): void {
$search_result = $this->findAssignStmt($stmts, $var_id, $original_location); $search_result = $this->findAssignStmt($stmts, $var_id, $original_location);
$assign_stmt = $search_result[0]; [$assign_stmt, $assign_exp] = $search_result;
$assign_exp = $search_result[1];
$chain_assignment = false; $chain_assignment = false;
if ($assign_stmt !== null && $assign_exp !== null) { if ($assign_stmt !== null && $assign_exp !== null) {
@ -245,8 +244,7 @@ class UnusedAssignmentRemover
if ($stmt instanceof PhpParser\Node\Stmt\Expression) { if ($stmt instanceof PhpParser\Node\Stmt\Expression) {
$search_result = $this->findAssignExp($stmt->expr, $var_id, $original_location->raw_file_start); $search_result = $this->findAssignExp($stmt->expr, $var_id, $original_location->raw_file_start);
$target_exp = $search_result[0]; [$target_exp, $levels_taken] = $search_result;
$levels_taken = $search_result[1];
if ($target_exp !== null) { if ($target_exp !== null) {
$assign_exp_found = true; $assign_exp_found = true;

View File

@ -118,8 +118,7 @@ class ArrayReduceReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionReturn
return Type::getMixed(); return Type::getMixed();
} }
$carry_param = $closure_atomic_type->params[0]; [$carry_param, $item_param] = $closure_atomic_type->params;
$item_param = $closure_atomic_type->params[1];
if ($carry_param->type if ($carry_param->type
&& ( && (

View File

@ -366,8 +366,7 @@ class CallableTypeComparator
return 'not-callable'; return 'not-callable';
} }
$lhs = $input_type_part->properties[0]; [$lhs, $rhs] = $input_type_part->properties;
$rhs = $input_type_part->properties[1];
$rhs_low_info = $rhs->hasMixed() || $rhs->hasScalar(); $rhs_low_info = $rhs->hasMixed() || $rhs->hasScalar();

View File

@ -293,8 +293,8 @@ class ErrorBaselineTest extends TestCase
/** @var \DOMElement[] $files */ /** @var \DOMElement[] $files */
$files = $baselineDocument->getElementsByTagName('files')[0]->childNodes; $files = $baselineDocument->getElementsByTagName('files')[0]->childNodes;
$file1 = $files[0]; [$file1, $file2] = $files;
$file2 = $files[1];
$this->assertSame('sample/sample-file.php', $file1->getAttribute('src')); $this->assertSame('sample/sample-file.php', $file1->getAttribute('src'));
$this->assertSame('sample/sample-file2.php', $file2->getAttribute('src')); $this->assertSame('sample/sample-file2.php', $file2->getAttribute('src'));