Skip to content

Commit

Permalink
Prevent overly greedy $scope->getType() calls in Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Dec 13, 2022
1 parent fcee93f commit 3a70b59
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Type/Php/ArrayFillFunctionReturnTypeExtension.php
Expand Up @@ -41,10 +41,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}

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

$isValidNumberType = IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($numberType);

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

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

if (
$startIndexType instanceof ConstantIntegerType
&& $numberType instanceof ConstantIntegerType
Expand Down
12 changes: 6 additions & 6 deletions src/Type/Php/ArraySliceFunctionReturnTypeExtension.php
Expand Up @@ -34,14 +34,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$offsetType = isset($functionCall->getArgs()[1]) ? $scope->getType($functionCall->getArgs()[1]->value) : null;
$offset = $offsetType instanceof ConstantIntegerType ? $offsetType->getValue() : 0;

$limitType = isset($functionCall->getArgs()[2]) ? $scope->getType($functionCall->getArgs()[2]->value) : null;
$limit = $limitType instanceof ConstantIntegerType ? $limitType->getValue() : null;

$preserveKeysType = isset($functionCall->getArgs()[3]) ? $scope->getType($functionCall->getArgs()[3]->value) : null;
$preserveKeys = $preserveKeysType instanceof ConstantBooleanType ? $preserveKeysType->getValue() : false;

$constantArrays = $valueType->getConstantArrays();
if (count($constantArrays) > 0) {
$limitType = isset($functionCall->getArgs()[2]) ? $scope->getType($functionCall->getArgs()[2]->value) : null;
$limit = $limitType instanceof ConstantIntegerType ? $limitType->getValue() : null;

$preserveKeysType = isset($functionCall->getArgs()[3]) ? $scope->getType($functionCall->getArgs()[3]->value) : null;
$preserveKeys = $preserveKeysType instanceof ConstantBooleanType ? $preserveKeysType->getValue() : false;

$results = [];
foreach ($constantArrays as $constantArray) {
$results[] = $constantArray->slice($offset, $limit, $preserveKeys);
Expand Down
7 changes: 4 additions & 3 deletions src/Type/Php/IsAFunctionTypeSpecifyingExtension.php
Expand Up @@ -37,15 +37,16 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
if (count($node->getArgs()) < 2) {
return new SpecifiedTypes();
}
$objectOrClassType = $scope->getType($node->getArgs()[0]->value);
$classType = $scope->getType($node->getArgs()[1]->value);
$allowStringType = isset($node->getArgs()[2]) ? $scope->getType($node->getArgs()[2]->value) : new ConstantBooleanType(false);
$allowString = !$allowStringType->equals(new ConstantBooleanType(false));

if (!$classType instanceof ConstantStringType && !$context->truthy()) {
return new SpecifiedTypes([], []);
}

$objectOrClassType = $scope->getType($node->getArgs()[0]->value);
$allowStringType = isset($node->getArgs()[2]) ? $scope->getType($node->getArgs()[2]->value) : new ConstantBooleanType(false);
$allowString = !$allowStringType->equals(new ConstantBooleanType(false));

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, true),
Expand Down
Expand Up @@ -56,7 +56,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

$this->cacheReturnTypes();

$urlType = $scope->getType($functionCall->getArgs()[0]->value);
if (count($functionCall->getArgs()) > 1) {
$componentType = $scope->getType($functionCall->getArgs()[1]->value);

Expand All @@ -73,6 +72,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$componentType = new ConstantIntegerType(-1);
}

$urlType = $scope->getType($functionCall->getArgs()[0]->value);
if ($urlType instanceof ConstantStringType) {
try {
$result = @parse_url($urlType->getValue(), $componentType->getValue());
Expand Down

0 comments on commit 3a70b59

Please sign in to comment.