Skip to content

Commit 3a70b59

Browse files
staabmondrejmirtes
authored andcommitted
Prevent overly greedy $scope->getType() calls in Extensions
1 parent fcee93f commit 3a70b59

4 files changed

+14
-13
lines changed

src/Type/Php/ArrayFillFunctionReturnTypeExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4141
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
4242
}
4343

44-
$startIndexType = $scope->getType($functionCall->getArgs()[0]->value);
4544
$numberType = $scope->getType($functionCall->getArgs()[1]->value);
46-
$valueType = $scope->getType($functionCall->getArgs()[2]->value);
47-
4845
$isValidNumberType = IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($numberType);
4946

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

55+
$startIndexType = $scope->getType($functionCall->getArgs()[0]->value);
56+
$valueType = $scope->getType($functionCall->getArgs()[2]->value);
57+
5858
if (
5959
$startIndexType instanceof ConstantIntegerType
6060
&& $numberType instanceof ConstantIntegerType

src/Type/Php/ArraySliceFunctionReturnTypeExtension.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3434
$offsetType = isset($functionCall->getArgs()[1]) ? $scope->getType($functionCall->getArgs()[1]->value) : null;
3535
$offset = $offsetType instanceof ConstantIntegerType ? $offsetType->getValue() : 0;
3636

37-
$limitType = isset($functionCall->getArgs()[2]) ? $scope->getType($functionCall->getArgs()[2]->value) : null;
38-
$limit = $limitType instanceof ConstantIntegerType ? $limitType->getValue() : null;
39-
40-
$preserveKeysType = isset($functionCall->getArgs()[3]) ? $scope->getType($functionCall->getArgs()[3]->value) : null;
41-
$preserveKeys = $preserveKeysType instanceof ConstantBooleanType ? $preserveKeysType->getValue() : false;
42-
4337
$constantArrays = $valueType->getConstantArrays();
4438
if (count($constantArrays) > 0) {
39+
$limitType = isset($functionCall->getArgs()[2]) ? $scope->getType($functionCall->getArgs()[2]->value) : null;
40+
$limit = $limitType instanceof ConstantIntegerType ? $limitType->getValue() : null;
41+
42+
$preserveKeysType = isset($functionCall->getArgs()[3]) ? $scope->getType($functionCall->getArgs()[3]->value) : null;
43+
$preserveKeys = $preserveKeysType instanceof ConstantBooleanType ? $preserveKeysType->getValue() : false;
44+
4545
$results = [];
4646
foreach ($constantArrays as $constantArray) {
4747
$results[] = $constantArray->slice($offset, $limit, $preserveKeys);

src/Type/Php/IsAFunctionTypeSpecifyingExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
3737
if (count($node->getArgs()) < 2) {
3838
return new SpecifiedTypes();
3939
}
40-
$objectOrClassType = $scope->getType($node->getArgs()[0]->value);
4140
$classType = $scope->getType($node->getArgs()[1]->value);
42-
$allowStringType = isset($node->getArgs()[2]) ? $scope->getType($node->getArgs()[2]->value) : new ConstantBooleanType(false);
43-
$allowString = !$allowStringType->equals(new ConstantBooleanType(false));
4441

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

46+
$objectOrClassType = $scope->getType($node->getArgs()[0]->value);
47+
$allowStringType = isset($node->getArgs()[2]) ? $scope->getType($node->getArgs()[2]->value) : new ConstantBooleanType(false);
48+
$allowString = !$allowStringType->equals(new ConstantBooleanType(false));
49+
4950
return $this->typeSpecifier->create(
5051
$node->getArgs()[0]->value,
5152
$this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, true),

src/Type/Php/ParseUrlFunctionDynamicReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
5656

5757
$this->cacheReturnTypes();
5858

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

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

75+
$urlType = $scope->getType($functionCall->getArgs()[0]->value);
7676
if ($urlType instanceof ConstantStringType) {
7777
try {
7878
$result = @parse_url($urlType->getValue(), $componentType->getValue());

0 commit comments

Comments
 (0)