Skip to content

Commit f0d21a7

Browse files
canvuralondrejmirtes
authored andcommitted
replace instanceof NeverType with isNever method
1 parent 77035d4 commit f0d21a7

File tree

65 files changed

+148
-174
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

+148
-174
lines changed

phpstan-baseline.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,18 @@ parameters:
14431443
count: 1
14441444
path: src/Type/IterableType.php
14451445

1446+
-
1447+
rawMessage: 'Doing instanceof PHPStan\Type\NeverType is error-prone and deprecated. Use Type::isNever() or Type::isExplicitNever() instead.'
1448+
identifier: phpstanApi.instanceofType
1449+
count: 2
1450+
path: src/Type/NeverType.php
1451+
1452+
-
1453+
rawMessage: 'Doing instanceof PHPStan\Type\NeverType is error-prone and deprecated. Use Type::isNever() or Type::isExplicitNever() instead.'
1454+
identifier: phpstanApi.instanceofType
1455+
count: 1
1456+
path: src/Type/NonAcceptingNeverType.php
1457+
14461458
-
14471459
rawMessage: 'Doing instanceof PHPStan\Type\ConstantScalarType is error-prone and deprecated. Use Type::isConstantScalarValue() or Type::getConstantScalarTypes() or Type::getConstantScalarValues() instead.'
14481460
identifier: phpstanApi.instanceofType
@@ -1785,6 +1797,12 @@ parameters:
17851797
count: 1
17861798
path: src/Type/TypehintHelper.php
17871799

1800+
-
1801+
rawMessage: 'Call to an undefined method PHPStan\Type\Type::isSubTypeOf().'
1802+
identifier: method.notFound
1803+
count: 1
1804+
path: src/Type/UnionType.php
1805+
17881806
-
17891807
rawMessage: 'Doing instanceof PHPStan\Type\CallableType is error-prone and deprecated. Use Type::isCallable() and Type::getCallableParametersAcceptors() instead.'
17901808
identifier: phpstanApi.instanceofType

