generated from ibexa/bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/lib/Rule/RemoveArgumentFromMethodCallRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
?> |
37 changes: 37 additions & 0 deletions
37
tests/lib/Rule/RemoveArgumentFromMethodCallRector/RemoveArgumentFromMethodCallRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/lib/Rule/RemoveArgumentFromMethodCallRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
] | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
?> |