Skip to content

Commit 438096e

Browse files
committed
mFixes
1 parent cdd7278 commit 438096e

File tree

6 files changed

+58
-43
lines changed

6 files changed

+58
-43
lines changed

src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php

+2-15
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,7 @@ public static function checkArgumentMatches(
127127
&& $param_type
128128
&& $param_type->hasArray()
129129
) {
130-
foreach ($param_type->getArrays() as $array_type) {
131-
if ($array_type instanceof TKeyedArray && $array_type->is_list) {
132-
$param_types []= $array_type->getGenericValueType();
133-
} elseif ($array_type instanceof TArray) {
134-
$param_types []= $array_type->type_params[1];
135-
}
136-
}
130+
$param_types = $param_type->getArrayValueTypes();
137131
} elseif ($param_type) {
138132
$param_types = [$param_type];
139133
}
@@ -1251,14 +1245,7 @@ private static function verifyExplicitParam(
12511245
} elseif ($param_type_part instanceof TCallable) {
12521246
$can_be_callable_like_array = false;
12531247

1254-
foreach ($param_type->getArrays() as $param_array_type) {
1255-
$row_type = null;
1256-
if ($param_array_type instanceof TArray) {
1257-
$row_type = $param_array_type->type_params[1];
1258-
} elseif ($param_array_type instanceof TKeyedArray) {
1259-
$row_type = $param_array_type->getGenericValueType();
1260-
}
1261-
1248+
foreach ($param_type->getArrayValueTypes() as $row_type) {
12621249
if ($row_type &&
12631250
($row_type->hasMixed() || $row_type->hasString())
12641251
) {

src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArrayFunctionArgumentsAnalyzer.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,7 @@ private static function checkClosureTypeArgs(
951951
continue;
952952
}
953953

954-
foreach ($array_arg_types[$i]->getArrays() as $array_arg_type) {
955-
$input_type = $array_arg_type instanceof TKeyedArray
956-
? $array_arg_type->getGenericValueType()
957-
: $array_arg_type->type_params[1];
958-
954+
foreach ($array_arg_types[$i]->getArrayValueTypes() as $input_type) {
959955
if ($input_type->hasMixed()) {
960956
continue;
961957
}

src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,15 @@ public static function analyze(
229229
null,
230230
);
231231

232-
if ($stmt->dim && $stmt_var_type->hasArray()) {
233-
$array_type = $stmt_var_type->getArray();
234-
235-
if ($array_type instanceof TClassStringMap) {
236-
$array_value_type = Type::getMixed();
237-
} elseif ($array_type instanceof TArray) {
238-
$array_value_type = $array_type->type_params[1];
239-
} else {
240-
$array_value_type = $array_type->getGenericValueType();
232+
if ($stmt->dim && ($array_value_type = $stmt_var_type->getArrayValueTypes())) {
233+
$has_non_mixed = false;
234+
foreach ($array_value_type as $t) {
235+
if (!$t->isMixed()) {
236+
$has_non_mixed = true;
237+
break;
238+
}
241239
}
242-
243-
if ($context->inside_assignment || !$array_value_type->isMixed()) {
240+
if ($context->inside_assignment || $has_non_mixed) {
244241
$can_store_result = true;
245242
}
246243
}

src/Psalm/Internal/Codebase/InternalCallMapHandler.php

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Psalm\NodeTypeProvider;
1414
use Psalm\Storage\FunctionLikeParameter;
1515
use Psalm\Type;
16-
use Psalm\Type\Atomic\TArray;
1716
use Psalm\Type\Atomic\TCallable;
1817
use Psalm\Type\Atomic\TKeyedArray;
1918
use Psalm\Type\TaintKind;

src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -841,16 +841,9 @@ private static function improveParamsFromDocblock(
841841
}
842842

843843
if (!$docblock_param_variadic && $storage_param->is_variadic) {
844-
$array_param_types = [];
845-
foreach ($new_param_type->getArrays() as $array_type) {
846-
if ($array_type instanceof TKeyedArray) {
847-
$array_param_types []= $array_type->getGenericValueType();
848-
} else {
849-
$array_param_types []= $array_type->type_params[1];
850-
}
851-
}
844+
$array_param_types = $new_param_type->getArrayValueTypes();
852845
if ($array_param_types) {
853-
$new_param_type = new Union($array_param_types);
846+
$new_param_type = Type::combineUnionTypeArray($array_param_types, $codebase);
854847
}
855848
}
856849

src/Psalm/Type/UnionTrait.php

+45-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InvalidArgumentException;
88
use Psalm\CodeLocation;
99
use Psalm\Codebase;
10+
use Psalm\Internal\Type\TypeCombiner;
1011
use Psalm\Internal\TypeVisitor\CanContainObjectTypeVisitor;
1112
use Psalm\Internal\TypeVisitor\ClasslikeReplacer;
1213
use Psalm\Internal\TypeVisitor\ContainsClassLikeVisitor;
@@ -16,6 +17,7 @@
1617
use Psalm\Internal\TypeVisitor\TypeScanner;
1718
use Psalm\StatementsSource;
1819
use Psalm\Storage\FileStorage;
20+
use Psalm\Type;
1921
use Psalm\Type\Atomic\TArray;
2022
use Psalm\Type\Atomic\TCallable;
2123
use Psalm\Type\Atomic\TClassString;
@@ -411,19 +413,59 @@ public function hasArray(): bool
411413
}
412414

413415
/**
414-
* @return list<(TArray|TKeyedArray)>
416+
* @return list<(TArray|TKeyedArray|TClassStringMap)>
415417
*/
416418
public function getArrays(): array
417419
{
418420
$result = [];
419421
foreach ($this->types as $t) {
420-
if ($t instanceof TKeyedArray || $t instanceof TArray) {
422+
if ($t instanceof TKeyedArray || $t instanceof TArray || $t instanceof TClassStringMap) {
421423
$result []= $t;
422424
}
423425
}
424426
return $result;
425427
}
426428

429+
430+
/**
431+
* @return list<Union>
432+
*/
433+
public function getArrayKeyTypes(): array
434+
{
435+
$result = [];
436+
foreach ($this->types as $t) {
437+
if ($t instanceof TKeyedArray) {
438+
$result []= $t->getGenericKeyType();
439+
} elseif ($t instanceof TArray) {
440+
$result []= $t->type_params[0];
441+
} elseif ($t instanceof TClassStringMap) {
442+
$result []= Type::getClassString($t->as_type?->value ?? 'object');
443+
}
444+
}
445+
return $result;
446+
}
447+
448+
/**
449+
* @return list<Union>
450+
*/
451+
public function getArrayValueTypes(): array
452+
{
453+
$result = [];
454+
foreach ($this->types as $t) {
455+
if ($t instanceof TKeyedArray) {
456+
$result []= $t->getGenericValueType();
457+
} elseif ($t instanceof TArray) {
458+
$result []= $t->type_params[1];
459+
} elseif ($t instanceof TClassStringMap) {
460+
if ($t->as_type) {
461+
$result []= new Union([$t->as_type]);
462+
} else {
463+
$result []= Type::getObject();
464+
}
465+
}
466+
}
467+
return $result;
468+
}
427469
/**
428470
* @psalm-mutation-free
429471
*/
@@ -1090,6 +1132,7 @@ public function isArray(): bool
10901132
foreach ($this->types as $t) {
10911133
if (!$t instanceof TKeyedArray
10921134
&& !$t instanceof TArray
1135+
&& !$t instanceof TClassStringMap
10931136
) {
10941137
return false;
10951138
}

0 commit comments

Comments
 (0)