Skip to content

Commit 9cf0e44

Browse files
committed
Disallow instanceof ObjectType
1 parent 17b0bbd commit 9cf0e44

7 files changed

+33
-9
lines changed

phpstan-baseline.neon

+25
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ parameters:
249249
count: 1
250250
path: src/PhpDoc/TypeNodeResolver.php
251251

252+
-
253+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
254+
count: 2
255+
path: src/PhpDoc/TypeNodeResolver.php
256+
252257
-
253258
message: "#^Dead catch \\- PHPStan\\\\BetterReflection\\\\Identifier\\\\Exception\\\\InvalidIdentifierName is never thrown in the try block\\.$#"
254259
count: 2
@@ -678,6 +683,11 @@ parameters:
678683
count: 2
679684
path: src/Type/Generic/GenericClassStringType.php
680685

686+
-
687+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
688+
count: 1
689+
path: src/Type/Generic/GenericObjectType.php
690+
681691
-
682692
message: "#^Doing instanceof PHPStan\\\\Type\\\\TypeWithClassName is error\\-prone\\. Use Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
683693
count: 1
@@ -718,6 +728,11 @@ parameters:
718728
count: 1
719729
path: src/Type/Generic/TemplateTypeFactory.php
720730

731+
-
732+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
733+
count: 1
734+
path: src/Type/Generic/TemplateTypeFactory.php
735+
721736
-
722737
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectWithoutClassType is error\\-prone\\. Use Type\\:\\:isObject\\(\\) instead\\.$#"
723738
count: 1
@@ -789,6 +804,11 @@ parameters:
789804
count: 1
790805
path: src/Type/ObjectType.php
791806

807+
-
808+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
809+
count: 6
810+
path: src/Type/ObjectType.php
811+
792812
-
793813
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectWithoutClassType is error\\-prone\\. Use Type\\:\\:isObject\\(\\) instead\\.$#"
794814
count: 3
@@ -947,6 +967,11 @@ parameters:
947967
count: 1
948968
path: src/Type/Php/StrlenFunctionReturnTypeExtension.php
949969

970+
-
971+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
972+
count: 3
973+
path: src/Type/StaticType.php
974+
950975
-
951976
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectWithoutClassType is error\\-prone\\. Use Type\\:\\:isObject\\(\\) instead\\.$#"
952977
count: 1

src/Rules/Api/ApiInstanceofTypeRule.php

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use PHPStan\Type\IntegerType;
3232
use PHPStan\Type\IterableType;
3333
use PHPStan\Type\NullType;
34+
use PHPStan\Type\ObjectType;
3435
use PHPStan\Type\ObjectWithoutClassType;
3536
use PHPStan\Type\StringType;
3637
use PHPStan\Type\TypeWithClassName;
@@ -62,6 +63,7 @@ class ApiInstanceofTypeRule implements Rule
6263
CallableType::class => 'Type::isCallable() and Type::getCallableParametersAcceptors()',
6364
IterableType::class => 'Type::isIterable()',
6465
ObjectWithoutClassType::class => 'Type::isObject()',
66+
ObjectType::class => 'Type::isObject() or Type::getObjectClassNames()',
6567

6668
// accessory types
6769
NonEmptyArrayType::class => 'Type::isIterableAtLeastOnce()',

src/Rules/Classes/AllowedSubTypesRule.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Node\InClassNode;
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
10-
use PHPStan\Type\ObjectType;
1110
use function array_values;
1211
use function sprintf;
1312

@@ -45,11 +44,11 @@ public function processNode(Node $node, Scope $scope): array
4544
}
4645

4746
foreach ($allowedSubTypes as $allowedSubType) {
48-
if (!$allowedSubType instanceof ObjectType) {
47+
if (!$allowedSubType->isObject()->yes()) {
4948
continue;
5049
}
5150

52-
if ($className === $allowedSubType->getClassName()) {
51+
if ($allowedSubType->getObjectClassNames() === [$className]) {
5352
continue 2;
5453
}
5554
}

src/Rules/Classes/LocalTypeAliasesRule.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPStan\Type\CircularTypeAliasErrorType;
1515
use PHPStan\Type\ErrorType;
1616
use PHPStan\Type\Generic\TemplateType;
17-
use PHPStan\Type\ObjectType;
1817
use PHPStan\Type\Type;
1918
use PHPStan\Type\TypeTraverser;
2019
use function array_key_exists;
@@ -178,7 +177,7 @@ private function isAliasNameValid(string $aliasName, ?NameScope $nameScope): boo
178177
}
179178

180179
$aliasNameResolvedType = $this->typeNodeResolver->resolve(new IdentifierTypeNode($aliasName), $nameScope->bypassTypeAliases());
181-
return ($aliasNameResolvedType instanceof ObjectType && !in_array($aliasName, ['self', 'parent'], true))
180+
return ($aliasNameResolvedType->isObject()->yes() && !in_array($aliasName, ['self', 'parent'], true))
182181
|| $aliasNameResolvedType instanceof TemplateType; // aliases take precedence over type parameters, this is reported by other rules using TemplateTypeCheck
183182
}
184183

src/Rules/Methods/MethodParameterComparisonHelper.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PHPStan\Type\ArrayType;
1313
use PHPStan\Type\IterableType;
1414
use PHPStan\Type\MixedType;
15-
use PHPStan\Type\ObjectType;
1615
use PHPStan\Type\Type;
1716
use PHPStan\Type\TypeCombinator;
1817
use PHPStan\Type\VerbosityLevel;
@@ -402,7 +401,7 @@ private function isTypeCompatible(Type $methodParameterType, Type $prototypePara
402401
if ($prototypeParameterType instanceof ArrayType) {
403402
return true;
404403
}
405-
if ($prototypeParameterType instanceof ObjectType && $prototypeParameterType->getClassName() === Traversable::class) {
404+
if ($prototypeParameterType->isObject()->yes() && $prototypeParameterType->getObjectClassNames() === [Traversable::class]) {
406405
return true;
407406
}
408407
}

src/Type/ParserNodeTypeToPHPStanType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function resolve($type, ?ClassReflection $classReflection): Type
5353
$types = [];
5454
foreach ($type->types as $intersectionTypeType) {
5555
$innerType = self::resolve($intersectionTypeType, $classReflection);
56-
if (!$innerType instanceof ObjectType) {
56+
if (!$innerType->isObject()->yes()) {
5757
return new NeverType();
5858
}
5959

src/Type/TypehintHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static function decideTypeFromReflection(
9999
$types = [];
100100
foreach ($reflectionType->getTypes() as $innerReflectionType) {
101101
$innerType = self::decideTypeFromReflection($innerReflectionType, null, $selfClass, false);
102-
if (!$innerType instanceof ObjectType) {
102+
if (!$innerType->isObject()->yes()) {
103103
return new NeverType();
104104
}
105105

0 commit comments

Comments
 (0)