Skip to content

Commit 487eb4f

Browse files
committed
replace instanceof NeverType with isNever method
1 parent 8e9401e commit 487eb4f

File tree

65 files changed

+146
-172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+146
-172
lines changed

phpstan-baseline.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,18 @@ parameters:
14311431
count: 1
14321432
path: src/Type/IterableType.php
14331433

1434+
-
1435+
rawMessage: 'Doing instanceof PHPStan\Type\NeverType is error-prone and deprecated. Use Type::isNever() or Type::isExplicitNever() instead.'
1436+
identifier: phpstanApi.instanceofType
1437+
count: 2
1438+
path: src/Type/NeverType.php
1439+
1440+
-
1441+
rawMessage: 'Doing instanceof PHPStan\Type\NeverType is error-prone and deprecated. Use Type::isNever() or Type::isExplicitNever() instead.'
1442+
identifier: phpstanApi.instanceofType
1443+
count: 1
1444+
path: src/Type/NonAcceptingNeverType.php
1445+
14341446
-
14351447
rawMessage: 'Doing instanceof PHPStan\Type\ConstantScalarType is error-prone and deprecated. Use Type::isConstantScalarValue() or Type::getConstantScalarTypes() or Type::getConstantScalarValues() instead.'
14361448
identifier: phpstanApi.instanceofType
@@ -1773,6 +1785,12 @@ parameters:
17731785
count: 1
17741786
path: src/Type/TypehintHelper.php
17751787

1788+
-
1789+
rawMessage: 'Call to an undefined method PHPStan\Type\Type::isSubTypeOf().'
1790+
identifier: method.notFound
1791+
count: 1
1792+
path: src/Type/UnionType.php
1793+
17761794
-
17771795
rawMessage: 'Doing instanceof PHPStan\Type\CallableType is error-prone and deprecated. Use Type::isCallable() and Type::getCallableParametersAcceptors() instead.'
17781796
identifier: phpstanApi.instanceofType

src/Analyser/MutatingScope.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,8 +3643,8 @@ public function removeTypeFromExpression(Expr $expr, Type $typeToRemove): self
36433643
{
36443644
$exprType = $this->getType($expr);
36453645
if (
3646-
$exprType instanceof NeverType ||
3647-
$typeToRemove instanceof NeverType
3646+
!$exprType->isNever()->no() ||
3647+
!$typeToRemove->isNever()->no()
36483648
) {
36493649
return $this;
36503650
}
@@ -5029,7 +5029,7 @@ private function exactInstantiation(New_ $node, Name $className): Type
50295029
}
50305030

50315031
$methodResult = $this->getType($methodCall);
5032-
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
5032+
if ($methodResult->isExplicitNever()->yes()) {
50335033
return $methodResult;
50345034
}
50355035

