Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab committed Mar 30, 2022
1 parent 05d7fe3 commit 98a618e
Showing 1 changed file with 20 additions and 12 deletions.
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();
}
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

0 comments on commit 98a618e

Please sign in to comment.