Skip to content

Commit

Permalink
Merge pull request #8907 from danog/fix_array_merge
Browse files Browse the repository at this point in the history
Fix array_merge edge case
  • Loading branch information
orklah committed Dec 16, 2022
2 parents 5132de2 + 2c00c64 commit 4d0ddf6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 70 deletions.
69 changes: 4 additions & 65 deletions psalm-baseline.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@2b0e979ee9434afdeb9dd59a3219bf4029c62dc8">
<files psalm-version="dev-master@6eb37b9dc2321e4eaade9d3d2dca1aff6f2c0a8f">
<file src="examples/TemplateChecker.php">
<PossiblyUndefinedIntArrayOffset occurrences="2">
<code>$comment_block-&gt;tags['variablesfrom'][0]</code>
Expand Down Expand Up @@ -390,6 +390,9 @@
<InvalidArgument occurrences="1">
<code>$class_strings ?: null</code>
</InvalidArgument>
<RedundantCondition occurrences="2">
<code>$is_replace</code>
</RedundantCondition>
</file>
<file src="src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayReduceReturnTypeProvider.php">
<PossiblyUndefinedIntArrayOffset occurrences="1">
Expand Down Expand Up @@ -593,10 +596,6 @@
<ImpurePropertyAssignment occurrences="1">
<code>$key_type-&gt;possibly_undefined</code>
</ImpurePropertyAssignment>
<InvalidPropertyAssignmentValue occurrences="2">
<code>$fallback_params</code>
<code>$fallback_params</code>
</InvalidPropertyAssignmentValue>
<PossiblyUndefinedIntArrayOffset occurrences="3">
<code>$this-&gt;properties[0]</code>
<code>$this-&gt;properties[0]</code>
Expand Down Expand Up @@ -705,64 +704,4 @@
<code>$subNodes['expr']</code>
</PossiblyUndefinedStringArrayOffset>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ShellExec.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$parts</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/MatchArm.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$conds</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Name.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>self::prepareName($name)</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Case_.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$stmts</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Catch_.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$stmts</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Do_.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$stmts</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/ElseIf_.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$stmts</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Else_.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$stmts</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Finally_.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$stmts</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Namespace_.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$stmts</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/TryCatch.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$stmts</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/While_.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$stmts</code>
</InvalidPropertyAssignmentValue>
</file>
</files>
Expand Up @@ -22,7 +22,6 @@
use function count;
use function is_string;
use function max;
use function substr;

/**
* @internal
Expand All @@ -47,7 +46,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
return Type::getMixed();
}

$is_replace = substr($event->getFunctionId(), 6, 7) === 'replace';
$is_replace = $event->getFunctionId() === 'array_replace';

$inner_value_types = [];
$inner_key_types = [];
Expand Down Expand Up @@ -130,10 +129,18 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$class_strings[$key] = true;
}

if (!isset($generic_properties[$key]) || !$type->possibly_undefined) {
if (!isset($generic_properties[$key]) || (
!$type->possibly_undefined
&& !$unpacking_possibly_empty
&& $is_replace
)) {
if ($unpacking_possibly_empty) {
$type = $type->setPossiblyUndefined(true);
}
$generic_properties[$key] = $type;
} else {
$was_possibly_undefined = $generic_properties[$key]->possibly_undefined;
$was_possibly_undefined = $generic_properties[$key]->possibly_undefined
|| $unpacking_possibly_empty;

$generic_properties[$key] = Type::combineUnionTypes(
$generic_properties[$key],
Expand All @@ -147,7 +154,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
}
}

if (!$unpacked_type_part->is_list && !$unpacking_possibly_empty) {
if (!$unpacked_type_part->is_list) {
$all_nonempty_lists = false;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/ArrayFunctionCallTest.php
Expand Up @@ -211,6 +211,24 @@ function getInts(): array { return [123]; }
'ignored_issues' => [],
'php_version' => '8.0',
],
'arrayMergeListOfShapes' => [
'code' => '<?php
/** @var list<array{a: int}> */
$a = [];
$b = array_merge(...$a);
/** @var non-empty-list<array{a: int}> */
$c = [];
$d = array_merge(...$c);
',
'assertions' => [
'$b' => 'array{a?: int}',
'$d' => 'array{a: int}',
]
],
'arrayMergeIntArrays' => [
'code' => '<?php
$d = array_merge(["a", "b", "c", "d"], [1, 2, 3]);',
Expand Down

0 comments on commit 4d0ddf6

Please sign in to comment.