use service type resolving over global one

This commit is contained in:
Tomas Votruba 2018-08-05 13:42:46 +02:00
parent a5f44c997d
commit ec2e0cb380
9 changed files with 38 additions and 14 deletions

View File

@ -28,13 +28,28 @@ final class ScopeToTypesResolver
public function resolveScopeToTypes(Node $node): array
{
// @todo check via exception
/** @var Scope $nodeScope */
$nodeScope = $node->getAttribute(Attribute::SCOPE);
// awww :(
$types = [];
if ($node instanceof Expr\Variable && $node->name === 'this') {
$types[] = $nodeScope->getClassReflection()->getName();
$types = array_merge($types, $nodeScope->getClassReflection()->getParentClassesNames());
foreach ($nodeScope->getClassReflection()->getInterfaces() as $classReflection) {
$types[] = $classReflection->getName();
}
return $types;
}
if (! $node instanceof Expr) {
return $this->resolveNonExprNodeToTypes($node);
}
$types = [];
$type = $nodeScope->getType($node);

View File

@ -178,11 +178,8 @@ final class PropertyFetchAnalyzer
return false;
}
$variableNodeTypes = $node->var->getAttribute(Attribute::TYPES);
if ($variableNodeTypes === null) {
return false;
}
$varNodeTypes = $this->scopeToTypesResolver->resolveScopeToTypes($node->var);
return in_array($type, $variableNodeTypes, true);
return in_array($type, $varNodeTypes, true);
}
}

View File

@ -17,10 +17,6 @@ use Rector\RectorDefinition\RectorDefinition;
final class PropertyToMethodRector extends AbstractRector
{
/**
* class => [
* property => [getMethod, setMethod]
* ]
*
* @var string[][][]
*/
private $perClassPropertyToMethods = [];
@ -138,6 +134,7 @@ CODE_SAMPLE
{
foreach ($this->perClassPropertyToMethods as $class => $propertyToMethods) {
$properties = array_keys($propertyToMethods);
if ($this->propertyFetchAnalyzer->isTypeAndProperties($propertyFetchNode, $class, $properties)) {
/** @var Identifier $identifierNode */
$identifierNode = $propertyFetchNode->name;

View File

@ -17,10 +17,10 @@ final class NamespaceReplacerRectorTest extends AbstractRectorTestCase
public function provideWrongToFixedFiles(): Iterator
{
// yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
// yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
// yield [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'];
// yield [__DIR__ . '/Wrong/wrong4.php.inc', __DIR__ . '/Correct/correct4.php.inc'];
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
yield [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'];
yield [__DIR__ . '/Wrong/wrong4.php.inc', __DIR__ . '/Correct/correct4.php.inc'];
yield [__DIR__ . '/Wrong/wrong5.php.inc', __DIR__ . '/Correct/correct5.php.inc'];
}

View File

@ -1,5 +1,7 @@
<?php declare (strict_types=1);
use Rector\Tests\Rector\Property\PropertyToMethodRector\Source\Translator;
class CustomTranslator extends Translator
{
public function fooMethod()

View File

@ -5,6 +5,9 @@ namespace Rector\Tests\Rector\Property\PropertyToMethodRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @see \Rector\Rector\Property\PropertyToMethodRector
*/
final class PropertyToMethodRectorTest extends AbstractRectorTestCase
{
/**

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Property\PropertyToMethodRector\Source;
class Translator
{
}

View File

@ -1,5 +1,7 @@
<?php declare (strict_types=1);
use Rector\Tests\Rector\Property\PropertyToMethodRector\Source\Translator;
class CustomTranslator extends Translator
{
public function fooMethod()

View File

@ -1,7 +1,7 @@
services:
Rector\Rector\Property\PropertyToMethodRector:
$perClassPropertyToMethods:
'Translator':
'Rector\Tests\Rector\Property\PropertyToMethodRector\Source\Translator':
'locale':
'get': 'getLocale'
'set': 'setLocale'