rector/packages/NodeTypeResolver/NodeTypeResolver/PropertyTypeResolver.php

54 lines
1.4 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2018-08-06 21:41:01 +00:00
2020-02-10 08:58:04 +00:00
namespace Rector\NodeTypeResolver\NodeTypeResolver;
2018-08-06 21:41:01 +00:00
use PhpParser\Node;
2018-08-13 21:25:54 +00:00
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
2018-08-06 21:41:01 +00:00
use PhpParser\Node\Stmt\Property;
2019-09-04 12:10:29 +00:00
use PHPStan\Type\Type;
2020-02-10 08:58:04 +00:00
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
2018-08-13 21:25:54 +00:00
use Rector\NodeTypeResolver\NodeTypeResolver;
2018-08-06 21:41:01 +00:00
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\PropertyTypeResolver\PropertyTypeResolverTest
2019-09-03 09:11:45 +00:00
*/
2020-02-10 08:58:04 +00:00
final class PropertyTypeResolver implements NodeTypeResolverInterface
2018-08-06 21:41:01 +00:00
{
/**
2018-08-13 21:25:54 +00:00
* @var NodeTypeResolver
2018-08-06 21:41:01 +00:00
*/
2018-08-13 21:25:54 +00:00
private $nodeTypeResolver;
2018-08-06 21:41:01 +00:00
2020-01-27 22:30:40 +00:00
/**
* @required
*/
public function autowirePropertyTypeResolver(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}
2018-08-06 21:41:01 +00:00
/**
* @return array<class-string<Node>>
2018-08-06 21:41:01 +00:00
*/
public function getNodeClasses(): array
{
return [Property::class];
}
/**
* @param Property $propertyNode
*/
2019-09-04 12:10:29 +00:00
public function resolve(Node $propertyNode): Type
2018-08-06 21:41:01 +00:00
{
2018-09-27 15:57:10 +00:00
// fake property to local PropertyFetch → PHPStan understands that
2020-06-29 21:19:37 +00:00
$propertyFetch = new PropertyFetch(new Variable('this'), (string) $propertyNode->props[0]->name);
$propertyFetch->setAttribute(AttributeKey::SCOPE, $propertyNode->getAttribute(AttributeKey::SCOPE));
2018-08-06 21:41:01 +00:00
2020-06-29 21:19:37 +00:00
return $this->nodeTypeResolver->resolve($propertyFetch);
2018-08-06 21:41:01 +00:00
}
}