Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent overly greedy $scope->getType() calls in Extensions #2101

Merged
merged 1 commit into from Dec 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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