@@ -5468,7 +5468,7 @@ public function getIterableKeyType(Type $iteratee): Type
54685468
{
54695469
if ($iteratee instanceof UnionType) {
54705470
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
5471-
if (!$filtered instanceof NeverType) {
5471+
if ($filtered->isNever()->no()) {
54725472
$iteratee = $filtered;
54735473
}
54745474
}
@@ -5480,7 +5480,7 @@ public function getIterableValueType(Type $iteratee): Type
54805480
{
54815481
if ($iteratee instanceof UnionType) {
54825482
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
5483-
if (!$filtered instanceof NeverType) {
5483+
if ($filtered->isNever()->no()) {
54845484
$iteratee = $filtered;
54855485
}
54865486
}

src/Analyser/NodeScopeResolver.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,7 @@ private function processStmtNode(
18331833
}
18341834
}
18351835

1836-
$exhaustive = $scopeForBranches->getType($stmt->cond) instanceof NeverType;
1836+
$exhaustive = $scopeForBranches->getType($stmt->cond)->isNever()->yes();
18371837

18381838
if (!$hasDefaultCase && !$exhaustive) {
18391839
$alwaysTerminating = false;
@@ -1978,7 +1978,7 @@ private function processStmtNode(
19781978
foreach ($throwPoints as $throwPoint) {
19791979
$newThrowPoint = $throwPoint->subtractCatchType($originalCatchType);
19801980

1981-
if ($newThrowPoint->getType() instanceof NeverType) {
1981+
if (!$newThrowPoint->getType()->isNever()->no()) {
19821982
continue;
19831983
}
19841984

@@ -2553,7 +2553,7 @@ private function findEarlyTerminatingExpr(Expr $expr, Scope $scope): ?Expr
25532553
}
25542554

25552555
$exprType = $scope->getType($expr);
2556-
if ($exprType instanceof NeverType && $exprType->isExplicit()) {
2556+
if ($exprType->isExplicitNever()->yes()) {
25572557
return $expr;
25582558
}
25592559

@@ -2803,7 +2803,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
28032803
if ($parametersAcceptor !== null) {
28042804
$normalizedExpr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr;
28052805
$returnType = $parametersAcceptor->getReturnType();
2806-
$isAlwaysTerminating = $isAlwaysTerminating || $returnType instanceof NeverType && $returnType->isExplicit();
2806+
$isAlwaysTerminating = $isAlwaysTerminating || $returnType->isExplicitNever()->yes();
28072807
}
28082808

28092809
if (
@@ -3156,7 +3156,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
31563156
if ($parametersAcceptor !== null) {
31573157
$normalizedExpr = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $expr) ?? $expr;
31583158
$returnType = $parametersAcceptor->getReturnType();
3159-
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
3159+
$isAlwaysTerminating = $returnType->isExplicitNever()->yes();
31603160
}
31613161

31623162
$result = $this->processArgs(
@@ -3367,7 +3367,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
33673367
if ($parametersAcceptor !== null) {
33683368
$normalizedExpr = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $expr) ?? $expr;
33693369
$returnType = $parametersAcceptor->getReturnType();
3370-
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
3370+
$isAlwaysTerminating = $returnType->isExplicitNever()->yes();
33713371
}
33723372
$result = $this->processArgs($stmt, $methodReflection, null, $parametersAcceptor, $normalizedExpr, $scope, $storage, $nodeCallback, $context, $closureBindScope ?? null);
33733373
$scope = $result->getScope();
@@ -3600,7 +3600,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36003600
$leftResult = $this->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
36013601
$rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getTruthyScope(), $storage, $nodeCallback, $context);
36023602
$rightExprType = $rightResult->getScope()->getType($expr->right);
3603-
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
3603+
if ($rightExprType->isExplicitNever()->yes()) {
36043604
$leftMergedWithRightScope = $leftResult->getFalseyScope();
36053605
} else {
36063606
$leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope());
@@ -3622,7 +3622,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36223622
$leftResult = $this->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
36233623
$rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getFalseyScope(), $storage, $nodeCallback, $context);
36243624
$rightExprType = $rightResult->getScope()->getType($expr->right);
3625-
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
3625+
if ($rightExprType->isExplicitNever()->yes()) {
36263626
$leftMergedWithRightScope = $leftResult->getTruthyScope();
36273627
} else {
36283628
$leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope());
@@ -3649,7 +3649,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36493649
$rightScope = $scope->filterByFalseyValue($expr);
36503650
$rightResult = $this->processExprNode($stmt, $expr->right, $rightScope, $storage, $nodeCallback, $context->enterDeep());
36513651
$rightExprType = $scope->getType($expr->right);
3652-
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
3652+
if ($rightExprType->isExplicitNever()->yes()) {
36533653
$scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left]));
36543654
} else {
36553655
$scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left]))->mergeWith($rightResult->getScope());
@@ -4090,12 +4090,12 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
40904090
} elseif ($condType->isFalse()->yes()) {
40914091
$finalScope = $ifFalseScope;
40924092
} else {
4093-
if ($ifTrueType instanceof NeverType && $ifTrueType->isExplicit()) {
4093+
if ($ifTrueType !== null && $ifTrueType->isExplicitNever()->yes()) {
40944094
$finalScope = $ifFalseScope;
40954095
} else {
40964096
$ifFalseType = $ifFalseScope->getType($expr->else);
40974097

4098-
if ($ifFalseType instanceof NeverType && $ifFalseType->isExplicit()) {
4098+
if ($ifFalseType->isExplicitNever()->yes()) {
40994099
$finalScope = $ifTrueScope;
41004100
} else {
41014101
$finalScope = $ifTrueScope->mergeWith($ifFalseScope);
@@ -4355,7 +4355,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
43554355

43564356
if (!$hasDefaultCond && !$hasAlwaysTrueCond) {
43574357
$remainingType = $matchScope->getType($expr->cond);
4358-
if (!$remainingType instanceof NeverType) {
4358+
if ($remainingType->isNever()->no()) {
43594359
$throwPoints[] = InternalThrowPoint::createExplicit($scope, new ObjectType(UnhandledMatchError::class), $expr, false);
43604360
}
43614361
}
@@ -4668,7 +4668,7 @@ private function getFunctionThrowPoint(
46684668
$throwType = $functionReflection->getThrowType();
46694669
if ($throwType === null && $parametersAcceptor !== null) {
46704670
$returnType = $parametersAcceptor->getReturnType();
4671-
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
4671+
if ($returnType->isExplicitNever()->yes()) {
46724672
$throwType = new ObjectType(Throwable::class);
46734673
}
46744674
}
@@ -4726,7 +4726,7 @@ private function getMethodThrowPoint(MethodReflection $methodReflection, Paramet
47264726
$throwType = $methodReflection->getThrowType();
47274727
if ($throwType === null) {
47284728
$returnType = $parametersAcceptor->getReturnType();
4729-
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
4729+
if ($returnType->isExplicitNever()->yes()) {
47304730
$throwType = new ObjectType(Throwable::class);
47314731
}
47324732
}
@@ -5038,7 +5038,7 @@ private function processClosureNode(
50385038
}
50395039

50405040
$returnType = $closureType->getReturnType();
5041-
$isAlwaysTerminating = ($returnType instanceof NeverType && $returnType->isExplicit());
5041+
$isAlwaysTerminating = ($returnType->isExplicitNever()->yes());
50425042

50435043
$this->callNodeCallback($nodeCallback, new InClosureNode($closureType, $expr), $closureScope, $storage);
50445044

@@ -5731,7 +5731,7 @@ private function processArgs(
57315731
$throwPoints = array_merge($throwPoints, $callableThrowPoints);
57325732
$impurePoints = array_merge($impurePoints, array_map(static fn (SimpleImpurePoint $impurePoint) => new ImpurePoint($scope, $arg->value, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $acceptors[0]->getImpurePoints()));
57335733
$returnType = $acceptors[0]->getReturnType();
5734-
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType instanceof NeverType && $returnType->isExplicit());
5734+
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType->isExplicitNever()->yes());
57355735
}
57365736
}
57375737
}
@@ -7196,7 +7196,7 @@ private function processCalledMethod(MethodReflection $methodReflection): ?Mutat
71967196
$endNode = $executionEnd->getNode();
71977197
if ($endNode instanceof Node\Stmt\Expression) {
71987198
$exprType = $statementResult->getScope()->getType($endNode->expr);
7199-
if ($exprType instanceof NeverType && $exprType->isExplicit()) {
7199+
if ($exprType->isExplicitNever()->yes()) {
72007200
continue;
72017201
}
72027202
}

src/Node/ClassPropertiesNode.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use PHPStan\Reflection\MethodReflection;
2222
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
2323
use PHPStan\TrinaryLogic;
24-
use PHPStan\Type\NeverType;
2524
use PHPStan\Type\TypeUtils;
2625
use function array_diff_key;
2726
use function array_key_exists;
@@ -276,7 +275,7 @@ private function collectUninitializedProperties(array $constructors, array $unin
276275
if ($statementResult->isAlwaysTerminating()) {
277276
if ($endNode instanceof Node\Stmt\Expression) {
278277
$exprType = $statementResult->getScope()->getType($endNode->expr);
279-
if ($exprType instanceof NeverType && $exprType->isExplicit()) {
278+
if ($exprType->isExplicitNever()->yes()) {
280279
continue;
281280
}
282281
}

src/Reflection/Callables/FunctionCallableVariant.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPStan\TrinaryLogic;
1010
use PHPStan\Type\Generic\TemplateTypeMap;
1111
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
12-
use PHPStan\Type\NeverType;
1312
use PHPStan\Type\ObjectType;
1413
use PHPStan\Type\Type;
1514
use Throwable;
@@ -97,7 +96,7 @@ public function getThrowPoints(): array
9796
$returnType = $this->variant->getReturnType();
9897
$throwType = $this->function->getThrowType();
9998
if ($throwType === null) {
100-
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
99+
if ($returnType->isExplicitNever()->yes()) {
101100
$throwType = new ObjectType(Throwable::class);
102101
}
103102
}

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ public function createFirstClassCallable(
916916
$returnTypeForThrow = $variant->getReturnType();
917917
$throwType = $function->getThrowType();
918918
if ($throwType === null) {
919-
if ($returnTypeForThrow instanceof NeverType && $returnTypeForThrow->isExplicit()) {
919+
if ($returnTypeForThrow->isExplicitNever()->yes()) {
920920
$throwType = new ObjectType(Throwable::class);
921921
}
922922
}
@@ -972,7 +972,7 @@ public function getBitwiseAndType(Expr $left, Expr $right, callable $getTypeCall
972972

973973
public function getBitwiseAndTypeFromTypes(Type $leftType, Type $rightType): Type
974974
{
975-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
975+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
976976
return $this->getNeverType($leftType, $rightType);
977977
}
978978

@@ -1030,7 +1030,7 @@ public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallb
10301030

10311031
public function getBitwiseOrTypeFromTypes(Type $leftType, Type $rightType): Type
10321032
{
1033-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1033+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
10341034
return $this->getNeverType($leftType, $rightType);
10351035
}
10361036

@@ -1186,7 +1186,7 @@ public function getSpaceshipType(Expr $left, Expr $right, callable $getTypeCallb
11861186

11871187
public function getSpaceshipTypeFromTypes(Type $leftTypes, Type $rightTypes): Type
11881188
{
1189-
if ($leftTypes instanceof NeverType || $rightTypes instanceof NeverType) {
1189+
if ($leftTypes->isNever()->or($rightTypes->isNever())->yes()) {
11901190
return $this->getNeverType($leftTypes, $rightTypes);
11911191
}
11921192

@@ -1283,7 +1283,7 @@ public function getModType(Expr $left, Expr $right, callable $getTypeCallback):
12831283

12841284
public function getModTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
12851285
{
1286-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1286+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
12871287
return $this->getNeverType($leftType, $rightType);
12881288
}
12891289

@@ -1397,7 +1397,7 @@ public function getPlusType(Expr $left, Expr $right, callable $getTypeCallback):
13971397

13981398
public function getPlusTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
13991399
{
1400-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1400+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
14011401
return $this->getNeverType($leftType, $rightType);
14021402
}
14031403

@@ -1725,7 +1725,7 @@ public function getShiftLeftType(Expr $left, Expr $right, callable $getTypeCallb
17251725

17261726
public function getShiftLeftTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
17271727
{
1728-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1728+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
17291729
return $this->getNeverType($leftType, $rightType);
17301730
}
17311731

@@ -1789,7 +1789,7 @@ public function getShiftRightType(Expr $left, Expr $right, callable $getTypeCall
17891789

17901790
public function getShiftRightTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
17911791
{
1792-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1792+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
17931793
return $this->getNeverType($leftType, $rightType);
17941794
}
17951795

@@ -1872,7 +1872,7 @@ private function optimizeScalarType(Type $type): Type
18721872
*/
18731873
public function resolveIdenticalType(Type $leftType, Type $rightType): TypeResult
18741874
{
1875-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1875+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
18761876
return new TypeResult(new ConstantBooleanType(false), []);
18771877
}
18781878

@@ -2054,7 +2054,7 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri
20542054
if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) {
20552055
return new ErrorType();
20562056
}
2057-
if ($leftNumberType instanceof NeverType || $rightNumberType instanceof NeverType) {
2057+
if ($leftNumberType->isNever()->yes() || $rightNumberType->isNever()->yes()) {
20582058
return $this->getNeverType($leftNumberType, $rightNumberType);
20592059
}
20602060

@@ -2689,10 +2689,10 @@ private function getReflectionProvider(): ReflectionProvider
26892689
private function getNeverType(Type $leftType, Type $rightType): Type
26902690
{
26912691
// make sure we don't lose the explicit flag in the process
2692-
if ($leftType instanceof NeverType && $leftType->isExplicit()) {
2692+
if ($leftType->isExplicitNever()->yes()) {
26932693
return $leftType;
26942694
}
2695-
if ($rightType instanceof NeverType && $rightType->isExplicit()) {
2695+
if ($rightType->isExplicitNever()->yes()) {
26962696
return $rightType;
26972697
}
26982698
return new NeverType();

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
use PHPStan\Type\Generic\TemplateTypeMap;
5050
use PHPStan\Type\Generic\TemplateTypeVariance;
5151
use PHPStan\Type\MixedType;
52-
use PHPStan\Type\NeverType;
5352
use PHPStan\Type\Type;
5453
use PHPStan\Type\TypeCombinator;
5554
use PHPStan\Type\TypehintHelper;
@@ -1178,7 +1177,7 @@ private function inferAndCachePropertyTypes(
11781177
}
11791178

11801179
$propertyType = $methodScope->getType($expr->expr);
1181-
if ($propertyType instanceof ErrorType || $propertyType instanceof NeverType) {
1180+
if ($propertyType instanceof ErrorType || !$propertyType->isNever()->no()) {
11821181
continue;
11831182
}
11841183

src/Reflection/Php/PhpPropertyReflection.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PHPStan\Reflection\MissingMethodFromReflectionException;
1212
use PHPStan\TrinaryLogic;
1313
use PHPStan\Type\MixedType;
14-
use PHPStan\Type\NeverType;
1514
use PHPStan\Type\Type;
1615
use PHPStan\Type\TypehintHelper;
1716
use function sprintf;
@@ -126,7 +125,7 @@ public function getWritableType(): Type
126125
return $this->writableType;
127126
}
128127

129-
if ($this->writablePhpDocType === null || $this->writablePhpDocType instanceof NeverType) {
128+
if ($this->writablePhpDocType === null || !$this->writablePhpDocType->isNever()->no()) {
130129
return $this->writableType = TypehintHelper::decideType(
131130
$this->nativeType,
132131
$this->readablePhpDocType,

0 commit comments

Comments
 (0)