mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
fixup! Add Rector Rule to inject normalization methods into export classes
This commit is contained in:
parent
21ec96b75c
commit
fcc61aa4c0
@ -1,13 +1,24 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" failOnRisky="true" failOnWarning="true" colors="true" cacheDirectory="var/cache/phpunit.rector.cache" requireCoverageMetadata="true" beStrictAboutCoverageMetadata="true">
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
<testsuites>
|
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
|
||||||
<testsuite name="default">
|
bootstrap="vendor/autoload.php"
|
||||||
<directory>utils/rector/tests</directory>
|
executionOrder="depends,defects"
|
||||||
</testsuite>
|
beStrictAboutOutputDuringTests="true"
|
||||||
</testsuites>
|
failOnRisky="true"
|
||||||
<source>
|
failOnWarning="true"
|
||||||
<include>
|
colors="true"
|
||||||
<directory suffix=".php">utils/rector/src</directory>
|
cacheDirectory="var/cache/phpunit.rector.cache"
|
||||||
</include>
|
requireCoverageMetadata="true"
|
||||||
</source>
|
beStrictAboutCoverageMetadata="true"
|
||||||
|
displayDetailsOnTestsThatTriggerWarnings="true">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="default">
|
||||||
|
<directory>utils/rector/tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
<source>
|
||||||
|
<include>
|
||||||
|
<directory suffix=".php">utils/rector/src</directory>
|
||||||
|
</include>
|
||||||
|
</source>
|
||||||
</phpunit>
|
</phpunit>
|
||||||
|
@ -78,9 +78,13 @@ class ChillBundleAddNormalizationMethodsOnExportRector extends AbstractRector
|
|||||||
|
|
||||||
foreach ($node->stmts as $k => $stmt) {
|
foreach ($node->stmts as $k => $stmt) {
|
||||||
if ($stmt instanceof Node\Stmt\TraitUse) {
|
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;
|
$hasTraitHelper = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
} elseif (!$stmt instanceof Node\Stmt\ClassMethod) {
|
} elseif (!$stmt instanceof Node\Stmt\ClassMethod) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -97,6 +101,10 @@ class ChillBundleAddNormalizationMethodsOnExportRector extends AbstractRector
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($hasDenormalizeMethod && $hasNormalizedMethod && $hasGetVersionMethod) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
$toAddBefore = [];
|
$toAddBefore = [];
|
||||||
$toAdd = [];
|
$toAdd = [];
|
||||||
|
|
||||||
@ -105,17 +113,20 @@ class ChillBundleAddNormalizationMethodsOnExportRector extends AbstractRector
|
|||||||
$propertiesOnForm = $this->getPropertiesOnForm($buildForm);
|
$propertiesOnForm = $this->getPropertiesOnForm($buildForm);
|
||||||
|
|
||||||
// if the trait is not present, we add it into the statements
|
// if the trait is not present, we add it into the statements
|
||||||
if (!$hasTraitHelper && array_reduce(
|
if (
|
||||||
$propertiesOnForm,
|
!$hasTraitHelper
|
||||||
function (bool $carry, string $item): bool {
|
&& (!$hasNormalizedMethod || !$hasDenormalizeMethod)
|
||||||
if ('entity' === $item || 'date' === $item) {
|
&& array_reduce(
|
||||||
return true;
|
$propertiesOnForm,
|
||||||
}
|
function (bool $carry, string $item): bool {
|
||||||
|
if ('entity' === $item || 'date' === $item) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return $carry;
|
return $carry;
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
)) {
|
)) {
|
||||||
$toAddBefore[] = new Node\Stmt\TraitUse([
|
$toAddBefore[] = new Node\Stmt\TraitUse([
|
||||||
new Node\Name('\\'.ExportDataNormalizerTrait::class),
|
new Node\Name('\\'.ExportDataNormalizerTrait::class),
|
||||||
]);
|
]);
|
||||||
|
@ -0,0 +1,109 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Chill\MainBundle\Export\FilterInterface;
|
||||||
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||||
|
use Chill\MainBundle\Export\ExportDataNormalizerTrait;
|
||||||
|
|
||||||
|
class MyFilter implements FilterInterface
|
||||||
|
{
|
||||||
|
use ExportDataNormalizerTrait;
|
||||||
|
|
||||||
|
public function getTitle()
|
||||||
|
{
|
||||||
|
return "some title";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
||||||
|
{
|
||||||
|
$builder->add('entity', EntityType::class, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFormDefaultData(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function describeAction($data, $format = 'string')
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRole(): ?string
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data, \Chill\MainBundle\Export\ExportGenerationContext $exportGenerationContext)
|
||||||
|
{
|
||||||
|
// nothing to add here
|
||||||
|
}
|
||||||
|
|
||||||
|
public function applyOn()
|
||||||
|
{
|
||||||
|
return ['something'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
-----
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Chill\MainBundle\Export\FilterInterface;
|
||||||
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||||
|
use Chill\MainBundle\Export\ExportDataNormalizerTrait;
|
||||||
|
|
||||||
|
class MyFilter implements FilterInterface
|
||||||
|
{
|
||||||
|
use ExportDataNormalizerTrait;
|
||||||
|
|
||||||
|
public function getTitle()
|
||||||
|
{
|
||||||
|
return "some title";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
||||||
|
{
|
||||||
|
$builder->add('entity', EntityType::class, []);
|
||||||
|
}
|
||||||
|
public function getNormalizationVersion(): int
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
public function normalizeFormData(array $formData): array
|
||||||
|
{
|
||||||
|
return ['entity' => $this->normalizeDoctrineEntity($formData['entity'])];
|
||||||
|
}
|
||||||
|
public function denormalizeFormData(array $formData, int $fromVersion): array
|
||||||
|
{
|
||||||
|
return ['entity' => $this->denormalizeDoctrineEntity($formData['entity'], $this->someRepository)];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFormDefaultData(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function describeAction($data, $format = 'string')
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRole(): ?string
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data, \Chill\MainBundle\Export\ExportGenerationContext $exportGenerationContext)
|
||||||
|
{
|
||||||
|
// nothing to add here
|
||||||
|
}
|
||||||
|
|
||||||
|
public function applyOn()
|
||||||
|
{
|
||||||
|
return ['something'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
Loading…
x
Reference in New Issue
Block a user