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

FEATURE: Rewrite NodeType::getName() #26

Merged
merged 3 commits into from
Sep 29, 2023
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
9 changes: 9 additions & 0 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
use Rector\Transform\ValueObject\MethodCallToPropertyFetch;
use Neos\Rector\ContentRepository90\Rules\WorkspaceGetNameRector;
use Neos\Rector\ContentRepository90\Rules\NodeGetIdentifierRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeTypeNameRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeGetNameRector;

return static function (RectorConfig $rectorConfig): void {
// Register FusionFileProcessor. All Fusion Rectors will be auto-registered at this processor.
Expand Down Expand Up @@ -215,6 +217,13 @@
// hasProperty() ** (included/compatible in old NodeInterface)
// getLabel() ** (included/compatible in old NodeInterface)

/**
* Neos\ContentRepository\Core\NodeType\NodeType
*/
// getName()
$rectorConfig->rule(rectorClass: FusionNodeTypeNameRector::class);
$rectorConfig->rule(rectorClass: NodeTypeGetNameRector::class);

/**
* Neos\ContentRepository\Domain\Projection\Content\TraversableNodeInterface
*/
Expand Down
33 changes: 33 additions & 0 deletions src/ContentRepository90/Rules/FusionNodeTypeNameRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Neos\Rector\ContentRepository90\Rules;

use Neos\Rector\Core\FusionProcessing\EelExpressionTransformer;
use Neos\Rector\Core\FusionProcessing\FusionRectorInterface;
use Neos\Rector\Utility\CodeSampleLoader;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class FusionNodeTypeNameRector implements FusionRectorInterface
{

public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('Fusion: Rewrite node.nodeType.name to node.nodeTypeName.value', __CLASS__);
}

public function refactorFileContent(string $fileContent): string
{
return EelExpressionTransformer::parse($fileContent)
->process(fn(string $eelExpression) => preg_replace(
'/(node|documentNode|site)\.nodeType\.name/',
'$1.nodeTypeName.value',
$eelExpression
))
->addCommentsIfRegexMatches(
'/\.nodeType.name/',
'// TODO 9.0 migration: Line %LINE: You may need to rewrite "VARIABLE.nodeType.name" to "VARIABLE.nodeTypeName.value". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.'
)->getProcessedContent();
}
}
53 changes: 53 additions & 0 deletions src/ContentRepository90/Rules/NodeTypeGetNameRector.php
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 Neos\Rector\Utility\CodeSampleLoader;
use PhpParser\Node;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class NodeTypeGetNameRector extends AbstractRector
{
use AllTraits;

public function __construct(
private readonly NodesToAddCollector $nodesToAddCollector
) {
}

public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('"NodeType::getName()" will be rewritten', __CLASS__);
}

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

/**
* @param \PhpParser\Node\Expr\MethodCall $node
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);

if (!$this->isObjectType($node->var, new ObjectType('Neos\ContentRepository\Core\NodeType\NodeType'))) {
return null;
}
if (!$this->isName($node->name, 'getName')) {
return null;
}

$propertyFetchAggregateId = $this->nodeFactory->createPropertyFetch($node->var, 'name');
return $this->nodeFactory->createPropertyFetch($propertyFetchAggregateId, 'value');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
prototype(Neos.Fusion.Form:Checkbox) < prototype(Neos.Fusion.Form:Component.Field) {

renderer = Neos.Fusion:Component {

#
# pass down props
#
attributes = ${node.nodeType.name || documentNode.nodeType.name}
renderer = afx`
<input
name={node.nodeType.name}
value={someOtherVariable.nodeType.name}
{...node.nodeType.name}
/>
`
}
}
-----
// TODO 9.0 migration: Line 13: You may need to rewrite "VARIABLE.nodeType.name" to "VARIABLE.nodeTypeName.value". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.
prototype(Neos.Fusion.Form:Checkbox) < prototype(Neos.Fusion.Form:Component.Field) {

renderer = Neos.Fusion:Component {

#
# pass down props
#
attributes = ${node.nodeTypeName.value || documentNode.nodeTypeName.value}
renderer = afx`
<input
name={node.nodeTypeName.value}
value={someOtherVariable.nodeType.name}
{...node.nodeTypeName.value}
/>
`
}
}
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\FusionNodePathRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FusionNodeTypeNameRectorTest 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', '*.fusion.inc');
}

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

declare (strict_types=1);

use Neos\Rector\ContentRepository90\Rules\FusionNodeTypeNameRector;
use Neos\Rector\Core\FusionProcessing\FusionFileProcessor;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$services = $rectorConfig->services();
$services->defaults()
->public()
->autowire()
->autoconfigure();
$services->set(FusionFileProcessor::class);
$rectorConfig->disableParallel(); // does not work for fusion files - see https://github.com/rectorphp/rector-src/pull/2597#issuecomment-1190120688

$rectorConfig->rule(FusionNodeTypeNameRector::class);
};
27 changes: 27 additions & 0 deletions tests/Rules/NodeTypeGetNameRector/Fixture/all.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Neos\ContentRepository\Core\NodeType\NodeType;

class SomeClass
{
public function run(NodeType $nodetype)
{
$nodetype = $nodetype->getName();
}
}

?>
-----
<?php

use Neos\ContentRepository\Core\NodeType\NodeType;

class SomeClass
{
public function run(NodeType $nodetype)
{
$nodetype = $nodetype->name->value;
}
}

?>
31 changes: 31 additions & 0 deletions tests/Rules/NodeTypeGetNameRector/NodeTypeGetNameRectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Neos\Rector\Tests\Rules\NodeTypeGetNameRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class NodeTypeGetNameRectorTest 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';
}
}
11 changes: 11 additions & 0 deletions tests/Rules/NodeTypeGetNameRector/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare (strict_types=1);
//namespace RectorPrefix202208;

use Neos\Rector\ContentRepository90\Rules\NodeTypeGetNameRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rule(NodeTypeGetNameRector::class);
};
Loading