-
-
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.
Merge pull request #89 from neos/task/node-label-generator
TASK: Add migration for getLabel with NodeLabelGeneratorRector
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
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
60 changes: 60 additions & 0 deletions
60
src/ContentRepository90/Rules/NodeLabelGeneratorRector.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,60 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
namespace Neos\Rector\ContentRepository90\Rules; | ||
|
||
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub; | ||
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 NodeLabelGeneratorRector extends AbstractRector | ||
{ | ||
use AllTraits; | ||
|
||
public function __construct( | ||
private readonly NodesToAddCollector $nodesToAddCollector | ||
) | ||
{ | ||
} | ||
|
||
public function getRuleDefinition() : RuleDefinition | ||
{ | ||
return CodeSampleLoader::fromFile('"$node->getLabel()" will be rewritten.', __CLASS__); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes() : array | ||
{ | ||
return [Node\Expr\MethodCall::class]; | ||
} | ||
/** | ||
* @param Node\Expr\MethodCall $node | ||
*/ | ||
public function refactor(Node $node) : ?Node | ||
{ | ||
assert($node instanceof Node\Expr\MethodCall); | ||
|
||
if (!$this->isObjectType($node->var, new ObjectType(NodeLegacyStub::class))) { | ||
return null; | ||
} | ||
|
||
if (!$this->isName($node->name, 'getLabel')) { | ||
return null; | ||
} | ||
|
||
return $this->nodeFactory->createMethodCall( | ||
$this->nodeFactory->createPropertyFetch( | ||
'this', | ||
'nodeLabelGenerator' | ||
), | ||
'getLabel', | ||
[$node->var] | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/ContentRepository90/Rules/NodeLabelGeneratorRector/Fixture/some_class.php.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,29 @@ | ||
<?php | ||
|
||
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub; | ||
|
||
class SomeClass | ||
{ | ||
public function run(NodeLegacyStub $node) | ||
{ | ||
$label = $node->getLabel(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub; | ||
|
||
class SomeClass | ||
{ | ||
#[\Neos\Flow\Annotations\Inject] | ||
protected \Neos\Neos\Domain\NodeLabel\NodeLabelGeneratorInterface $nodeLabelGenerator; | ||
public function run(NodeLegacyStub $node) | ||
{ | ||
$label = $this->nodeLabelGenerator->getLabel($node); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
tests/ContentRepository90/Rules/NodeLabelGeneratorRector/NodeLabelGeneratorRectorTest.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\NodeLabelGeneratorRector; | ||
|
||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class NodeLabelGeneratorRectorTest 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'; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/ContentRepository90/Rules/NodeLabelGeneratorRector/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,17 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
|
||
use Neos\Neos\Domain\NodeLabel\NodeLabelGeneratorInterface; | ||
use Neos\Rector\ContentRepository90\Rules\NodeLabelGeneratorRector; | ||
use Neos\Rector\Generic\Rules\InjectServiceIfNeededRector; | ||
use Neos\Rector\Generic\ValueObject\AddInjection; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(NodeLabelGeneratorRector::class); | ||
|
||
$rectorConfig->ruleWithConfiguration(InjectServiceIfNeededRector::class, [ | ||
new AddInjection('nodeLabelGenerator', NodeLabelGeneratorInterface::class), | ||
]); | ||
}; |