Skip to content

Commit

Permalink
Do not use instanceof ThisType as it can be unreliable in case of i…
Browse files Browse the repository at this point in the history
…ntersection types
  • Loading branch information
ondrejmirtes committed Jul 3, 2023
1 parent 8d2ff17 commit 7935945
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Node/ClassStatementsGatherer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PHPStan\Node\Property\PropertyWrite;
use PHPStan\Reflection\ClassReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ThisType;
use PHPStan\Type\TypeUtils;
use function count;
use function in_array;

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Properties/ReadOnlyByPhpDocPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ThisType;
use PHPStan\Type\TypeUtils;
use function in_array;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array
in_array($scopeMethod->getName(), $this->constructorsHelper->getConstructors($scopeClassReflection), true)
|| strtolower($scopeMethod->getName()) === '__unserialize'
) {
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
if (TypeUtils::findThisType($scope->getType($propertyFetch->var)) === null) {
$errors[] = RuleErrorBuilder::message(sprintf('@readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Properties/ReadOnlyPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ThisType;
use PHPStan\Type\TypeUtils;
use function in_array;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array
in_array($scopeMethod->getName(), $this->constructorsHelper->getConstructors($scopeClassReflection), true)
|| strtolower($scopeMethod->getName()) === '__unserialize'
) {
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
if (TypeUtils::findThisType($scope->getType($propertyFetch->var)) === null) {
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Type/Php/GetClassDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use function count;

Expand Down Expand Up @@ -49,7 +49,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

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

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

Expand Down

0 comments on commit 7935945

Please sign in to comment.