Skip to content

Commit

Permalink
Inferred property type for missing assigned property should not be se…
Browse files Browse the repository at this point in the history
…t to implicit mixed
  • Loading branch information
ondrejmirtes committed Dec 13, 2020
1 parent 4827dcf commit 5b89088
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,11 +875,11 @@ private function collectTraits(\ReflectionClass $class): array
private function inferPrivatePropertyType(
string $propertyName,
MethodReflection $constructor
): Type
): ?Type
{
$declaringClassName = $constructor->getDeclaringClass()->getName();
if (isset($this->inferClassConstructorPropertyTypesInProcess[$declaringClassName])) {
return new MixedType();
return null;
}
$this->inferClassConstructorPropertyTypesInProcess[$declaringClassName] = true;
$propertyTypes = $this->inferAndCachePropertyTypes($constructor);
Expand All @@ -888,7 +888,7 @@ private function inferPrivatePropertyType(
return $propertyTypes[$propertyName];
}

return new MixedType();
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,13 @@ public function testPromotedProperties(): void
]);
}

public function testBug4227(): void
{
if (PHP_VERSION_ID < 70400 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 7.4.');
}

$this->analyse([__DIR__ . '/data/bug-4227.php'], []);
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-4227.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bug4227;

class Foo
{
private bool $property;
private int $count = 0;
private ?string $string = null;

public function __construct(bool $property)
{
$this->property = $property;
}

public function count(): int
{
return $this->count;
}
}

0 comments on commit 5b89088

Please sign in to comment.