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
5 changes: 3 additions & 2 deletions src/Type/Php/ArgumentBasedFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}
$argumentPosition = self::FUNCTION_NAMES[$functionReflection->getName()];

if (!isset($functionCall->getArgs()[$argumentPosition])) {
$args = $functionCall->getArgs();
if (!isset($args[$argumentPosition])) {
return null;
}

$argument = $functionCall->getArgs()[$argumentPosition];
$argument = $args[$argumentPosition];
$argumentType = $scope->getType($argument->value);
$argumentKeyType = $argumentType->getIterableKeyType();
$argumentValueType = $argumentType->getIterableValueType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ 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 null;
}

$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
if (!isset($functionCall->getArgs()[1])) {
$arrayType = $scope->getType($args[0]->value);
if (!isset($args[1])) {
$case = CASE_LOWER;
} else {
$caseType = $scope->getType($functionCall->getArgs()[1]->value);
$caseType = $scope->getType($args[1]->value);
$scalarValues = $caseType->getConstantScalarValues();
if (count($scalarValues) === 1) {
$case = (int) $scalarValues[0];
Expand Down
9 changes: 5 additions & 4 deletions src/Type/Php/ArrayChunkFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) < 2) {
$args = $functionCall->getArgs();
if (count($args) < 2) {
return null;
}

$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
$arrayType = $scope->getType($args[0]->value);
if ($arrayType->isArray()->no()) {
return $this->phpVersion->arrayFunctionsReturnNullWithNonArray() ? new NullType() : new NeverType();
}

$lengthType = $scope->getType($functionCall->getArgs()[1]->value);
$lengthType = $scope->getType($args[1]->value);
$negativeOrZero = IntegerRangeType::fromInterval(null, 0);
if ($negativeOrZero->isSuperTypeOf($lengthType)->yes()) {
return $this->phpVersion->throwsValueErrorForInternalFunctions() ? new NeverType() : new NullType();
}

$preserveKeysType = isset($functionCall->getArgs()[2]) ? $scope->getType($functionCall->getArgs()[2]->value) : new ConstantBooleanType(false);
$preserveKeysType = isset($args[2]) ? $scope->getType($args[2]->value) : new ConstantBooleanType(false);

return $arrayType->chunkArray($lengthType, $preserveKeysType->isTrue());
}
Expand Down
9 changes: 5 additions & 4 deletions src/Type/Php/ArrayColumnFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
$numArgs = count($functionCall->getArgs());
$args = $functionCall->getArgs();
$numArgs = count($args);
if ($numArgs < 2) {
return null;
}

$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
$columnType = $scope->getType($functionCall->getArgs()[1]->value);
$indexType = $numArgs >= 3 ? $scope->getType($functionCall->getArgs()[2]->value) : new NullType();
$arrayType = $scope->getType($args[0]->value);
$columnType = $scope->getType($args[1]->value);
$indexType = $numArgs >= 3 ? $scope->getType($args[2]->value) : new NullType();

$constantArrayTypes = $arrayType->getConstantArrays();
if (count($constantArrayTypes) === 1) {
Expand Down
7 changes: 4 additions & 3 deletions src/Type/Php/ArrayCombineFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) < 2) {
$args = $functionCall->getArgs();
if (count($args) < 2) {
return null;
}

$firstArg = $functionCall->getArgs()[0]->value;
$secondArg = $functionCall->getArgs()[1]->value;
$firstArg = $args[0]->value;
$secondArg = $args[1]->value;

$keysParamType = $scope->getType($firstArg);
$valuesParamType = $scope->getType($secondArg);
Expand Down
5 changes: 3 additions & 2 deletions src/Type/Php/ArrayCurrentDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ 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 null;
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
$argType = $scope->getType($args[0]->value);
$iterableAtLeastOnce = $argType->isIterableAtLeastOnce();
if ($iterableAtLeastOnce->no()) {
return new ConstantBooleanType(false);
Expand Down
9 changes: 5 additions & 4 deletions src/Type/Php/ArrayFillFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) < 3) {
$args = $functionCall->getArgs();
if (count($args) < 3) {
return null;
}

$numberType = $scope->getType($functionCall->getArgs()[1]->value);
$numberType = $scope->getType($args[1]->value);
$isValidNumberType = IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($numberType);

// check against negative-int, which is not allowed
Expand All @@ -53,8 +54,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return new ConstantBooleanType(false);
}

