[CodeQuality] Add trait and parent class support for CompleteDynamicPropertiesRector

This commit is contained in:
Tomas Votruba 2019-05-31 08:08:39 +02:00
parent bcd3ef61d3
commit f799d5385c
6 changed files with 55 additions and 2 deletions

View File

@ -2,6 +2,7 @@
namespace Rector\CodeQuality\Rector\Array_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
@ -195,7 +196,7 @@ CODE_SAMPLE
if ($hasClassMethodReturn) {
$anonymousFunction->stmts[] = new Return_($innerMethodCall);
} else {
$anonymousFunction->stmts[] = new Node\Stmt\Expression($innerMethodCall);
$anonymousFunction->stmts[] = new Expression($innerMethodCall);
}
if ($node instanceof Variable) {

View File

@ -92,6 +92,10 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if ($node->isAnonymous()) {
return null;
}
$fetchedLocalPropertyNameToTypes = $this->resolveFetchedLocalPropertyNameToTypes($node);
$propertyNames = [];
@ -108,6 +112,17 @@ CODE_SAMPLE
$fetchedLocalPropertyNames = array_keys($fetchedLocalPropertyNameToTypes);
$propertiesToComplete = array_diff($fetchedLocalPropertyNames, $propertyNames);
// remove other properties that are accessible from this scope
/** @var string $class */
$class = $this->getName($node);
foreach ($propertiesToComplete as $key => $propertyToComplete) {
if (! property_exists($class, $propertyToComplete)) {
continue;
}
unset($propertiesToComplete[$key]);
}
$newProperties = $this->createNewProperties($fetchedLocalPropertyNameToTypes, $propertiesToComplete);
$node->stmts = array_merge_recursive($newProperties, $node->stmts);

View File

@ -13,6 +13,8 @@ final class CompleteDynamicPropertiesRectorTest extends AbstractRectorTestCase
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/multiple_types.php.inc',
__DIR__ . '/Fixture/skip_defined.php.inc',
__DIR__ . '/Fixture/skip_parent_property.php.inc',
__DIR__ . '/Fixture/skip_trait_used.php.inc',
]);
}

View File

@ -0,0 +1,16 @@
<?php
namespace Rector\CodeQuality\Tests\Rector\Class_\CompleteDynamicPropertiesRector\Fixture;
class SomeChildClass extends SomeParentClassWithProperty
{
public function set()
{
$this->value = 5;
}
}
class SomeParentClassWithProperty
{
public $value;
}

View File

@ -0,0 +1,18 @@
<?php
namespace Rector\CodeQuality\Tests\Rector\Class_\CompleteDynamicPropertiesRector\Fixture;
class AddicatedClass
{
use PropertyInjection;
public function set()
{
$this->value = 5;
}
}
trait PropertyInjection
{
private $value;
}

View File

@ -2,6 +2,7 @@
namespace Rector\Php\Rector\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Scalar\LNumber;
@ -66,7 +67,7 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof BinaryOp\Concat) {
if ($node instanceof Concat) {
return null;
}