Skip to content

Commit

Permalink
Add SwapMethodCallArgumentsRector (#3726)
Browse files Browse the repository at this point in the history
* ✨ add SwapMethodCallArgumentsRector

* Update rules/Arguments/Rector/MethodCall/SwapMethodCallArgumentsRector.php

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

* Clean up

---------

Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
3 people committed May 6, 2023
1 parent c6cb214 commit 575e799
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 2 deletions.
47 changes: 45 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# 420 Rules Overview
# 421 Rules Overview

<br>

## Categories

- [Arguments](#arguments) (5)
- [Arguments](#arguments) (6)

- [CodeQuality](#codequality) (79)

Expand Down Expand Up @@ -264,6 +264,49 @@ return static function (RectorConfig $rectorConfig): void {

<br>

### SwapMethodCallArgumentsRector

Reorder arguments in method calls

:wrench: **configure it!**

- class: [`Rector\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector`](../rules/Arguments/Rector/MethodCall/SwapMethodCallArgumentsRector.php)

```php
<?php

declare(strict_types=1);

use Rector\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector;
use Rector\Arguments\ValueObject\SwapMethodCallArguments;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(SwapMethodCallArgumentsRector::class, [
new SwapMethodCallArguments('Caller', 'call', [
2,
1,
0,
]),
]);
};
```


```diff
final class SomeClass
{
public function run(Caller $caller)
{
- return $caller->call('one', 'two', 'three');
+ return $caller->call('three', 'two', 'one');
}
}
```

<br>

## CodeQuality

### AbsolutizeRequireAndIncludePathRector
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

final class Fixture
{
public function run(MethodCaller $caller)
{
$caller->someCall($one, $two, $three);
}
}

?>
-----
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

final class Fixture
{
public function run(MethodCaller $caller)
{
$caller->someCall($three, $two, $one);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

final class Fixture
{
public function run(MethodCaller $caller)
{
return $caller->someCall($one, $two, $three);
}
}

?>
-----
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

final class Fixture
{
public function run(MethodCaller $caller)
{
return $caller->someCall($three, $two, $one);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

final class ParentCall extends MethodCaller
{
public function run()
{
parent::someCall($one, $two, $three);
}
}

?>
-----
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

final class ParentCall extends MethodCaller
{
public function run()
{
parent::someCall($three, $two, $one);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

final class SelfCall extends MethodCaller
{
public function run()
{
self::someCall($one, $two, $three);
}
}

?>
-----
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

final class SelfCall extends MethodCaller
{
public function run()
{
self::someCall($three, $two, $one);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

class StaticCall extends MethodCaller
{
public function run()
{
static::someCall($one, $two, $three);
}
}

?>
-----
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

class StaticCall extends MethodCaller
{
public function run()
{
static::someCall($three, $two, $one);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source;

class MethodCaller
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector;

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

final class SwapMethodCallArgumentsRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
foreach (self::yieldFilesFromDirectory(__DIR__ . '/Fixture') as $filePath) {
yield basename($filePath[0]) => $filePath;
}
}

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

declare(strict_types=1);

use Rector\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector;
use Rector\Arguments\ValueObject\SwapMethodCallArguments;
use Rector\Config\RectorConfig;
use Rector\Tests\Arguments\Rector\MethodCall\SwapMethodCallArgumentsRector\Source\MethodCaller;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(SwapMethodCallArgumentsRector::class, [
new SwapMethodCallArguments(MethodCaller::class, 'someCall', [2, 1, 0]),
]);
};
Loading

0 comments on commit 575e799

Please sign in to comment.