Skip to content

Commit 9b20b0e

Browse files
committed
replace instanceof NeverType with isNever method
1 parent 6dc1d1f commit 9b20b0e

File tree

65 files changed

+145
-171
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

+145
-171
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,7 +3689,7 @@ public function removeTypeFromExpression(Expr $expr, Type $typeToRemove): self
36893689
}
36903690

36913691
$exprType = $this->getType($expr);
3692-
if ($exprType instanceof NeverType) {
3692+
if ($exprType->isNever()->yes()) {
36933693
return $this;
36943694
}
36953695

@@ -5093,7 +5093,7 @@ private function exactInstantiation(New_ $node, Name $className): Type
50935093
}
50945094

50955095
$methodResult = $this->getType($methodCall);
5096-
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
5096+
if ($methodResult->isExplicitNever()->yes()) {
50975097
return $methodResult;
50985098
}
50995099

@@ -5532,7 +5532,7 @@ public function getIterableKeyType(Type $iteratee): Type
55325532
{
55335533
if ($iteratee instanceof UnionType) {
55345534
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
5535-
if (!$filtered instanceof NeverType) {
5535+
if ($filtered->isNever()->no()) {
55365536
$iteratee = $filtered;
55375537
}
55385538
}
@@ -5544,7 +5544,7 @@ public function getIterableValueType(Type $iteratee): Type
55445544
{
55455545
if ($iteratee instanceof UnionType) {
55465546
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
5547-
if (!$filtered instanceof NeverType) {
5547+
if ($filtered->isNever()->no()) {
55485548
$iteratee = $filtered;
55495549
}
55505550
}

src/Analyser/NodeScopeResolver.php

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

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

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

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

@@ -2575,7 +2575,7 @@ private function findEarlyTerminatingExpr(Expr $expr, Scope $scope): ?Expr
25752575
}
25762576

25772577
$exprType = $scope->getType($expr);
2578-
if ($exprType instanceof NeverType && $exprType->isExplicit()) {
2578+
if ($exprType->isExplicitNever()->yes()) {
25792579
return $expr;
25802580
}
25812581

@@ -2825,7 +2825,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
28252825
if ($parametersAcceptor !== null) {
28262826
$normalizedExpr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr;
28272827
$returnType = $parametersAcceptor->getReturnType();
2828-
$isAlwaysTerminating = $isAlwaysTerminating || $returnType instanceof NeverType && $returnType->isExplicit();
2828+
$isAlwaysTerminating = $isAlwaysTerminating || $returnType->isExplicitNever()->yes();
28292829
}
28302830

28312831
if (
@@ -3188,7 +3188,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
31883188
if ($parametersAcceptor !== null) {
31893189
$normalizedExpr = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $expr) ?? $expr;
31903190
$returnType = $parametersAcceptor->getReturnType();
3191-
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
3191+
$isAlwaysTerminating = $returnType->isExplicitNever()->yes();
31923192
}
31933193

31943194
$result = $this->processArgs(
@@ -3399,7 +3399,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
33993399
if ($parametersAcceptor !== null) {
34003400
$normalizedExpr = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $expr) ?? $expr;
34013401
$returnType = $parametersAcceptor->getReturnType();
3402-
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
3402+
$isAlwaysTerminating = $returnType->isExplicitNever()->yes();
34033403
}
34043404
$result = $this->processArgs($stmt, $methodReflection, null, $parametersAcceptor, $normalizedExpr, $scope, $storage, $nodeCallback, $context, $closureBindScope ?? null);
34053405
$scope = $result->getScope();
@@ -3632,7 +3632,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36323632
$leftResult = $this->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
36333633
$rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getTruthyScope(), $storage, $nodeCallback, $context);
36343634
$rightExprType = $rightResult->getScope()->getType($expr->right);
3635-
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
3635+
if ($rightExprType->isExplicitNever()->yes()) {
36363636
$leftMergedWithRightScope = $leftResult->getFalseyScope();
36373637
} else {
36383638
$leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope());
@@ -3654,7 +3654,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36543654
$leftResult = $this->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
36553655
$rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getFalseyScope(), $storage, $nodeCallback, $context);
36563656
$rightExprType = $rightResult->getScope()->getType($expr->right);
3657-
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
3657+
if ($rightExprType->isExplicitNever()->yes()) {
36583658
$leftMergedWithRightScope = $leftResult->getTruthyScope();
36593659
} else {
36603660
$leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope());
@@ -3681,7 +3681,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36813681
$rightScope = $scope->filterByFalseyValue($expr);
36823682
$rightResult = $this->processExprNode($stmt, $expr->right, $rightScope, $storage, $nodeCallback, $context->enterDeep());
36833683
$rightExprType = $scope->getType($expr->right);
3684-
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
3684+
if ($rightExprType->isExplicitNever()->yes()) {
36853685
$scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left]));
36863686
} else {
36873687
$scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left]))->mergeWith($rightResult->getScope());
@@ -4122,12 +4122,12 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
41224122
} elseif ($condType->isFalse()->yes()) {
41234123
$finalScope = $ifFalseScope;
41244124
} else {
4125-
if ($ifTrueType instanceof NeverType && $ifTrueType->isExplicit()) {
4125+
if ($ifTrueType !== null && $ifTrueType->isExplicitNever()->yes()) {
41264126
$finalScope = $ifFalseScope;
41274127
} else {
41284128
$ifFalseType = $ifFalseScope->getType($expr->else);
41294129

4130-
if ($ifFalseType instanceof NeverType && $ifFalseType->isExplicit()) {
4130+
if ($ifFalseType->isExplicitNever()->yes()) {
41314131
$finalScope = $ifTrueScope;
41324132
} else {
41334133
$finalScope = $ifTrueScope->mergeWith($ifFalseScope);
@@ -4396,7 +4396,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
43964396
$isExhaustive = $hasDefaultCond || $hasAlwaysTrueCond;
43974397
if (!$isExhaustive) {
43984398
$remainingType = $matchScope->getType($expr->cond);
4399-
if ($remainingType instanceof NeverType) {
4399+
if ($remainingType->isNever()->no()) {
44004400
$isExhaustive = true;
44014401
}
44024402
}
@@ -4726,7 +4726,7 @@ private function getFunctionThrowPoint(
47264726
$throwType = $functionReflection->getThrowType();
47274727
if ($throwType === null && $parametersAcceptor !== 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
}
@@ -4784,7 +4784,7 @@ private function getMethodThrowPoint(MethodReflection $methodReflection, Paramet
47844784
$throwType = $methodReflection->getThrowType();
47854785
if ($throwType === null) {
47864786
$returnType = $parametersAcceptor->getReturnType();
4787-
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
4787+
if ($returnType->isExplicitNever()->yes()) {
47884788
$throwType = new ObjectType(Throwable::class);
47894789
}
47904790
}
@@ -5096,7 +5096,7 @@ private function processClosureNode(
50965096
}
50975097

50985098
$returnType = $closureType->getReturnType();
5099-
$isAlwaysTerminating = ($returnType instanceof NeverType && $returnType->isExplicit());
5099+
$isAlwaysTerminating = ($returnType->isExplicitNever()->yes());
51005100

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

@@ -5789,7 +5789,7 @@ private function processArgs(
57895789
$throwPoints = array_merge($throwPoints, $callableThrowPoints);
57905790
$impurePoints = array_merge($impurePoints, array_map(static fn (SimpleImpurePoint $impurePoint) => new ImpurePoint($scope, $arg->value, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $acceptors[0]->getImpurePoints()));
57915791
$returnType = $acceptors[0]->getReturnType();
5792-
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType instanceof NeverType && $returnType->isExplicit());
5792+
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType->isExplicitNever()->yes());
57935793
}
57945794
}
57955795
}
@@ -7254,7 +7254,7 @@ private function processCalledMethod(MethodReflection $methodReflection): ?Mutat
72547254
$endNode = $executionEnd->getNode();
72557255
if ($endNode instanceof Node\Stmt\Expression) {
72567256
$exprType = $statementResult->getScope()->getType($endNode->expr);
7257-
if ($exprType instanceof NeverType && $exprType->isExplicit()) {
7257+
if ($exprType->isExplicitNever()->yes()) {
72587258
continue;
72597259
}
72607260
}

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
@@ -933,7 +933,7 @@ public function createFirstClassCallable(
933933
$returnTypeForThrow = $variant->getReturnType();
934934
$throwType = $function->getThrowType();
935935
if ($throwType === null) {
936-
if ($returnTypeForThrow instanceof NeverType && $returnTypeForThrow->isExplicit()) {
936+
if ($returnTypeForThrow->isExplicitNever()->yes()) {
937937
$throwType = new ObjectType(Throwable::class);
938938
}
939939
}
@@ -989,7 +989,7 @@ public function getBitwiseAndType(Expr $left, Expr $right, callable $getTypeCall
989989

990990
public function getBitwiseAndTypeFromTypes(Type $leftType, Type $rightType): Type
991991
{
992-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
992+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
993993
return $this->getNeverType($leftType, $rightType);
994994
}
995995

@@ -1047,7 +1047,7 @@ public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallb
10471047

10481048
public function getBitwiseOrTypeFromTypes(Type $leftType, Type $rightType): Type
10491049
{
1050-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1050+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
10511051
return $this->getNeverType($leftType, $rightType);
10521052
}
10531053

@@ -1203,7 +1203,7 @@ public function getSpaceshipType(Expr $left, Expr $right, callable $getTypeCallb
12031203

12041204
public function getSpaceshipTypeFromTypes(Type $leftTypes, Type $rightTypes): Type
12051205
{
1206-
if ($leftTypes instanceof NeverType || $rightTypes instanceof NeverType) {
1206+
if ($leftTypes->isNever()->or($rightTypes->isNever())->yes()) {
12071207
return $this->getNeverType($leftTypes, $rightTypes);
12081208
}
12091209

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

13011301
public function getModTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
13021302
{
1303-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1303+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
13041304
return $this->getNeverType($leftType, $rightType);
13051305
}
13061306

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

14151415
public function getPlusTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
14161416
{
1417-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1417+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
14181418
return $this->getNeverType($leftType, $rightType);
14191419
}
14201420

@@ -1761,7 +1761,7 @@ public function getShiftLeftType(Expr $left, Expr $right, callable $getTypeCallb
17611761

17621762
public function getShiftLeftTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
17631763
{
1764-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1764+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
17651765
return $this->getNeverType($leftType, $rightType);
17661766
}
17671767

@@ -1825,7 +1825,7 @@ public function getShiftRightType(Expr $left, Expr $right, callable $getTypeCall
18251825

18261826
public function getShiftRightTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
18271827
{
1828-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1828+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
18291829
return $this->getNeverType($leftType, $rightType);
18301830
}
18311831

@@ -1908,7 +1908,7 @@ private function optimizeScalarType(Type $type): Type
19081908
*/
19091909
public function resolveIdenticalType(Type $leftType, Type $rightType): TypeResult
19101910
{
1911-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1911+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
19121912
return new TypeResult(new ConstantBooleanType(false), []);
19131913
}
19141914

@@ -2090,7 +2090,7 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri
20902090
if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) {
20912091
return new ErrorType();
20922092
}
2093-
if ($leftNumberType instanceof NeverType || $rightNumberType instanceof NeverType) {
2093+
if ($leftNumberType->isNever()->yes() || $rightNumberType->isNever()->yes()) {
20942094
return $this->getNeverType($leftNumberType, $rightNumberType);
20952095
}
20962096

@@ -2725,10 +2725,10 @@ private function getReflectionProvider(): ReflectionProvider
27252725
private function getNeverType(Type $leftType, Type $rightType): Type
27262726
{
27272727
// make sure we don't lose the explicit flag in the process
2728-
if ($leftType instanceof NeverType && $leftType->isExplicit()) {
2728+
if ($leftType->isExplicitNever()->yes()) {
27292729
return $leftType;
27302730
}
2731-
if ($rightType instanceof NeverType && $rightType->isExplicit()) {
2731+
if ($rightType->isExplicitNever()->yes()) {
27322732
return $rightType;
27332733
}
27342734
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)