src/Analyser/MutatingScope.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ private function resolveType(string $exprString, Expr $node): Type
10681068
) {
10691069
return new BooleanType();
10701070
}
1071-
if ($expressionType instanceof NeverType) {
1071+
if (!$expressionType->isNever()->no()) {
10721072
return new ConstantBooleanType(false);
10731073
}
10741074

@@ -4578,8 +4578,8 @@ public function removeTypeFromExpression(Expr $expr, Type $typeToRemove): self
45784578
{
45794579
$exprType = $this->getType($expr);
45804580
if (
4581-
$exprType instanceof NeverType ||
4582-
$typeToRemove instanceof NeverType
4581+
!$exprType->isNever()->no() ||
4582+
!$typeToRemove->isNever()->no()
45834583
) {
45844584
return $this;
45854585
}
@@ -5891,7 +5891,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
58915891
}
58925892

58935893
$methodResult = $this->getType($methodCall);
5894-
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
5894+
if ($methodResult->isExplicitNever()->yes()) {
58955895
return $methodResult;
58965896
}
58975897

@@ -6347,7 +6347,7 @@ public function getIterableKeyType(Type $iteratee): Type
63476347
{
63486348
if ($iteratee instanceof UnionType) {
63496349
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
6350-
if (!$filtered instanceof NeverType) {
6350+
if ($filtered->isNever()->no()) {
63516351
$iteratee = $filtered;
63526352
}
63536353
}
@@ -6359,7 +6359,7 @@ public function getIterableValueType(Type $iteratee): Type
63596359
{
63606360
if ($iteratee instanceof UnionType) {
63616361
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
6362-
if (!$filtered instanceof NeverType) {
6362+
if ($filtered->isNever()->no()) {
63636363
$iteratee = $filtered;
63646364
}
63656365
}

src/Analyser/NodeScopeResolver.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ private function processStmtNode(
17481748
}
17491749
}
17501750

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

17531753
if (!$hasDefaultCase && !$exhaustive) {
17541754
$alwaysTerminating = false;
@@ -1893,7 +1893,7 @@ private function processStmtNode(
18931893
foreach ($throwPoints as $throwPoint) {
18941894
$newThrowPoint = $throwPoint->subtractCatchType($originalCatchType);
18951895

1896-
if ($newThrowPoint->getType() instanceof NeverType) {
1896+
if (!$newThrowPoint->getType()->isNever()->no()) {
18971897
continue;
18981898
}
18991899

@@ -2489,7 +2489,7 @@ private function findEarlyTerminatingExpr(Expr $expr, Scope $scope): ?Expr
24892489
}
24902490

24912491
$exprType = $scope->getType($expr);
2492-
if ($exprType instanceof NeverType && $exprType->isExplicit()) {
2492+
if ($exprType->isExplicitNever()->yes()) {
24932493
return $expr;
24942494
}
24952495

@@ -2709,7 +2709,7 @@ static function (): void {
27092709
if ($parametersAcceptor !== null) {
27102710
$expr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr;
27112711
$returnType = $parametersAcceptor->getReturnType();
2712-
$isAlwaysTerminating = $isAlwaysTerminating || $returnType instanceof NeverType && $returnType->isExplicit();
2712+
$isAlwaysTerminating = $isAlwaysTerminating || $returnType->isExplicitNever()->yes();
27132713
}
27142714

27152715
if (
@@ -3032,7 +3032,7 @@ static function (): void {
30323032
if ($parametersAcceptor !== null) {
30333033
$expr = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $expr) ?? $expr;
30343034
$returnType = $parametersAcceptor->getReturnType();
3035-
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
3035+
$isAlwaysTerminating = $returnType->isExplicitNever()->yes();
30363036
}
30373037

30383038
$result = $this->processArgs(
@@ -3224,7 +3224,7 @@ static function (): void {
32243224
if ($parametersAcceptor !== null) {
32253225
$expr = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $expr) ?? $expr;
32263226
$returnType = $parametersAcceptor->getReturnType();
3227-
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
3227+
$isAlwaysTerminating = $returnType->isExplicitNever()->yes();
32283228
}
32293229
$result = $this->processArgs($stmt, $methodReflection, null, $parametersAcceptor, $expr, $scope, $nodeCallback, $context, $closureBindScope ?? null);
32303230
$scope = $result->getScope();
@@ -3450,7 +3450,7 @@ static function (): void {
34503450
$leftResult = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep());
34513451
$rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getTruthyScope(), $nodeCallback, $context);
34523452
$rightExprType = $rightResult->getScope()->getType($expr->right);
3453-
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
3453+
if ($rightExprType->isExplicitNever()->yes()) {
34543454
$leftMergedWithRightScope = $leftResult->getFalseyScope();
34553455
} else {
34563456
$leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope());
@@ -3471,7 +3471,7 @@ static function (): void {
34713471
$leftResult = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep());
34723472
$rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getFalseyScope(), $nodeCallback, $context);
34733473
$rightExprType = $rightResult->getScope()->getType($expr->right);
3474-
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
3474+
if ($rightExprType->isExplicitNever()->yes()) {
34753475
$leftMergedWithRightScope = $leftResult->getTruthyScope();
34763476
} else {
34773477
$leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope());
@@ -3498,7 +3498,7 @@ static function (): void {
34983498
$rightScope = $scope->filterByFalseyValue($expr);
34993499
$rightResult = $this->processExprNode($stmt, $expr->right, $rightScope, $nodeCallback, $context->enterDeep());
35003500
$rightExprType = $scope->getType($expr->right);
3501-
if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) {
3501+
if ($rightExprType->isExplicitNever()->yes()) {
35023502
$scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left]));
35033503
} else {
35043504
$scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left]))->mergeWith($rightResult->getScope());
@@ -3936,12 +3936,12 @@ static function (): void {
39363936
} elseif ($condType->isFalse()->yes()) {
39373937
$finalScope = $ifFalseScope;
39383938
} else {
3939-
if ($ifTrueType instanceof NeverType && $ifTrueType->isExplicit()) {
3939+
if ($ifTrueType !== null && $ifTrueType->isExplicitNever()->yes()) {
39403940
$finalScope = $ifFalseScope;
39413941
} else {
39423942
$ifFalseType = $ifFalseScope->getType($expr->else);
39433943

3944-
if ($ifFalseType instanceof NeverType && $ifFalseType->isExplicit()) {
3944+
if ($ifFalseType->isExplicitNever()->yes()) {
39453945
$finalScope = $ifTrueScope;
39463946
} else {
39473947
$finalScope = $ifTrueScope->mergeWith($ifFalseScope);
@@ -4196,7 +4196,7 @@ static function (): void {
41964196
}
41974197

41984198
$remainingType = $matchScope->getType($expr->cond);
4199-
if (!$hasDefaultCond && !$hasAlwaysTrueCond && !$remainingType instanceof NeverType) {
4199+
if (!$hasDefaultCond && !$hasAlwaysTrueCond && $remainingType->isNever()->no()) {
42004200
$throwPoints[] = ThrowPoint::createExplicit($scope, new ObjectType(UnhandledMatchError::class), $expr, false);
42014201
}
42024202

@@ -4502,7 +4502,7 @@ private function getFunctionThrowPoint(
45024502
$throwType = $functionReflection->getThrowType();
45034503
if ($throwType === null && $parametersAcceptor !== null) {
45044504
$returnType = $parametersAcceptor->getReturnType();
4505-
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
4505+
if ($returnType->isExplicitNever()->yes()) {
45064506
$throwType = new ObjectType(Throwable::class);
45074507
}
45084508
}
@@ -4560,7 +4560,7 @@ private function getMethodThrowPoint(MethodReflection $methodReflection, Paramet
45604560
$throwType = $methodReflection->getThrowType();
45614561
if ($throwType === null) {
45624562
$returnType = $parametersAcceptor->getReturnType();
4563-
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
4563+
if ($returnType->isExplicitNever()->yes()) {
45644564
$throwType = new ObjectType(Throwable::class);
45654565
}
45664566
}
@@ -4857,7 +4857,7 @@ private function processClosureNode(
48574857
}
48584858

48594859
$returnType = $closureType->getReturnType();
4860-
$isAlwaysTerminating = ($returnType instanceof NeverType && $returnType->isExplicit());
4860+
$isAlwaysTerminating = ($returnType->isExplicitNever()->yes());
48614861

48624862
$nodeCallback(new InClosureNode($closureType, $expr), $closureScope);
48634863

@@ -5545,7 +5545,7 @@ private function processArgs(
55455545
$throwPoints = array_merge($throwPoints, $callableThrowPoints);
55465546
$impurePoints = array_merge($impurePoints, array_map(static fn (SimpleImpurePoint $impurePoint) => new ImpurePoint($scope, $arg->value, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $acceptors[0]->getImpurePoints()));
55475547
$returnType = $acceptors[0]->getReturnType();
5548-
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType instanceof NeverType && $returnType->isExplicit());
5548+
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType->isExplicitNever()->yes());
55495549
}
55505550
}
55515551
}
@@ -6961,7 +6961,7 @@ private function processCalledMethod(MethodReflection $methodReflection): ?Mutat
69616961
$endNode = $executionEnd->getNode();
69626962
if ($endNode instanceof Node\Stmt\Expression) {
69636963
$exprType = $statementResult->getScope()->getType($endNode->expr);
6964-
if ($exprType instanceof NeverType && $exprType->isExplicit()) {
6964+
if ($exprType->isExplicitNever()->yes()) {
69656965
continue;
69666966
}
69676967
}

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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ public function createFirstClassCallable(
903903
$returnTypeForThrow = $variant->getReturnType();
904904
$throwType = $function->getThrowType();
905905
if ($throwType === null) {
906-
if ($returnTypeForThrow instanceof NeverType && $returnTypeForThrow->isExplicit()) {
906+
if ($returnTypeForThrow->isExplicitNever()->yes()) {
907907
$throwType = new ObjectType(Throwable::class);
908908
}
909909
}
@@ -954,7 +954,7 @@ public function getBitwiseAndType(Expr $left, Expr $right, callable $getTypeCall
954954
$leftType = $getTypeCallback($left);
955955
$rightType = $getTypeCallback($right);
956956

957-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
957+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
958958
return $this->getNeverType($leftType, $rightType);
959959
}
960960

@@ -1023,7 +1023,7 @@ public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallb
10231023
$leftType = $getTypeCallback($left);
10241024
$rightType = $getTypeCallback($right);
10251025

1026-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1026+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
10271027
return $this->getNeverType($leftType, $rightType);
10281028
}
10291029

@@ -1082,7 +1082,7 @@ public function getBitwiseXorType(Expr $left, Expr $right, callable $getTypeCall
10821082
$leftType = $getTypeCallback($left);
10831083
$rightType = $getTypeCallback($right);
10841084

1085-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1085+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
10861086
return $this->getNeverType($leftType, $rightType);
10871087
}
10881088

@@ -1141,7 +1141,7 @@ public function getSpaceshipType(Expr $left, Expr $right, callable $getTypeCallb
11411141
$callbackLeftType = $getTypeCallback($left);
11421142
$callbackRightType = $getTypeCallback($right);
11431143

1144-
if ($callbackLeftType instanceof NeverType || $callbackRightType instanceof NeverType) {
1144+
if ($callbackLeftType->isNever()->yes() || $callbackRightType->isNever()->yes()) {
11451145
return $this->getNeverType($callbackLeftType, $callbackRightType);
11461146
}
11471147

@@ -1228,7 +1228,7 @@ public function getModType(Expr $left, Expr $right, callable $getTypeCallback):
12281228
$leftType = $getTypeCallback($left);
12291229
$rightType = $getTypeCallback($right);
12301230

1231-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1231+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
12321232
return $this->getNeverType($leftType, $rightType);
12331233
}
12341234

@@ -1337,7 +1337,7 @@ public function getPlusType(Expr $left, Expr $right, callable $getTypeCallback):
13371337
$leftType = $getTypeCallback($left);
13381338
$rightType = $getTypeCallback($right);
13391339

1340-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1340+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
13411341
return $this->getNeverType($leftType, $rightType);
13421342
}
13431343

@@ -1645,7 +1645,7 @@ public function getShiftLeftType(Expr $left, Expr $right, callable $getTypeCallb
16451645
$leftType = $getTypeCallback($left);
16461646
$rightType = $getTypeCallback($right);
16471647

1648-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1648+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
16491649
return $this->getNeverType($leftType, $rightType);
16501650
}
16511651

@@ -1704,7 +1704,7 @@ public function getShiftRightType(Expr $left, Expr $right, callable $getTypeCall
17041704
$leftType = $getTypeCallback($left);
17051705
$rightType = $getTypeCallback($right);
17061706

1707-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1707+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
17081708
return $this->getNeverType($leftType, $rightType);
17091709
}
17101710

@@ -1787,7 +1787,7 @@ private function optimizeScalarType(Type $type): Type
17871787
*/
17881788
public function resolveIdenticalType(Type $leftType, Type $rightType): TypeResult
17891789
{
1790-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1790+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
17911791
return new TypeResult(new ConstantBooleanType(false), []);
17921792
}
17931793

@@ -1969,7 +1969,7 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri
19691969
if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) {
19701970
return new ErrorType();
19711971
}
1972-
if ($leftNumberType instanceof NeverType || $rightNumberType instanceof NeverType) {
1972+
if ($leftNumberType->isNever()->yes() || $rightNumberType->isNever()->yes()) {
19731973
return $this->getNeverType($leftNumberType, $rightNumberType);
19741974
}
19751975

@@ -2587,10 +2587,10 @@ private function getReflectionProvider(): ReflectionProvider
25872587
private function getNeverType(Type $leftType, Type $rightType): Type
25882588
{
25892589
// make sure we don't lose the explicit flag in the process
2590-
if ($leftType instanceof NeverType && $leftType->isExplicit()) {
2590+
if ($leftType->isExplicitNever()->yes()) {
25912591
return $leftType;
25922592
}
2593-
if ($rightType instanceof NeverType && $rightType->isExplicit()) {
2593+
if ($rightType->isExplicitNever()->yes()) {
25942594
return $rightType;
25952595
}
25962596
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;
@@ -1161,7 +1160,7 @@ private function inferAndCachePropertyTypes(
11611160
}
11621161

11631162
$propertyType = $methodScope->getType($expr->expr);
1164-
if ($propertyType instanceof ErrorType || $propertyType instanceof NeverType) {
1163+
if ($propertyType instanceof ErrorType || !$propertyType->isNever()->no()) {
11651164
continue;
11661165
}
11671166

0 commit comments

Comments
 (0)