Make static some methods in AbstractAggregatorTest.php + rector rule to adapt existing

This commit is contained in:
Julien Fastré 2024-02-16 19:16:07 +01:00
parent 35d55cced4
commit 11f6b78b26
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
10 changed files with 193 additions and 21 deletions

View File

@ -35,19 +35,19 @@ abstract class AbstractAggregatorTest extends KernelTestCase
/** /**
* provide data for `testAliasDidNotDisappears`. * provide data for `testAliasDidNotDisappears`.
*/ */
public function dataProviderAliasDidNotDisappears() public static function dataProviderAliasDidNotDisappears()
{ {
$datas = $this->getFormData(); $datas = static::getFormData();
if (!\is_array($datas)) { if (!\is_array($datas)) {
$datas = iterator_to_array($datas); $datas = iterator_to_array($datas);
} }
foreach ($this->getQueryBuilders() as $qb) { foreach (static::getQueryBuilders() as $qb) {
if ([] === $datas) { if ([] === $datas) {
yield [clone $qb, []]; yield [clone $qb, []];
} else { } else {
foreach ($this->getFormData() as $data) { foreach (static::getFormData() as $data) {
yield [clone $qb, $data]; yield [clone $qb, $data];
} }
} }
@ -57,38 +57,38 @@ abstract class AbstractAggregatorTest extends KernelTestCase
/** /**
* provide data for `testAlterQuery`. * provide data for `testAlterQuery`.
*/ */
public function dataProviderAlterQuery() public static function dataProviderAlterQuery()
{ {
$datas = $this->getFormData(); $datas = static::getFormData();
if (!\is_array($datas)) { if (!\is_array($datas)) {
$datas = iterator_to_array($datas); $datas = iterator_to_array($datas);
} }
foreach ($this->getQueryBuilders() as $qb) { foreach (static::getQueryBuilders() as $qb) {
if ([] === $datas) { if ([] === $datas) {
yield [clone $qb, []]; yield [clone $qb, []];
} else { } else {
foreach ($this->getFormData() as $data) { foreach (static::getFormData() as $data) {
yield [clone $qb, $data]; yield [clone $qb, $data];
} }
} }
} }
} }
public function dataProviderQueryExecution(): iterable public static function dataProviderQueryExecution(): iterable
{ {
$datas = $this->getFormData(); $datas = static::getFormData();
if (!\is_array($datas)) { if (!\is_array($datas)) {
$datas = iterator_to_array($datas); $datas = iterator_to_array($datas);
} }
foreach ($this->getQueryBuilders() as $qb) { foreach (static::getQueryBuilders() as $qb) {
if ([] === $datas) { if ([] === $datas) {
yield [clone $qb, []]; yield [clone $qb, []];
} else { } else {
foreach ($this->getFormData() as $data) { foreach (static::getFormData() as $data) {
yield [clone $qb, $data]; yield [clone $qb, $data];
} }
} }
@ -98,9 +98,9 @@ abstract class AbstractAggregatorTest extends KernelTestCase
/** /**
* prepare data for `testGetQueryKeys`. * prepare data for `testGetQueryKeys`.
*/ */
public function dataProviderGetQueryKeys() public static function dataProviderGetQueryKeys()
{ {
$datas = $this->getFormData(); $datas = static::getFormData();
if (!\is_array($datas)) { if (!\is_array($datas)) {
$datas = iterator_to_array($datas); $datas = iterator_to_array($datas);
@ -114,15 +114,15 @@ abstract class AbstractAggregatorTest extends KernelTestCase
/** /**
* prepare date for method `testGetResultsAndLabels`. * prepare date for method `testGetResultsAndLabels`.
*/ */
public function dataProviderGetResultsAndLabels() public static function dataProviderGetResultsAndLabels()
{ {
$datas = $this->getFormData(); $datas = static::getFormData();
if (!\is_array($datas)) { if (!\is_array($datas)) {
$datas = iterator_to_array($datas); $datas = iterator_to_array($datas);
} }
foreach ($this->getQueryBuilders() as $qb) { foreach (static::getQueryBuilders() as $qb) {
if ([] === $datas) { if ([] === $datas) {
yield [clone $qb, []]; yield [clone $qb, []];
} else { } else {
@ -151,7 +151,7 @@ abstract class AbstractAggregatorTest extends KernelTestCase
* *
* @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values * @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values
*/ */
abstract public function getFormData(); abstract public static function getFormData();
/** /**
* get an array of query builders that the aggregator will use. * get an array of query builders that the aggregator will use.
@ -163,7 +163,7 @@ abstract class AbstractAggregatorTest extends KernelTestCase
* *
* @return QueryBuilder[] * @return QueryBuilder[]
*/ */
abstract public function getQueryBuilders(); abstract public static function getQueryBuilders();
/** /**
* Compare aliases array before and after that aggregator alter query. * Compare aliases array before and after that aggregator alter query.

View File

@ -0,0 +1,94 @@
<?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 Chill\Utils\Rector\Rector;
use Chill\MainBundle\Test\Export\AbstractAggregatorTest;
use Chill\Utils\Rector\Tests\ChillBundleMakeDataProviderStaticForAbstractFilterTest\ChillBundleMakeDataProviderStaticForAbstractFilterTestTest;
use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see ChillBundleMakeDataProviderStaticForAbstractFilterTestTest
*/
final class ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Make static each method which provide data on AbstractAggregatorTest',
['']
);
}
public function getNodeTypes(): array
{
return [Node\Stmt\Class_::class];
}
public function refactor(Node $node)
{
if (!$node instanceof Node\Stmt\Class_) {
return null;
}
if (null === $node->extends) {
return null;
}
if (AbstractAggregatorTest::class !== $node->extends->toString()) {
return null;
}
$new = [];
$matched = false;
foreach ($node->stmts as $k => $stmt) {
if (!$stmt instanceof Node\Stmt\ClassMethod) {
$new[] = $stmt;
continue;
}
if ('getFormData' === $stmt->name->name || 'getQueryBuilders' === $stmt->name->name) {
if ($stmt->isStatic()) {
$new[] = $stmt;
continue;
}
$matched = true;
$method = new Node\Stmt\ClassMethod($stmt->name->name);
$method->flags = Node\Stmt\Class_::MODIFIER_PUBLIC | Node\Stmt\Class_::MODIFIER_STATIC;
$method->returnType = match ($stmt->name->name) {
'getFormData' => new Node\Identifier('array'),
'getQueryBuilders' => new Node\Identifier('iterable'),
default => throw new \UnexpectedValueException('this name is not supported: '.$stmt->name->name)
};
foreach ($stmt->getStmts() as $s) {
$method->stmts[] = $s;
}
$new[] = $method;
} else {
$new[] = $stmt;
}
}
if (!$matched) {
return null;
}
$node->stmts = $new;
return $node;
}
}

View File

@ -41,8 +41,12 @@ final class ChillBundleMakeDataProviderStaticForAbstractFilterTestRector extends
return null; return null;
} }
if (null === $node->extends) {
return null;
}
if (AbstractFilterTest::class !== $node->extends->toString()) { if (AbstractFilterTest::class !== $node->extends->toString()) {
return; return null;
} }
$new = []; $new = [];

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 Chill\Utils\Rector\Tests\ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @internal
*
* @coversNothing
*/
class ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__.'/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__.'/config/config.php';
}
}

View File

@ -0,0 +1,5 @@
<?php
class MyException extends \RuntimeException
{
}

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(Chill\Utils\Rector\Rector\ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRector::class);
};

View File

@ -0,0 +1,5 @@
<?php
class MyException extends \RuntimeException
{
}

View File

@ -0,0 +1,5 @@
<?php
final readonly class MyClass {
}

View File

@ -10,5 +10,5 @@ declare(strict_types=1);
*/ */
return static function (Rector\Config\RectorConfig $rectorConfig): void { return static function (Rector\Config\RectorConfig $rectorConfig): void {
$rectorConfig->rule(Chill\Utils\Rector\Rector\ChillBundleMakeDataProviderStaticForAbstractFilterTesting::class); $rectorConfig->rule(Chill\Utils\Rector\Rector\ChillBundleMakeDataProviderStaticForAbstractFilterTestRector::class);
}; };