2020-02-10 00:42:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-02-10 08:58:04 +00:00
|
|
|
namespace Rector\NodeTypeResolver\NodeTypeResolver;
|
2020-02-10 00:42:15 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
2020-03-28 23:06:05 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
|
|
|
use PhpParser\Node\Stmt\Trait_;
|
2020-02-10 00:42:15 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2021-02-28 07:47:48 +00:00
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
2020-02-10 00:42:15 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
2021-01-27 16:00:42 +00:00
|
|
|
use PHPStan\Type\ObjectType;
|
2020-02-10 00:42:15 +00:00
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
2020-02-10 08:58:04 +00:00
|
|
|
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
|
2020-02-10 00:42:15 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
2020-03-28 23:06:05 +00:00
|
|
|
use Rector\NodeTypeResolver\PHPStan\Collector\TraitNodeScopeCollector;
|
2020-02-10 00:42:15 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-27 16:00:42 +00:00
|
|
|
* @see \Rector\NodeTypeResolver\Tests\PerNodeTypeResolver\PropertyFetchTypeResolver\PropertyFetchTypeResolverTest
|
2020-02-10 00:42:15 +00:00
|
|
|
*/
|
2020-02-10 08:58:04 +00:00
|
|
|
final class PropertyFetchTypeResolver implements NodeTypeResolverInterface
|
2020-02-10 00:42:15 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var NodeTypeResolver
|
|
|
|
*/
|
|
|
|
private $nodeTypeResolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var NodeNameResolver
|
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
|
2020-03-28 23:06:05 +00:00
|
|
|
/**
|
|
|
|
* @var TraitNodeScopeCollector
|
|
|
|
*/
|
|
|
|
private $traitNodeScopeCollector;
|
|
|
|
|
2021-01-27 16:00:42 +00:00
|
|
|
/**
|
2021-02-28 07:47:48 +00:00
|
|
|
* @var ReflectionProvider
|
2021-01-27 16:00:42 +00:00
|
|
|
*/
|
2021-02-28 07:47:48 +00:00
|
|
|
private $reflectionProvider;
|
2021-02-08 21:00:45 +00:00
|
|
|
|
2020-02-10 00:42:15 +00:00
|
|
|
public function __construct(
|
2020-07-26 07:49:22 +00:00
|
|
|
NodeNameResolver $nodeNameResolver,
|
2021-01-27 16:00:42 +00:00
|
|
|
TraitNodeScopeCollector $traitNodeScopeCollector,
|
2021-02-28 07:47:48 +00:00
|
|
|
ReflectionProvider $reflectionProvider
|
2020-02-10 00:42:15 +00:00
|
|
|
) {
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
2020-03-28 23:06:05 +00:00
|
|
|
$this->traitNodeScopeCollector = $traitNodeScopeCollector;
|
2021-02-28 07:47:48 +00:00
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
2020-02-10 00:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
2020-10-02 10:29:18 +00:00
|
|
|
public function autowirePropertyFetchTypeResolver(NodeTypeResolver $nodeTypeResolver): void
|
2020-02-10 00:42:15 +00:00
|
|
|
{
|
|
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-28 07:47:48 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-02-10 00:42:15 +00:00
|
|
|
*/
|
|
|
|
public function getNodeClasses(): array
|
|
|
|
{
|
|
|
|
return [PropertyFetch::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PropertyFetch $node
|
|
|
|
*/
|
|
|
|
public function resolve(Node $node): Type
|
|
|
|
{
|
|
|
|
// compensate 3rd party non-analysed property reflection
|
|
|
|
$vendorPropertyType = $this->getVendorPropertyFetchType($node);
|
2020-03-28 23:06:05 +00:00
|
|
|
if (! $vendorPropertyType instanceof MixedType) {
|
2020-02-10 00:42:15 +00:00
|
|
|
return $vendorPropertyType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Scope|null $scope */
|
|
|
|
$scope = $node->getAttribute(AttributeKey::SCOPE);
|
2020-03-28 23:06:05 +00:00
|
|
|
|
2021-01-19 11:24:48 +00:00
|
|
|
if (! $scope instanceof Scope) {
|
2020-03-28 23:06:05 +00:00
|
|
|
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
|
|
|
|
if ($classNode instanceof Trait_) {
|
|
|
|
/** @var string $traitName */
|
|
|
|
$traitName = $classNode->getAttribute(AttributeKey::CLASS_NAME);
|
|
|
|
|
|
|
|
$scope = $this->traitNodeScopeCollector->getScopeForTraitAndNode($traitName, $node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $scope instanceof Scope) {
|
2020-03-28 23:06:05 +00:00
|
|
|
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
|
|
|
|
// fallback to class, since property fetches are not scoped by PHPStan
|
|
|
|
if ($classNode instanceof ClassLike) {
|
|
|
|
$scope = $classNode->getAttribute(AttributeKey::SCOPE);
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $scope instanceof Scope) {
|
2020-03-28 23:06:05 +00:00
|
|
|
return new MixedType();
|
|
|
|
}
|
2020-02-10 00:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $scope->getType($node);
|
|
|
|
}
|
|
|
|
|
2020-03-28 23:06:05 +00:00
|
|
|
private function getVendorPropertyFetchType(PropertyFetch $propertyFetch): Type
|
2020-02-10 00:42:15 +00:00
|
|
|
{
|
|
|
|
// 3rd party code
|
|
|
|
$propertyName = $this->nodeNameResolver->getName($propertyFetch->name);
|
|
|
|
if ($propertyName === null) {
|
2020-03-28 23:06:05 +00:00
|
|
|
return new MixedType();
|
2020-02-10 00:42:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 07:47:48 +00:00
|
|
|
$varType = $this->nodeTypeResolver->resolve($propertyFetch->var);
|
|
|
|
if (! $varType instanceof ObjectType) {
|
2020-03-28 23:06:05 +00:00
|
|
|
return new MixedType();
|
2020-02-10 00:42:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 07:47:48 +00:00
|
|
|
if (! $this->reflectionProvider->hasClass($varType->getClassName())) {
|
2020-03-28 23:06:05 +00:00
|
|
|
return new MixedType();
|
2020-02-10 00:42:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 07:47:48 +00:00
|
|
|
$classReflection = $this->reflectionProvider->getClass($varType->getClassName());
|
|
|
|
if (! $classReflection->hasProperty($propertyName)) {
|
2020-03-28 23:06:05 +00:00
|
|
|
return new MixedType();
|
2020-02-10 00:42:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 07:47:48 +00:00
|
|
|
$propertyFetchScope = $propertyFetch->getAttribute(AttributeKey::SCOPE);
|
|
|
|
$propertyReflection = $classReflection->getProperty($propertyName, $propertyFetchScope);
|
|
|
|
|
|
|
|
return $propertyReflection->getReadableType();
|
2020-02-10 00:42:15 +00:00
|
|
|
}
|
|
|
|
}
|