Skip to content

Commit a46cbc0

Browse files
committed
Remove mentions of scalar node types in favor of constant types
1 parent 9f5fddd commit a46cbc0

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
use PHPStan\Type\ArrayType;
5858
use PHPStan\Type\CommentHelper;
5959
use PHPStan\Type\Constant\ConstantArrayType;
60+
use PHPStan\Type\Constant\ConstantStringType;
6061
use PHPStan\Type\ErrorType;
6162
use PHPStan\Type\FileTypeMapper;
6263
use PHPStan\Type\IntegerType;
@@ -232,11 +233,14 @@ private function specifyFetchedPropertyForInnerScope(Node $node, Scope $inScope,
232233
&& $node->name instanceof Name
233234
&& $this->broker->resolveFunctionName($node->name, $scope) === 'property_exists'
234235
&& count($node->args) === 2
235-
&& $node->args[1]->value instanceof Node\Scalar\String_
236236
) {
237-
$scope = $scope->specifyFetchedPropertyFromIsset(
238-
new PropertyFetch($node->args[0]->value, $node->args[1]->value->value)
239-
);
237+
$secondArgumentType = $scope->getType($node->args[1]->value);
238+
239+
if ($secondArgumentType instanceof ConstantStringType) {
240+
$scope = $scope->specifyFetchedPropertyFromIsset(
241+
new PropertyFetch($node->args[0]->value, $secondArgumentType->getValue())
242+
);
243+
}
240244
}
241245
} else {
242246
if ($node instanceof Expr\Empty_) {
@@ -367,8 +371,8 @@ private function processNode(\PhpParser\Node $node, Scope $scope, \Closure $node
367371
&& $argValue->class instanceof Name
368372
) {
369373
$scopeClass = $scope->resolveName($argValue->class);
370-
} elseif ($argValue instanceof Node\Scalar\String_) {
371-
$scopeClass = $argValue->value;
374+
} elseif ($argValueType instanceof ConstantStringType) {
375+
$scopeClass = $argValueType->getValue();
372376
}
373377
}
374378
$closureBindScope = $scope->enterClosureBind($thisType, $scopeClass);

src/Analyser/TypeSpecifier.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPStan\Type\CallableType;
2121
use PHPStan\Type\Constant\ConstantBooleanType;
2222
use PHPStan\Type\Constant\ConstantIntegerType;
23+
use PHPStan\Type\Constant\ConstantStringType;
2324
use PHPStan\Type\FloatType;
2425
use PHPStan\Type\IntegerType;
2526
use PHPStan\Type\IterableType;
@@ -168,10 +169,8 @@ public function specifyTypesInCondition(Scope $scope, Expr $expr, int $context =
168169
case 'is_a':
169170
if (isset($expr->args[1])) {
170171
$classNameArgExpr = $expr->args[1]->value;
171-
if ($classNameArgExpr instanceof Node\Scalar\String_) {
172-
$objectType = new ObjectType($classNameArgExpr->value);
173-
$types = $this->create($innerExpr, $objectType, $context);
174-
} elseif (
172+
$classNameArgExprType = $scope->getType($classNameArgExpr);
173+
if (
175174
$classNameArgExpr instanceof Expr\ClassConstFetch
176175
&& $classNameArgExpr->class instanceof Name
177176
&& is_string($classNameArgExpr->name)
@@ -184,6 +183,9 @@ public function specifyTypesInCondition(Scope $scope, Expr $expr, int $context =
184183
$objectType = new ObjectType($className);
185184
}
186185
$types = $this->create($innerExpr, $objectType, $context);
186+
} elseif ($classNameArgExprType instanceof ConstantStringType) {
187+
$objectType = new ObjectType($classNameArgExprType->getValue());
188+
$types = $this->create($innerExpr, $objectType, $context);
187189
} elseif ($context & self::CONTEXT_TRUE) {
188190
$objectType = new ObjectWithoutClassType();
189191
$types = $this->create($innerExpr, $objectType, $context);

src/Rules/Classes/UnusedConstructorParametersRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function processNode(Node $node, Scope $scope): array
4545
}
4646

4747
return $this->check->getUnusedParameters(
48+
$scope,
4849
array_map(function (Param $parameter): string {
4950
return $parameter->name;
5051
}, $node->params),

src/Rules/Functions/UnusedClosureUsesRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function processNode(Node $node, Scope $scope): array
3434
}
3535

3636
return $this->check->getUnusedParameters(
37+
$scope,
3738
array_map(function (Node\Expr\ClosureUse $use): string {
3839
return $use->var;
3940
}, $node->uses),

src/Rules/UnusedFunctionParametersCheck.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@
33
namespace PHPStan\Rules;
44

55
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Type\Constant\ConstantStringType;
68

79
class UnusedFunctionParametersCheck
810
{
911

1012
/**
13+
* @param \PHPStan\Analyser\Scope $scope
1114
* @param string[] $parameterNames
1215
* @param \PhpParser\Node[] $statements
1316
* @param string $unusedParameterMessage
1417
* @return string[]
1518
*/
1619
public function getUnusedParameters(
20+
Scope $scope,
1721
array $parameterNames,
1822
array $statements,
1923
string $unusedParameterMessage
2024
): array
2125
{
2226
$unusedParameters = array_fill_keys($parameterNames, true);
23-
foreach ($this->getUsedVariables($statements) as $variableName) {
27+
foreach ($this->getUsedVariables($scope, $statements) as $variableName) {
2428
if (isset($unusedParameters[$variableName])) {
2529
unset($unusedParameters[$variableName]);
2630
}
@@ -34,10 +38,11 @@ public function getUnusedParameters(
3438
}
3539

3640
/**
41+
* @param \PHPStan\Analyser\Scope $scope
3742
* @param \PhpParser\Node[]|\PhpParser\Node $node
3843
* @return string[]
3944
*/
40-
private function getUsedVariables($node): array
45+
private function getUsedVariables(Scope $scope, $node): array
4146
{
4247
$variableNames = [];
4348
if ($node instanceof Node) {
@@ -53,8 +58,9 @@ private function getUsedVariables($node): array
5358
&& (string) $node->name === 'compact'
5459
) {
5560
foreach ($node->args as $arg) {
56-
if ($arg->value instanceof Node\Scalar\String_) {
57-
$variableNames[] = $arg->value->value;
61+
$argType = $scope->getType($arg->value);
62+
if ($argType instanceof ConstantStringType) {
63+
$variableNames[] = $argType->getValue();
5864
}
5965
}
6066
}
@@ -63,11 +69,11 @@ private function getUsedVariables($node): array
6369
continue;
6470
}
6571
$subNode = $node->{$subNodeName};
66-
$variableNames = array_merge($variableNames, $this->getUsedVariables($subNode));
72+
$variableNames = array_merge($variableNames, $this->getUsedVariables($scope, $subNode));
6773
}
6874
} elseif (is_array($node)) {
6975
foreach ($node as $subNode) {
70-
$variableNames = array_merge($variableNames, $this->getUsedVariables($subNode));
76+
$variableNames = array_merge($variableNames, $this->getUsedVariables($scope, $subNode));
7177
}
7278
}
7379

0 commit comments

Comments
 (0)