Skip to content

Commit efedb3c

Browse files
committed
Custom rector for 'whereNot'
1 parent 5f0c3da commit efedb3c

5 files changed

Lines changed: 131 additions & 0 deletions

File tree

config/sets/sulu/sulu-30.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Rector\Config\RectorConfig;
66
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
77
use Rector\Renaming\ValueObject\MethodCallRename;
8+
use Sulu\Rector\Rector\ListBuilderInterfaceRector;
89
use Sulu\Rector\Rector\RequestParameterTraitRector;
910

1011
return static function (RectorConfig $rectorConfig): void {
@@ -26,4 +27,5 @@
2627
);
2728

2829
$rectorConfig->rule(RequestParameterTraitRector::class);
30+
$rectorConfig->rule(ListBuilderInterfaceRector::class);
2931
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sulu\Rector\Rector;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\ClassConstFetch;
10+
use PhpParser\Node\Expr\MethodCall;
11+
use PhpParser\Node\Identifier;
12+
use PhpParser\Node\Name;
13+
use Rector\Contract\Rector\RectorInterface;
14+
use Rector\Rector\AbstractRector;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
use PHPStan\Type\ObjectType;
18+
19+
class ListBuilderInterfaceRector extends AbstractRector implements RectorInterface
20+
{
21+
public function getRuleDefinition(): RuleDefinition
22+
{
23+
return new RuleDefinition(
24+
'Fixes ListBuilderInterface',
25+
[
26+
new CodeSample(
27+
<<<CODE_SAMPLE
28+
use Sulu\Component\Rest\ListBuilder\ListBuilderInterface;
29+
class Test implements ListBuilderInterface {
30+
public function invoke() {
31+
\$this->whereNot(\$fieldDescriptor, 'value');
32+
}
33+
}
34+
CODE_SAMPLE,
35+
<<<CODE_SAMPLE
36+
use Sulu\Component\Rest\ListBuilder\ListBuilderInterface;
37+
class Test implements ListBuilderInterface {
38+
public function invoke() {
39+
\$this->where(\$fieldDescriptor, 'value', ListBuilderInterface::WHERE_COMPARATOR_UNEQUAL);
40+
}
41+
}
42+
CODE_SAMPLE
43+
),
44+
],
45+
);
46+
}
47+
48+
public function getNodeTypes(): array
49+
{
50+
return [MethodCall::class];
51+
}
52+
53+
public function refactor(Node $node)
54+
{
55+
/** @var MethodCall $node */
56+
$objectType = new ObjectType('Sulu\Component\Rest\ListBuilder\ListBuilderInterface');
57+
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $objectType)) {
58+
return null;
59+
}
60+
61+
$node->name = new Identifier('where');
62+
$node->args[] = new Arg(
63+
new ClassConstFetch(
64+
new Name(['ListBuilderInterface']),
65+
new Identifier('WHERE_COMPARATOR_UNEQUAL'),
66+
)
67+
);
68+
69+
return $node;
70+
}
71+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Sulu\Component\Rest\ListBuilder\ListBuilderInterface;
4+
5+
class Test implements ListBuilderInterface
6+
{
7+
public function invoke() {
8+
$this->whereNot($fieldDescriptor, 'value');
9+
}
10+
}
11+
-----
12+
<?php
13+
14+
use Sulu\Component\Rest\ListBuilder\ListBuilderInterface;
15+
16+
class Test implements ListBuilderInterface
17+
{
18+
public function invoke() {
19+
$this->where($fieldDescriptor, 'value', ListBuilderInterface::WHERE_COMPARATOR_UNEQUAL);
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sulu\Rector\Tests\Rector\ListBuilderInterfaceRector;
6+
7+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
8+
9+
class ListBuilderInterfaceRectorTest extends AbstractRectorTestCase
10+
{
11+
/** @dataProvider provideData */
12+
public function test(string $filePath): void
13+
{
14+
$this->doTestFile($filePath);
15+
}
16+
17+
public static function provideData(): \Iterator
18+
{
19+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
20+
}
21+
22+
public function provideConfigFilePath(): string
23+
{
24+
return __DIR__ . '/config/config.php';
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Sulu\Rector\Rector\ListBuilderInterfaceRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([
10+
ListBuilderInterfaceRector::class,
11+
]);

0 commit comments

Comments
 (0)