Skip to content

Commit 58f3ff6

Browse files
committed
replace instanceof NeverType with isNever method
1 parent 3611a96 commit 58f3ff6

File tree

65 files changed

+133
-159
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

+133
-159
lines changed

phpstan-baseline.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,18 @@ parameters:
14371437
count: 1
14381438
path: src/Type/IterableType.php
14391439

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: 2
1444+
path: src/Type/NeverType.php
1445+
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: 1
1450+
path: src/Type/NonAcceptingNeverType.php
1451+
14401452
-
14411453
rawMessage: 'Doing instanceof PHPStan\Type\ConstantScalarType is error-prone and deprecated. Use Type::isConstantScalarValue() or Type::getConstantScalarTypes() or Type::getConstantScalarValues() instead.'
14421454
identifier: phpstanApi.instanceofType
@@ -1767,6 +1779,12 @@ parameters:
17671779
count: 1
17681780
path: src/Type/TypehintHelper.php
17691781

1782+
-
1783+
rawMessage: 'Call to an undefined method PHPStan\Type\Type::isSubTypeOf().'
1784+
identifier: method.notFound
1785+
count: 1
1786+
path: src/Type/UnionType.php
1787+
17701788
-
17711789
rawMessage: 'Doing instanceof PHPStan\Type\CallableType is error-prone and deprecated. Use Type::isCallable() and Type::getCallableParametersAcceptors() instead.'
17721790
identifier: phpstanApi.instanceofType

src/Analyser/MutatingScope.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,7 +3028,7 @@ public function removeTypeFromExpression(Expr $expr, Type $typeToRemove): self
30283028
}
30293029

