mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-31 20:51:46 +01:00
use service type resolving over global one
This commit is contained in:
parent
a5f44c997d
commit
ec2e0cb380
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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'];
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
use Rector\Tests\Rector\Property\PropertyToMethodRector\Source\Translator;
|
||||
|
||||
class CustomTranslator extends Translator
|
||||
{
|
||||
public function fooMethod()
|
||||
|
@ -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
|
||||
{
|
||||
/**
|
||||
|
@ -0,0 +1,8 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Property\PropertyToMethodRector\Source;
|
||||
|
||||
class Translator
|
||||
{
|
||||
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
use Rector\Tests\Rector\Property\PropertyToMethodRector\Source\Translator;
|
||||
|
||||
class CustomTranslator extends Translator
|
||||
{
|
||||
public function fooMethod()
|
||||
|
@ -1,7 +1,7 @@
|
||||
services:
|
||||
Rector\Rector\Property\PropertyToMethodRector:
|
||||
$perClassPropertyToMethods:
|
||||
'Translator':
|
||||
'Rector\Tests\Rector\Property\PropertyToMethodRector\Source\Translator':
|
||||
'locale':
|
||||
'get': 'getLocale'
|
||||
'set': 'setLocale'
|
||||
|
Loading…
x
Reference in New Issue
Block a user