Skip to content

Commit

Permalink
IBX-8535: Added REST related rectors for 5.0 update (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ViniTou authored Sep 4, 2024
1 parent 0def678 commit 4e3b809
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/contracts/Sets/ibexa-50.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

namespace Ibexa\Contracts\Rector\Sets;

use Ibexa\Rector\Rule\RemoveArgumentFromMethodCallRector;
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\RenameClassConstFetch;
use Rector\Renaming\ValueObject\RenameProperty;

Expand Down Expand Up @@ -50,4 +53,30 @@
),
]
);

$rectorConfig->ruleWithConfiguration(
RenameMethodRector::class,
[
new MethodCallRename(
'Ibexa\Contracts\Rest\Output\Generator',
'generateMediaType',
'generateMediaTypeWithVendor'
),
new MethodCallRename(
'Ibexa\Rest\Output\FieldTypeSerializer',
'serializeFieldValue',
'serializeContentFieldValue'
),
]
);

$rectorConfig->ruleWithConfiguration(
RemoveArgumentFromMethodCallRector::class,
[
'class_name' => 'Ibexa\Rest\Output\FieldTypeSerializer',
'method_name' => 'serializeContentFieldValue',
'argument_index_to_remove' => 1,
'more_than' => 2,
]
);
};
108 changes: 108 additions & 0 deletions src/lib/Rule/RemoveArgumentFromMethodCallRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rector\Rule;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class RemoveArgumentFromMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
private int $argumentIndex;

private string $className;

private string $methodName;

private int $moreThan;

/**
* @throws \Exception
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove argument with given index from method call', [new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function someMethod($a, $b, $c = null) {}
}
$instance = new SomeClass();
$instance->someMethod('arg1', 'arg2', 'arg3');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function someMethod($a, $b, $c) {}
}
$instance = new SomeClass();
$instance->someMethod('arg1', 'arg2');
CODE_SAMPLE
,
['var_dump']
)]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param \PhpParser\Node\Expr\MethodCall $node
*/
public function refactor(Node $node): ?Node
{
$className = $this->resolveClassName($node);
if ($className !== $this->className) {
return null;
}
if ($this->nodeNameResolver->isName($node->name, $this->methodName)
&& count($node->args) > $this->moreThan
) {
if (isset($node->args[$this->argumentIndex])) {
unset($node->args[$this->argumentIndex]);
$node->args = array_values($node->args);
}
}

return $node;
}

private function resolveClassName(MethodCall $methodCall): ?string
{
$type = $this->nodeTypeResolver->getType($methodCall->var);

if ($type instanceof ObjectType) {
return $type->getClassName();
}

return null;
}

public function configure(array $configuration): void
{
$this->argumentIndex = (int)$configuration['argument_index_to_remove'];
$this->className = $configuration['class_name'];
$this->methodName = $configuration['method_name'];

//Used to protect already renamed methods.
$this->moreThan = (int)$configuration['more_than'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php /** @noinspection ALL */

namespace Ibexa\Rector\Tests\Rule\RemoveArgumentFromMethodCallRector\Fixture;

class SomeClass
{
public function foo($a, $b = null, $c = null): void
{
}
}

class SomeOtherClass
{
public function foo($a, $b = null, $c = null): void
{
}
}

$a = 'a';
$b = 'b';
$c = 'c';

$some = new SomeClass();
$some->foo($a, $b, $c);

$some->foo($a, $b);

$someOther = new SomeOtherClass();
$someOther->foo($a, $b, $c);
?>
-----
<?php /** @noinspection ALL */

namespace Ibexa\Rector\Tests\Rule\RemoveArgumentFromMethodCallRector\Fixture;

class SomeClass
{
public function foo($a, $b = null, $c = null): void
{
}
}

class SomeOtherClass
{
public function foo($a, $b = null, $c = null): void
{
}
}

$a = 'a';
$b = 'b';
$c = 'c';

$some = new SomeClass();
$some->foo($a, $c);

$some->foo($a, $b);

$someOther = new SomeOtherClass();
$someOther->foo($a, $b, $c);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rector\Tests\Rule\RemoveArgumentFromMethodCallRector;

use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @covers \Ibexa\Rector\Rule\Internal\RemoveLegacyClassAliasRector
*/
final class RemoveArgumentFromMethodCallRectorTest extends AbstractRectorTestCase
{
/**
* @throws \Rector\Exception\ShouldNotHappenException
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

use Ibexa\Rector\Rule\RemoveArgumentFromMethodCallRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(
RemoveArgumentFromMethodCallRector::class,
[
'class_name' => 'Ibexa\Rector\Tests\Rule\RemoveArgumentFromMethodCallRector\Fixture\SomeClass',
'method_name' => 'foo',
'argument_index_to_remove' => 1,
'more_than' => 2,
]
);
};
55 changes: 55 additions & 0 deletions tests/lib/Sets/Ibexa50/Fixture/rest_rename.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Ibexa\Rector\Tests\Sets\Ibexa50\Fixture;

use Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider;

class Foo {
public function mediaTypeGenerator(): void
{
$generator = new \Ibexa\Contracts\Rest\Output\Generator();

return $generator->generateMediaType('name', 'type');
}

public function fieldTypeSerializer(): void
{
$serializer = new \Ibexa\Rest\Output\FieldTypeSerializer();

$generator = new \Ibexa\Contracts\Rest\Output\Generator();
$contentType = new \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType();
$field = new \Ibexa\Contracts\Core\Repository\Values\Content\Field();

return $serializer->serializeFieldValue($generator, $contentType, $field);
}
}

?>
-----
<?php

namespace Ibexa\Rector\Tests\Sets\Ibexa50\Fixture;

use Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider;

class Foo {
public function mediaTypeGenerator(): void
{
$generator = new \Ibexa\Contracts\Rest\Output\Generator();

return $generator->generateMediaTypeWithVendor('name', 'type');
}

public function fieldTypeSerializer(): void
{
$serializer = new \Ibexa\Rest\Output\FieldTypeSerializer();

$generator = new \Ibexa\Contracts\Rest\Output\Generator();
$contentType = new \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType();
$field = new \Ibexa\Contracts\Core\Repository\Values\Content\Field();

return $serializer->serializeContentFieldValue($generator, $field);
}
}

?>

0 comments on commit 4e3b809

Please sign in to comment.