[MagicMethodRector] decouple dependency on specific class

This commit is contained in:
Tomas Votruba 2018-05-31 17:49:19 +02:00
parent e278a9cc04
commit 97932a59bd
8 changed files with 28 additions and 15 deletions

View File

@ -47,18 +47,25 @@ final class MagicMethodRector extends AbstractRector
*/
private $nodeTypeResolver;
/**
* @var string
*/
private $parentClass;
public function __construct(
MethodBuilder $methodBuilder,
DocBlockAnalyzer $docBlockAnalyzer,
SmartClassReflector $smartClassReflector,
MagicMethodMatcher $magicMethodMatcher,
NodeTypeResolver $nodeTypeResolver
NodeTypeResolver $nodeTypeResolver,
string $parentClass = 'Nette\Object'
) {
$this->methodBuilder = $methodBuilder;
$this->docBlockAnalyzer = $docBlockAnalyzer;
$this->smartClassReflector = $smartClassReflector;
$this->magicMethodMatcher = $magicMethodMatcher;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->parentClass = $parentClass;
}
public function getDefinition(): RectorDefinition
@ -151,6 +158,6 @@ CODE_SAMPLE
{
$classNodeTypes = $this->nodeTypeResolver->resolve($classNode);
return in_array('Nette\Object', $classNodeTypes, true);
return in_array($this->parentClass, $classNodeTypes, true);
}
}

View File

@ -2,9 +2,9 @@
namespace SomeNamespace;
use Nette\Object;
use Rector\Tests\Rector\Contrib\Nette\Utils\MagicMethodRector\Source\SomeObject;
class SomeClassExtendingNetteObject extends Object
class SomeClassExtendingNetteObject extends SomeObject
{
private $value;
private $anotherValue;

View File

@ -2,9 +2,9 @@
namespace SomeNamespace;
use Nette\Object;
use Rector\Tests\Rector\Contrib\Nette\Utils\MagicMethodRector\Source\SomeObject;
class SomeClassExtendingNetteObject2 extends Object
class SomeClassExtendingNetteObject2 extends SomeObject
{
public $name;
public $enabled;

View File

@ -15,10 +15,6 @@ final class MagicMethodRectorTest extends AbstractRectorTestCase
*/
public function test(string $wrong, string $fixed): void
{
if (PHP_VERSION >= '7.2.0') {
$this->markTestSkipped('This test needs PHP 7.1 or lower.');
}
$this->doTestFileMatchesExpectedContent($wrong, $fixed);
}

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\Nette\Utils\MagicMethodRector\Source;
class SomeObject
{
}

View File

@ -2,13 +2,13 @@
namespace SomeNamespace;
use Nette\Object;
use Rector\Tests\Rector\Contrib\Nette\Utils\MagicMethodRector\Source\SomeObject;
/**
* @method getValue()
* @method int getAnotherValue()
*/
class SomeClassExtendingNetteObject extends Object
class SomeClassExtendingNetteObject extends SomeObject
{
private $value;
private $anotherValue;

View File

@ -2,7 +2,7 @@
namespace SomeNamespace;
use Nette\Object;
use Rector\Tests\Rector\Contrib\Nette\Utils\MagicMethodRector\Source\SomeObject;
/**
* @method void setName(string $var)
@ -12,7 +12,7 @@ use Nette\Object;
* @method getItems
* @method setEnabled (bool)
*/
class SomeClassExtendingNetteObject2 extends Object
class SomeClassExtendingNetteObject2 extends SomeObject
{
public $name;
public $enabled;

View File

@ -1,2 +1,4 @@
services:
Rector\Rector\Contrib\Nette\Utils\MagicMethodRector: ~
Rector\Rector\Contrib\Nette\Utils\MagicMethodRector:
arguments:
$parentClass: 'Rector\Tests\Rector\Contrib\Nette\Utils\MagicMethodRector\Source\SomeObject'