BootstrapToRouterFactoryRector detect

This commit is contained in:
TomasVotruba 2017-10-03 21:59:14 +02:00
parent e3303e27f8
commit 2b7c57f9a6
7 changed files with 64 additions and 25 deletions

View File

@ -69,4 +69,6 @@ parameters:
- */packages/NodeValueResolver/src/PerNodeValueResolver/*ValueResolver.php
Symplify\CodingStandard\Sniffs\DependencyInjection\NoClassInstantiationSniff:
# temp ArgvInput: added to master
- bin/rector.php
- bin/rector.php
# test file
- tests/Rector/Contrib/Nette/Routing/BootstrapToRouterFactoryRector/Wrong/bootstrap.php

View File

@ -25,7 +25,7 @@ final class FormNegativeRulesRector extends AbstractRector
private const RULE_NAMES = ['FILLED', 'EQUAL'];
/**
* Detects "$form->addRule(~Form::FILLED, ...)"
* Detects "~Form::FILLED"
*/
public function isCandidate(Node $node): bool
{

View File

@ -3,34 +3,67 @@
namespace Rector\Rector\Contrib\Nette\Routing;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use Rector\FileSystem\CurrentFileProvider;
use Rector\Rector\AbstractRector;
final class BootstrapToRouterFactoryRector extends AbstractRector
{
/**
* @var string
*/
private const BOOTSTRAP_FILE_NAME = 'bootstrap.php';
/**
* @var CurrentFileProvider
*/
private $currentFileProvider;
/**
* @var mixed[]
*/
private $collectedRouteNodes = [];
public function __construct(CurrentFileProvider $currentFileProvider)
{
$this->currentFileProvider = $currentFileProvider;
}
/**
* Matches $container->router[] =
*/
public function isCandidate(Node $node): bool
{
dump($this->currentFileProvider->getCurrentFile());
$fileInfo = $this->currentFileProvider->getCurrentFile();
if ($fileInfo->getFilename() !== self::BOOTSTRAP_FILE_NAME) {
return false;
}
// current file bootstrap.php
if (! $node instanceof Assign) {
return false;
}
// TODO: Implement isCandidate() method.
if (! $node->var instanceof ArrayDimFetch) {
return false;
}
if ($node->var->var->var->name !== 'container') {
return false;
}
return $node->var->var->name->name === 'router';
}
public function refactor(Node $node): ?Node
/**
* Collect new Route(...) and remove
*
* @param Assign $assignNode
*/
public function refactor(Node $assignNode): ?Node
{
// extract routing part
// store it to app/Router/RouterFactory.php
// TODO: Implement refactor() method.
$this->collectedRouteNodes[] = $assignNode->var;
return null;
}
}

View File

@ -1,11 +1,14 @@
<?php declare(strict_types=1);
use Nette\Application\Routers\Route;
use Nette\Application\Routers\RouterList;
final class RouterFactory
{
public function create(): Nette\Application\Routers\RouterList
public function create(): RouterList
{
$router = new Nette\Application\Routers\RouterList;
$router[] = new Nette\Application\Routers\Route('index', 'Page:default');
$router = new RouterList;
$router[] = new Route('index', 'Page:default');
return $router;
}

View File

@ -10,7 +10,7 @@ final class Test extends AbstractRectorTestCase
public function test(): void
{
$this->doTestFileMatchesExpectedContent(
__DIR__ . '/Wrong/wrong.php.inc',
__DIR__ . '/Wrong/bootstrap.php',
__DIR__ . '/Correct/correct.php.inc'
);
}

View File

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
use Nette\Application\Routers\RouterList;
use Nette\Config\Configurator;
$configurator = new Configurator;
$container = $configurator->createContainer();
$container->router = new RouterList;
$container->router[] = new Router('index', 'Page:default');
return $container;

View File

@ -1,12 +0,0 @@
<?php declare (strict_types=1);
use Nette\Application\Routers\Route;
$configurator = new Nette\Config\Configurator;
$container = $configurator->createContainer();
$container->router = new Nette\Application\Routers\RouterList;
$container->router[] = new Router('index', 'Page:default');
return $container;