Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
cleanup
  • Loading branch information
clxmstaab committed Mar 30, 2022
commit 98a618e81726374bfef9e7025e6836707910df26
32 changes: 20 additions & 12 deletions src/Type/Php/ArrayMergeFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,30 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
if (!isset($functionCall->getArgs()[0])) {
$args = $functionCall->getArgs();

if (!isset($args[0])) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}

$argTypes = [];
$allConstant = true;
foreach ($functionCall->getArgs() as $arg) {
foreach ($args as $i => $arg) {
$argType = $scope->getType($arg->value);
if ($arg->unpack || !$argType instanceof ConstantArrayType) {
$allConstant = false;
break;
$argTypes[$i] = $argType;

if (!$arg->unpack && $argType instanceof ConstantArrayType) {
continue;
}

$allConstant = false;
}

if ($allConstant) {
$newArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
foreach ($functionCall->getArgs() as $arg) {
$argType = $scope->getType($arg->value);
foreach ($args as $i => $arg) {
$argType = $argTypes[$i];

if (!$argType instanceof ConstantArrayType) {
throw new ShouldNotHappenException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could push only ConstantArrayType into $argTypes and this condition would not be necessary :)

Copy link
Contributor Author

@staabm staabm Mar 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then I couldn't re-use the $artTypes for the 2nd-foreach loop..?

or I have all non-constant-array-args to be resolved twice via scope->getType
or alternatively I need 2 arrays to hold the different args per type

}
Expand All @@ -53,12 +60,12 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$valueTypes = $argType->getValueTypes();
$optionalKeys = $argType->getOptionalKeys();

foreach ($keyTypes as $i => $keyType) {
$isOptional = in_array($i, $optionalKeys, true);
foreach ($keyTypes as $k => $keyType) {
$isOptional = in_array($k, $optionalKeys, true);

$newArrayBuilder->setOffsetValueType(
$keyType,
$valueTypes[$i],
$valueTypes[$k],
$isOptional,
);
}
Expand All @@ -70,8 +77,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$keyTypes = [];
$valueTypes = [];
$nonEmpty = false;
foreach ($functionCall->getArgs() as $arg) {
$argType = $scope->getType($arg->value);
foreach ($args as $i => $arg) {
$argType = $argTypes[$i];

if ($arg->unpack) {
$argType = $argType->getIterableValueType();
if ($argType instanceof UnionType) {
Expand Down