Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Nette 3.1 #5195

Merged
merged 17 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions config/set/nette-31.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

use Rector\Composer\Rector\ChangePackageVersionRector;
use Rector\Composer\Rector\RemovePackageRector;
use Rector\Composer\ValueObject\PackageAndVersion;
use Rector\Generic\Rector\Assign\DimFetchAssignToMethodCallRector;
use Rector\Generic\ValueObject\DimFetchAssignToMethodCall;
use Rector\Nette\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\Rector\StaticCall\RenameStaticMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\RenameStaticMethod;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(RenameClassRector::class)->call('configure', [[
RenameClassRector::OLD_TO_NEW_CLASSES => [
// https://github.com/nette/application/compare/v3.0.7...v3.1.0
'Nette\Application\IRouter' => 'Nette\Routing\Router',
'Nette\Application\IResponse' => 'Nette\Application\Response',
'Nette\Application\UI\IRenderable' => 'Nette\Application\UI\Renderable',
'Nette\Application\UI\ISignalReceiver' => 'Nette\Application\UI\SignalReceiver',
'Nette\Application\UI\IStatePersistent' => 'Nette\Application\UI\StatePersistent',
'Nette\Application\UI\ITemplate' => 'Nette\Application\UI\Template',
'Nette\Application\UI\ITemplateFactory' => 'Nette\Application\UI\TemplateFactory',
'Nette\Bridges\ApplicationLatte\ILatteFactory' => 'Nette\Bridges\ApplicationLatte\LatteFactory',

// https://github.com/nette/bootstrap/compare/v3.0.2...v3.1.0
'Nette\Configurator' => 'Nette\Bootstrap\Configurator',

// https://github.com/nette/caching/compare/v3.0.2...v3.1.0
'Nette\Caching\IBulkReader' => 'Nette\Caching\BulkReader',
'Nette\Caching\IStorage' => 'Nette\Caching\Storage',
'Nette\Caching\Storages\IJournal' => 'Nette\Caching\Storages\Journal',

// https://github.com/nette/database/compare/v3.0.7...v3.1.1
'Nette\Database\ISupplementalDriver' => 'Nette\Database\Driver',
'Nette\Database\IConventions' => 'Nette\Database\Conventions',
'Nette\Database\Context' => 'Nette\Database\Explorer',
'Nette\Database\IRow' => 'Nette\Database\Row',
'Nette\Database\IRowContainer' => 'Nette\Database\ResultSet',
'Nette\Database\Table\IRow' => 'Nette\Database\Table\ActiveRow',
'Nette\Database\Table\IRowContainer' => 'Nette\Database\Table\Selection',

// https://github.com/nette/forms/compare/v3.0.7...v3.1.0-RC2
'Nette\Forms\IControl' => 'Nette\Forms\Control',
'Nette\Forms\IFormRenderer' => 'Nette\Forms\FormRenderer',
'Nette\Forms\ISubmitterControl' => 'Nette\Forms\SubmitterControl',

// https://github.com/nette/mail/compare/v3.0.1...v3.1.5
'Nette\Mail\IMailer' => 'Nette\Mail\Mailer',

// https://github.com/nette/security/compare/v3.0.5...v3.1.2
'Nette\Security\IAuthorizator' => 'Nette\Security\Authorizator',
'Nette\Security\Identity' => 'Nette\Security\SimpleIdentity',
'Nette\Security\IResource' => 'Nette\Security\Resource',
'Nette\Security\IRole' => 'Nette\Security\Role',

// https://github.com/nette/utils/compare/v3.1.4...v3.2.1
'Nette\Utils\IHtmlString' => 'Nette\HtmlStringable',
'Nette\Localization\ITranslator' => 'Nette\Localization\Translator',

// https://github.com/nette/latte/compare/v2.5.5...v2.9.2
'Latte\ILoader' => 'Latte\Loader',
'Latte\IMacro' => 'Latte\Macro',
'Latte\Runtime\IHtmlString' => 'Latte\Runtime\HtmlStringable',
'Latte\Runtime\ISnippetBridge' => 'Latte\Runtime\SnippetBridge',
],
]]);

