mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-14 12:29:43 +01:00
BootstrapToRouterFactoryRector detect
This commit is contained in:
parent
e3303e27f8
commit
2b7c57f9a6
@ -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
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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'
|
||||
);
|
||||
}
|
||||
|
@ -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;
|
@ -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;
|
Loading…
x
Reference in New Issue
Block a user