Skip to content

Commit 244b8d4

Browse files
authored
Cleanup instanceof ConstantStringType calls (#5752)
1 parent 8d88e85 commit 244b8d4

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ parameters:
117117
-
118118
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.'
119119
identifier: phpstanApi.instanceofType
120-
count: 5
120+
count: 2
121121
path: src/Analyser/TypeSpecifier.php
122122

123123
-

src/Analyser/TypeSpecifier.php

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,10 +3095,11 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
30953095
&& in_array(strtolower($unwrappedLeftExpr->name->toString()), ['get_class', 'get_debug_type'], true)
30963096
&& isset($unwrappedLeftExpr->getArgs()[0])
30973097
) {
3098-
if ($rightType instanceof ConstantStringType && $this->reflectionProvider->hasClass($rightType->getValue())) {
3098+
$constantStringTypes = $rightType->getConstantStrings();
3099+
if (count($constantStringTypes) === 1 && $this->reflectionProvider->hasClass($constantStringTypes[0]->getValue())) {
30993100
return $this->create(
31003101
$unwrappedLeftExpr->getArgs()[0]->value,
3101-
new ObjectType($rightType->getValue(), classReflection: $this->reflectionProvider->getClass($rightType->getValue())->asFinal()),
3102+
new ObjectType($constantStringTypes[0]->getValue(), classReflection: $this->reflectionProvider->getClass($constantStringTypes[0]->getValue())->asFinal()),
31023103
$context,
31033104
$scope,
31043105
)->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr);
@@ -3217,26 +3218,27 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
32173218
$unwrappedLeftExpr->class instanceof Expr &&
32183219
$unwrappedLeftExpr->name instanceof Node\Identifier &&
32193220
$unwrappedRightExpr instanceof ClassConstFetch &&
3220-
$rightType instanceof ConstantStringType &&
3221-
$rightType->getValue() !== '' &&
32223221
strtolower($unwrappedLeftExpr->name->toString()) === 'class'
32233222
) {
3224-
if ($this->reflectionProvider->hasClass($rightType->getValue())) {
3225-
return $this->create(
3226-
$unwrappedLeftExpr->class,
3227-
new ObjectType($rightType->getValue(), classReflection: $this->reflectionProvider->getClass($rightType->getValue())->asFinal()),
3228-
$context,
3223+
$constantStrings = $rightType->getConstantStrings();
3224+
if (count($constantStrings) === 1 && $constantStrings[0]->getValue() !== '') {
3225+
if ($this->reflectionProvider->hasClass($constantStrings[0]->getValue())) {
3226+
return $this->create(
3227+
$unwrappedLeftExpr->class,
3228+
new ObjectType($constantStrings[0]->getValue(), classReflection: $this->reflectionProvider->getClass($constantStrings[0]->getValue())->asFinal()),
3229+
$context,
3230+
$scope,
3231+
)->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr);
3232+
}
3233+
return $this->specifyTypesInCondition(
32293234
$scope,
3235+
new Instanceof_(
3236+
$unwrappedLeftExpr->class,
3237+
new Name($constantStrings[0]->getValue()),
3238+
),
3239+
$context,
32303240
)->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr);
32313241
}
3232-
return $this->specifyTypesInCondition(
3233-
$scope,
3234-
new Instanceof_(
3235-
$unwrappedLeftExpr->class,
3236-
new Name($rightType->getValue()),
3237-
),
3238-
$context,
3239-
)->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr);
32403242
}
32413243

32423244
$leftType = $scope->getType($leftExpr);
@@ -3248,27 +3250,28 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
32483250
$unwrappedRightExpr->class instanceof Expr &&
32493251
$unwrappedRightExpr->name instanceof Node\Identifier &&
32503252
$unwrappedLeftExpr instanceof ClassConstFetch &&
3251-
$leftType instanceof ConstantStringType &&
3252-
$leftType->getValue() !== '' &&
32533253
strtolower($unwrappedRightExpr->name->toString()) === 'class'
32543254
) {
3255-
if ($this->reflectionProvider->hasClass($leftType->getValue())) {
3256-
return $this->create(
3257-
$unwrappedRightExpr->class,
3258-
new ObjectType($leftType->getValue(), classReflection: $this->reflectionProvider->getClass($leftType->getValue())->asFinal()),
3259-
$context,
3255+
$constantStrings = $leftType->getConstantStrings();
3256+
if (count($constantStrings) === 1 && $constantStrings[0]->getValue() !== '') {
3257+
if ($this->reflectionProvider->hasClass($constantStrings[0]->getValue())) {
3258+
return $this->create(
3259+
$unwrappedRightExpr->class,
3260+
new ObjectType($constantStrings[0]->getValue(), classReflection: $this->reflectionProvider->getClass($constantStrings[0]->getValue())->asFinal()),
3261+
$context,
3262+
$scope,
3263+
)->unionWith($this->create($rightExpr, $leftType, $context, $scope)->setRootExpr($expr));
3264+
}
3265+
3266+
return $this->specifyTypesInCondition(
32603267
$scope,
3268+
new Instanceof_(
3269+
$unwrappedRightExpr->class,
3270+
new Name($constantStrings[0]->getValue()),
3271+
),
3272+
$context,
32613273
)->unionWith($this->create($rightExpr, $leftType, $context, $scope)->setRootExpr($expr));
32623274
}
3263-
3264-
return $this->specifyTypesInCondition(
3265-
$scope,
3266-
new Instanceof_(
3267-
$unwrappedRightExpr->class,
3268-
new Name($leftType->getValue()),
3269-
),
3270-
$context,
3271-
)->unionWith($this->create($rightExpr, $leftType, $context, $scope)->setRootExpr($expr));
32723275
}
32733276

32743277
if ($context->false()) {

0 commit comments

Comments
 (0)