$services->set(RenameMethodRector::class)->call('configure', [[
RenameMethodRector::METHOD_CALL_RENAMES => ValueObjectInliner::inline([
// https://github.com/nette/caching/commit/60281abf366c4ab76e9436dc1bfe2e402db18b67
new MethodCallRename('Nette\Caching\Cache', 'start', 'capture'),
// https://github.com/nette/forms/commit/faaaf8b8fd3408a274a9de7ca3f342091010ad5d
new MethodCallRename('Nette\Forms\Container', 'addImage', 'addImageButton'),
// https://github.com/nette/utils/commit/d0427c1811462dbb6c503143eabe5478b26685f7
new MethodCallRename('Nette\Utils\Arrays', 'searchKey', 'getKeyOffset'),
]),
]]);

$services->set(RenameStaticMethodRector::class)
->call('configure', [[
RenameStaticMethodRector::OLD_TO_NEW_METHODS_BY_CLASSES => ValueObjectInliner::inline([
// https://github.com/nette/utils/commit/8a4b795acd00f3f6754c28a73a7e776b60350c34
new RenameStaticMethod('Nette\Utils\Callback', 'closure', 'Closure', 'fromCallable'),
]),
]]);

$services->set(DimFetchAssignToMethodCallRector::class)
->call('configure', [[
DimFetchAssignToMethodCallRector::DIM_FETCH_ASSIGN_TO_METHOD_CALL => ValueObjectInliner::inline([
new DimFetchAssignToMethodCall(
'Nette\Application\Routers\RouteList',
'Nette\Application\Routers\Route',
'addRoute'
),
])
]]);

$services->set(ContextGetByTypeToConstructorInjectionRector::class);

$services->set(ChangePackageVersionRector::class)
->call('configure', [[
ChangePackageVersionRector::PACKAGES_AND_VERSIONS => ValueObjectInliner::inline([
// meta package
new PackageAndVersion('nette/nette', '^3.1'),
// https://github.com/nette/nette/blob/v3.0.0/composer.json vs https://github.com/nette/nette/blob/v3.1.0/composer.json
new PackageAndVersion('nette/application', '^3.1'),
new PackageAndVersion('nette/bootstrap', '^3.1'),
new PackageAndVersion('nette/caching', '^3.1'),
new PackageAndVersion('nette/database', '^3.1'),
new PackageAndVersion('nette/di', '^3.0'),
new PackageAndVersion('nette/finder', '^2.5'),
new PackageAndVersion('nette/forms', '3.1.0-RC2'), // TODO change when 3.1 will be released
new PackageAndVersion('nette/http', '^3.1'),
new PackageAndVersion('nette/mail', '^3.1'),
new PackageAndVersion('nette/php-generator', '^3.5'),
new PackageAndVersion('nette/robot-loader', '^3.3'),
new PackageAndVersion('nette/safe-stream', '^2.4'),
new PackageAndVersion('nette/security', '^3.1'),
new PackageAndVersion('nette/tokenizer', '^3.0'),
new PackageAndVersion('nette/utils', '^3.2'),
new PackageAndVersion('latte/latte', '^2.9'),
new PackageAndVersion('tracy/tracy', '^2.8'),
]),
]]);

$services->set(RemovePackageRector::class)
->call('configure', [[
RemovePackageRector::PACKAGE_NAMES => [
'nette/component-model',
'nette/neon'
],
]]);
};
5 changes: 5 additions & 0 deletions packages/set/src/ValueObject/SetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ final class SetList
*/
public const NETTE_30_DEPENDENCY_INJECTION = __DIR__ . '/../../../../config/set/nette-30-dependency-injection.php';

/**
* @var string
*/
public const NETTE_31 = __DIR__ . '/../../../../config/set/nette-31.php';

/**
* @var string
*/
Expand Down
131 changes: 131 additions & 0 deletions rules/generic/src/Rector/Assign/DimFetchAssignToMethodCallRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);

namespace Rector\Generic\Rector\Assign;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Generic\ValueObject\DimFetchAssignToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Generic\Tests\Rector\Assign\DimFetchAssignToMethodCallRector\DimFetchAssignToMethodCallRectorTest
*/
final class DimFetchAssignToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
public const DIM_FETCH_ASSIGN_TO_METHOD_CALL = 'dim_fetch_assign_to_method_call';
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var DimFetchAssignToMethodCall[]
*/
private $dimFetchAssignToMethodCalls = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change magic array access add to $list, to explicit $list->$addMethod(...)',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use Nette\Application\Routers\Route;
use Nette\Application\Routers\RouteList;

