[NetteCodeQuality] Get component (#3870)

This commit is contained in:
Tomas Votruba 2020-08-02 11:39:52 +02:00 committed by GitHub
parent 6186cdeab7
commit 91bda1b75b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 58 additions and 162 deletions

View File

@ -3,7 +3,6 @@
declare(strict_types=1);
use Rector\Nette\Rector\ClassMethod\TemplateMagicAssignToExplicitVariableArrayRector;
use Rector\NetteCodeQuality\Rector\ArrayDimFetch\ArrayDimFetchControlToGetComponentMethodCallRector;
use Rector\NetteCodeQuality\Rector\ArrayDimFetch\ChangeControlArrayAccessToAnnotatedControlVariableRector;
use Rector\NetteCodeQuality\Rector\Assign\ArrayAccessGetControlToGetComponentMethodCallRector;
use Rector\NetteCodeQuality\Rector\Assign\ArrayAccessSetControlToAddComponentMethodCallRector;
@ -19,8 +18,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(ChangeControlArrayAccessToAnnotatedControlVariableRector::class);
$services->set(ArrayDimFetchControlToGetComponentMethodCallRector::class);
$services->set(ArrayAccessSetControlToAddComponentMethodCallRector::class);
$services->set(ArrayAccessGetControlToGetComponentMethodCallRector::class);

View File

@ -1,4 +1,4 @@
# All 549 Rectors Overview
# All 548 Rectors Overview
- [Projects](#projects)
---
@ -31,7 +31,7 @@
- [MysqlToMysqli](#mysqltomysqli) (4)
- [Naming](#naming) (3)
- [Nette](#nette) (14)
- [NetteCodeQuality](#nettecodequality) (7)
- [NetteCodeQuality](#nettecodequality) (6)
- [NetteKdyby](#nettekdyby) (4)
- [NetteTesterToPHPUnit](#nettetestertophpunit) (3)
- [NetteToSymfony](#nettetosymfony) (9)
@ -6996,33 +6996,6 @@ Change magic arrays access set, to explicit `$this->setComponent(...)` method
<br><br>
### `ArrayDimFetchControlToGetComponentMethodCallRector`
- class: [`Rector\NetteCodeQuality\Rector\ArrayDimFetch\ArrayDimFetchControlToGetComponentMethodCallRector`](/../master/rules/nette-code-quality/src/Rector/ArrayDimFetch/ArrayDimFetchControlToGetComponentMethodCallRector.php)
- [test fixtures](/../master/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ArrayDimFetchControlToGetComponentMethodCallRector/Fixture)
Change array dim `$this["someComponent"]` to more explicit `$this->getComponent("someComponent")`
```diff
use Nette\Application\UI\Presenter;
class SomePresenter extends Presenter
{
public function someAction()
{
- $form = $this['someForm'];
+ $form = $this->getComponent('someForm');
}
protected function createComponentSomeForm()
{
return new Form();
}
}
```
<br><br>
### `ChangeControlArrayAccessToAnnotatedControlVariableRector`
- class: [`Rector\NetteCodeQuality\Rector\ArrayDimFetch\ChangeControlArrayAccessToAnnotatedControlVariableRector`](/../master/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php)

View File

@ -1,91 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\NetteCodeQuality\Rector\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
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 https://doc.nette.org/en/3.0/components#toc-advanced-use-of-components
*
* @see \Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ArrayDimFetchControlToGetComponentMethodCallRector\ArrayDimFetchControlToGetComponentMethodCallRectorTest
*/
final class ArrayDimFetchControlToGetComponentMethodCallRector extends AbstractArrayDimFetchToAnnotatedControlVariableRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Change array dim $this["someComponent"] to more explicit $this->getComponent("someComponent")',
[
new CodeSample(
<<<'PHP'
use Nette\Application\UI\Presenter;
class SomePresenter extends Presenter
{
public function someAction()
{
$form = $this['someForm'];
}
protected function createComponentSomeForm()
{
return new Form();
}
}
PHP
,
<<<'PHP'
use Nette\Application\UI\Presenter;
class SomePresenter extends Presenter
{
public function someAction()
{
$form = $this->getComponent('someForm');
}
protected function createComponentSomeForm()
{
return new Form();
}
}
PHP
),
]
);
}
/**
* @param ArrayDimFetch $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
$controlName = $this->controlDimFetchAnalyzer->matchNameOnControlVariable($node);
return new MethodCall($node->var, 'getComponent', $this->createArgs([$controlName]));
}
private function shouldSkip(ArrayDimFetch $arrayDimFetch): bool
{
$parent = $arrayDimFetch->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof Assign && $parent->var === $arrayDimFetch) {
return true;
}
return $parent instanceof MethodCall;
}
}

View File

@ -14,7 +14,6 @@ use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Rector\BetterPhpDocParser\PhpDocManipulator\VarAnnotationManipulator;
@ -207,7 +206,7 @@ PHP
$methodName = sprintf('createComponent%s', ucfirst($componentName));
$calledOnType = $scope->getType($expr);
if (! $calledOnType instanceof ObjectType) {
if (! $calledOnType instanceof TypeWithClassName) {
return new MixedType();
}

View File

@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ArrayDimFetchControlToGetComponentMethodCallRector;
use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\NetteCodeQuality\Rector\ArrayDimFetch\ArrayDimFetchControlToGetComponentMethodCallRector;
use Symplify\SmartFileSystem\SmartFileInfo;
final class ArrayDimFetchControlToGetComponentMethodCallRectorTest 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 ArrayDimFetchControlToGetComponentMethodCallRector::class;
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ArrayDimFetchControlToGetComponentMethodCallRector\Fixture;
namespace Rector\NetteCodeQuality\Tests\Rector\Assign\ArrayAccessGetControlToGetComponentMethodCallRector\Fixture;
use Nette\Application\UI\Presenter;
@ -21,7 +21,7 @@ class SomePresenter extends Presenter
-----
<?php
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ArrayDimFetchControlToGetComponentMethodCallRector\Fixture;
namespace Rector\NetteCodeQuality\Tests\Rector\Assign\ArrayAccessGetControlToGetComponentMethodCallRector\Fixture;
use Nette\Application\UI\Presenter;

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ArrayDimFetchControlToGetComponentMethodCallRector\Fixture;
namespace Rector\NetteCodeQuality\Tests\Rector\Assign\ArrayAccessGetControlToGetComponentMethodCallRector\Fixture;
use Nette\Application\UI\Presenter;
use Nette\Forms\Form;

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ArrayDimFetchControlToGetComponentMethodCallRector\Fixture;
namespace Rector\NetteCodeQuality\Tests\Rector\Assign\ArrayAccessGetControlToGetComponentMethodCallRector\Fixture;
use Nette\Application\UI\Presenter;
use Nette\Application\UI\Form;

View File

@ -0,0 +1,50 @@
<?php
namespace Rector\NetteCodeQuality\Tests\Rector\Assign\MakeGetComponentAssignAnnotatedRector\Fixture;
use Nette\Application\UI\Presenter;
use Rector\NetteCodeQuality\Tests\Rector\Assign\MakeGetComponentAssignAnnotatedRector\Source\AnotherControl;
final class InPresenter extends Presenter
{
public function run()
{
$anotherControl = $this->getComponent('another');
}
/**
* @return AnotherControl
*/
protected function createComponentAnother()
{
return new AnotherControl();
}
}
?>
-----
<?php
namespace Rector\NetteCodeQuality\Tests\Rector\Assign\MakeGetComponentAssignAnnotatedRector\Fixture;
use Nette\Application\UI\Presenter;
use Rector\NetteCodeQuality\Tests\Rector\Assign\MakeGetComponentAssignAnnotatedRector\Source\AnotherControl;
final class InPresenter extends Presenter
{
public function run()
{
/** @var \Rector\NetteCodeQuality\Tests\Rector\Assign\MakeGetComponentAssignAnnotatedRector\Source\AnotherControl $anotherControl */
$anotherControl = $this->getComponent('another');
}
/**
* @return AnotherControl
*/
protected function createComponentAnother()
{
return new AnotherControl();
}
}
?>

View File

@ -10,5 +10,4 @@ if (class_exists('Nette\Application\UI\Presenter')) {
class Presenter extends Control
{
}

View File

@ -98,7 +98,7 @@ abstract class Container implements IContainer
* Returns component specified by name or path.
* @param bool $throw throw exception if component doesn't exist?
*/
final public function getComponent(string $name, bool $throw = true): ?IComponent
public function getComponent(string $name, bool $throw = true): ?IComponent
{
[$name] = $parts = explode(self::NAME_SEPARATOR, $name, 2);