Skip to content

Commit

Permalink
Merge pull request #69 from neos/migrate-utility
Browse files Browse the repository at this point in the history
TASK: Correctly migrate Neos\ContentRepository\Utility
  • Loading branch information
dlubitz authored Jun 17, 2024
2 parents ca5ad77 + 38001f5 commit efcb725
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 6 deletions.
8 changes: 2 additions & 6 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
use Neos\Rector\Generic\Rules\FusionReplacePrototypeNameRector;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameReplacement;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameAddComment;
use Neos\Rector\ContentRepository90\Rules\ContentRepositoryUtilityRenderValidNodeNameRector;

return static function (RectorConfig $rectorConfig): void {
// Register FusionFileProcessor. All Fusion Rectors will be auto-registered at this processor.
Expand Down Expand Up @@ -394,12 +395,7 @@
new FusionPrototypeNameAddComment('Neos.Fusion:Attributes', 'TODO 9.0 migration: Neos.Fusion:Attributes has been removed without a replacement. You need to replace it by the property attributes in Neos.Fusion:Tag')
]);

$rectorConfig->ruleWithConfiguration(RenameStaticMethodRector::class, [new RenameStaticMethod(
\Neos\ContentRepository\Utility::class,
'renderValidNodeName',
\Neos\ContentRepository\Core\SharedModel\Node\NodeName::class,
'transliterateFromString'
)]);
$rectorConfig->rule(ContentRepositoryUtilityRenderValidNodeNameRector::class);

/**
* SPECIAL rules
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare (strict_types=1);

namespace Neos\Rector\ContentRepository90\Rules;

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use PHPStan\Type\ObjectType;

final class ContentRepositoryUtilityRenderValidNodeNameRector extends AbstractRector
{

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replaces Utility::renderValidNodeName(...) into NodeName::fromString(...)->value.', [new CodeSample('\Neos\ContentRepository\Utility::renderValidNodeName(\'foo\');', '\Neos\ContentRepository\Core\SharedModel\Node\NodeName::fromString(\'foo\')->value;')]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}

/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (!$this->isName($node->name, 'renderValidNodeName')) {
return null;
}
if (!$this->isObjectType($node->class, new ObjectType(\Neos\ContentRepository\Utility::class))) {
return null;
}
$staticCall = $this->rename($node, \Neos\ContentRepository\Core\SharedModel\Node\NodeName::class, 'fromString');
return $this->nodeFactory->createPropertyFetch($staticCall, 'value');
}

private function rename(StaticCall $staticCall, $newClassName, $newMethodName): StaticCall
{
$staticCall->name = new Identifier($newMethodName);
$staticCall->class = new FullyQualified($newClassName);
return $staticCall;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Neos\Rector\Tests\ContentRepository90\Rules\ContextGetFirstLevelNodeCacheRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

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

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

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Neos\ContentRepository\Utility;

class SomeClass
{
public function __construct($nodeName)
{
$nodeName = Utility::renderValidNodeName($nodeName);
$liveNodeName = Utility::renderValidNodeName('live');
}
}

?>
-----
<?php

use Neos\ContentRepository\Utility;

class SomeClass
{
public function __construct($nodeName)
{
$nodeName = \Neos\ContentRepository\Core\SharedModel\Node\NodeName::fromString($nodeName)->value;
$liveNodeName = \Neos\ContentRepository\Core\SharedModel\Node\NodeName::fromString('live')->value;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare (strict_types=1);

use Rector\Config\RectorConfig;
use Neos\Rector\Generic\Rules\InjectServiceIfNeededRector;
use Neos\Rector\Generic\ValueObject\AddInjection;
use Neos\Neos\Domain\Service\RenderingModeService;

return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rule(\Neos\Rector\ContentRepository90\Rules\ContentRepositoryUtilityRenderValidNodeNameRector::class);
};

0 comments on commit efcb725

Please sign in to comment.