fixup! Add Rector Rule to inject normalization methods into export classes

This commit is contained in:
2025-03-11 16:21:16 +01:00
parent 21ec96b75c
commit fcc61aa4c0
3 changed files with 153 additions and 22 deletions

View File

@@ -78,9 +78,13 @@ class ChillBundleAddNormalizationMethodsOnExportRector extends AbstractRector
foreach ($node->stmts as $k => $stmt) {
if ($stmt instanceof Node\Stmt\TraitUse) {
if (ExportDataNormalizerTrait::class === $stmt->traits[0]->toString()) {
$trait = $stmt->traits[0];
if (str_contains($trait->toString(), ExportDataNormalizerTrait::class)) {
$hasTraitHelper = true;
}
continue;
} elseif (!$stmt instanceof Node\Stmt\ClassMethod) {
continue;
}
@@ -97,6 +101,10 @@ class ChillBundleAddNormalizationMethodsOnExportRector extends AbstractRector
}
}
if ($hasDenormalizeMethod && $hasNormalizedMethod && $hasGetVersionMethod) {
return null;
}
$toAddBefore = [];
$toAdd = [];
@@ -105,17 +113,20 @@ class ChillBundleAddNormalizationMethodsOnExportRector extends AbstractRector
$propertiesOnForm = $this->getPropertiesOnForm($buildForm);
// if the trait is not present, we add it into the statements
if (!$hasTraitHelper && array_reduce(
$propertiesOnForm,
function (bool $carry, string $item): bool {
if ('entity' === $item || 'date' === $item) {
return true;
}
if (
!$hasTraitHelper
&& (!$hasNormalizedMethod || !$hasDenormalizeMethod)
&& array_reduce(
$propertiesOnForm,
function (bool $carry, string $item): bool {
if ('entity' === $item || 'date' === $item) {
return true;
}
return $carry;
},
false,
)) {
return $carry;
},
false,
)) {
$toAddBefore[] = new Node\Stmt\TraitUse([
new Node\Name('\\'.ExportDataNormalizerTrait::class),
]);