mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
[Nette] Add RemoveParentAndNameFromComponentConstructorRector (#4515)
* [Nette] Add RemoveParentAndNameFromComponentConstructorRector * add refactorClassMethod() * add refactorStaticCall() * add refactorNew() method * [Nette] Rename AddDatePickerToDateControlRector to AddNextrasDatePickerToDateControlRector * [ci-review] Rector Rectify Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
parent
2c120e283e
commit
3ed76c6559
|
@ -6,7 +6,8 @@ use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
|
|||
use Rector\Generic\Rector\ClassMethod\ArgumentDefaultValueReplacerRector;
|
||||
use Rector\Generic\Rector\MethodCall\FormerNullableArgumentToScalarTypedRector;
|
||||
use Rector\Generic\ValueObject\ArgumentDefaultValueReplacer;
|
||||
use Rector\Nette\Rector\MethodCall\AddDatePickerToDateControlRector;
|
||||
use Rector\Nette\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector;
|
||||
use Rector\Nette\Rector\MethodCall\AddNextrasDatePickerToDateControlRector;
|
||||
use Rector\Nette\Rector\MethodCall\GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector;
|
||||
use Rector\Nette\Rector\MethodCall\MagicHtmlCallToAppendAttributeRector;
|
||||
use Rector\Nette\Rector\MethodCall\RequestGetCookieDefaultArgumentToCoalesceRector;
|
||||
|
@ -25,84 +26,78 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$containerConfigurator->import(__DIR__ . '/nette-30-dependency-injection.php');
|
||||
$containerConfigurator->import(__DIR__ . '/nette-30-return-types.php');
|
||||
$containerConfigurator->import(__DIR__ . '/nette-30-param-types.php');
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(AddDatePickerToDateControlRector::class);
|
||||
$services->set(AddNextrasDatePickerToDateControlRector::class);
|
||||
$services->set(ChangeFormArrayAccessToAnnotatedControlVariableRector::class);
|
||||
$services->set(GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector::class);
|
||||
|
||||
// Control class has remove __construct(), e.g. https://github.com/Pixidos/GPWebPay/pull/16/files#diff-fdc8251950f85c5467c63c249df05786
|
||||
$services->set(RemoveParentCallWithoutParentRector::class);
|
||||
// https://github.com/nette/utils/commit/d0041ba59f5d8bf1f5b3795fd76d43fb13ea2e15
|
||||
|
||||
$services->set(FormerNullableArgumentToScalarTypedRector::class);
|
||||
$services->set(StaticCallToMethodCallRector::class)->call('configure', [[
|
||||
StaticCallToMethodCallRector::STATIC_CALLS_TO_METHOD_CALLS => inline_value_objects([
|
||||
|
||||
$services->set(StaticCallToMethodCallRector::class)
|
||||
->call('configure', [[
|
||||
StaticCallToMethodCallRector::STATIC_CALLS_TO_METHOD_CALLS => inline_value_objects([
|
||||
new StaticCallToMethodCall('Nette\Security\Passwords', 'hash', 'Nette\Security\Passwords', 'hash'),
|
||||
new StaticCallToMethodCall('Nette\Security\Passwords', 'verify', 'Nette\Security\Passwords', 'verify'),
|
||||
new StaticCallToMethodCall(
|
||||
new StaticCallToMethodCall('Nette\Security\Passwords', 'hash', 'Nette\Security\Passwords', 'hash'),
|
||||
new StaticCallToMethodCall('Nette\Security\Passwords', 'verify', 'Nette\Security\Passwords', 'verify'),
|
||||
new StaticCallToMethodCall(
|
||||
'Nette\Security\Passwords',
|
||||
'needsRehash',
|
||||
'Nette\Security\Passwords',
|
||||
'needsRehash'
|
||||
), ]
|
||||
),
|
||||
]]);
|
||||
|
||||
]),
|
||||
]]);
|
||||
// https://github.com/contributte/event-dispatcher-extra/tree/v0.4.3 and higher
|
||||
$services->set(RenameClassConstantRector::class)
|
||||
->call('configure', [[
|
||||
RenameClassConstantRector::CLASS_CONSTANT_RENAME => inline_value_objects([
|
||||
new RenameClassConstant('Contributte\Events\Extra\Event\Security\LoggedInEvent', 'NAME', 'class'),
|
||||
new RenameClassConstant('Contributte\Events\Extra\Event\Security\LoggedOutEvent', 'NAME', 'class'),
|
||||
new RenameClassConstant('Contributte\Events\Extra\Event\Application\ShutdownEvent', 'NAME', 'class'), ]
|
||||
$services->set(RenameClassConstantRector::class)->call('configure', [[
|
||||
RenameClassConstantRector::CLASS_CONSTANT_RENAME => inline_value_objects([
|
||||
|
||||
new RenameClassConstant('Contributte\Events\Extra\Event\Security\LoggedInEvent', 'NAME', 'class'),
|
||||
new RenameClassConstant('Contributte\Events\Extra\Event\Security\LoggedOutEvent', 'NAME', 'class'),
|
||||
new RenameClassConstant('Contributte\Events\Extra\Event\Application\ShutdownEvent', 'NAME', 'class'),
|
||||
|
||||
]),
|
||||
]]);
|
||||
$services->set(RenameClassRector::class)->call('configure', [[
|
||||
RenameClassRector::OLD_TO_NEW_CLASSES => [
|
||||
# nextras/forms was split into 2 packages
|
||||
'Nextras\FormComponents\Controls\DatePicker' => 'Nextras\FormComponents\Controls\DateControl',
|
||||
# @see https://github.com/nette/di/commit/a0d361192f8ac35f1d9f82aab7eb351e4be395ea
|
||||
'Nette\DI\ServiceDefinition' => 'Nette\DI\Definitions\ServiceDefinition',
|
||||
'Nette\DI\Statement' => 'Nette\DI\Definitions\Statement',
|
||||
],
|
||||
]]);
|
||||
$services->set(ArgumentDefaultValueReplacerRector::class)->call('configure', [[
|
||||
ArgumentDefaultValueReplacerRector::REPLACED_ARGUMENTS => inline_value_objects([
|
||||
// json 2nd argument is now `int` typed
|
||||
new ArgumentDefaultValueReplacer(
|
||||
'Nette\Utils\Json',
|
||||
'decode',
|
||||
1,
|
||||
true,
|
||||
'Nette\Utils\Json::FORCE_ARRAY'
|
||||
),
|
||||
]]);
|
||||
$services->set(RenameClassRector::class)
|
||||
->call('configure', [[
|
||||
RenameClassRector::OLD_TO_NEW_CLASSES => [
|
||||
# nextras/forms was split into 2 packages
|
||||
'Nextras\FormComponents\Controls\DatePicker' => 'Nextras\FormComponents\Controls\DateControl',
|
||||
# @see https://github.com/nette/di/commit/a0d361192f8ac35f1d9f82aab7eb351e4be395ea
|
||||
'Nette\DI\ServiceDefinition' => 'Nette\DI\Definitions\ServiceDefinition',
|
||||
'Nette\DI\Statement' => 'Nette\DI\Definitions\Statement',
|
||||
],
|
||||
]]);
|
||||
$services->set(ArgumentDefaultValueReplacerRector::class)
|
||||
->call('configure', [[
|
||||
ArgumentDefaultValueReplacerRector::REPLACED_ARGUMENTS => inline_value_objects([
|
||||
// json 2nd argument is now `int` typed
|
||||
new ArgumentDefaultValueReplacer(
|
||||
'Nette\Utils\Json',
|
||||
'decode',
|
||||
1,
|
||||
true,
|
||||
'Nette\Utils\Json::FORCE_ARRAY'
|
||||
),
|
||||
|
||||
// @see https://github.com/nette/forms/commit/574b97f9d5e7a902a224e57d7d584e7afc9fefec
|
||||
new ArgumentDefaultValueReplacer('Nette\Forms\Form', 'decode', 0, true, 'array'),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$services->set(RenameMethodRector::class)
|
||||
->call('configure', [[
|
||||
RenameMethodRector::METHOD_CALL_RENAMES => inline_value_objects([
|
||||
new MethodCallRename('Nette\Forms\Controls\BaseControl', 'setAttribute', 'setHtmlAttribute'),
|
||||
// see https://github.com/nette/forms/commit/b99385aa9d24d729a18f6397a414ea88eab6895a
|
||||
new MethodCallRename('Nette\Forms\Controls\BaseControl', 'setType', 'setHtmlType'),
|
||||
new MethodCallRename('Nette\Forms\Controls\BaseControl', 'setAttribute', 'setHtmlAttribute'),
|
||||
new MethodCallRename(
|
||||
'Nette\DI\Definitions\ServiceDefinition',
|
||||
# see https://github.com/nette/di/commit/1705a5db431423fc610a6f339f88dead1b5dc4fb
|
||||
'setClass',
|
||||
'setType'
|
||||
),
|
||||
new MethodCallRename('Nette\DI\Definitions\ServiceDefinition', 'getClass', 'getType'),
|
||||
new MethodCallRename('Nette\DI\Definitions\Definition', 'isAutowired', 'getAutowired'), ]),
|
||||
]]);
|
||||
|
||||
// @see https://github.com/nette/forms/commit/574b97f9d5e7a902a224e57d7d584e7afc9fefec
|
||||
new ArgumentDefaultValueReplacer('Nette\Forms\Form', 'decode', 0, true, 'array'),
|
||||
]),
|
||||
]]);
|
||||
$services->set(RenameMethodRector::class)->call('configure', [[
|
||||
RenameMethodRector::METHOD_CALL_RENAMES => inline_value_objects([
|
||||
new MethodCallRename('Nette\Forms\Controls\BaseControl', 'setAttribute', 'setHtmlAttribute'),
|
||||
// see https://github.com/nette/forms/commit/b99385aa9d24d729a18f6397a414ea88eab6895a
|
||||
new MethodCallRename('Nette\Forms\Controls\BaseControl', 'setType', 'setHtmlType'),
|
||||
new MethodCallRename('Nette\Forms\Controls\BaseControl', 'setAttribute', 'setHtmlAttribute'),
|
||||
new MethodCallRename(
|
||||
'Nette\DI\Definitions\ServiceDefinition',
|
||||
# see https://github.com/nette/di/commit/1705a5db431423fc610a6f339f88dead1b5dc4fb
|
||||
'setClass',
|
||||
'setType'
|
||||
),
|
||||
new MethodCallRename('Nette\DI\Definitions\ServiceDefinition', 'getClass', 'getType'),
|
||||
new MethodCallRename('Nette\DI\Definitions\Definition', 'isAutowired', 'getAutowired'),
|
||||
]),
|
||||
]]);
|
||||
$services->set(MagicHtmlCallToAppendAttributeRector::class);
|
||||
$services->set(RequestGetCookieDefaultArgumentToCoalesceRector::class);
|
||||
$services->set(RemoveParentAndNameFromComponentConstructorRector::class);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# All 598 Rectors Overview
|
||||
# All 599 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
---
|
||||
|
@ -31,7 +31,7 @@
|
|||
- [MockistaToMockery](#mockistatomockery) (2)
|
||||
- [MysqlToMysqli](#mysqltomysqli) (4)
|
||||
- [Naming](#naming) (11)
|
||||
- [Nette](#nette) (17)
|
||||
- [Nette](#nette) (18)
|
||||
- [NetteCodeQuality](#nettecodequality) (6)
|
||||
- [NetteKdyby](#nettekdyby) (4)
|
||||
- [NetteTesterToPHPUnit](#nettetestertophpunit) (3)
|
||||
|
@ -7838,10 +7838,10 @@ Change under_score names to camelCase
|
|||
|
||||
## Nette
|
||||
|
||||
### `AddDatePickerToDateControlRector`
|
||||
### `AddNextrasDatePickerToDateControlRector`
|
||||
|
||||
- class: [`Rector\Nette\Rector\MethodCall\AddDatePickerToDateControlRector`](/rules/nette/src/Rector/MethodCall/AddDatePickerToDateControlRector.php)
|
||||
- [test fixtures](/rules/nette/tests/Rector/MethodCall/AddDatePickerToDateControlRector/Fixture)
|
||||
- class: [`Rector\Nette\Rector\MethodCall\AddNextrasDatePickerToDateControlRector`](/rules/nette/src/Rector/MethodCall/AddNextrasDatePickerToDateControlRector.php)
|
||||
- [test fixtures](/rules/nette/tests/Rector/MethodCall/AddNextrasDatePickerToDateControlRector/Fixture)
|
||||
|
||||
Nextras/Form upgrade of addDatePicker method call to DateControl assign
|
||||
|
||||
|
@ -8091,6 +8091,29 @@ Use `Nette\Utils\Strings` over bare `preg_match()` and `preg_match_all()` functi
|
|||
|
||||
<br><br>
|
||||
|
||||
### `RemoveParentAndNameFromComponentConstructorRector`
|
||||
|
||||
- class: [`Rector\Nette\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector`](/rules/nette/src/Rector/ClassMethod/RemoveParentAndNameFromComponentConstructorRector.php)
|
||||
- [test fixtures](/rules/nette/tests/Rector/ClassMethod/RemoveParentAndNameFromComponentConstructorRector/Fixture)
|
||||
|
||||
Remove `$parent` and `$name` in control constructor
|
||||
|
||||
```diff
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
class SomeControl extends Control
|
||||
{
|
||||
- public function __construct(IContainer $parent = null, $name = null, int $value)
|
||||
+ public function __construct(int $value)
|
||||
{
|
||||
- parent::__construct($parent, $name);
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br><br>
|
||||
|
||||
### `RequestGetCookieDefaultArgumentToCoalesceRector`
|
||||
|
||||
- class: [`Rector\Nette\Rector\MethodCall\RequestGetCookieDefaultArgumentToCoalesceRector`](/rules/nette/src/Rector/MethodCall/RequestGetCookieDefaultArgumentToCoalesceRector.php)
|
||||
|
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Rector\NodeCollector\Reflection;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Analyser\Scope;
|
||||
|
@ -15,9 +17,11 @@ use PHPStan\Reflection\ReflectionProvider;
|
|||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\TypeUtils;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
use ReflectionMethod;
|
||||
|
||||
final class MethodReflectionProvider
|
||||
{
|
||||
|
@ -89,6 +93,14 @@ final class MethodReflectionProvider
|
|||
return $this->provideParameterTypesFromMethodReflection($methodReflection);
|
||||
}
|
||||
|
||||
public function provideByNew(New_ $new): ?MethodReflection
|
||||
{
|
||||
$objectType = $this->nodeTypeResolver->resolve($new->class);
|
||||
$classes = TypeUtils::getDirectClassNames($objectType);
|
||||
|
||||
return $this->provideByClassNamesAndMethodName($classes, MethodName::CONSTRUCT, $new);
|
||||
}
|
||||
|
||||
public function provideByStaticCall(StaticCall $staticCall): ?MethodReflection
|
||||
{
|
||||
$objectType = $this->nodeTypeResolver->resolve($staticCall->class);
|
||||
|
@ -99,20 +111,7 @@ final class MethodReflectionProvider
|
|||
return null;
|
||||
}
|
||||
|
||||
/** @var Scope|null $scope */
|
||||
$scope = $staticCall->getAttribute(AttributeKey::SCOPE);
|
||||
if (! $scope instanceof Scope) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
|
||||
foreach ($classes as $class) {
|
||||
$methodReflection = $this->provideByClassAndMethodName($class, $methodName, $scope);
|
||||
if ($methodReflection instanceof MethodReflection) {
|
||||
return $methodReflection;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return $this->provideByClassNamesAndMethodName($classes, $methodName, $staticCall);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -154,6 +153,52 @@ final class MethodReflectionProvider
|
|||
public function getParameterReflectionsFromMethodReflection(MethodReflection $methodReflection): array
|
||||
{
|
||||
$methodReflectionVariant = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
|
||||
|
||||
return $methodReflectionVariant->getParameters();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function provideParameterNamesByNew(New_ $new): array
|
||||
{
|
||||
$objectType = $this->nodeTypeResolver->resolve($new->class);
|
||||
$classes = TypeUtils::getDirectClassNames($objectType);
|
||||
|
||||
$parameterNames = [];
|
||||
|
||||
foreach ($classes as $class) {
|
||||
if (! method_exists($class, MethodName::CONSTRUCT)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$methodReflection = new ReflectionMethod($class, MethodName::CONSTRUCT);
|
||||
foreach ($methodReflection->getParameters() as $reflectionParameter) {
|
||||
$parameterNames[] = $reflectionParameter->name;
|
||||
}
|
||||
}
|
||||
|
||||
return $parameterNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $classes
|
||||
*/
|
||||
private function provideByClassNamesAndMethodName(array $classes, string $methodName, Node $node): ?MethodReflection
|
||||
{
|
||||
/** @var Scope|null $scope */
|
||||
$scope = $node->getAttribute(AttributeKey::SCOPE);
|
||||
if (! $scope instanceof Scope) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
|
||||
foreach ($classes as $class) {
|
||||
$methodReflection = $this->provideByClassAndMethodName($class, $methodName, $scope);
|
||||
if ($methodReflection instanceof MethodReflection) {
|
||||
return $methodReflection;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -114,7 +114,7 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($this->shouldSkip($node)) {
|
||||
if ($this->shouldSkipClassMethod($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -142,7 +142,7 @@ CODE_SAMPLE
|
|||
return $node;
|
||||
}
|
||||
|
||||
private function shouldSkip(ClassMethod $classMethod): bool
|
||||
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
|
||||
{
|
||||
/** @var string|null $parentClassName */
|
||||
$parentClassName = $classMethod->getAttribute(AttributeKey::PARENT_CLASS_NAME);
|
||||
|
|
38
rules/nette/src/NodeAnalyzer/StaticCallAnalyzer.php
Normal file
38
rules/nette/src/NodeAnalyzer/StaticCallAnalyzer.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
|
||||
final class StaticCallAnalyzer
|
||||
{
|
||||
/**
|
||||
* @var NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
|
||||
public function __construct(NodeNameResolver $nodeNameResolver)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
}
|
||||
|
||||
public function isParentCallNamed(StaticCall $staticCall, string $desiredMethodName): bool
|
||||
{
|
||||
if ($staticCall->class instanceof Expr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->nodeNameResolver->isName($staticCall->class, 'parent')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($staticCall->name instanceof Expr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->nodeNameResolver->isName($staticCall->name, $desiredMethodName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,231 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\Rector\ClassMethod;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Rector\Nette\NodeAnalyzer\StaticCallAnalyzer;
|
||||
use Rector\NodeCollector\Reflection\MethodReflectionProvider;
|
||||
|
||||
/**
|
||||
* @see https://github.com/nette/component-model/commit/1fb769f4602cf82694941530bac1111b3c5cd11b
|
||||
*
|
||||
* @see \Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\RemoveParentAndNameFromComponentConstructorRectorTest
|
||||
*/
|
||||
final class RemoveParentAndNameFromComponentConstructorRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const COMPONENT_CONTAINER_CLASS = 'Nette\ComponentModel\IContainer';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const PARENT = 'parent';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const NAME = 'name';
|
||||
|
||||
/**
|
||||
* @var StaticCallAnalyzer
|
||||
*/
|
||||
private $staticCallAnalyzer;
|
||||
|
||||
/**
|
||||
* @var MethodReflectionProvider
|
||||
*/
|
||||
private $methodReflectionProvider;
|
||||
|
||||
public function __construct(
|
||||
StaticCallAnalyzer $staticCallAnalyzer,
|
||||
MethodReflectionProvider $methodReflectionProvider
|
||||
) {
|
||||
$this->staticCallAnalyzer = $staticCallAnalyzer;
|
||||
$this->methodReflectionProvider = $methodReflectionProvider;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Remove $parent and $name in control constructor', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
class SomeControl extends Control
|
||||
{
|
||||
public function __construct(IContainer $parent = null, $name = null, int $value)
|
||||
{
|
||||
parent::__construct($parent, $name);
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
class SomeControl extends Control
|
||||
{
|
||||
public function __construct(int $value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class, StaticCall::class, New_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|StaticCall|New_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node instanceof ClassMethod) {
|
||||
return $this->refactorClassMethod($node);
|
||||
}
|
||||
|
||||
if ($node instanceof StaticCall) {
|
||||
return $this->refactorStaticCall($node);
|
||||
}
|
||||
|
||||
if ($node instanceof New_ && $this->isObjectType($node->class, self::COMPONENT_CONTAINER_CLASS)) {
|
||||
return $this->refactorNew($node);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
|
||||
{
|
||||
if (! $this->isInObjectType($classMethod, Control::class)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isName($classMethod, MethodName::CONSTRUCT)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hasClassMethodChanged = false;
|
||||
foreach ($classMethod->params as $param) {
|
||||
if ($this->isName($param, self::PARENT) && $param->type !== null && $this->isName(
|
||||
$param->type,
|
||||
self::COMPONENT_CONTAINER_CLASS
|
||||
)) {
|
||||
$this->removeNode($param);
|
||||
$hasClassMethodChanged = true;
|
||||
}
|
||||
|
||||
if ($this->isName($param, self::NAME)) {
|
||||
$this->removeNode($param);
|
||||
$hasClassMethodChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (! $hasClassMethodChanged) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $classMethod;
|
||||
}
|
||||
|
||||
private function refactorStaticCall(StaticCall $staticCall): ?StaticCall
|
||||
{
|
||||
if (! $this->staticCallAnalyzer->isParentCallNamed($staticCall, MethodName::CONSTRUCT)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hasStaticCallChanged = false;
|
||||
|
||||
/** @var Arg $staticCallArg */
|
||||
foreach ((array) $staticCall->args as $staticCallArg) {
|
||||
if (! $staticCallArg->value instanceof Variable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var Variable $variable */
|
||||
$variable = $staticCallArg->value;
|
||||
if (! $this->isNames($variable, [self::NAME, self::PARENT])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->removeNode($staticCallArg);
|
||||
$hasStaticCallChanged = true;
|
||||
}
|
||||
|
||||
if (! $hasStaticCallChanged) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->shouldRemoveEmptyCall($staticCall)) {
|
||||
$this->removeNode($staticCall);
|
||||
return null;
|
||||
}
|
||||
|
||||
return $staticCall;
|
||||
}
|
||||
|
||||
private function refactorNew(New_ $new): ?New_
|
||||
{
|
||||
$parameterNames = $this->methodReflectionProvider->provideParameterNamesByNew($new);
|
||||
|
||||
$hasNewChanged = false;
|
||||
foreach ($new->args as $position => $arg) {
|
||||
// is on position of $parent or $name?
|
||||
if (! isset($parameterNames[$position])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parameterName = $parameterNames[$position];
|
||||
if (! in_array($parameterName, [self::PARENT, self::NAME], true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$hasNewChanged = true;
|
||||
$this->removeNode($arg);
|
||||
}
|
||||
|
||||
if (! $hasNewChanged) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $new;
|
||||
}
|
||||
|
||||
private function shouldRemoveEmptyCall(StaticCall $staticCall): bool
|
||||
{
|
||||
foreach ($staticCall->args as $arg) {
|
||||
if ($this->isNodeRemoved($arg)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -21,9 +21,9 @@ use Rector\NodeTypeResolver\Node\AttributeKey;
|
|||
/**
|
||||
* @sponsor Thanks https://amateri.com for sponsoring this rule - visit them on https://www.startupjobs.cz/startup/scrumworks-s-r-o
|
||||
*
|
||||
* @see \Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\AddDatePickerToDateControlRectorTest
|
||||
* @see \Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector\AddNextrasDatePickerToDateControlRectorTest
|
||||
*/
|
||||
final class AddDatePickerToDateControlRector extends AbstractRector
|
||||
final class AddNextrasDatePickerToDateControlRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
use Nette\ComponentModel\IContainer;
|
||||
|
||||
class BackOrderControl extends Control
|
||||
{
|
||||
public function __construct($name = null, int $value, IContainer $parent = null)
|
||||
{
|
||||
parent::__construct($parent, $name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
use Nette\ComponentModel\IContainer;
|
||||
|
||||
class BackOrderControl extends Control
|
||||
{
|
||||
public function __construct(int $value)
|
||||
{
|
||||
parent::__construct($value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
use Nette\ComponentModel\IContainer;
|
||||
|
||||
class SomeControl extends Control
|
||||
{
|
||||
public function __construct(IContainer $parent = null, $name = null, int $value)
|
||||
{
|
||||
parent::__construct($parent, $name);
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
use Nette\ComponentModel\IContainer;
|
||||
|
||||
class SomeControl extends Control
|
||||
{
|
||||
public function __construct(int $value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Fixture;
|
||||
|
||||
use Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Source\SomeControlWithConstructorParentAndName;
|
||||
|
||||
final class NewInstance
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$someControlWithConstructorParentAndName = new SomeControlWithConstructorParentAndName(null, 'hey');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Fixture;
|
||||
|
||||
use Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Source\SomeControlWithConstructorParentAndName;
|
||||
|
||||
final class NewInstance
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$someControlWithConstructorParentAndName = new SomeControlWithConstructorParentAndName();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Fixture;
|
||||
|
||||
use Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Source\SomeControlWithoutConstructorParentAndName;
|
||||
|
||||
final class SkipAnotherParamInNewInstance
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$someControlWithConstructorParentAndName = new SomeControlWithoutConstructorParentAndName('key', 'value');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Nette\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class RemoveParentAndNameFromComponentConstructorRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return RemoveParentAndNameFromComponentConstructorRector::class;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Source;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
final class SomeControlWithConstructorParentAndName extends Control
|
||||
{
|
||||
public function __construct($parent = null, $name = '')
|
||||
{
|
||||
$this->parent = $parent;
|
||||
$this->name = $name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\RemoveParentAndNameFromComponentConstructorRector\Source;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
final class SomeControlWithoutConstructorParentAndName extends Control
|
||||
{
|
||||
private $key;
|
||||
private $value;
|
||||
|
||||
public function __construct($key, $value)
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector;
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Nette\Rector\MethodCall\AddDatePickerToDateControlRector;
|
||||
use Rector\Nette\Rector\MethodCall\AddNextrasDatePickerToDateControlRector;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class AddDatePickerToDateControlRectorTest extends AbstractRectorTestCase
|
||||
final class AddNextrasDatePickerToDateControlRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
|
@ -26,6 +26,6 @@ final class AddDatePickerToDateControlRectorTest extends AbstractRectorTestCase
|
|||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return AddDatePickerToDateControlRector::class;
|
||||
return AddNextrasDatePickerToDateControlRector::class;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
|
@ -18,7 +18,7 @@ class AddRule
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
|
@ -19,7 +19,7 @@ class AddRuleChainCalls
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
|
@ -17,7 +17,7 @@ class AssignedValue
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
|
@ -17,7 +17,7 @@ class SomeClass
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddNextrasDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
Loading…
Reference in New Issue
Block a user