Skip to content

Commit

Permalink
Merge pull request #52 from neos/prototype-comment
Browse files Browse the repository at this point in the history
Add generic rule for adding comments to fusion prototype usage
  • Loading branch information
mhsdesign authored Apr 22, 2024
2 parents a73b26c + a58545f commit 4843c45
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 51 deletions.
9 changes: 6 additions & 3 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Neos\Rector\ContentRepository90\Rules\FusionNodeParentRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodePathRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeTypeNameRector;
use Neos\Rector\ContentRepository90\Rules\FusionPrimaryContentRector;
use Neos\Rector\ContentRepository90\Rules\NodeFactoryResetRector;
use Neos\Rector\ContentRepository90\Rules\NodeFindParentNodeRector;
use Neos\Rector\ContentRepository90\Rules\NodeGetChildNodesRector;
Expand All @@ -45,7 +44,6 @@
use Neos\Rector\ContentRepository90\Rules\NodeTypeGetAutoCreatedChildNodesRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeGetNameRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeGetTypeOfAutoCreatedChildNodeRector;
use Neos\Rector\ContentRepository90\Rules\NodeTypeHasAutoCreatedChildNodesRector;
use Neos\Rector\ContentRepository90\Rules\WorkspaceGetNameRector;
use Neos\Rector\ContentRepository90\Rules\WorkspaceRepositoryCountByNameRector;
use Neos\Rector\ContentRepository90\Rules\YamlDimensionConfigRector;
Expand All @@ -70,6 +68,7 @@
use Neos\Rector\ContentRepository90\Rules\FusionNodeHiddenBeforeDateTimeRector;
use Neos\Rector\Generic\Rules\FusionReplacePrototypeNameRector;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameReplacement;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameAddComment;

return static function (RectorConfig $rectorConfig): void {
// Register FusionFileProcessor. All Fusion Rectors will be auto-registered at this processor.
Expand Down Expand Up @@ -380,8 +379,12 @@

/**
* Neos.Neos:PrimaryContent
* Neos.Fusion:Attributes
*/
$rectorConfig->rule(FusionPrimaryContentRector::class);
$rectorConfig->ruleWithConfiguration(FusionPrototypeNameAddComment::class, [
new FusionPrototypeNameAddComment("Neos.Neos:PrimaryContent", 'TODO 9.0 migration: You need to refactor "Neos.Neos:PrimaryContent" to use "Neos.Neos:ContentCollection" instead.'),
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')
]);

/**
* SPECIAL rules
Expand Down
29 changes: 0 additions & 29 deletions src/ContentRepository90/Rules/FusionPrimaryContentRector.php

This file was deleted.

53 changes: 53 additions & 0 deletions src/Generic/Rules/FusionPrototypeNameAddCommentRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Neos\Rector\Generic\Rules;

use Neos\Rector\Core\FusionProcessing\FusionRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Neos\Rector\Utility\CodeSampleLoader;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Neos\Rector\Generic\ValueObject\FusionNodePropertyPathToWarningComment;
use Webmozart\Assert\Assert;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameReplacement;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameAddComment;

class FusionPrototypeNameAddCommentRector implements FusionRectorInterface, ConfigurableRectorInterface
{
/**
* @var FusionPrototypeNameAddComment[]
*/
private array $fusionPrototypeNameAddComments;

public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('Fusion: Add comment to file if prototype name matches at least once.', __CLASS__);
}

public function refactorFileContent(string $fileContent): string
{
foreach ($this->fusionPrototypeNameAddComments as $fusionPrototypeNameAddComment) {
$matches = [];
$pattern = '/(^|[=\s\(<\/])(' .$fusionPrototypeNameAddComment->name. ')([\s\{\)\/>]|$)/';
preg_match($pattern, $fileContent, $matches);

if (count($matches) > 0) {
$comments[] = "// " . $fusionPrototypeNameAddComment->comment;
}
}

if (count($comments) > 0){
$fileContent = implode("\n", $comments) . "\n" . $fileContent;
}

return $fileContent;
}

/**
* @param FusionPrototypeNameAddComment[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, FusionPrototypeNameAddComment::class);
$this->fusionPrototypeNameAddComments = $configuration;
}
}
13 changes: 13 additions & 0 deletions src/Generic/ValueObject/FusionPrototypeNameAddComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace Neos\Rector\Generic\ValueObject;

class FusionPrototypeNameAddComment
{
public function __construct(
public readonly string $name,
public readonly string $comment,
) {
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
prototype(Neos.Neos:SomethingOld) < prototype(Neos.Neos:Raw) {

foo = Neos.Neos:Foo

renderer = Neos.Neos:Raw {

old = Neos.Neos:SomethingOld

renderer = afx`
<Neos.Neos:SomethingOld foo=""></Neos.Neos:SomethingOld>
<Neos.Neos:Rawer />
<Neos.Neos:Raw />
`
}
}
-----
// Neos.Neos:Raw: Add this comment to top of file.
// Neos.Neos:Foo: Add this comment to top of file.
prototype(Neos.Neos:SomethingOld) < prototype(Neos.Neos:Raw) {

foo = Neos.Neos:Foo

renderer = Neos.Neos:Raw {

old = Neos.Neos:SomethingOld

renderer = afx`
<Neos.Neos:SomethingOld foo=""></Neos.Neos:SomethingOld>
<Neos.Neos:Rawer />
<Neos.Neos:Raw />
`
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php declare(strict_types=1);
<?php

namespace Neos\Rector\Tests\ContentRepository90\Rules\FusionPrimaryContentRector;
declare(strict_types=1);

namespace Neos\Rector\Tests\Generic\Rules\FusionPrototypeNameReplacement;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FusionPrimaryContentRectorTest extends AbstractRectorTestCase
final class FusionPrototypeNameCommentTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare (strict_types=1);

use Neos\Rector\Core\FusionProcessing\FusionFileProcessor;
use Rector\Config\RectorConfig;
use Neos\Rector\Generic\Rules\FusionPrototypeNameAddCommentRector;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameAddComment;

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->ruleWithConfiguration(FusionPrototypeNameAddCommentRector::class, [
new FusionPrototypeNameAddComment('Neos.Neos:Raw', 'Neos.Neos:Raw: Add this comment to top of file.'),
new FusionPrototypeNameAddComment('Neos.Neos:Foo', 'Neos.Neos:Foo: Add this comment to top of file.'),
new FusionPrototypeNameAddComment('Neos.Neos:Bar', 'Neos.Neos:Bar: Add this comment NOT to top of file.'),
new FusionPrototypeNameAddComment('Neos.Neos:PrimaryContent', 'TODO 9.0 migration: You need to refactor "Neos.Neos:PrimaryContent" to use "Neos.Neos:ContentCollection" instead.'),
]);
};

0 comments on commit 4843c45

Please sign in to comment.