class RouterFactory
{
public static function createRouter()
{
$routeList = new RouteList();
$routeList[] = new Route('<module>/<presenter>/<action>[/<id>]', 'Homepage:default');
return $routeList;
}
}
CODE_SAMPLE,
<<<'CODE_SAMPLE'
use Nette\Application\Routers\RouteList;

class RouterFactory
{
public static function createRouter()
{
$routeList = new RouteList();
$routeList->addRoute('<module>/<presenter>/<action>[/<id>]', 'Homepage:default');
return $routeList;
}
}
CODE_SAMPLE,
[
self::DIM_FETCH_ASSIGN_TO_METHOD_CALL => [
new DimFetchAssignToMethodCall(
'Nette\Application\Routers\RouteList',
'Nette\Application\Routers\Route',
'addRoute'
)
],
]
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Assign::class];
}

/**
* @param Assign $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->var instanceof ArrayDimFetch) {
return null;
}

$arrayDimFetch = $node->var;
if (! $arrayDimFetch->var instanceof Variable) {
return null;
}

if (! $node->expr instanceof New_) {
return null;
}

$dimFetchAssignToMethodCall = $this->findDimFetchAssignToMethodCall($node);
if ($dimFetchAssignToMethodCall === null) {
return null;
}

return new MethodCall($arrayDimFetch->var, $dimFetchAssignToMethodCall->getAddMethod(), $node->expr->args);
}

private function findDimFetchAssignToMethodCall(Assign $assign): ?DimFetchAssignToMethodCall
{
/** @var ArrayDimFetch $arrayDimFetch */
$arrayDimFetch = $assign->var;

foreach ($this->dimFetchAssignToMethodCalls as $dimFetchAssignToMethodCall) {
if ($this->isObjectType($arrayDimFetch->var, $dimFetchAssignToMethodCall->getListClass()) && $this->isObjectType($assign->expr, $dimFetchAssignToMethodCall->getItemClass())) {
return $dimFetchAssignToMethodCall;
}
}
return null;
}

public function configure(array $configuration): void
{
$dimFetchAssignToMethodCalls = $configuration[self::DIM_FETCH_ASSIGN_TO_METHOD_CALL] ?? [];
Assert::allIsInstanceOf($dimFetchAssignToMethodCalls, DimFetchAssignToMethodCall::class);
$this->dimFetchAssignToMethodCalls = $dimFetchAssignToMethodCalls;
}
}
52 changes: 52 additions & 0 deletions rules/generic/src/ValueObject/DimFetchAssignToMethodCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Rector\Generic\ValueObject;

final class DimFetchAssignToMethodCall
{
/**
* @var string
*/
private $listClass;

/**
* @var string
*/
private $itemClass;

/**
* @var string
*/
private $addMethod;

public function __construct(string $listClass, string $itemClass, string $addMethod)
{
$this->listClass = $listClass;
$this->itemClass = $itemClass;
$this->addMethod = $addMethod;
}

/**
* @return string
*/
public function getListClass(): string
{
return $this->listClass;
}

/**
* @return string
*/
public function getItemClass(): string
{
return $this->itemClass;
}

/**
* @return string
*/
public function getAddMethod(): string
{
return $this->addMethod;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Rector\Generic\Tests\Rector\Assign\DimFetchAssignToMethodCallRector;

use Iterator;
use Rector\Generic\Rector\Assign\DimFetchAssignToMethodCallRector;
use Rector\Generic\ValueObject\DimFetchAssignToMethodCall;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DimFetchAssignToMethodCallRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorsWithConfiguration(): array
{
return [
DimFetchAssignToMethodCallRector::class => [
DimFetchAssignToMethodCallRector::DIM_FETCH_ASSIGN_TO_METHOD_CALL => [
new DimFetchAssignToMethodCall(
'Nette\Application\Routers\RouteList',
'Nette\Application\Routers\Route',
'addRoute'
),
],
],
];
}
}
Loading