rector/rules/TypeDeclaration/TypeAnalyzer/PropertyFetchTypeAnalyzer.php
Tomas Votruba f923fd0998 Updated Rector to commit 43dd2efc5d663bc16327911fe31c6f6ecf60e753
43dd2efc5d Remove deprecated ReturnTypeDeclarationRector, TypedPropertyRector, ParamTypeDeclarationRector rules (#3350)
2023-02-06 19:23:07 +00:00

53 lines
1.8 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\TypeAnalyzer;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\WrapperPropertyReflection;
use PHPStan\Type\MixedType;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class PropertyFetchTypeAnalyzer
{
public function isPropertyFetchExprNotNativelyTyped(Expr $expr) : bool
{
if (!$expr instanceof PropertyFetch) {
return \false;
}
if (!$expr->name instanceof Identifier) {
return \false;
}
$scope = $expr->getAttribute(AttributeKey::SCOPE);
if (!$scope instanceof Scope) {
return \false;
}
$propertyName = $expr->name->toString();
$propertyHolderType = $scope->getType($expr->var);
if (!$propertyHolderType->hasProperty($propertyName)->yes()) {
return \false;
}
$propertyReflection = $propertyHolderType->getProperty($propertyName, $scope);
$phpPropertyReflection = $this->getNativeReflectionForProperty($propertyReflection);
if (!$phpPropertyReflection instanceof PhpPropertyReflection) {
return \false;
}
return $phpPropertyReflection->getNativeType() instanceof MixedType;
}
private function getNativeReflectionForProperty(PropertyReflection $propertyReflection) : ?PhpPropertyReflection
{
$reflection = $propertyReflection;
while ($reflection instanceof WrapperPropertyReflection) {
$reflection = $reflection->getOriginalReflection();
}
if (!$reflection instanceof PhpPropertyReflection) {
return null;
}
return $reflection;
}
}