-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Rewrite NodeType::getName()
- Loading branch information
Showing
8 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/ContentRepository90/Rules/FusionNodeTypeNameRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('"NodeInterface::getChildNodes()" 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'); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/ContentRepository90/Rules/FusionNodeTypeNameRector/Fixture/some_file.fusion.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
/> | ||
` | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/ContentRepository90/Rules/FusionNodeTypeNameRector/FusionNodeTypeNameRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/ContentRepository90/Rules/FusionNodeTypeNameRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
tests/Rules/NodeTypeGetNameRector/NodeTypeGetNameRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
tests/Rules/NodeTypeGetNameRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |