[export][rector] first rector rule to add new method to filters

This commit is contained in:
Julien Fastré 2023-06-04 01:11:38 +02:00
parent cb0a6bbd21
commit 02afcb30d4
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
12 changed files with 700 additions and 1 deletions

View File

@ -13,6 +13,7 @@ $finder = PhpCsFixer\Finder::create();
$finder
->in(__DIR__.'/src')
->in(__DIR__.'/utils')
->append([__FILE__])
->exclude(['docs/', 'tests/app'])
->notPath('tests/app')

View File

@ -67,6 +67,7 @@
"fakerphp/faker": "^1.13",
"jangregor/phpstan-prophecy": "^1.0",
"nelmio/alice": "^3.8",
"nikic/php-parser": "^4.15",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan": "^1.9",
@ -110,7 +111,9 @@
"psr-4": {
"App\\": "tests/app/src/",
"Chill\\DocGeneratorBundle\\Tests\\": "src/Bundle/ChillDocGeneratorBundle/tests",
"Chill\\WopiBundle\\Tests\\": "src/Bundle/ChillDocGeneratorBundle/tests"
"Chill\\WopiBundle\\Tests\\": "src/Bundle/ChillDocGeneratorBundle/tests",
"Utils\\Rector\\": "utils/rector/src",
"Utils\\Rector\\Tests\\": "utils/rector/tests"
}
},
"config": {

View File

@ -2,6 +2,7 @@ parameters:
level: 5
paths:
- src/
- utils/
tmpDir: .cache/
reportUnmatchedIgnoredErrors: false
excludePaths:

29
phpunit.rector.xml Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="tests/app/vendor/autoload.php"
cacheResultFile=".cache/phpunit/test-results-rector"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true"
colors="true"
>
<testsuites>
<testsuite name="default">
<directory>utils/rector/tests</directory>
</testsuite>
</testsuites>
<coverage cacheDirectory=".cache/phpunit/code-coverage-rector"
processUncoveredFiles="true">
<include>
<directory suffix=".php">utils/rector/src</directory>
</include>
</coverage>
</phpunit>

View File

@ -0,0 +1,170 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Utils\Rector\Rector;
use Chill\MainBundle\Export\FilterInterface;
use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use Rector\Symfony\NodeAnalyzer\ClassAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector extends AbstractRector
{
public function __construct(
private readonly ClassAnalyzer $classAnalyzer,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add a getFormDefault data method on exports, filters and aggregators, filled with default data',
[]
);
}
public function getNodeTypes(): array
{
return [Node\Stmt\Class_::class];
}
public function refactor(Node $node): ?Node
{
if (!$node instanceof Node\Stmt\Class_) {
return null;
}
if (!$this->classAnalyzer->hasImplements($node, FilterInterface::class)) {
return null;
}
$buildFormStmtIndex = null;
$hasGetFormDefaultDataMethod = false;
foreach ($node->stmts as $k => $stmt) {
if (!$stmt instanceof Node\Stmt\ClassMethod) {
continue;
}
if ('buildForm' === $stmt->name->name) {
$buildFormStmtIndex = $k;
}
if ('getFormDefaultData' === $stmt->name->name) {
$hasGetFormDefaultDataMethod = true;
}
}
if ($hasGetFormDefaultDataMethod || null === $buildFormStmtIndex) {
return null;
}
$stmtBefore = array_slice($node->stmts, 0, $buildFormStmtIndex, false);
$stmtAfter = array_slice($node->stmts, $buildFormStmtIndex + 1);
// lines to satisfay phpstan parser
if (!$node->stmts[$buildFormStmtIndex] instanceof Node\Stmt\ClassMethod) {
throw new \LogicException();
}
['build_form_method' => $buildFormMethod, 'empty_to_replace' => $emptyToReplace]
= $this->filterBuildFormMethod($node->stmts[$buildFormStmtIndex]);
$node->stmts = [
...$stmtBefore,
$buildFormMethod,
$this->makeGetFormDefaultData($node->stmts[$buildFormStmtIndex], $emptyToReplace),
...$stmtAfter,
];
return $node;
}
private function makeGetFormDefaultData(Node\Stmt\ClassMethod $buildFormMethod, array $emptyToReplace): Node\Stmt\ClassMethod
{
$method = new Node\Stmt\ClassMethod('getFormDefaultData');
$method->flags = Node\Stmt\Class_::MODIFIER_PUBLIC;
$method->returnType = new Node\Identifier('array');
$data = new Node\Expr\Array_([]);
foreach ($emptyToReplace as $key => $value) {
$item = new Node\Expr\ArrayItem($value, new Node\Scalar\String_($key));
$data->items[] = $item;
}
$method->stmts[] = new Node\Stmt\Return_($data);
return $method;
}
/**
* @param Node\Stmt\ClassMethod $buildFormMethod
* @return array{"build_form_method": Node\Stmt\ClassMethod, "empty_to_replace": array<string, mixed>}
*/
private function filterBuildFormMethod(Node\Stmt\ClassMethod $buildFormMethod): array
{
$builderName = $buildFormMethod->params[0]->var->name;
$newStmts = [];
$emptyDataToReplace = [];
foreach ($buildFormMethod->stmts as $stmt) {
if ($stmt instanceof Node\Stmt\Expression
// it must be a method call
&& $stmt->expr instanceof Node\Expr\MethodCall
// the method call must be "add"
&& $stmt->expr->name instanceof Node\Identifier
&& $stmt->expr->name->name === 'add'
// and the method call must apply on the builder (compare with builderName)
&& $stmt->expr->var instanceof Node\Expr\Variable
&& $stmt->expr->var->name === $builderName
// it must have a first argument, a string
// TODO what happens if a value, or a const ?
&& ($stmt->expr->args[0] ?? null) instanceof Node\Arg
&& $stmt->expr->args[0]->value instanceof Node\Scalar\String_
// and a third argument, an array
&& ($stmt->expr->args[2] ?? null) instanceof Node\Arg
&& $stmt->expr->args[2]->value instanceof Node\Expr\Array_
) {
// we parse on the 3rd argument, to find if there is an 'empty_data' key
$emptyDataIndex = null;
foreach ($stmt->expr->args[2]->value->items as $arrayItemIndex => $item) {
/* @phpstan-ignore-next-line */
if ($item->key->value === 'data') {
$k = $stmt->expr->args[0]->value->value;
$emptyDataToReplace[$k] = $item->value;
$emptyDataIndex = $arrayItemIndex;
}
}
if (null !== $emptyDataIndex) {
$stmt->expr->args[2]->value->items = array_values(
array_filter(
$stmt->expr->args[2]->value->items,
/* @phpstan-ignore-next-line */
fn (Node\Expr\ArrayItem $item) => $item->key->value !== 'data'
)
);
}
$newStmts[] = $stmt;
} else {
$newStmts[] = $stmt;
}
}
$buildFormMethod->stmts = $newStmts;
return ['build_form_method' => $buildFormMethod, "empty_to_replace" => $emptyDataToReplace];
}
}

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* @internal
* @coversNothing
*/
class ChillBundleAddFormDefaultDataOnExportFilterAggregatorRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
public function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__.'/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__.'/config/config.php';
}
}

View File

@ -0,0 +1,107 @@
<?php
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector\Fixture;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class MyClass implements FilterInterface
{
public function describeAction($data, $format = 'string')
{
// TODO: Implement describeAction() method.
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('foo', PickRollingDateType::class, [
'label' => 'Test thing',
'data' => new RollingDate(RollingDate::T_TODAY)
]);
$builder->add('baz', TextType::class, [
'label' => 'OrNiCar',
'data' => 'Castor'
]);
}
public function getTitle()
{
// TODO: Implement getTitle() method.
}
public function addRole(): ?string
{
// TODO: Implement addRole() method.
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: Implement alterQuery() method.
}
public function applyOn()
{
// TODO: Implement applyOn() method.
}
}
?>
-----
<?php
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector\Fixture;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class MyClass implements FilterInterface
{
public function describeAction($data, $format = 'string')
{
// TODO: Implement describeAction() method.
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('foo', PickRollingDateType::class, [
'label' => 'Test thing'
]);
$builder->add('baz', TextType::class, [
'label' => 'OrNiCar'
]);
}
public function getFormDefaultData(): array
{
return ['foo' => new RollingDate(RollingDate::T_TODAY), 'baz' => 'Castor'];
}
public function getTitle()
{
// TODO: Implement getTitle() method.
}
public function addRole(): ?string
{
// TODO: Implement addRole() method.
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: Implement alterQuery() method.
}
public function applyOn()
{
// TODO: Implement applyOn() method.
}
}
?>

View File

@ -0,0 +1,105 @@
<?php
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector\Fixture;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class MyClass implements FilterInterface
{
public function describeAction($data, $format = 'string')
{
// TODO: Implement describeAction() method.
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('foo', PickRollingDateType::class, [
'label' => 'Test thing',
]);
$builder->add('baz', TextType::class, [
'label' => 'OrNiCar',
]);
}
public function getTitle()
{
// TODO: Implement getTitle() method.
}
public function addRole(): ?string
{
// TODO: Implement addRole() method.
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: Implement alterQuery() method.
}
public function applyOn()
{
// TODO: Implement applyOn() method.
}
}
?>
-----
<?php
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector\Fixture;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class MyClass implements FilterInterface
{
public function describeAction($data, $format = 'string')
{
// TODO: Implement describeAction() method.
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('foo', PickRollingDateType::class, [
'label' => 'Test thing',
]);
$builder->add('baz', TextType::class, [
'label' => 'OrNiCar',
]);
}
public function getFormDefaultData(): array
{
return [];
}
public function getTitle()
{
// TODO: Implement getTitle() method.
}
public function addRole(): ?string
{
// TODO: Implement addRole() method.
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: Implement alterQuery() method.
}
public function applyOn()
{
// TODO: Implement applyOn() method.
}
}
?>

View File

@ -0,0 +1,96 @@
<?php
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector\Fixture;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
class MyClass implements FilterInterface
{
public function describeAction($data, $format = 'string')
{
// TODO: Implement describeAction() method.
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('test', PickRollingDateType::class, [
'label' => 'Test thing',
'data' => new RollingDate(RollingDate::T_TODAY)
]);
}
public function getTitle()
{
// TODO: Implement getTitle() method.
}
public function addRole(): ?string
{
// TODO: Implement addRole() method.
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: Implement alterQuery() method.
}
public function applyOn()
{
// TODO: Implement applyOn() method.
}
}
?>
-----
<?php
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector\Fixture;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
class MyClass implements FilterInterface
{
public function describeAction($data, $format = 'string')
{
// TODO: Implement describeAction() method.
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('test', PickRollingDateType::class, [
'label' => 'Test thing'
]);
}
public function getFormDefaultData(): array
{
return ['test' => new RollingDate(RollingDate::T_TODAY)];
}
public function getTitle()
{
// TODO: Implement getTitle() method.
}
public function addRole(): ?string
{
// TODO: Implement addRole() method.
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: Implement alterQuery() method.
}
public function applyOn()
{
// TODO: Implement applyOn() method.
}
}
?>

View File

@ -0,0 +1,87 @@
<?php
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector\Fixture;
use Chill\MainBundle\Export\FilterInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
class MyClass implements FilterInterface
{
public function describeAction($data, $format = 'string')
{
// TODO: Implement describeAction() method.
}
public function buildForm(FormBuilderInterface $builder)
{
// TODO: Implement buildForm() method.
}
public function getTitle()
{
// TODO: Implement getTitle() method.
}
public function addRole(): ?string
{
// TODO: Implement addRole() method.
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: Implement alterQuery() method.
}
public function applyOn()
{
// TODO: Implement applyOn() method.
}
}
?>
-----
<?php
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector\Fixture;
use Chill\MainBundle\Export\FilterInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
class MyClass implements FilterInterface
{
public function describeAction($data, $format = 'string')
{
// TODO: Implement describeAction() method.
}
public function buildForm(FormBuilderInterface $builder)
{
// TODO: Implement buildForm() method.
}
public function getFormDefaultData(): array
{
return [];
}
public function getTitle()
{
// TODO: Implement getTitle() method.
}
public function addRole(): ?string
{
// TODO: Implement addRole() method.
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: Implement alterQuery() method.
}
public function applyOn()
{
// TODO: Implement applyOn() method.
}
}
?>

View File

@ -0,0 +1,46 @@
<?php
namespace Utils\Rector\Tests\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector\Fixture;
use Chill\MainBundle\Export\FilterInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
class MyClass implements FilterInterface
{
public function describeAction($data, $format = 'string')
{
// TODO: Implement describeAction() method.
}
public function buildForm(FormBuilderInterface $builder)
{
// TODO: Implement buildForm() method.
}
public function getFormDefaultData(): array
{
return [];
}
public function getTitle()
{
// TODO: Implement getTitle() method.
}
public function addRole(): ?string
{
// TODO: Implement addRole() method.
}
public function alterQuery(QueryBuilder $qb, $data)
{
// TODO: Implement alterQuery() method.
}
public function applyOn()
{
// TODO: Implement applyOn() method.
}
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
return static function (\Rector\Config\RectorConfig $rectorConfig): void {
$rectorConfig->rule(\Utils\Rector\Rector\ChillBundleAddFormDefaultDataOnExportFilterAggregatorRector::class);
};