mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
Merge pull request #152 from rectorphp/remove-router-rector
Drop BootstrapToRouterFactoryRector
This commit is contained in:
commit
f46c1354a3
@ -54,7 +54,6 @@ parameters:
|
||||
- */src/Rector/Contrib/*/*Rector.php
|
||||
- */src/Rector/Contrib/**/Helper/**.php
|
||||
- */packages/NodeTypeResolver/tests/NodeCallerTypeResolver/**Test.php
|
||||
- src/Builder/Contrib/Nette/RouterFactoryClassBuilder.php
|
||||
- */packages/NodeTypeResolver/**/PerNodeTypeResolver/**TypeResolver.php
|
||||
- */packages/NodeTypeResolver/**/PerNodeTypeResolver/**TypeResolver/Test.php
|
||||
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff:
|
||||
@ -62,8 +61,6 @@ parameters:
|
||||
- */packages/NodeValueResolver/src/PerNodeValueResolver/*ValueResolver.php
|
||||
Symplify\CodingStandard\Sniffs\DependencyInjection\NoClassInstantiationSniff:
|
||||
- bin/rector.php
|
||||
# test file
|
||||
- tests/Rector/Contrib/Nette/Routing/BootstrapToRouterFactoryRector/Wrong/bootstrap.php
|
||||
PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\EmptyStatementSniff:
|
||||
# intentionally
|
||||
- packages/BetterReflection/src/Reflector/SmartClassReflector.php
|
||||
|
@ -38,7 +38,6 @@ parameters:
|
||||
|
||||
excludes_analyse:
|
||||
# test files
|
||||
- '*tests/Rector/Contrib/Nette/Routing/BootstrapToRouterFactoryRector/Wrong/bootstrap.php'
|
||||
- '*tests/Rector/Dynamic/MethodNameReplacerRector/wrong/*'
|
||||
- '*tests/Rector/Dynamic/MethodNameReplacerRector/correct/*'
|
||||
- '*packages/**/tests/**/**Source/**'
|
||||
|
@ -1,97 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Builder\Contrib\Nette;
|
||||
|
||||
use PhpParser\BuilderFactory;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Node\NodeFactory;
|
||||
|
||||
final class RouterFactoryClassBuilder
|
||||
{
|
||||
/**
|
||||
* @var BuilderFactory
|
||||
*/
|
||||
private $builderFactory;
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
|
||||
public function __construct(BuilderFactory $builderFactory, NodeFactory $nodeFactory)
|
||||
{
|
||||
$this->builderFactory = $builderFactory;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param New_[] $newNodes
|
||||
* @return Node[]
|
||||
*/
|
||||
public function build(array $newNodes): array
|
||||
{
|
||||
$allNodes = [];
|
||||
|
||||
$allNodes[] = $this->nodeFactory->createDeclareStrictTypes();
|
||||
|
||||
$allNodes[] = $this->builderFactory->namespace('App\Routing\RouterFactory')
|
||||
->getNode();
|
||||
|
||||
$allNodes[] = $this->builderFactory->use('Nette\Application\Routers\Route')
|
||||
->getNode();
|
||||
|
||||
$allNodes[] = $this->builderFactory->use('Nette\Application\Routers\RouterList')
|
||||
->getNode();
|
||||
|
||||
$classBuild = $this->builderFactory->class('RouterFactory')
|
||||
->makeFinal();
|
||||
|
||||
$classMethodNode = $this->buildMethod($newNodes);
|
||||
|
||||
$classBuild->addStmts([$classMethodNode]);
|
||||
|
||||
$allNodes[] = $classBuild->getNode();
|
||||
|
||||
return $allNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param New_[] $newRouteNodes
|
||||
*/
|
||||
private function buildMethod(array $newRouteNodes): ClassMethod
|
||||
{
|
||||
$routerVariableNode = new Variable('router');
|
||||
|
||||
$methodStatements = [];
|
||||
|
||||
// adds "$router = new RouterList;"
|
||||
$methodStatements[] = new Assign($routerVariableNode, new New_(new Name('RouterList')));
|
||||
|
||||
// adds routes $router[] = new ...
|
||||
foreach ($newRouteNodes as $newRouteNode) {
|
||||
$methodStatements[] = new Assign(
|
||||
new ArrayDimFetch(
|
||||
$routerVariableNode
|
||||
),
|
||||
$newRouteNode
|
||||
);
|
||||
}
|
||||
|
||||
// adds "return $router;"
|
||||
$methodStatements[] = new Return_($routerVariableNode);
|
||||
|
||||
$methodBuild = $this->builderFactory->method('create')
|
||||
->makePublic()
|
||||
->addStmts($methodStatements)
|
||||
->setReturnType('RouterList');
|
||||
|
||||
return $methodBuild->getNode();
|
||||
}
|
||||
}
|
@ -1,131 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Nette\Routing;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\PrettyPrinter\Standard;
|
||||
use Rector\Builder\Contrib\Nette\RouterFactoryClassBuilder;
|
||||
use Rector\FileSystem\CurrentFileProvider;
|
||||
use Rector\NodeAnalyzer\AssignAnalyzer;
|
||||
use Rector\NodeVisitor\Collector\NodeCollector;
|
||||
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 = [];
|
||||
|
||||
/**
|
||||
* @var AssignAnalyzer
|
||||
*/
|
||||
private $assignAnalyzer;
|
||||
|
||||
/**
|
||||
* @var RouterFactoryClassBuilder
|
||||
*/
|
||||
private $routerFactoryClassBuilder;
|
||||
|
||||
/**
|
||||
* @var Standard
|
||||
*/
|
||||
private $standard;
|
||||
|
||||
/**
|
||||
* @var NodeCollector
|
||||
*/
|
||||
private $nodeCollector;
|
||||
|
||||
public function __construct(
|
||||
CurrentFileProvider $currentFileProvider,
|
||||
AssignAnalyzer $assignAnalyzer,
|
||||
RouterFactoryClassBuilder $routerFactoryClassBuilder,
|
||||
Standard $standard,
|
||||
NodeCollector $nodeCollector
|
||||
) {
|
||||
$this->currentFileProvider = $currentFileProvider;
|
||||
$this->assignAnalyzer = $assignAnalyzer;
|
||||
$this->routerFactoryClassBuilder = $routerFactoryClassBuilder;
|
||||
$this->standard = $standard;
|
||||
$this->nodeCollector = $nodeCollector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches $container->router[] = new ...;
|
||||
*/
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $this->isBootstrapFile()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node instanceof Expression) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->assignAnalyzer->isArrayAssignTypeAndProperty(
|
||||
$node->expr,
|
||||
'Nette\DI\Container',
|
||||
'router'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect new Route(...) and remove from origin file
|
||||
*
|
||||
* @param Expression $expressionNode
|
||||
*/
|
||||
public function refactor(Node $expressionNode): ?Node
|
||||
{
|
||||
/** @var Assign $assignNode */
|
||||
$assignNode = $expressionNode->expr;
|
||||
|
||||
$this->collectedRouteNodes[] = $assignNode->expr;
|
||||
|
||||
$this->nodeCollector->addNodeToRemove($expressionNode);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return Node[]
|
||||
*/
|
||||
public function afterTraverse(array $nodes): array
|
||||
{
|
||||
$routerFactoryClassNodes = $this->routerFactoryClassBuilder->build($this->collectedRouteNodes);
|
||||
|
||||
$this->collectedRouteNodes = [];
|
||||
|
||||
// save file to same location as bootstrap.php is
|
||||
$currentFileInfo = $this->currentFileProvider->getCurrentFile();
|
||||
$fileLocation = dirname($currentFileInfo->getRealPath()) . DIRECTORY_SEPARATOR . 'RouterFactory.php';
|
||||
|
||||
file_put_contents(
|
||||
$fileLocation,
|
||||
$this->standard->prettyPrintFile($routerFactoryClassNodes)
|
||||
);
|
||||
|
||||
return $nodes;
|
||||
}
|
||||
|
||||
private function isBootstrapFile(): bool
|
||||
{
|
||||
$fileInfo = $this->currentFileProvider->getCurrentFile();
|
||||
|
||||
return $fileInfo->getFilename() === self::BOOTSTRAP_FILE_NAME;
|
||||
}
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Nette\Routing;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use Rector\FileSystem\CurrentFileProvider;
|
||||
use Rector\NodeAnalyzer\AssignAnalyzer;
|
||||
use Rector\NodeVisitor\Collector\NodeCollector;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
* Cleanup Rector to @see BootstrapToRouterFactoryRector
|
||||
*/
|
||||
final class CleanupBootstrapToRouterFactoryRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const BOOTSTRAP_FILE_NAME = 'bootstrap.php';
|
||||
|
||||
/**
|
||||
* @var CurrentFileProvider
|
||||
*/
|
||||
private $currentFileProvider;
|
||||
|
||||
/**
|
||||
* @var AssignAnalyzer
|
||||
*/
|
||||
private $assignAnalyzer;
|
||||
|
||||
/**
|
||||
* @var NodeCollector
|
||||
*/
|
||||
private $nodeCollector;
|
||||
|
||||
public function __construct(
|
||||
CurrentFileProvider $currentFileProvider,
|
||||
AssignAnalyzer $assignAnalyzer,
|
||||
NodeCollector $nodeCollector
|
||||
) {
|
||||
$this->currentFileProvider = $currentFileProvider;
|
||||
$this->assignAnalyzer = $assignAnalyzer;
|
||||
$this->nodeCollector = $nodeCollector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches $container->router[] = new ...;
|
||||
*/
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $this->isBootstrapFile()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node instanceof Expression) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isContainerRouterAssign($node->expr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect new Route(...) and remove from origin file
|
||||
*
|
||||
* @param Expression $expressionNode
|
||||
*/
|
||||
public function refactor(Node $expressionNode): ?Node
|
||||
{
|
||||
$this->nodeCollector->addNodeToRemove($expressionNode);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isBootstrapFile(): bool
|
||||
{
|
||||
$fileInfo = $this->currentFileProvider->getCurrentFile();
|
||||
|
||||
return $fileInfo->getFilename() === self::BOOTSTRAP_FILE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects "$container->router = "
|
||||
*/
|
||||
private function isContainerRouterAssign(Expr $exprNode): bool
|
||||
{
|
||||
return $this->assignAnalyzer->isAssignTypeAndProperty(
|
||||
$exprNode,
|
||||
'Nette\DI\Container',
|
||||
'router'
|
||||
);
|
||||
}
|
||||
}
|
@ -2,5 +2,3 @@ rectors:
|
||||
Rector\Rector\Dynamic\ClassReplacerRector:
|
||||
'Environment': 'Nette\Environment'
|
||||
'Image': 'Nette\Image'
|
||||
Rector\Rector\Contrib\Nette\Routing\BootstrapToRouterFactoryRector: ~
|
||||
Rector\Rector\Contrib\Nette\Routing\CleanupBootstrapToRouterFactoryRector: ~
|
||||
|
@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace App\Routing\RouterFactory;
|
||||
|
||||
use Nette\Application\Routers\Route;
|
||||
use Nette\Application\Routers\RouterList;
|
||||
final class RouterFactory
|
||||
{
|
||||
public function create() : RouterList
|
||||
{
|
||||
$router = new RouterList();
|
||||
$router[] = new Route('index', 'Page:default');
|
||||
return $router;
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
use Nette\Application\Routers\Route;
|
||||
use Nette\Application\Routers\RouteList;
|
||||
use Nette\Config\Configurator;
|
||||
|
||||
$configurator = new Configurator;
|
||||
|
||||
$container = $configurator->createContainer();
|
||||
|
||||
|
||||
|
||||
|
||||
return $container;
|
@ -1,45 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\Nette\Routing\BootstrapToRouterFactoryRector;
|
||||
|
||||
use Rector\Rector\Contrib\Nette\Routing\BootstrapToRouterFactoryRector;
|
||||
use Rector\Rector\Contrib\Nette\Routing\CleanupBootstrapToRouterFactoryRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class Test extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const GENERATED_ROUTER_FACTORY_FILE = __DIR__ . '/Wrong/RouterFactory.php';
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
unlink(self::GENERATED_ROUTER_FACTORY_FILE);
|
||||
}
|
||||
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/Wrong/bootstrap.php',
|
||||
__DIR__ . '/Correct/correct.php.inc'
|
||||
);
|
||||
|
||||
$this->assertFileExists(self::GENERATED_ROUTER_FACTORY_FILE);
|
||||
$this->assertFileEquals(
|
||||
self::GENERATED_ROUTER_FACTORY_FILE,
|
||||
__DIR__ . '/Correct/RouterFactory.php.expected.inc'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRectorClasses(): array
|
||||
{
|
||||
return [
|
||||
BootstrapToRouterFactoryRector::class,
|
||||
CleanupBootstrapToRouterFactoryRector::class,
|
||||
];
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
use Nette\Application\Routers\Route;
|
||||
use Nette\Application\Routers\RouteList;
|
||||
use Nette\Config\Configurator;
|
||||
|
||||
$configurator = new Configurator;
|
||||
|
||||
$container = $configurator->createContainer();
|
||||
|
||||
$container->router = new RouteList;
|
||||
$container->router[] = new Route('index', 'Page:default');
|
||||
|
||||
return $container;
|
Loading…
x
Reference in New Issue
Block a user