Skip to content

Commit

Permalink
Generalize scalar type when inferring properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 10, 2019
1 parent 733509c commit ad8d823
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Reflection/Php/PhpClassReflectionExtension.php
Expand Up @@ -39,6 +39,7 @@
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;
use PHPStan\Type\TypeUtils;

class PhpClassReflectionExtension
implements PropertiesClassReflectionExtension, MethodsClassReflectionExtension, BrokerAwareExtension
Expand Down Expand Up @@ -764,7 +765,7 @@ private function inferAndCachePropertyTypes(
continue;
}

$propertyTypes[$propertyFetch->name->toString()] = $propertyType;
$propertyTypes[$propertyFetch->name->toString()] = TypeUtils::generalizeType($propertyType);
}

return $this->propertyTypesCache[$declaringClass->getName()] = $propertyTypes;
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -9737,6 +9737,14 @@ public function dataInferPrivatePropertyTypeFromConstructor(): array
'mixed',
'$this->explicitMixed',
],
[
'bool',
'$this->bool',
],
[
'array',
'$this->array',
],
];
}

Expand Down
Expand Up @@ -20,6 +20,10 @@ class Foo
/** @var mixed */
private $explicitMixed;

private $bool;

private $array;

/**
* @param self|Bar $unionProp
*/
Expand All @@ -37,6 +41,8 @@ public function __construct(
$this->stdClassProp = new \stdClass();
$this->unrelatedDocComment = $unrelatedDocComment;
$this->explicitMixed = $explicitMixed;
$this->bool = false;
$this->array = [];
}

public function doFoo()
Expand Down

0 comments on commit ad8d823

Please sign in to comment.