-
-
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 #52 from neos/prototype-comment
Add generic rule for adding comments to fusion prototype usage
- Loading branch information
Showing
10 changed files
with
135 additions
and
51 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
29 changes: 0 additions & 29 deletions
29
src/ContentRepository90/Rules/FusionPrimaryContentRector.php
This file was deleted.
Oops, something went wrong.
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 | ||
|
||
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; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neos\Rector\Generic\ValueObject; | ||
|
||
class FusionPrototypeNameAddComment | ||
{ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly string $comment, | ||
) { | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
tests/ContentRepository90/Rules/FusionPrimaryContentRector/config/configured_rule.php
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions
33
tests/Generic/Rules/FusionPrototypeNameAddComment/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,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 /> | ||
` | ||
} | ||
} |
8 changes: 5 additions & 3 deletions
8
...Rector/FusionPrimaryContentRectorTest.php → ...omment/FusionPrototypeNameCommentTest.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
25 changes: 25 additions & 0 deletions
25
tests/Generic/Rules/FusionPrototypeNameAddComment/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,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.'), | ||
]); | ||
}; |