mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
[NodeTypeResolver] add property
This commit is contained in:
parent
bc604a6606
commit
9c9b5c8111
|
@ -10,6 +10,7 @@ use PhpParser\Node\Expr\Variable;
|
|||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\NodeTypeResolver\TypeContext;
|
||||
|
||||
|
@ -37,6 +38,7 @@ final class ClassLikeTypeResolver extends NodeVisitorAbstract
|
|||
{
|
||||
$this->typeContext = $typeContext;
|
||||
|
||||
// consider mini subscribers
|
||||
$this->perNodeResolvers[Variable::class] = function (Variable $variableNode): void {
|
||||
$this->processVariableNode($variableNode);
|
||||
};
|
||||
|
@ -46,6 +48,9 @@ final class ClassLikeTypeResolver extends NodeVisitorAbstract
|
|||
$this->perNodeResolvers[PropertyFetch::class] = function (PropertyFetch $propertyFetchNode): void {
|
||||
$this->processPropertyFetch($propertyFetchNode);
|
||||
};
|
||||
$this->perNodeResolvers[Property::class] = function (Property $propertyNode): void {
|
||||
$this->processProperty($propertyNode);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -131,4 +136,14 @@ final class ClassLikeTypeResolver extends NodeVisitorAbstract
|
|||
$propertyFetchNode->setAttribute(self::TYPE_ATTRIBUTE, $propertyType);
|
||||
}
|
||||
}
|
||||
|
||||
private function processProperty(Property $propertyNode): void
|
||||
{
|
||||
$propertyName = (string) $propertyNode->props[0]->name;
|
||||
$propertyType = $this->typeContext->getTypeForProperty($propertyName);
|
||||
|
||||
if ($propertyType) {
|
||||
$propertyNode->setAttribute(self::TYPE_ATTRIBUTE, $propertyType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,24 +35,24 @@ final class PropertyTest extends AbstractContainerAwareTestCase
|
|||
$this->nodes = $this->standaloneTraverseNodeTraverser->traverse($nodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* $this->property;
|
||||
*/
|
||||
public function testPropertyFetch(): void
|
||||
{
|
||||
/** @var PropertyFetch $propertyFetchNode */
|
||||
$propertyFetchNode = $this->nodes[1]->stmts[1]->stmts[2]->stmts[0]->expr;
|
||||
$this->assertSame(Html::class, $propertyFetchNode->getAttribute('type'));
|
||||
}
|
||||
|
||||
// /**
|
||||
// * $property;
|
||||
// * $this->property;
|
||||
// */
|
||||
// public function testProperty(): void
|
||||
// public function testPropertyFetch(): void
|
||||
// {
|
||||
// $propertyNode = $this->nodes[1]->stmts[1]->stmts[0];
|
||||
// $this->assertSame(Html::class, $propertyNode->getAttribute('type'));
|
||||
// /** @var PropertyFetch $propertyFetchNode */
|
||||
// $propertyFetchNode = $this->nodes[1]->stmts[1]->stmts[2]->stmts[0]->expr;
|
||||
// $this->assertSame(Html::class, $propertyFetchNode->getAttribute('type'));
|
||||
// }
|
||||
|
||||
/**
|
||||
* $property;
|
||||
*/
|
||||
public function testProperty(): void
|
||||
{
|
||||
$propertyNode = $this->nodes[1]->stmts[1]->stmts[0];
|
||||
$this->assertSame(Html::class, $propertyNode->getAttribute('type'));
|
||||
}
|
||||
//
|
||||
// public function testMethodParameter(): void
|
||||
// {
|
||||
|
|
Loading…
Reference in New Issue
Block a user