Skip to content

Commit 7935945

Browse files
committed
Do not use instanceof ThisType as it can be unreliable in case of intersection types
1 parent 8d2ff17 commit 7935945

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

src/Node/ClassStatementsGatherer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use PHPStan\Node\Property\PropertyWrite;
1818
use PHPStan\Reflection\ClassReflection;
1919
use PHPStan\ShouldNotHappenException;
20-
use PHPStan\Type\ThisType;
20+
use PHPStan\Type\TypeUtils;
2121
use function count;
2222
use function in_array;
2323

@@ -220,7 +220,7 @@ private function tryToApplyPropertyReads(Expr\FuncCall $node, Scope $scope): voi
220220
}
221221

222222
$firstArgValue = $args[0]->value;
223-
if (!$scope->getType($firstArgValue) instanceof ThisType) {
223+
if (TypeUtils::findThisType($scope->getType($firstArgValue)) === null) {
224224
return;
225225
}
226226

src/Rules/Properties/ReadOnlyByPhpDocPropertyAssignRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use PHPStan\ShouldNotHappenException;
13-
use PHPStan\Type\ThisType;
13+
use PHPStan\Type\TypeUtils;
1414
use function in_array;
1515
use function sprintf;
1616
use function strtolower;
@@ -76,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array
7676
in_array($scopeMethod->getName(), $this->constructorsHelper->getConstructors($scopeClassReflection), true)
7777
|| strtolower($scopeMethod->getName()) === '__unserialize'
7878
) {
79-
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
79+
if (TypeUtils::findThisType($scope->getType($propertyFetch->var)) === null) {
8080
$errors[] = RuleErrorBuilder::message(sprintf('@readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
8181
}
8282

src/Rules/Properties/ReadOnlyPropertyAssignRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use PHPStan\ShouldNotHappenException;
13-
use PHPStan\Type\ThisType;
13+
use PHPStan\Type\TypeUtils;
1414
use function in_array;
1515
use function sprintf;
1616
use function strtolower;
@@ -76,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array
7676
in_array($scopeMethod->getName(), $this->constructorsHelper->getConstructors($scopeClassReflection), true)
7777
|| strtolower($scopeMethod->getName()) === '__unserialize'
7878
) {
79-
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
79+
if (TypeUtils::findThisType($scope->getType($propertyFetch->var)) === null) {
8080
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
8181
}
8282

src/Type/Php/GetClassDynamicReturnTypeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
use PHPStan\Type\ObjectType;
1818
use PHPStan\Type\ObjectWithoutClassType;
1919
use PHPStan\Type\StaticType;
20-
use PHPStan\Type\ThisType;
2120
use PHPStan\Type\Type;
2221
use PHPStan\Type\TypeTraverser;
22+
use PHPStan\Type\TypeUtils;
2323
use PHPStan\Type\UnionType;
2424
use function count;
2525

@@ -49,7 +49,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4949

5050
$argType = $scope->getType($args[0]->value);
5151

52-
if ($scope->isInTrait() && $argType instanceof ThisType) {
52+
if ($scope->isInTrait() && TypeUtils::findThisType($argType) !== null) {
5353
return new ClassStringType();
5454
}
5555

0 commit comments

Comments
 (0)