$startIndexType = $scope->getType($functionCall->getArgs()[0]->value);
$valueType = $scope->getType($functionCall->getArgs()[2]->value);
$startIndexType = $scope->getType($args[0]->value);
$valueType = $scope->getType($args[2]->value);

if (
$startIndexType instanceof ConstantIntegerType
Expand Down
7 changes: 4 additions & 3 deletions src/Type/Php/ArrayFillKeysFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) < 2) {
$args = $functionCall->getArgs();
if (count($args) < 2) {
return null;
}

$keysType = $scope->getType($functionCall->getArgs()[0]->value);
$keysType = $scope->getType($args[0]->value);
if ($keysType->isArray()->no()) {
return $this->phpVersion->arrayFunctionsReturnNullWithNonArray() ? new NullType() : new NeverType();
}

return $keysType->fillKeysArray($scope->getType($functionCall->getArgs()[1]->value));
return $keysType->fillKeysArray($scope->getType($args[1]->value));
}

}
7 changes: 4 additions & 3 deletions src/Type/Php/ArrayFilterFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$arrayArg = $functionCall->getArgs()[0]->value ?? null;
$callbackArg = $functionCall->getArgs()[1]->value ?? null;
$flagArg = $functionCall->getArgs()[2]->value ?? null;
$args = $functionCall->getArgs();
$arrayArg = $args[0]->value ?? null;
$callbackArg = $args[1]->value ?? null;
$flagArg = $args[2]->value ?? null;