30303030
$exprType = $this->getType($expr);
3031-
if ($exprType instanceof NeverType) {
3031+
if ($exprType->isNever()->yes()) {
30323032
return $this;
30333033
}
30343034

@@ -4532,7 +4532,7 @@ public function getIterableKeyType(Type $iteratee): Type
45324532
{
45334533
if ($iteratee instanceof UnionType) {
45344534
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
4535-
if (!$filtered instanceof NeverType) {
4535+
if ($filtered->isNever()->no()) {
45364536
$iteratee = $filtered;
45374537
}
45384538
}
@@ -4544,7 +4544,7 @@ public function getIterableValueType(Type $iteratee): Type
45444544
{
45454545
if ($iteratee instanceof UnionType) {
45464546
$filtered = $iteratee->filterTypes(static fn (Type $innerType) => $innerType->isIterable()->yes());
4547-
if (!$filtered instanceof NeverType) {
4547+
if ($filtered->isNever()->no()) {
45484548
$iteratee = $filtered;
45494549
}
45504550
}

src/Analyser/NodeScopeResolver.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,7 @@ public function processStmtNode(
17991799
}
18001800
}
18011801

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

18041804
if (!$hasDefaultCase && !$exhaustive) {
18051805
$alwaysTerminating = false;
@@ -1949,7 +1949,7 @@ public function processStmtNode(
19491949
foreach ($throwPoints as $throwPoint) {
19501950
$newThrowPoint = $throwPoint->subtractCatchType($originalCatchType);
19511951

1952-
if ($newThrowPoint->getType() instanceof NeverType) {
1952+
if (!$newThrowPoint->getType()->isNever()->no()) {
19531953
continue;
19541954
}
19551955

@@ -2460,7 +2460,7 @@ private function findEarlyTerminatingExpr(Expr $expr, Scope $scope): ?Expr
24602460
}
24612461

24622462
$exprType = $scope->getType($expr);
2463-
if ($exprType instanceof NeverType && $exprType->isExplicit()) {
2463+
if ($exprType->isExplicitNever()->yes()) {
24642464
return $expr;
24652465
}
24662466

@@ -2723,7 +2723,7 @@ public function processClosureNode(
27232723
}
27242724

27252725
$returnType = $closureType->getReturnType();
2726-
$isAlwaysTerminating = ($returnType instanceof NeverType && $returnType->isExplicit());
2726+
$isAlwaysTerminating = ($returnType->isExplicitNever()->yes());
27272727

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

@@ -3416,7 +3416,7 @@ public function processArgs(
34163416
$throwPoints = array_merge($throwPoints, $callableThrowPoints);
34173417
$impurePoints = array_merge($impurePoints, array_map(static fn (SimpleImpurePoint $impurePoint) => new ImpurePoint($scope, $arg->value, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $acceptors[0]->getImpurePoints()));
34183418
$returnType = $acceptors[0]->getReturnType();
3419-
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType instanceof NeverType && $returnType->isExplicit());
3419+
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType->isExplicitNever()->yes());
34203420
}
34213421
}
34223422
}
@@ -4088,7 +4088,7 @@ public function processCalledMethod(MethodReflection $methodReflection): ?Mutati
40884088
$endNode = $executionEnd->getNode();
40894089
if ($endNode instanceof Node\Stmt\Expression) {
40904090
$exprType = $statementResult->getScope()->getType($endNode->expr);
4091-
if ($exprType instanceof NeverType && $exprType->isExplicit()) {
4091+
if ($exprType->isExplicitNever()->yes()) {
40924092
continue;
40934093
}
40944094
}

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
@@ -10,7 +10,6 @@
1010
use PHPStan\TrinaryLogic;
1111
use PHPStan\Type\Generic\TemplateTypeMap;
1212
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
13-
use PHPStan\Type\NeverType;
1413
use PHPStan\Type\ObjectType;
1514
use PHPStan\Type\Type;
1615
use Throwable;
@@ -98,7 +97,7 @@ public function getThrowPoints(): array
9897
$returnType = $this->variant->getReturnType();
9998
$throwType = $this->function->getThrowType();
10099
if ($throwType === null) {
101-
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
100+
if ($returnType->isExplicitNever()->yes()) {
102101
$throwType = new ObjectType(Throwable::class);
103102
}
104103
}

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
}
@@ -991,7 +991,7 @@ public function getBitwiseAndType(Expr $left, Expr $right, callable $getTypeCall
991991

992992
public function getBitwiseAndTypeFromTypes(Type $leftType, Type $rightType): Type
993993
{
994-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
994+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
995995
return $this->getNeverType($leftType, $rightType);
996996
}
997997

@@ -1049,7 +1049,7 @@ public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallb
10491049

10501050
public function getBitwiseOrTypeFromTypes(Type $leftType, Type $rightType): Type
10511051
{
1052-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1052+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
10531053
return $this->getNeverType($leftType, $rightType);
10541054
}
10551055

@@ -1205,7 +1205,7 @@ public function getSpaceshipType(Expr $left, Expr $right, callable $getTypeCallb
12051205

12061206
public function getSpaceshipTypeFromTypes(Type $leftTypes, Type $rightTypes): Type
12071207
{
1208-
if ($leftTypes instanceof NeverType || $rightTypes instanceof NeverType) {
1208+
if ($leftTypes->isNever()->or($rightTypes->isNever())->yes()) {
12091209
return $this->getNeverType($leftTypes, $rightTypes);
12101210
}
12111211

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

13031303
public function getModTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
13041304
{
1305-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1305+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
13061306
return $this->getNeverType($leftType, $rightType);
13071307
}
13081308

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

14171417
public function getPlusTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
14181418
{
1419-
if ($leftType instanceof NeverType || $rightType instanceof NeverType) {
1419+
if ($leftType->isNever()->yes() || $rightType->isNever()->yes()) {
14201420
return $this->getNeverType($leftType, $rightType);
14211421
}
14221422

@@ -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,

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use PHPStan\Type\BenevolentUnionType;
1414
use PHPStan\Type\ErrorType;
1515
use PHPStan\Type\MixedType;
16-
use PHPStan\Type\NeverType;
1716
use PHPStan\Type\Type;
1817
use PHPStan\Type\TypeCombinator;
1918
use PHPStan\Type\TypeUtils;
@@ -65,7 +64,7 @@ public function check(
6564
if ($type->hasOffsetValueType($dimType)->no()) {
6665
if ($type->isArray()->yes()) {
6766
$validArrayDimType = TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);
68-
if ($validArrayDimType instanceof NeverType) {
67+
if (!$validArrayDimType->isNever()->no()) {
6968
// Already reported by InvalidKeyInArrayDimFetchRule
7069
return [];
7170
}

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use PHPStan\Type\Generic\GenericClassStringType;
2525
use PHPStan\Type\IntersectionType;
2626
use PHPStan\Type\MixedType;
27-
use PHPStan\Type\NeverType;
2827
use PHPStan\Type\ObjectType;
2928
use PHPStan\Type\Type;
3029
use PHPStan\Type\TypeTraverser;
@@ -150,7 +149,7 @@ public function findSpecifiedType(
150149

151150
if (!$haystackType instanceof ConstantArrayType || count($haystackType->getValueTypes()) > 0) {
152151
$haystackArrayTypes = $haystackType->getArrays();
153-
if (count($haystackArrayTypes) === 1 && $haystackArrayTypes[0]->getIterableValueType() instanceof NeverType) {
152+
if (count($haystackArrayTypes) === 1 && !$haystackArrayTypes[0]->getIterableValueType()->isNever()->no()) {
154153
return null;
155154
}
156155

0 commit comments

Comments
 (0)