Skip to content

Commit

Permalink
[DowngradePhp54] Add DowngradeThisInClosureRector (#1995)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 2, 2022
1 parent e077d7c commit 9f9b29c
Show file tree
Hide file tree
Showing 10 changed files with 437 additions and 3 deletions.
62 changes: 59 additions & 3 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 506 Rules Overview
# 508 Rules Overview

<br>

Expand All @@ -20,7 +20,7 @@

- [DowngradePhp53](#downgradephp53) (1)

- [DowngradePhp54](#downgradephp54) (6)
- [DowngradePhp54](#downgradephp54) (7)

- [DowngradePhp55](#downgradephp55) (4)

Expand All @@ -38,7 +38,7 @@

- [DowngradePhp80](#downgradephp80) (28)

- [DowngradePhp81](#downgradephp81) (8)
- [DowngradePhp81](#downgradephp81) (9)

- [EarlyReturn](#earlyreturn) (11)

Expand Down Expand Up @@ -3916,6 +3916,33 @@ Remove static from closure

<br>

### DowngradeThisInClosureRector

Downgrade `$this->` inside Closure to use assigned `$self` = `$this` before Closure

- class: [`Rector\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector`](../rules/DowngradePhp54/Rector/Closure/DowngradeThisInClosureRector.php)

```diff
class SomeClass
{
public $property = 'test';

public function run()
{
- $function = function () {
- echo $this->property;
+ $self = $this;
+ $function = function () use ($self) {
+ echo $self->property;
};

$function();
}
}
```

<br>

### ShortArrayToLongArrayRector

Replace short arrays by long arrays
Expand Down Expand Up @@ -5826,6 +5853,35 @@ Removes union type property type definition, adding `@var` annotations instead.

## DowngradePhp81

### DowngradeArrayIsListRector

Replace `array_is_list()` function

- class: [`Rector\DowngradePhp81\Rector\FuncCall\DowngradeArrayIsListRector`](../rules/DowngradePhp81/Rector/FuncCall/DowngradeArrayIsListRector.php)

```diff
-array_is_list([1 => 'apple', 'orange']);
+$arrayIsList = function (array $array) : bool {
+ if (function_exists('array_is_list')) {
+ return array_is_list($array);
+ }
+ if ($array === []) {
+ return true;
+ }
+ $current_key = 0;
+ foreach ($array as $key => $noop) {
+ if ($key !== $current_key) {
+ return false;
+ }
+ ++$current_key;
+ }
+ return true;
+};
+$arrayIsList([1 => 'apple', 'orange']);
```

<br>

### DowngradeArraySpreadStringKeyRector

Replace array spread with string key to array_merge function
Expand Down
2 changes: 2 additions & 0 deletions config/set/downgrade-php54.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp54\Rector\Array_\ShortArrayToLongArrayRector;
use Rector\DowngradePhp54\Rector\Closure\DowngradeStaticClosureRector;
use Rector\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector;
use Rector\DowngradePhp54\Rector\FuncCall\DowngradeIndirectCallByArrayRector;
use Rector\DowngradePhp54\Rector\FunctionLike\DowngradeCallableTypeDeclarationRector;
use Rector\DowngradePhp54\Rector\LNumber\DowngradeBinaryNotationRector;
Expand All @@ -23,4 +24,5 @@
$services->set(DowngradeCallableTypeDeclarationRector::class);
$services->set(DowngradeBinaryNotationRector::class);
$services->set(DowngradeInstanceMethodCallRector::class);
$services->set(DowngradeThisInClosureRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DowngradeThisInClosureRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->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,46 @@
<?php

namespace Rector\Tests\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector\Fixture;

class DeepClosure
{
public $property = 'test';

public function run()
{
$function = function () {
$f = function () {
echo $this->property;
};
$f();
};

$function();
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector\Fixture;

class DeepClosure
{
public $property = 'test';

public function run()
{
$self = $this;
$function = function () use($self) {
$f = function () use($self) {
echo $self->property;
};
$f();
};

$function();
}
}

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

namespace Rector\Tests\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector\Fixture;

class Fixture
{
public $property = 'test';

public function run()
{
$function = function () {
echo $this->property;
};

$function();
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector\Fixture;

class Fixture
{
public $property = 'test';

public function run()
{
$self = $this;
$function = function () use($self) {
echo $self->property;
};

$function();
}
}

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

namespace Rector\Tests\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector\Fixture;

class SelfVariableExists
{
public $property = 'test';

public function run()
{
$self = 'test';
$function = function () {
echo $this->property;
};

$function();
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector\Fixture;

class SelfVariableExists
{
public $property = 'test';

public function run()
{
$self = 'test';
$self2 = $this;
$function = function () use($self2) {
echo $self2->property;
};

$function();
}
}

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

namespace Rector\Tests\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector\Fixture;

class SkipInAnonymousClass
{
public $property = 'test';

public function run()
{
$function = function () {
new class {
public function test()
{
echo $this->property;
}
};
};

$function();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector\Fixture;

class SkipPrivateProperty
{
private $property = 'test';

public function run()
{
$function = function () {
echo $this->property;
};

$function();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\DowngradePhp54\Rector\Closure\DowngradeThisInClosureRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeThisInClosureRector::class);
};

0 comments on commit 9f9b29c

Please sign in to comment.