return $this->arrayFilterFunctionReturnTypeHelper->getType($scope, $arrayArg, $callbackArg, $flagArg);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Type/Php/ArrayFindFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) < 2) {
$args = $functionCall->getArgs();
if (count($args) < 2) {
return null;
}

$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
$arrayType = $scope->getType($args[0]->value);
if (count($arrayType->getArrays()) < 1) {
return null;
}

$arrayArg = $functionCall->getArgs()[0]->value ?? null;
$callbackArg = $functionCall->getArgs()[1]->value ?? null;
$arrayArg = $args[0]->value ?? null;
$callbackArg = $args[1]->value ?? null;

$resultTypes = $this->arrayFilterFunctionReturnTypeHelper->getType($scope, $arrayArg, $callbackArg, null);
$resultType = TypeCombinator::union(...array_map(static fn ($type) => $type->getIterableValueType(), $resultTypes->getArrays()));
Expand Down
5 changes: 3 additions & 2 deletions src/Type/Php/ArrayFindKeyFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) < 2) {
$args = $functionCall->getArgs();
if (count($args) < 2) {
return null;
}

$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
$arrayType = $scope->getType($args[0]->value);
if (count($arrayType->getArrays()) < 1) {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Type/Php/ArrayFlipFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) !== 1) {
$args = $functionCall->getArgs();
if (count($args) !== 1) {
return null;
}

$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
$arrayType = $scope->getType($args[0]->value);
if ($arrayType->isArray()->no()) {
return $this->phpVersion->arrayFunctionsReturnNullWithNonArray() ? new NullType() : new NeverType();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Type/Php/ArrayKeyDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ 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 null;
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
$argType = $scope->getType($args[0]->value);
$iterableAtLeastOnce = $argType->isIterableAtLeastOnce();
if ($iterableAtLeastOnce->no()) {
return new NullType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ public function specifyTypes(
TypeSpecifierContext $context,
): SpecifiedTypes
{
if (count($node->getArgs()) < 2) {
$args = $node->getArgs();
if (count($args) < 2) {
return new SpecifiedTypes();
}
$key = $node->getArgs()[0]->value;
$array = $node->getArgs()[1]->value;
$key = $args[0]->value;
$array = $args[1]->value;
$keyType = $scope->getType($key)->toArrayKey();
$arrayType = $scope->getType($array);

Expand Down
17 changes: 9 additions & 8 deletions src/Type/Php/ArrayMapFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
$numArgs = count($functionCall->getArgs());
$args = $functionCall->getArgs();
$numArgs = count($args);
if ($numArgs < 2) {
return null;
}

$singleArrayArgument = !isset($functionCall->getArgs()[2]);
$callback = $functionCall->getArgs()[0]->value;
$singleArrayArgument = !isset($args[2]);
$callback = $args[0]->value;
$callableType = $scope->getType($callback);
$callableIsNull = $callableType->isNull()->yes();

Expand All @@ -53,15 +54,15 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$callback,
array_map(
static fn (Node\Arg $arg) => new Node\Arg(new TypeExpr($scope->getType($arg->value)->getIterableValueType())),
array_slice($functionCall->getArgs(), 1),
array_slice($args, 1),
),
));
} elseif ($callableIsNull) {
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
$argTypes = [];
$areAllSameSize = true;
$expectedSize = null;
foreach (array_slice($functionCall->getArgs(), 1) as $index => $arg) {
foreach (array_slice($args, 1) as $index => $arg) {
$argTypes[$index] = $argType = $scope->getType($arg->value);
if (!$areAllSameSize || $numArgs === 2) {
continue;
Expand All @@ -85,9 +86,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

if (!$areAllSameSize) {
$firstArr = $functionCall->getArgs()[1]->value;
$firstArr = $args[1]->value;
$identities = [];
foreach (array_slice($functionCall->getArgs(), 2) as $arg) {
foreach (array_slice($args, 2) as $arg) {
$identities[] = new Node\Expr\BinaryOp\Identical($firstArr, $arg->value);
}

Expand Down Expand Up @@ -117,7 +118,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$valueType = new MixedType();
}

$arrayType = $scope->getType($functionCall->getArgs()[1]->value);
$arrayType = $scope->getType($args[1]->value);

if ($singleArrayArgument) {
if ($callableIsNull) {
Expand Down
5 changes: 3 additions & 2 deletions src/Type/Php/ArrayNextDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ 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 null;
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
$argType = $scope->getType($args[0]->value);
$iterableAtLeastOnce = $argType->isIterableAtLeastOnce();
if ($iterableAtLeastOnce->no()) {
return new ConstantBooleanType(false);
Expand Down
9 changes: 5 additions & 4 deletions src/Type/Php/ArrayPadDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

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

$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
$itemType = $scope->getType($functionCall->getArgs()[2]->value);
$arrayType = $scope->getType($args[0]->value);
$itemType = $scope->getType($args[2]->value);

$returnType = new ArrayType(
TypeCombinator::union($arrayType->getIterableKeyType(), new IntegerType()),
TypeCombinator::union($arrayType->getIterableValueType(), $itemType),
);

$lengthType = $scope->getType($functionCall->getArgs()[1]->value);
$lengthType = $scope->getType($args[1]->value);
if (
$arrayType->isIterableAtLeastOnce()->yes()
|| $lengthType->isSuperTypeOf(new ConstantIntegerType(0))->no()
Expand Down
5 changes: 3 additions & 2 deletions src/Type/Php/ArrayPopFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ 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 null;
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
$argType = $scope->getType($args[0]->value);
$iterableAtLeastOnce = $argType->isIterableAtLeastOnce();
if ($iterableAtLeastOnce->no()) {
return new NullType();
Expand Down
7 changes: 4 additions & 3 deletions src/Type/Php/ArrayRandFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
$argsCount = count($functionCall->getArgs());
$args = $functionCall->getArgs();
$argsCount = count($args);
if ($argsCount < 1) {
return null;
}

$firstArgType = $scope->getType($functionCall->getArgs()[0]->value);
$firstArgType = $scope->getType($args[0]->value);
$isInteger = $firstArgType->getIterableKeyType()->isInteger();
$isString = $firstArgType->getIterableKeyType()->isString();

Expand All @@ -49,7 +50,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return $valueType;
}

$secondArgType = $scope->getType($functionCall->getArgs()[1]->value);
$secondArgType = $scope->getType($args[1]->value);

$one = new ConstantIntegerType(1);
if ($one->isSuperTypeOf($secondArgType)->yes()) {
Expand Down
Loading
Loading