Skip to content

Commit

Permalink
PropertyReflectionFinder - fixed #2745
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 4, 2020
1 parent 2b0013f commit d4bfc70
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Rules/Properties/PropertyReflectionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public function findPropertyReflectionFromNode($propertyFetch, Scope $scope): ?F

private function findPropertyReflection(Type $propertyHolderType, string $propertyName, Scope $scope, bool $fetchedOnThis): ?FoundPropertyReflection
{
if (!$propertyHolderType->hasProperty($propertyName)->yes()) {
return null;
}

$transformedPropertyHolderType = TypeTraverser::map($propertyHolderType, static function (Type $type, callable $traverse) use ($scope, $fetchedOnThis): Type {
if ($type instanceof StaticType) {
if ($fetchedOnThis && $scope->isInClass()) {
Expand All @@ -62,6 +58,10 @@ private function findPropertyReflection(Type $propertyHolderType, string $proper
return $traverse($type);
});

if (!$transformedPropertyHolderType->hasProperty($propertyName)->yes()) {
return null;
}

$originalProperty = $transformedPropertyHolderType->getProperty($propertyName, $scope);
$readableType = $this->transformPropertyType($originalProperty->getReadableType(), $transformedPropertyHolderType, $scope, $fetchedOnThis);
$writableType = $this->transformPropertyType($originalProperty->getWritableType(), $transformedPropertyHolderType, $scope, $fetchedOnThis);
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ public function testCollectWarnings(): void
$this->assertSame(PHP_VERSION_ID >= 70400 ? 22 : 27, $errors[0]->getLine());
}

public function testPropertyAssignIntersectionStaticTypeBug(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/property-assign-intersection-static-type-bug.php');
$this->assertCount(0, $errors);
}

/**
* @param string $file
* @return \PHPStan\Analyser\Error[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace PropertyAssignIntersectionStaticTypeBug;

abstract class Base
{
/** @var string */
private $foo;

public function __construct(string $foo)
{
\assert($this instanceof Frontend || $this instanceof Backend);

$this->foo = $foo;
}
}

class Frontend extends Base
{

}

class Backend extends Base
{

}

0 comments on commit d4bfc70

Please sign in to comment.