Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ Rector **instantly upgrades and instantly refactors PHP code of your application
composer require rector/rector --dev
```

**Do you have conflicts on `composer require`?**
**Do you have conflicts on `composer require` or on run?**

Install [prefixed version](https://github.com/rectorphp/rector-prefixed) with isolated dependencies.
- use [Docker image](#run-rector-in-docker) or
- install [prefixed version](https://github.com/rectorphp/rector-prefixed) with isolated dependencies (currently [looking for maintainer](https://github.com/rectorphp/prefixer/issues/1))

### Extra Autoloading

Expand Down Expand Up @@ -163,8 +164,10 @@ Just follow 3 rules:

We would be happy to merge your feature then.

## Run rector in docker
## Run Rector in Docker

With this command, you can process your project with rector from docker:

```bash
docker run -v $(pwd):/project rector/rector:latest
```
111 changes: 109 additions & 2 deletions docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
- [Php\Each](#phpeach)
- [Php\FuncCall](#phpfunccall)
- [Php\FunctionLike](#phpfunctionlike)
- [Php\If_](#phpif_)
- [Php\List_](#phplist_)
- [Php\MagicConstClass](#phpmagicconstclass)
- [Php\MethodCall](#phpmethodcall)
- [Php\Name](#phpname)
- [Php\Property](#phpproperty)
- [Php\StaticCall](#phpstaticcall)
Expand Down Expand Up @@ -1488,7 +1490,7 @@ Changes unquoted non-existing constants to strings

```diff
-var_dump(VAR);
+var("VAR");
+var_dump("VAR");
```

<br>
Expand Down Expand Up @@ -1605,7 +1607,7 @@ Null is no more allowed in get_class()

- class: `Rector\Php\Rector\FuncCall\TrailingCommaArgumentsRector`

Adds trailing commas to function and methods calls
Adds trailing commas to function and methods calls

```diff
calling(
Expand Down Expand Up @@ -1983,6 +1985,33 @@ Changes PHP 4 style constructor to __construct.

<br>

## Php\If_

### `IfToSpaceshipRector`

- class: `Rector\Php\Rector\If_\IfToSpaceshipRector`

Changes if/else to spaceship <=> where useful

```diff
class SomeClass
{
public function run()
{
usort($languages, function ($a, $b) {
- if ($a[0] === $b[0]) {
- return 0;
- }
-
- return ($a[0] < $b[0]) ? 1 : -1;
+ return $b[0] <=> $a[0];
});
}
}
```

<br>

## Php\List_

### `ListSplitStringRector`
Expand Down Expand Up @@ -2045,6 +2074,58 @@ Change __CLASS__ to self::class

<br>

## Php\MethodCall

### `ThisCallOnStaticMethodToStaticCallRector`

- class: `Rector\Php\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector`

Changes $this->call() to static method to static call

```diff
class SomeClass
{
public static function run()
{
- $this->eat();
+ self::eat();
}

public static function eat()
{
}
}
```

<br>

### `PreferThisOrSelfMethodCallRector`

- class: `Rector\Php\Rector\MethodCall\PreferThisOrSelfMethodCallRector`

Changes $this->... to self:: or vise versa for specific types

```yaml
services:
Rector\Php\Rector\MethodCall\PreferThisOrSelfMethodCallRector:
PHPUnit\TestCase: self
```


```diff
class SomeClass extends PHPUnit\TestCase
{
public function run()
{
- $this->assertThis();
+ self::assertThis();
}
}
```

<br>

## Php\Name

### `ReservedObjectRector`
Expand Down Expand Up @@ -2108,6 +2189,32 @@ Changes property `@var` annotations from annotation to type.

## Php\StaticCall

### `StaticCallOnNonStaticToInstanceCallRector`

- class: `Rector\Php\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector`

Changes static call to instance call, where not useful

```diff
class Something
{
public function doWork()
{
}
}

class Another
{
public function run()
{
- return Something::doWork();
+ return (new Something)->doWork();
}
}
```

<br>

### `ExportToReflectionFunctionRector`

- class: `Rector\Php\Rector\StaticCall\ExportToReflectionFunctionRector`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php declare(strict_types=1);

namespace Rector\Php\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use Rector\Exception\Rector\InvalidRectorConfigurationException;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
use function Safe\sprintf;

final class PreferThisOrSelfMethodCallRector extends AbstractRector
{
/**
* @var string
*/
private const PREFER_SELF = 'self';

/**
* @var string
*/
private const PREFER_THIS = 'this';

/**
* @var string[]
*/
private $typeToPreference = [];

/**
* @param string[] $typeToPreference
*/
public function __construct(array $typeToPreference = [])
{
$this->typeToPreference = $typeToPreference;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Changes $this->... to self:: or vise versa for specific types', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass extends PHPUnit\TestCase
{
public function run()
{
$this->assertThis();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass extends PHPUnit\TestCase
{
public function run()
{
self::assertThis();
}
}
CODE_SAMPLE
,
['PHPUnit\TestCase' => self::PREFER_SELF]
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class];
}

/**
* @param MethodCall|StaticCall $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->typeToPreference as $type => $preference) {
if (! $this->isType($node, $type)) {
continue;
}

$this->ensurePreferceIsValid($preference);

if ($preference === self::PREFER_SELF) {
return $this->processToSelf($node);
}

if ($preference === self::PREFER_THIS) {
return $this->processToThis($node);
}
}

return $node;
}

/**
* @param MethodCall|StaticCall $node
*/
private function processToSelf(Node $node): ?StaticCall
{
if ($node instanceof StaticCall) {
return null;
}

if (! $this->isName($node->var, 'this')) {
return null;
}

return new StaticCall(new Name('self'), $node->name);
}

/**
* @param MethodCall|StaticCall $node
*/
private function processToThis(Node $node): ?MethodCall
{
if ($node instanceof MethodCall) {
return null;
}

if (! $this->isName($node->class, 'self')) {
return null;
}

return new MethodCall(new Variable('this'), $node->name);
}

/**
* @param mixed $preference
*/
private function ensurePreferceIsValid($preference): void
{
$allowedPreferences = [self::PREFER_THIS, self::PREFER_SELF];
if (in_array($preference, $allowedPreferences, true)) {
return;
}

throw new InvalidRectorConfigurationException(sprintf(
'Preference configuration "%s" for "%s" is not valid. Use one of "%s"',
$preference,
self::class,
implode('", "', $allowedPreferences)
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Php\Tests\Rector\MethodCall\PreferThisOrSelfMethodCallRector\Fixture;

use Rector\Php\Tests\Rector\MethodCall\PreferThisOrSelfMethodCallRector\Source\AbstractTestCase;

class ToSelf extends AbstractTestCase
{
public function run()
{
$this->assertThis();
self::assertThis();
parent::assertThis();
}
}

?>
-----
<?php

namespace Rector\Php\Tests\Rector\MethodCall\PreferThisOrSelfMethodCallRector\Fixture;

use Rector\Php\Tests\Rector\MethodCall\PreferThisOrSelfMethodCallRector\Source\AbstractTestCase;

class ToSelf extends AbstractTestCase
{
public function run()
{
self::assertThis();
self::assertThis();
parent::assertThis();
}
}

?>
Loading