Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/Type/Php/ArrayMergeFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function array_keys;
use function count;
use function in_array;
use function is_int;
Expand Down Expand Up @@ -51,6 +50,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$argType = $scope->getType($arg->value);

if ($arg->unpack) {
$startIndex = count($argTypes);

if ($argType->isConstantArray()->yes()) {
foreach ($argType->getConstantArrays() as $constantArray) {
foreach ($constantArray->getValueTypes() as $valueType) {
Expand All @@ -62,10 +63,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

if (!$argType->isIterableAtLeastOnce()->yes()) {
// unpacked params can be empty, making them optional
$optionalArgTypesOffset = count($argTypes) - 1;
foreach (array_keys($argTypes) as $key) {
$optionalArgTypes[] = $optionalArgTypesOffset + $key;
for ($i = $startIndex; $i < count($argTypes); $i++) {
$optionalArgTypes[] = $i;
}
}
} else {
Expand All @@ -80,7 +79,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

if ($allConstant->yes()) {
$newArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
foreach ($argTypes as $argType) {
foreach ($argTypes as $argIndex => $argType) {
$isOptionalArg = in_array($argIndex, $optionalArgTypes, true);

/** @var array<int|string, ConstantIntegerType|ConstantStringType> $keyTypes */
$keyTypes = [];
foreach ($argType->getConstantArrays() as $constantArray) {
Expand All @@ -93,7 +94,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$newArrayBuilder->setOffsetValueType(
$keyType instanceof ConstantIntegerType ? null : $keyType,
$argType->getOffsetValueType($keyType),
!$argType->hasOffsetValueType($keyType)->yes(),
$isOptionalArg || !$argType->hasOffsetValueType($keyType)->yes(),
);
}
}
Expand All @@ -102,7 +103,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

$offsetTypes = [];
foreach ($argTypes as $argType) {
foreach ($argTypes as $argIndex => $argType) {
if (in_array($argIndex, $optionalArgTypes, true)) {
continue;
}

$constArrays = $argType->getConstantArrays();
if ($constArrays !== []) {
foreach ($constArrays as $constantArray) {
Expand Down
21 changes: 13 additions & 8 deletions src/Type/Php/ArrayReplaceFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function array_keys;
use function count;
use function in_array;
use function is_string;
Expand Down Expand Up @@ -51,6 +50,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$argType = $scope->getType($arg->value);

if ($arg->unpack) {
$startIndex = count($argTypes);

if ($argType->isConstantArray()->yes()) {
foreach ($argType->getConstantArrays() as $constantArray) {
foreach ($constantArray->getValueTypes() as $valueType) {
Expand All @@ -62,10 +63,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

if (!$argType->isIterableAtLeastOnce()->yes()) {
// unpacked params can be empty, making them optional
$optionalArgTypesOffset = count($argTypes) - 1;
foreach (array_keys($argTypes) as $key) {
$optionalArgTypes[] = $optionalArgTypesOffset + $key;
for ($i = $startIndex; $i < count($argTypes); $i++) {
$optionalArgTypes[] = $i;
}
}
} else {
Expand All @@ -81,7 +80,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
if ($allConstant->yes()) {
$newArrayBuilder = ConstantArrayTypeBuilder::createEmpty();

foreach ($argTypes as $argType) {
foreach ($argTypes as $argIndex => $argType) {
$isOptionalArg = in_array($argIndex, $optionalArgTypes, true);

/** @var array<int|string, ConstantIntegerType|ConstantStringType> $keyTypes */
$keyTypes = [];
foreach ($argType->getConstantArrays() as $constantArray) {
Expand All @@ -94,7 +95,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$newArrayBuilder->setOffsetValueType(
$keyType,
$argType->getOffsetValueType($keyType),
!$argType->hasOffsetValueType($keyType)->yes(),
$isOptionalArg || !$argType->hasOffsetValueType($keyType)->yes(),
);
}
}
Expand All @@ -103,7 +104,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

$offsetTypes = [];
foreach ($argTypes as $argType) {
foreach ($argTypes as $argIndex => $argType) {
if (in_array($argIndex, $optionalArgTypes, true)) {
continue;
}

$constArrays = $argType->getConstantArrays();
if ($constArrays !== []) {
foreach ($constArrays as $constantArray) {
Expand Down
73 changes: 73 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14526.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Bug14526;

use function array_merge;
use function array_replace;
use function PHPStan\Testing\assertType;

/**
* @param array{array{foo: int}, array<string, int>}|array{} $values
*/
function testMergeUnpackUnionWithEmpty(array $values): void
{
$result = array_merge(...$values);
assertType('array<string, int>', $result);
}

/**
* @param array{non-empty-array<string, int>, array<string, int>}|array{} $values
*/
function testMergeUnpackUnionNonEmptyFirstWithEmpty(array $values): void
{
$result = array_merge(...$values);
assertType('array<string, int>', $result);
}

/**
* @param array{non-empty-array<string, int>}|array{} $values
*/
function testMergeUnpackUnionSingleWithEmpty(array $values): void
{
$result = array_merge(...$values);
assertType('array<string, int>', $result);
}

function testMergeUnpackConstantUnionWithEmpty(): void
{
$values = rand(0, 1) ? [['a' => 1], ['b' => 2]] : [];
$result = array_merge(...$values);
assertType('array{a?: 1, b?: 2}', $result);
}

function testMergeUnpackConstantUnionWithEmptyThreeElements(): void
{
$values = rand(0, 1) ? [['a' => 1], ['b' => 2], ['c' => 3]] : [];
$result = array_merge(...$values);
assertType('array{a?: 1, b?: 2, c?: 3}', $result);
}

/**
* @param array{array{foo: int}, array<string, int>}|array{} $values
*/
function testReplaceUnpackUnionWithEmpty(array $values): void
{
$result = array_replace(...$values);
assertType('array<string, int>', $result);
}

/**
* @param array{non-empty-array<string, int>, array<string, int>}|array{} $values
*/
function testReplaceUnpackUnionNonEmptyFirstWithEmpty(array $values): void
{
$result = array_replace(...$values);
assertType('array<string, int>', $result);
}

function testReplaceUnpackConstantUnionWithEmpty(): void
{
$values = rand(0, 1) ? [['a' => 1], ['b' => 2]] : [];
$result = array_replace(...$values);
assertType('array{a?: 1, b?: 2}', $result);
}
Loading