2022-04-10 00:31:45 +00:00
# 510 Rules Overview
2020-11-27 12:00:56 +00:00
< br >
2020-02-13 13:42:40 +00:00
2020-11-25 21:34:34 +00:00
## Categories
2020-02-13 13:42:40 +00:00
2022-03-05 11:39:32 +00:00
- [Arguments ](#arguments ) (5)
2021-02-20 23:21:19 +00:00
2022-03-15 17:33:16 +00:00
- [CodeQuality ](#codequality ) (72)
2020-02-13 13:42:40 +00:00
2022-01-26 18:39:23 +00:00
- [CodingStyle ](#codingstyle ) (35)
2020-02-13 13:42:40 +00:00
2021-11-21 00:34:01 +00:00
- [Compatibility ](#compatibility ) (1)
2021-05-16 08:19:51 +00:00
- [Composer ](#composer ) (6)
2021-01-16 12:23:37 +00:00
2022-04-01 07:19:38 +00:00
- [DeadCode ](#deadcode ) (49)
2021-01-01 18:59:23 +00:00
2021-11-25 18:41:12 +00:00
- [DependencyInjection ](#dependencyinjection ) (2)
2021-01-01 18:59:23 +00:00
2021-05-09 00:15:50 +00:00
- [DowngradePhp53 ](#downgradephp53 ) (1)
2021-04-18 13:01:56 +00:00
2022-04-02 15:46:27 +00:00
- [DowngradePhp54 ](#downgradephp54 ) (7)
2021-10-24 00:34:27 +00:00
2021-11-25 12:53:25 +00:00
- [DowngradePhp55 ](#downgradephp55 ) (4)
2021-11-08 19:40:36 +00:00
2022-01-02 00:31:07 +00:00
- [DowngradePhp56 ](#downgradephp56 ) (5)
2021-11-14 00:36:15 +00:00
2022-01-26 10:10:51 +00:00
- [DowngradePhp70 ](#downgradephp70 ) (19)
2020-11-28 18:06:05 +00:00
2022-02-13 00:29:38 +00:00
- [DowngradePhp71 ](#downgradephp71 ) (11)
2019-09-15 18:28:10 +00:00
2022-02-06 08:25:55 +00:00
- [DowngradePhp72 ](#downgradephp72 ) (6)
2019-09-15 18:28:10 +00:00
2022-02-13 00:29:38 +00:00
- [DowngradePhp73 ](#downgradephp73 ) (7)
2019-09-15 18:28:10 +00:00
2021-12-06 19:42:49 +00:00
- [DowngradePhp74 ](#downgradephp74 ) (12)
2019-09-15 18:28:10 +00:00
2022-02-02 21:14:38 +00:00
- [DowngradePhp80 ](#downgradephp80 ) (28)
2020-12-22 15:48:25 +00:00
2022-04-02 15:46:27 +00:00
- [DowngradePhp81 ](#downgradephp81 ) (9)
2021-07-23 16:35:16 +00:00
2021-02-28 07:52:22 +00:00
- [EarlyReturn ](#earlyreturn ) (11)
2019-09-15 18:28:10 +00:00
2020-11-25 21:34:34 +00:00
- [MysqlToMysqli ](#mysqltomysqli ) (4)
2020-07-24 11:46:57 +00:00
2021-08-01 00:25:55 +00:00
- [Naming ](#naming ) (6)
2020-09-24 18:54:39 +00:00
2020-11-25 21:34:34 +00:00
- [PSR4 ](#psr4 ) (2)
- [Php52 ](#php52 ) (2)
2021-10-28 22:22:07 +00:00
- [Php53 ](#php53 ) (3)
2020-11-25 21:34:34 +00:00
- [Php54 ](#php54 ) (2)
2022-03-28 09:17:08 +00:00
- [Php55 ](#php55 ) (5)
2020-11-25 21:34:34 +00:00
- [Php56 ](#php56 ) (2)
- [Php70 ](#php70 ) (19)
- [Php71 ](#php71 ) (9)
2020-12-22 15:48:25 +00:00
- [Php72 ](#php72 ) (10)
2020-11-25 21:34:34 +00:00
2021-02-07 10:11:42 +00:00
- [Php73 ](#php73 ) (9)
2020-11-25 21:34:34 +00:00
2022-03-28 09:17:08 +00:00
- [Php74 ](#php74 ) (14)
2020-11-25 21:34:34 +00:00
2022-02-15 22:46:36 +00:00
- [Php80 ](#php80 ) (17)
2020-11-25 21:34:34 +00:00
2022-01-16 01:52:41 +00:00
- [Php81 ](#php81 ) (9)
2021-05-16 08:19:51 +00:00
2021-03-03 21:28:27 +00:00
- [PostRector ](#postrector ) (7)
2021-07-22 18:40:24 +00:00
- [Privatization ](#privatization ) (10)
2020-11-25 21:34:34 +00:00
2022-04-06 12:39:03 +00:00
- [Removing ](#removing ) (7)
2021-01-30 20:30:37 +00:00
2022-02-15 22:46:36 +00:00
- [RemovingStatic ](#removingstatic ) (1)
2020-11-25 21:34:34 +00:00
2021-01-28 18:26:56 +00:00
- [Renaming ](#renaming ) (11)
2020-11-25 21:34:34 +00:00
2021-10-07 17:46:41 +00:00
- [Restoration ](#restoration ) (5)
2020-11-25 21:34:34 +00:00
2021-10-05 08:51:30 +00:00
- [Strict ](#strict ) (5)
2022-03-15 16:04:34 +00:00
- [Transform ](#transform ) (35)
2020-11-25 21:34:34 +00:00
2022-03-13 00:30:25 +00:00
- [TypeDeclaration ](#typedeclaration ) (23)
2020-09-11 09:21:48 +00:00
2022-04-10 00:31:45 +00:00
- [Visibility ](#visibility ) (3)
2020-09-24 18:54:39 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-11 09:21:48 +00:00
2021-02-20 23:21:19 +00:00
## Arguments
### ArgumentAdderRector
This Rector adds new default arguments in calls of defined methods and class types.
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector` ](../rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php )
2021-02-20 23:21:19 +00:00
```php
2021-09-19 11:08:13 +00:00
use PHPStan\Type\ObjectType;
2021-02-20 23:21:19 +00:00
use Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector;
use Rector\Arguments\ValueObject\ArgumentAdder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-20 23:21:19 +00:00
$services->set(ArgumentAdderRector::class)
2022-02-20 00:35:49 +00:00
->configure(
[new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', true, new ObjectType('SomeType'))]
);
2021-02-20 23:21:19 +00:00
};
```
↓
```diff
$someObject = new SomeExampleClass;
-$someObject->someMethod();
+$someObject->someMethod(true);
class MyCustomClass extends SomeExampleClass
{
- public function someMethod()
+ public function someMethod($value = true)
{
}
}
```
< br >
2021-06-13 10:00:46 +00:00
### FunctionArgumentDefaultValueReplacerRector
2021-02-20 23:21:19 +00:00
2021-06-13 10:00:46 +00:00
Streamline the operator arguments of version_compare function
2021-02-20 23:21:19 +00:00
:wrench: **configure it!**
2021-06-13 10:00:46 +00:00
- class: [`Rector\Arguments\Rector\FuncCall\FunctionArgumentDefaultValueReplacerRector` ](../rules/Arguments/Rector/FuncCall/FunctionArgumentDefaultValueReplacerRector.php )
2021-02-20 23:21:19 +00:00
```php
2021-06-13 10:00:46 +00:00
use Rector\Arguments\Rector\FuncCall\FunctionArgumentDefaultValueReplacerRector;
use Rector\Arguments\ValueObject\ReplaceFuncCallArgumentDefaultValue;
2021-02-20 23:21:19 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-06-13 10:00:46 +00:00
$services->set(FunctionArgumentDefaultValueReplacerRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ReplaceFuncCallArgumentDefaultValue('version_compare', 2, 'gte', 'ge')]);
2021-02-20 23:21:19 +00:00
};
```
↓
```diff
2021-06-13 10:00:46 +00:00
-version_compare(PHP_VERSION, '5.6', 'gte');
+version_compare(PHP_VERSION, '5.6', 'ge');
2021-02-20 23:21:19 +00:00
```
< br >
2022-03-05 11:39:32 +00:00
### RemoveMethodCallParamRector
Remove parameter of method call
:wrench: **configure it!**
- class: [`Rector\Arguments\Rector\MethodCall\RemoveMethodCallParamRector` ](../rules/Arguments/Rector/MethodCall/RemoveMethodCallParamRector.php )
```php
use Rector\Arguments\Rector\MethodCall\RemoveMethodCallParamRector;
use Rector\Arguments\ValueObject\RemoveMethodCallParam;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveMethodCallParamRector::class)
->configure([new RemoveMethodCallParam('Caller', 'process', 1)]);
};
```
↓
```diff
final class SomeClass
{
public function run(Caller $caller)
{
- $caller->process(1, 2);
+ $caller->process(1);
}
}
```
< br >
2021-06-13 10:00:46 +00:00
### ReplaceArgumentDefaultValueRector
2021-05-23 09:33:26 +00:00
2021-06-13 10:00:46 +00:00
Replaces defined map of arguments in defined methods and their calls.
2021-05-23 09:33:26 +00:00
:wrench: **configure it!**
2021-06-13 10:00:46 +00:00
- class: [`Rector\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector` ](../rules/Arguments/Rector/ClassMethod/ReplaceArgumentDefaultValueRector.php )
2021-05-23 09:33:26 +00:00
```php
2021-06-13 10:00:46 +00:00
use Rector\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector;
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
2021-05-23 09:33:26 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-06-13 10:00:46 +00:00
$services->set(ReplaceArgumentDefaultValueRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ReplaceArgumentDefaultValue('SomeClass', 'someMethod', 0, 'SomeClass::OLD_CONSTANT', false)]);
2021-05-23 09:33:26 +00:00
};
```
↓
```diff
2021-06-13 10:00:46 +00:00
$someObject = new SomeClass;
-$someObject->someMethod(SomeClass::OLD_CONSTANT);
2021-08-01 00:25:55 +00:00
+$someObject->someMethod(false);
2021-05-23 09:33:26 +00:00
```
< br >
2021-02-20 23:21:19 +00:00
### SwapFuncCallArgumentsRector
Swap arguments in function calls
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Arguments\Rector\FuncCall\SwapFuncCallArgumentsRector` ](../rules/Arguments/Rector/FuncCall/SwapFuncCallArgumentsRector.php )
2021-02-20 23:21:19 +00:00
```php
use Rector\Arguments\Rector\FuncCall\SwapFuncCallArgumentsRector;
use Rector\Arguments\ValueObject\SwapFuncCallArguments;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-20 23:21:19 +00:00
$services->set(SwapFuncCallArgumentsRector::class)
2021-12-06 21:12:03 +00:00
->configure([new SwapFuncCallArguments('some_function', [1, 0])]);
2021-02-20 23:21:19 +00:00
};
```
↓
```diff
final class SomeClass
{
public function run($one, $two)
{
- return some_function($one, $two);
+ return some_function($two, $one);
}
}
```
< br >
2020-11-25 21:34:34 +00:00
## CodeQuality
### AbsolutizeRequireAndIncludePathRector
2022-01-16 09:58:52 +00:00
include/require to absolute path. This Rector might introduce backwards incompatible code, when the include/require being changed depends on the current working directory.
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector` ](../rules/CodeQuality/Rector/Include_/AbsolutizeRequireAndIncludePathRector.php )
2020-11-25 21:34:34 +00:00
```diff
class SomeClass
2020-11-16 17:50:38 +00:00
{
public function run()
{
2020-11-25 21:34:34 +00:00
- require 'autoload.php';
+ require __DIR__ . '/autoload.php';
require $variable;
2020-01-03 18:20:13 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-03 18:20:13 +00:00
2020-11-25 21:34:34 +00:00
### AddPregQuoteDelimiterRector
2019-11-06 23:52:19 +00:00
2021-04-10 18:18:49 +00:00
Add preg_quote delimiter when missing
2019-11-06 23:52:19 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector` ](../rules/CodeQuality/Rector/FuncCall/AddPregQuoteDelimiterRector.php )
2019-11-06 23:52:19 +00:00
2020-11-25 21:34:34 +00:00
```diff
-'#' . preg_quote('name') . '#';
+'#' . preg_quote('name', '#') . '#';
```
2019-11-06 23:52:19 +00:00
2020-11-25 21:34:34 +00:00
< br >
2019-11-06 23:52:19 +00:00
2020-11-25 21:34:34 +00:00
### AndAssignsToSeparateLinesRector
2019-03-09 13:24:30 +00:00
2020-11-25 21:34:34 +00:00
Split 2 assigns ands to separate line
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\LogicalAnd\AndAssignsToSeparateLinesRector` ](../rules/CodeQuality/Rector/LogicalAnd/AndAssignsToSeparateLinesRector.php )
2019-03-09 13:24:30 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-03-09 13:24:30 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2019-03-09 13:24:30 +00:00
{
2020-11-25 21:34:34 +00:00
$tokens = [];
- $token = 4 and $tokens[] = $token;
+ $token = 4;
+ $tokens[] = $token;
2019-03-09 13:24:30 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-03-09 13:24:30 +00:00
2020-11-25 21:34:34 +00:00
### ArrayKeyExistsTernaryThenValueToCoalescingRector
2020-01-03 18:20:13 +00:00
2022-01-16 09:58:52 +00:00
Change `array_key_exists()` ternary to coalescing
2020-01-03 18:20:13 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector` ](../rules/CodeQuality/Rector/Ternary/ArrayKeyExistsTernaryThenValueToCoalescingRector.php )
2020-01-03 18:20:13 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-01-03 18:20:13 +00:00
{
2020-11-25 21:34:34 +00:00
public function run($values, $keyToMatch)
2020-01-03 18:20:13 +00:00
{
2020-11-25 21:34:34 +00:00
- $result = array_key_exists($keyToMatch, $values) ? $values[$keyToMatch] : null;
+ $result = $values[$keyToMatch] ?? null;
2020-01-03 18:20:13 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-03 18:20:13 +00:00
2020-11-25 21:34:34 +00:00
### ArrayKeysAndInArrayToArrayKeyExistsRector
2020-03-27 22:49:22 +00:00
2020-11-25 21:34:34 +00:00
Replace `array_keys()` and `in_array()` to `array_key_exists()`
2020-03-27 22:49:22 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector` ](../rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php )
2020-03-27 22:49:22 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-03-27 22:49:22 +00:00
{
2020-11-25 21:34:34 +00:00
public function run($packageName, $values)
2020-03-27 22:49:22 +00:00
{
2020-11-25 21:34:34 +00:00
- $keys = array_keys($values);
- return in_array($packageName, $keys, true);
+ return array_key_exists($packageName, $values);
2020-03-27 22:49:22 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-03-27 22:49:22 +00:00
2020-11-25 21:34:34 +00:00
### ArrayMergeOfNonArraysToSimpleArrayRector
2019-11-06 23:52:19 +00:00
2021-04-10 18:18:49 +00:00
Change array_merge of non arrays to array directly
2019-11-06 23:52:19 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector` ](../rules/CodeQuality/Rector/FuncCall/ArrayMergeOfNonArraysToSimpleArrayRector.php )
2019-11-06 23:52:19 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function go()
{
$value = 5;
$value2 = 10;
2019-11-06 23:52:19 +00:00
2020-11-25 21:34:34 +00:00
- return array_merge([$value], [$value2]);
+ return [$value, $value2];
}
}
2019-11-06 23:52:19 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-11-06 23:52:19 +00:00
2020-11-25 21:34:34 +00:00
### ArrayThisCallToThisMethodCallRector
2020-06-10 18:07:43 +00:00
2020-11-25 21:34:34 +00:00
Change `[$this, someMethod]` without any args to `$this->someMethod()`
2020-06-10 18:07:43 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Array_\ArrayThisCallToThisMethodCallRector` ](../rules/CodeQuality/Rector/Array_/ArrayThisCallToThisMethodCallRector.php )
2020-06-10 18:07:43 +00:00
```diff
class SomeClass
{
public function run()
{
2020-11-25 21:34:34 +00:00
- $values = [$this, 'giveMeMore'];
+ $values = $this->giveMeMore();
}
public function giveMeMore()
{
return 'more';
2020-06-10 18:07:43 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-06-10 18:07:43 +00:00
2020-11-25 21:34:34 +00:00
### BooleanNotIdenticalToNotIdenticalRector
2018-12-22 11:22:28 +00:00
2020-11-25 21:34:34 +00:00
Negated identical boolean compare to not identical compare (does not apply to non-bool values)
2018-12-22 11:22:28 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector` ](../rules/CodeQuality/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php )
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
```diff
class SomeClass
{
public function run()
{
$a = true;
$b = false;
2018-12-22 11:22:28 +00:00
2020-11-25 21:34:34 +00:00
- var_dump(! $a === $b); // true
- var_dump(! ($a === $b)); // true
+ var_dump($a !== $b); // true
+ var_dump($a !== $b); // true
var_dump($a !== $b); // true
}
}
```
2018-12-22 11:22:28 +00:00
2020-11-25 21:34:34 +00:00
< br >
2018-12-31 11:50:32 +00:00
2021-05-09 00:15:50 +00:00
### CallUserFuncWithArrowFunctionToInlineRector
Refactor `call_user_func()` with arrow function to direct call
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\CallUserFuncWithArrowFunctionToInlineRector` ](../rules/CodeQuality/Rector/FuncCall/CallUserFuncWithArrowFunctionToInlineRector.php )
2021-05-09 00:15:50 +00:00
```diff
final class SomeClass
{
public function run()
{
- $result = \call_user_func(fn () => 100);
+ $result = 100;
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### CallableThisArrayToAnonymousFunctionRector
2018-12-31 11:50:32 +00:00
2020-11-25 21:34:34 +00:00
Convert [$this, "method"] to proper anonymous function
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector` ](../rules/CodeQuality/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php )
2018-12-31 11:50:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run()
{
$values = [1, 5, 3];
- usort($values, [$this, 'compareSize']);
+ usort($values, function ($first, $second) {
+ return $this->compareSize($first, $second);
+ });
return $values;
}
private function compareSize($first, $second)
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
return $first < => $second;
2019-06-02 07:45:37 +00:00
}
}
2018-12-31 11:50:32 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-31 11:50:32 +00:00
2020-11-25 21:34:34 +00:00
### ChangeArrayPushToArrayAssignRector
2020-01-03 23:31:17 +00:00
2020-11-25 21:34:34 +00:00
Change `array_push()` to direct variable assign
2020-01-03 23:31:17 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector` ](../rules/CodeQuality/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php )
2020-01-03 23:31:17 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-01-03 23:31:17 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2020-01-03 23:31:17 +00:00
{
2020-11-25 21:34:34 +00:00
$items = [];
- array_push($items, $item);
+ $items[] = $item;
2020-01-03 23:31:17 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-03 23:31:17 +00:00
2020-11-25 21:34:34 +00:00
### CombineIfRector
2020-01-15 20:40:44 +00:00
2020-11-25 21:34:34 +00:00
Merges nested if statements
2020-01-15 20:40:44 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\If_\CombineIfRector` ](../rules/CodeQuality/Rector/If_/CombineIfRector.php )
2020-01-15 20:40:44 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run()
{
- if ($cond1) {
- if ($cond2) {
- return 'foo';
- }
+ if ($cond1 & & $cond2) {
+ return 'foo';
}
}
}
2020-01-15 20:40:44 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-15 20:40:44 +00:00
2020-11-25 21:34:34 +00:00
### CombinedAssignRector
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
Simplify `$value` = `$value` + 5; assignments to shorter ones
2018-10-21 22:26:45 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Assign\CombinedAssignRector` ](../rules/CodeQuality/Rector/Assign/CombinedAssignRector.php )
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
```diff
-$value = $value + 5;
+$value += 5;
```
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
< br >
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
### CommonNotEqualRector
2020-07-27 08:16:16 +00:00
2020-11-25 21:34:34 +00:00
Use common != instead of less known < > with same meaning
2020-07-27 08:16:16 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector` ](../rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php )
2020-07-27 08:16:16 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeClass
2020-07-27 08:16:16 +00:00
{
2020-11-25 21:34:34 +00:00
public function run($one, $two)
{
- return $one < > $two;
+ return $one != $two;
}
2020-07-27 08:16:16 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-07-27 08:16:16 +00:00
2020-11-25 21:34:34 +00:00
### CompactToVariablesRector
2019-05-01 23:56:58 +00:00
2020-11-25 21:34:34 +00:00
Change `compact()` call to own array
2019-05-01 23:56:58 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector` ](../rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php )
2019-05-29 13:40:20 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
$checkout = 'one';
$form = 'two';
- return compact('checkout', 'form');
+ return ['checkout' => $checkout, 'form' => $form];
2019-06-02 07:45:37 +00:00
}
}
2019-05-29 13:40:20 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-29 13:40:20 +00:00
2020-11-25 21:34:34 +00:00
### CompleteDynamicPropertiesRector
2019-05-29 13:40:20 +00:00
2020-11-25 21:34:34 +00:00
Add missing dynamic properties
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector` ](../rules/CodeQuality/Rector/Class_/CompleteDynamicPropertiesRector.php )
2019-06-02 07:45:37 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
+ /**
+ * @var int
+ */
+ public $value;
2021-04-10 18:18:49 +00:00
+
2020-11-25 21:34:34 +00:00
public function set()
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
$this->value = 5;
2019-06-02 07:45:37 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
### ConsecutiveNullCompareReturnsToNullCoalesceQueueRector
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
Change multiple null compares to ?? queue
2019-06-02 07:45:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector` ](../rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php )
2019-05-01 23:56:58 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run()
2019-05-01 23:56:58 +00:00
{
2020-11-25 21:34:34 +00:00
- if (null !== $this->orderItem) {
- return $this->orderItem;
- }
-
- if (null !== $this->orderItemUnit) {
- return $this->orderItemUnit;
- }
-
- return null;
+ return $this->orderItem ?? $this->orderItemUnit;
2019-05-01 23:56:58 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-12-05 22:33:27 +00:00
### DateTimeToDateTimeInterfaceRector
Changes DateTime type-hint to DateTimeInterface
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector` ](../rules/CodeQuality/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php )
2020-12-05 22:33:27 +00:00
```diff
class SomeClass {
- public function methodWithDateTime(\DateTime $dateTime)
+ /**
+ * @param \DateTime|\DateTimeImmutable $dateTime
+ */
+ public function methodWithDateTime(\DateTimeInterface $dateTime)
{
return true;
}
}
```
< br >
2021-11-14 00:36:15 +00:00
### DoWhileBreakFalseToIfElseRector
Replace do (...} while (false); with more readable if/else conditions
- class: [`Rector\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector` ](../rules/CodeQuality/Rector/Do_/DoWhileBreakFalseToIfElseRector.php )
```diff
-do {
- if (mt_rand(0, 1)) {
- $value = 5;
- break;
- }
-
+if (mt_rand(0, 1)) {
+ $value = 5;
+} else {
$value = 10;
-} while (false);
+}
```
< br >
2020-11-25 21:34:34 +00:00
### ExplicitBoolCompareRector
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
Make if conditions more explicit
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector` ](../rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php )
2019-05-19 08:27:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeController
2019-05-19 08:27:38 +00:00
{
2020-11-25 21:34:34 +00:00
public function run($items)
2019-05-19 08:27:38 +00:00
{
2020-11-25 21:34:34 +00:00
- if (!count($items)) {
+ if (count($items) === 0) {
return 'no items';
}
2019-05-19 08:27:38 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-19 08:27:38 +00:00
2021-10-07 17:46:41 +00:00
### ExplicitMethodCallOverMagicGetSetRector
Replace magic property fetch using `__get()` and `__set()` with existing method get*()/set*() calls
- class: [`Rector\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector` ](../rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php )
```diff
class MagicCallsObject
{
// adds magic __get() and __set() methods
use \Nette\SmartObject;
private $name;
public function getName()
{
return $this->name;
}
}
class SomeClass
{
public function run(MagicObject $magicObject)
{
- return $magicObject->name;
+ return $magicObject->getName();
}
}
```
< br >
2021-01-24 09:07:23 +00:00
### FlipTypeControlToUseExclusiveTypeRector
Flip type control to use exclusive type
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector` ](../rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php )
2021-01-24 09:07:23 +00:00
```diff
class SomeClass
{
public function __construct(array $values)
{
- /** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO);
- if ($phpDocInfo === null) {
+ if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### ForRepeatedCountToOwnVariableRector
2020-01-04 17:49:26 +00:00
2020-11-25 21:34:34 +00:00
Change `count()` in for function to own variable
2020-01-04 17:49:26 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector` ](../rules/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector.php )
2020-01-04 17:49:26 +00:00
2020-11-25 21:34:34 +00:00
```diff
class SomeClass
{
public function run($items)
{
- for ($i = 5; $i < = count($items); $i++) {
+ $itemsCount = count($items);
+ for ($i = 5; $i < = $itemsCount; $i++) {
echo $items[$i];
}
}
}
```
2020-01-04 17:49:26 +00:00
2020-11-25 21:34:34 +00:00
< br >
2020-01-04 17:49:26 +00:00
2020-11-25 21:34:34 +00:00
### ForToForeachRector
2019-02-04 00:32:53 +00:00
2020-11-25 21:34:34 +00:00
Change `for()` to `foreach()` where useful
2019-02-04 00:32:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\For_\ForToForeachRector` ](../rules/CodeQuality/Rector/For_/ForToForeachRector.php )
2019-02-04 00:32:53 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run($tokens)
{
- for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
- if ($tokens[$i][0] === T_STRING && $tokens[$i][1] === 'fn') {
+ foreach ($tokens as $i => $token) {
+ if ($token[0] === T_STRING & & $token[1] === 'fn') {
$tokens[$i][0] = self::T_FN;
}
}
}
2019-05-29 13:40:20 +00:00
}
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
### ForeachItemsAssignToEmptyArrayToAssignRector
2020-01-04 17:49:26 +00:00
2020-11-25 21:34:34 +00:00
Change `foreach()` items assign to empty array to direct assign
2020-01-04 17:49:26 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector` ](../rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php )
2020-01-04 17:49:26 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run($items)
{
$collectedItems = [];
- foreach ($items as $item) {
- $collectedItems[] = $item;
- }
+ $collectedItems = $items;
}
2020-01-04 17:49:26 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-04 17:49:26 +00:00
2020-11-25 21:34:34 +00:00
### ForeachToInArrayRector
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
Simplify `foreach` loops into `in_array` when possible
2018-10-21 22:26:45 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector` ](../rules/CodeQuality/Rector/Foreach_/ForeachToInArrayRector.php )
2018-10-21 22:26:45 +00:00
```diff
2020-11-25 21:34:34 +00:00
-foreach ($items as $item) {
- if ($item === 'something') {
- return true;
- }
-}
-
-return false;
2021-02-20 23:02:05 +00:00
+return in_array('something', $items, true);
2020-11-25 21:34:34 +00:00
```
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
< br >
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
### GetClassToInstanceOfRector
2020-11-16 17:50:38 +00:00
2021-04-10 18:18:49 +00:00
Changes comparison with get_class to instanceof
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Identical\GetClassToInstanceOfRector` ](../rules/CodeQuality/Rector/Identical/GetClassToInstanceOfRector.php )
2020-11-25 21:34:34 +00:00
```diff
-if (EventsListener::class === get_class($event->job)) { }
+if ($event->job instanceof EventsListener) { }
2018-10-21 22:26:45 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-10-23 20:13:13 +00:00
2020-11-25 21:34:34 +00:00
### InArrayAndArrayKeysToArrayKeyExistsRector
2018-10-23 20:13:13 +00:00
2020-11-25 21:34:34 +00:00
Simplify `in_array` and `array_keys` functions combination into `array_key_exists` when `array_keys` has one argument only
2018-11-11 12:22:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector` ](../rules/CodeQuality/Rector/FuncCall/InArrayAndArrayKeysToArrayKeyExistsRector.php )
2018-10-23 20:13:13 +00:00
```diff
2020-11-25 21:34:34 +00:00
-in_array("key", array_keys($array), true);
+array_key_exists("key", $array);
```
2018-10-23 20:13:13 +00:00
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2022-03-15 17:33:16 +00:00
### InlineConstructorDefaultToPropertyRector
Move property default from constructor to property default
- class: [`Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector` ](../rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php )
```diff
final class SomeClass
{
- private $name;
-
- public function __construct()
- {
- $this->name = 'John';
- }
+ private $name = 'John';
}
```
< br >
2020-11-25 21:34:34 +00:00
### InlineIfToExplicitIfRector
2019-05-19 08:27:38 +00:00
2020-11-25 21:34:34 +00:00
Change inline if to explicit if
2019-05-19 08:27:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector` ](../rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php )
2019-05-19 08:27:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
{
$userId = null;
- is_null($userId) && $userId = 5;
+ if (is_null($userId)) {
+ $userId = 5;
+ }
}
2020-11-16 17:50:38 +00:00
}
2019-06-02 07:45:37 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
### IntvalToTypeCastRector
2020-02-22 12:05:45 +00:00
2020-11-25 21:34:34 +00:00
Change `intval()` to faster and readable (int) `$value`
2020-02-22 12:05:45 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector` ](../rules/CodeQuality/Rector/FuncCall/IntvalToTypeCastRector.php )
2020-02-22 12:05:45 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run($value)
2020-02-22 12:05:45 +00:00
{
2020-11-25 21:34:34 +00:00
- return intval($value);
+ return (int) $value;
2020-02-22 12:05:45 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-02-22 12:05:45 +00:00
2020-11-25 21:34:34 +00:00
### IsAWithStringWithThirdArgumentRector
2019-12-27 17:50:00 +00:00
2020-11-25 21:34:34 +00:00
Complete missing 3rd argument in case `is_a()` function in case of strings
2019-12-27 17:50:00 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector` ](../rules/CodeQuality/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php )
2019-12-27 17:50:00 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-12-27 17:50:00 +00:00
{
2020-11-25 21:34:34 +00:00
public function __construct(string $value)
2019-12-27 17:50:00 +00:00
{
2020-11-25 21:34:34 +00:00
- return is_a($value, 'stdClass');
+ return is_a($value, 'stdClass', true);
2019-12-27 17:50:00 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-12-27 17:50:00 +00:00
2020-11-25 21:34:34 +00:00
### IssetOnPropertyObjectToPropertyExistsRector
2019-08-05 21:10:47 +00:00
2021-02-20 23:02:05 +00:00
Change isset on property object to `property_exists()` and not null check
2019-08-05 21:10:47 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector` ](../rules/CodeQuality/Rector/Isset_/IssetOnPropertyObjectToPropertyExistsRector.php )
2020-06-16 11:39:38 +00:00
2019-08-05 21:10:47 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
private $x;
public function run(): void
{
- isset($this->x);
+ property_exists($this, 'x') & & $this->x !== null;
}
}
2019-08-05 21:10:47 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
### JoinStringConcatRector
2020-09-15 12:30:36 +00:00
2021-01-05 20:06:16 +00:00
Joins concat of 2 strings, unless the length is too long
2020-09-15 12:30:36 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Concat\JoinStringConcatRector` ](../rules/CodeQuality/Rector/Concat/JoinStringConcatRector.php )
2020-09-15 12:30:36 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run()
{
- $name = 'Hi' . ' Tom';
+ $name = 'Hi Tom';
}
2020-09-15 12:30:36 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-15 12:30:36 +00:00
2020-11-25 21:34:34 +00:00
### LogicalToBooleanRector
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
Change OR, AND to ||, & & with more common understanding
2019-06-02 07:45:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector` ](../rules/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector.php )
2019-06-02 07:45:37 +00:00
```diff
2020-11-25 21:34:34 +00:00
-if ($f = false or true) {
+if (($f = false) || true) {
return $f;
}
2019-05-19 08:27:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-19 08:27:38 +00:00
2021-05-02 08:26:23 +00:00
### NarrowUnionTypeDocRector
Changes docblock by narrowing type
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector` ](../rules/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector.php )
2021-05-02 08:26:23 +00:00
```diff
class SomeClass {
/**
- * @param object|DateTime $message
+ * @param DateTime $message
*/
public function getMessage(object $message)
{
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### NewStaticToNewSelfRector
2020-10-20 22:01:47 +00:00
2020-11-25 21:34:34 +00:00
Change unsafe new `static()` to new `self()`
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\New_\NewStaticToNewSelfRector` ](../rules/CodeQuality/Rector/New_/NewStaticToNewSelfRector.php )
2020-10-20 22:01:47 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-10-20 22:01:47 +00:00
{
2020-11-25 21:34:34 +00:00
public function build()
2020-10-20 22:01:47 +00:00
{
2020-11-25 21:34:34 +00:00
- return new static();
+ return new self();
2020-10-20 22:01:47 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-20 22:01:47 +00:00
2022-02-15 22:46:36 +00:00
### OptionalParametersAfterRequiredRector
Move required parameters after optional ones
- class: [`Rector\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector` ](../rules/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector.php )
```diff
class SomeObject
{
- public function run($optional = 1, $required)
+ public function run($required, $optional = 1)
{
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveAlwaysTrueConditionSetInConstructorRector
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
If conditions is always true, perform the content right away
2019-08-05 21:10:47 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector` ](../rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php )
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
```diff
final class SomeClass
{
private $value;
2019-08-05 21:10:47 +00:00
2021-02-20 23:02:05 +00:00
public function __construct(stdClass $value)
2020-11-25 21:34:34 +00:00
{
$this->value = $value;
}
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
public function go()
{
- if ($this->value) {
- return 'yes';
- }
+ return 'yes';
}
}
2019-08-05 21:10:47 +00:00
```
2020-11-25 21:34:34 +00:00
< br >
### RemoveSoleValueSprintfRector
Remove `sprintf()` wrapper if not needed
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector` ](../rules/CodeQuality/Rector/FuncCall/RemoveSoleValueSprintfRector.php )
2019-08-05 21:10:47 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run()
{
- $value = sprintf('%s', 'hi');
+ $value = 'hi';
$welcome = 'hello';
- $value = sprintf('%s', $welcome);
+ $value = $welcome;
}
}
2020-11-16 17:50:38 +00:00
```
2019-09-27 22:34:34 +00:00
2020-11-16 17:50:38 +00:00
< br >
2019-09-27 22:34:34 +00:00
2021-11-28 00:36:13 +00:00
### ReplaceMultipleBooleanNotRector
Replace the Double not operator (!!) by type-casting to boolean
- class: [`Rector\CodeQuality\Rector\BooleanNot\ReplaceMultipleBooleanNotRector` ](../rules/CodeQuality/Rector/BooleanNot/ReplaceMultipleBooleanNotRector.php )
```diff
-$bool = !!$var;
+$bool = (bool) $var;
```
< br >
2020-11-25 21:34:34 +00:00
### SetTypeToCastRector
2019-09-27 22:34:34 +00:00
2020-11-25 21:34:34 +00:00
Changes `settype()` to (type) where possible
2019-09-27 22:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SetTypeToCastRector` ](../rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php )
2019-09-27 22:34:34 +00:00
2020-11-25 21:34:34 +00:00
```diff
class SomeClass
{
2021-08-01 00:25:55 +00:00
public function run($foo)
2020-11-25 21:34:34 +00:00
{
- settype($foo, 'string');
+ $foo = (string) $foo;
2020-07-27 08:16:16 +00:00
2020-11-25 21:34:34 +00:00
- return settype($foo, 'integer');
+ return (int) $foo;
}
}
2020-11-16 17:50:38 +00:00
```
2020-11-25 21:34:34 +00:00
< br >
### ShortenElseIfRector
Shortens else/if to elseif
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\If_\ShortenElseIfRector` ](../rules/CodeQuality/Rector/If_/ShortenElseIfRector.php )
2020-07-27 08:16:16 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-07-27 08:16:16 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2020-07-27 08:16:16 +00:00
{
2020-11-25 21:34:34 +00:00
if ($cond1) {
return $action1;
- } else {
- if ($cond2) {
- return $action2;
- }
+ } elseif ($cond2) {
+ return $action2;
}
2020-07-27 08:16:16 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-07-27 08:16:16 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyArraySearchRector
2019-10-04 17:31:37 +00:00
2021-04-10 18:18:49 +00:00
Simplify array_search to in_array
2019-10-04 17:31:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Identical\SimplifyArraySearchRector` ](../rules/CodeQuality/Rector/Identical/SimplifyArraySearchRector.php )
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
```diff
-array_search("searching", $array) !== false;
+in_array("searching", $array);
2020-11-16 17:50:38 +00:00
```
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
< br >
2018-11-11 12:22:35 +00:00
```diff
2020-11-25 21:34:34 +00:00
-array_search("searching", $array, true) !== false;
+in_array("searching", $array, true);
2018-11-11 12:22:35 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyBoolIdenticalTrueRector
2018-12-31 11:50:32 +00:00
2022-01-16 09:58:52 +00:00
Simplify bool value compare to true or false
2018-12-31 11:50:32 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector` ](../rules/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector.php )
2018-12-31 11:50:32 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2018-12-31 11:50:32 +00:00
{
2020-11-25 21:34:34 +00:00
public function run(bool $value, string $items)
2019-05-29 13:40:20 +00:00
{
2020-11-25 21:34:34 +00:00
- $match = in_array($value, $items, TRUE) === TRUE;
- $match = in_array($value, $items, TRUE) !== FALSE;
+ $match = in_array($value, $items, TRUE);
+ $match = in_array($value, $items, TRUE);
2018-12-31 11:50:32 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-31 11:50:32 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyConditionsRector
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
Simplify conditions
2018-10-21 22:26:45 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Identical\SimplifyConditionsRector` ](../rules/CodeQuality/Rector/Identical/SimplifyConditionsRector.php )
2018-10-21 22:26:45 +00:00
```diff
2020-11-25 21:34:34 +00:00
-if (! ($foo !== 'bar')) {...
+if ($foo === 'bar') {...
2020-11-16 17:50:38 +00:00
```
2019-06-02 07:45:37 +00:00
2020-11-16 17:50:38 +00:00
< br >
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyDeMorganBinaryRector
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
Simplify negated conditions with de Morgan theorem
2019-06-02 07:45:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector` ](../rules/CodeQuality/Rector/BooleanNot/SimplifyDeMorganBinaryRector.php )
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
```diff
$a = 5;
$b = 10;
-$result = !($a > 20 || $b < = 50);
+$result = $a < = 20 & & $b > 50;
```
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
< br >
2019-03-31 12:25:39 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyDuplicatedTernaryRector
2019-03-31 12:25:39 +00:00
2020-11-25 21:34:34 +00:00
Remove ternary that duplicated return value of true : false
2019-03-31 12:25:39 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector` ](../rules/CodeQuality/Rector/Ternary/SimplifyDuplicatedTernaryRector.php )
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
```diff
class SomeClass
2019-03-31 12:25:39 +00:00
{
2020-11-25 21:34:34 +00:00
public function run(bool $value, string $name)
2019-03-31 12:25:39 +00:00
{
2020-11-25 21:34:34 +00:00
- $isTrue = $value ? true : false;
+ $isTrue = $value;
$isName = $name ? true : false;
2019-03-31 12:25:39 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-03-31 12:25:39 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyEmptyArrayCheckRector
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
Simplify `is_array` and `empty` functions combination into a simple identical check for an empty array
2018-11-11 12:22:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector` ](../rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php )
2018-10-21 22:26:45 +00:00
```diff
2020-11-25 21:34:34 +00:00
-is_array($values) & & empty($values)
+$values === []
2018-10-21 22:26:45 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyForeachToArrayFilterRector
2019-02-18 15:51:24 +00:00
2020-11-25 21:34:34 +00:00
Simplify foreach with function filtering to array filter
2019-02-18 15:51:24 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToArrayFilterRector` ](../rules/CodeQuality/Rector/Foreach_/SimplifyForeachToArrayFilterRector.php )
2019-02-18 15:51:24 +00:00
```diff
2020-11-25 21:34:34 +00:00
-$directories = [];
2022-02-18 00:42:23 +00:00
-
2020-11-25 21:34:34 +00:00
$possibleDirectories = [];
-foreach ($possibleDirectories as $possibleDirectory) {
- if (file_exists($possibleDirectory)) {
- $directories[] = $possibleDirectory;
- }
-}
+$directories = array_filter($possibleDirectories, 'file_exists');
2019-05-29 13:40:20 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-29 13:40:20 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyForeachToCoalescingRector
2019-05-29 13:40:20 +00:00
2020-11-25 21:34:34 +00:00
Changes foreach that returns set value to ??
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector` ](../rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php )
2019-05-29 13:40:20 +00:00
```diff
2020-11-25 21:34:34 +00:00
-foreach ($this->oldToNewFunctions as $oldFunction => $newFunction) {
- if ($currentFunction === $oldFunction) {
- return $newFunction;
- }
-}
-
-return null;
+return $this->oldToNewFunctions[$currentFunction] ?? null;
2019-02-18 15:51:24 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyFuncGetArgsCountRector
2021-04-10 18:18:49 +00:00
Simplify count of `func_get_args()` to `func_num_args()`
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SimplifyFuncGetArgsCountRector` ](../rules/CodeQuality/Rector/FuncCall/SimplifyFuncGetArgsCountRector.php )
2020-11-25 21:34:34 +00:00
2019-06-02 07:45:37 +00:00
```diff
2020-11-25 21:34:34 +00:00
-count(func_get_args());
+func_num_args();
2019-06-02 07:45:37 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyIfElseToTernaryRector
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
Changes if/else for same value as assign to ternary
2019-06-02 07:45:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector` ](../rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php )
2018-12-31 11:50:32 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run()
2018-12-31 11:50:32 +00:00
{
2020-11-25 21:34:34 +00:00
- if (empty($value)) {
- $this->arrayBuilt[][$key] = true;
- } else {
- $this->arrayBuilt[][$key] = $value;
- }
+ $this->arrayBuilt[][$key] = empty($value) ? true : $value;
2018-12-31 11:50:32 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-31 11:50:32 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyIfIssetToNullCoalescingRector
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
Simplify binary if to null coalesce
2018-11-11 12:22:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector` ](../rules/CodeQuality/Rector/If_/SimplifyIfIssetToNullCoalescingRector.php )
2018-10-21 22:26:45 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeController
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
public function run($possibleStatieYamlFile)
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
- if (isset($possibleStatieYamlFile['import'])) {
- $possibleStatieYamlFile['import'] = array_merge($possibleStatieYamlFile['import'], $filesToImport);
- } else {
- $possibleStatieYamlFile['import'] = $filesToImport;
- }
+ $possibleStatieYamlFile['import'] = array_merge($possibleStatieYamlFile['import'] ?? [], $filesToImport);
2019-06-02 07:45:37 +00:00
}
}
2018-10-21 22:26:45 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-10-23 18:58:57 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyIfNotNullReturnRector
2019-02-18 15:51:24 +00:00
2020-11-25 21:34:34 +00:00
Changes redundant null check to instant return
2019-02-18 15:51:24 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfNotNullReturnRector` ](../rules/CodeQuality/Rector/If_/SimplifyIfNotNullReturnRector.php )
2019-02-18 15:51:24 +00:00
```diff
2021-07-22 18:40:24 +00:00
$newNode = 'something';
2020-11-25 21:34:34 +00:00
-if ($newNode !== null) {
- return $newNode;
-}
-
-return null;
+return $newNode;
2019-02-18 15:51:24 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-18 15:51:24 +00:00
2021-04-10 18:18:49 +00:00
### SimplifyIfNullableReturnRector
Direct return on if nullable check before return
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfNullableReturnRector` ](../rules/CodeQuality/Rector/If_/SimplifyIfNullableReturnRector.php )
2021-04-10 18:18:49 +00:00
```diff
class SomeClass
{
public function run()
{
- /** @var \stdClass|null $value */
- $value = $this->foo->bar();
- if (! $value instanceof \stdClass) {
- return null;
- }
-
- return $value;
+ return $this->foo->bar();
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### SimplifyIfReturnBoolRector
2019-01-22 20:34:38 +00:00
2020-11-25 21:34:34 +00:00
Shortens if return false/true to direct return
2019-01-22 20:34:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector` ](../rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php )
2019-01-22 20:34:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
-if (strpos($docToken->getContent(), "\n") === false) {
- return true;
-}
-
-return false;
+return strpos($docToken->getContent(), "\n") === false;
2019-01-22 20:34:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-01-22 20:34:38 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyInArrayValuesRector
2019-05-19 08:27:38 +00:00
2020-11-25 21:34:34 +00:00
Removes unneeded `array_values()` in `in_array()` call
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector` ](../rules/CodeQuality/Rector/FuncCall/SimplifyInArrayValuesRector.php )
2019-05-19 08:27:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
-in_array("key", array_values($array), true);
+in_array("key", $array, true);
2019-05-19 08:27:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-19 08:27:38 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyRegexPatternRector
2019-02-04 00:32:53 +00:00
2020-11-25 21:34:34 +00:00
Simplify regex pattern to known ranges
2019-02-04 00:32:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector` ](../rules/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector.php )
2019-02-04 00:32:53 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run($value)
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
- preg_match('#[a-zA-Z0-9+]#', $value);
+ preg_match('#[\w\d+]#', $value);
2020-11-16 17:50:38 +00:00
}
}
2019-05-29 13:40:20 +00:00
```
2019-02-04 00:32:53 +00:00
2020-11-16 17:50:38 +00:00
< br >
2018-12-22 11:22:28 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyStrposLowerRector
2018-12-22 11:22:28 +00:00
2021-01-09 11:16:22 +00:00
Simplify `strpos(strtolower()` , "...") calls
2018-12-22 11:22:28 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector` ](../rules/CodeQuality/Rector/FuncCall/SimplifyStrposLowerRector.php )
2018-12-22 11:22:28 +00:00
```diff
2021-07-22 18:40:24 +00:00
-strpos(strtolower($var), "...")
+stripos($var, "...")
2018-12-22 11:22:28 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-10-23 18:58:57 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyTautologyTernaryRector
Simplify tautology ternary to value
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector` ](../rules/CodeQuality/Rector/Ternary/SimplifyTautologyTernaryRector.php )
2020-11-25 21:34:34 +00:00
2018-10-23 18:58:57 +00:00
```diff
2020-11-25 21:34:34 +00:00
-$value = ($fullyQualifiedTypeHint !== $typeHint) ? $fullyQualifiedTypeHint : $typeHint;
+$value = $fullyQualifiedTypeHint;
2018-10-23 18:58:57 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-10-21 22:26:45 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyUselessVariableRector
2019-05-01 23:56:58 +00:00
2020-11-25 21:34:34 +00:00
Removes useless variable assigns
2019-05-01 23:56:58 +00:00
2022-04-10 18:18:46 +00:00
- class: [`Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector` ](../rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php )
2019-05-01 23:56:58 +00:00
```diff
2020-11-25 21:34:34 +00:00
function () {
- $a = true;
- return $a;
+ return true;
};
2019-05-01 23:56:58 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-04-19 13:54:00 +00:00
2020-11-25 21:34:34 +00:00
### SingleInArrayToCompareRector
Changes `in_array()` with single element to ===
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector` ](../rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php )
2020-11-25 21:34:34 +00:00
2020-04-19 13:54:00 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run()
{
- if (in_array(strtolower($type), ['$this'], true)) {
+ if (strtolower($type) === '$this') {
return strtolower($type);
}
}
}
2020-04-19 13:54:00 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-04-19 13:54:00 +00:00
2021-01-25 18:59:06 +00:00
### SingularSwitchToIfRector
Change switch with only 1 check to if
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector` ](../rules/CodeQuality/Rector/Switch_/SingularSwitchToIfRector.php )
2021-01-25 18:59:06 +00:00
```diff
class SomeObject
{
public function run($value)
{
$result = 1;
- switch ($value) {
- case 100:
+ if ($value === 100) {
$result = 1000;
}
return $result;
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### SplitListAssignToSeparateLineRector
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
Splits `[$a, $b] = [5, 10]` scalar assign to standalone lines
2019-08-05 21:10:47 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Assign\SplitListAssignToSeparateLineRector` ](../rules/CodeQuality/Rector/Assign/SplitListAssignToSeparateLineRector.php )
2020-06-16 11:39:38 +00:00
2019-08-05 21:10:47 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeClass
2019-08-05 21:10:47 +00:00
{
2020-11-25 21:34:34 +00:00
public function run(): void
2019-08-05 21:10:47 +00:00
{
2020-11-25 21:34:34 +00:00
- [$a, $b] = [1, 2];
+ $a = 1;
+ $b = 2;
2019-08-05 21:10:47 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
### StrlenZeroToIdenticalEmptyStringRector
2019-08-05 21:10:47 +00:00
2021-04-10 18:18:49 +00:00
Changes strlen comparison to 0 to direct empty string compare
2019-08-05 21:10:47 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector` ](../rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php )
2019-08-05 21:10:47 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-10-14 15:36:34 +00:00
public function run(string $value)
2020-11-25 21:34:34 +00:00
{
- $empty = strlen($value) === 0;
+ $empty = $value === '';
}
}
2019-08-05 21:10:47 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-25 19:55:16 +00:00
2021-01-01 18:59:23 +00:00
### SwitchNegatedTernaryRector
Switch negated ternary condition rector
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector` ](../rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php )
2021-01-01 18:59:23 +00:00
```diff
class SomeClass
{
public function run(bool $upper, string $name)
{
- return ! $upper
- ? $name
- : strtoupper($name);
+ return $upper
+ ? strtoupper($name)
+ : $name;
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### ThrowWithPreviousExceptionRector
When throwing into a catch block, checks that the previous exception is passed to the new throw clause
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Catch_\ThrowWithPreviousExceptionRector` ](../rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php )
2020-11-25 21:34:34 +00:00
2018-11-11 12:22:35 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run()
{
try {
$someCode = 1;
} catch (Throwable $throwable) {
- throw new AnotherException('ups');
+ throw new AnotherException('ups', $throwable->getCode(), $throwable);
}
}
}
2018-11-11 12:22:35 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
### UnnecessaryTernaryExpressionRector
2020-04-24 17:03:14 +00:00
2020-11-25 21:34:34 +00:00
Remove unnecessary ternary expressions.
2020-04-24 17:03:14 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector` ](../rules/CodeQuality/Rector/Ternary/UnnecessaryTernaryExpressionRector.php )
2020-04-24 17:03:14 +00:00
```diff
2020-11-25 21:34:34 +00:00
-$foo === $bar ? true : false;
+$foo === $bar;
2020-11-16 17:50:38 +00:00
```
< br >
2020-11-25 21:34:34 +00:00
### UnusedForeachValueToArrayKeysRector
Change foreach with unused `$value` but only `$key,` to `array_keys()`
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector` ](../rules/CodeQuality/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php )
2020-11-25 21:34:34 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run()
{
$items = [];
- foreach ($values as $key => $value) {
+ foreach (array_keys($values) as $key) {
$items[$key] = null;
}
}
}
2020-04-24 17:03:14 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-04-24 17:03:14 +00:00
2020-12-22 15:48:25 +00:00
### UnwrapSprintfOneArgumentRector
unwrap `sprintf()` with one argument
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\UnwrapSprintfOneArgumentRector` ](../rules/CodeQuality/Rector/FuncCall/UnwrapSprintfOneArgumentRector.php )
2020-12-22 15:48:25 +00:00
```diff
-echo sprintf('value');
+echo 'value';
```
< br >
2020-11-25 21:34:34 +00:00
### UseIdenticalOverEqualWithSameTypeRector
2018-12-22 14:15:11 +00:00
2020-11-25 21:34:34 +00:00
Use ===/!== over ==/!=, it values have the same type
2018-12-22 14:15:11 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector` ](../rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php )
2018-12-22 14:15:11 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run(int $firstValue, int $secondValue)
{
- $isSame = $firstValue == $secondValue;
- $isDiffernt = $firstValue != $secondValue;
+ $isSame = $firstValue === $secondValue;
+ $isDiffernt = $firstValue !== $secondValue;
}
}
2020-11-16 17:50:38 +00:00
```
2019-05-29 13:40:20 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-11-25 21:34:34 +00:00
## CodingStyle
2019-05-26 19:26:33 +00:00
2020-11-25 21:34:34 +00:00
### AddArrayDefaultToArrayPropertyRector
2020-02-10 17:10:16 +00:00
2020-11-25 21:34:34 +00:00
Adds array default value to property to prevent foreach over null error
2020-02-10 17:10:16 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector` ](../rules/CodingStyle/Rector/Class_/AddArrayDefaultToArrayPropertyRector.php )
2020-02-10 17:10:16 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
/**
* @var int[]
*/
- private $values;
+ private $values = [];
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
public function isEmpty()
{
- return $this->values === null;
+ return $this->values === [];
}
}
2020-02-10 17:10:16 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-02-10 17:10:16 +00:00
2021-01-01 18:59:23 +00:00
### AddFalseDefaultToBoolPropertyRector
Add false default to bool properties, to prevent null compare errors
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Property\AddFalseDefaultToBoolPropertyRector` ](../rules/CodingStyle/Rector/Property/AddFalseDefaultToBoolPropertyRector.php )
2021-01-01 18:59:23 +00:00
```diff
class SomeClass
{
/**
* @var bool
*/
- private $isDisabled;
+ private $isDisabled = false;
}
```
< br >
2020-11-25 21:34:34 +00:00
### BinarySwitchToIfElseRector
2019-09-27 22:34:34 +00:00
2020-11-25 21:34:34 +00:00
Changes switch with 2 options to if-else
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector` ](../rules/CodingStyle/Rector/Switch_/BinarySwitchToIfElseRector.php )
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
-switch ($foo) {
- case 'my string':
- $result = 'ok';
- break;
-
- default:
- $result = 'not ok';
+if ($foo == 'my string') {
2021-08-01 00:25:55 +00:00
+ $result = 'ok';
2020-11-25 21:34:34 +00:00
+} else {
+ $result = 'not ok';
}
2019-09-27 22:34:34 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-09-27 22:34:34 +00:00
2021-05-09 00:15:50 +00:00
### CallUserFuncArrayToVariadicRector
2020-02-21 01:06:13 +00:00
2021-05-09 00:15:50 +00:00
Replace `call_user_func_array()` with variadic
2020-02-21 01:06:13 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\FuncCall\CallUserFuncArrayToVariadicRector` ](../rules/CodingStyle/Rector/FuncCall/CallUserFuncArrayToVariadicRector.php )
2020-02-21 01:06:13 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run()
{
- call_user_func_array('some_function', $items);
+ some_function(...$items);
}
}
2020-02-21 01:06:13 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-02-21 01:06:13 +00:00
2021-05-09 00:15:50 +00:00
### CallUserFuncToMethodCallRector
Refactor `call_user_func()` on known class method to a method call
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRector` ](../rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php )
2021-05-09 00:15:50 +00:00
```diff
final class SomeClass
{
public function run()
{
- $result = \call_user_func([$this->property, 'method'], $args);
+ $result = $this->property->method($args);
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### CatchExceptionNameMatchingTypeRector
2019-05-26 11:47:23 +00:00
2020-11-25 21:34:34 +00:00
Type and name of catch exception should match
2019-05-26 11:47:23 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector` ](../rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php )
2019-05-26 11:47:23 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-05-26 11:47:23 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2019-05-26 11:47:23 +00:00
{
2020-11-25 21:34:34 +00:00
try {
// ...
- } catch (SomeException $typoException) {
- $typoException->getMessage();
+ } catch (SomeException $someException) {
+ $someException->getMessage();
}
2019-05-26 11:47:23 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-11 09:21:48 +00:00
2020-11-25 21:34:34 +00:00
### ConsistentImplodeRector
2020-09-24 18:54:39 +00:00
2021-04-10 18:18:49 +00:00
Changes various implode forms to consistent one
2020-09-11 09:21:48 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\FuncCall\ConsistentImplodeRector` ](../rules/CodingStyle/Rector/FuncCall/ConsistentImplodeRector.php )
2020-09-24 18:54:39 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run(array $items)
{
- $itemsAsStrings = implode($items);
- $itemsAsStrings = implode($items, '|');
+ $itemsAsStrings = implode('', $items);
+ $itemsAsStrings = implode('|', $items);
2020-09-11 09:21:48 +00:00
2020-11-25 21:34:34 +00:00
$itemsAsStrings = implode('|', $items);
}
}
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
### ConsistentPregDelimiterRector
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
Replace PREG delimiter with configured one
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2019-08-05 21:10:47 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector` ](../rules/CodingStyle/Rector/FuncCall/ConsistentPregDelimiterRector.php )
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
```php
use Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(ConsistentPregDelimiterRector::class)
2022-02-20 00:35:49 +00:00
->configure([
ConsistentPregDelimiterRector::DELIMITER => '#',
]);
2020-11-25 21:34:34 +00:00
};
```
2019-05-01 23:56:58 +00:00
2020-11-25 21:34:34 +00:00
↓
2019-05-01 23:56:58 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run()
{
- preg_match('~value~', $value);
- preg_match_all('~value~im', $value);
+ preg_match('#value#', $value);
+ preg_match_all('#value#im', $value);
}
}
2019-05-01 23:56:58 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-01 23:56:58 +00:00
2021-01-30 20:30:37 +00:00
### CountArrayToEmptyArrayComparisonRector
2021-04-10 18:18:49 +00:00
Change count array comparison to empty array comparison to improve performance
2021-01-30 20:30:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector` ](../rules/CodingStyle/Rector/FuncCall/CountArrayToEmptyArrayComparisonRector.php )
2021-01-30 20:30:37 +00:00
```diff
-count($array) === 0;
-count($array) > 0;
-! count($array);
+$array === [];
+$array !== [];
+$array === [];
```
< br >
2020-11-25 21:34:34 +00:00
### EncapsedStringsToSprintfRector
2019-10-15 14:46:31 +00:00
2021-04-10 18:18:49 +00:00
Convert enscaped {$string} to more readable sprintf
2019-10-15 14:46:31 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector` ](../rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php )
2019-10-15 14:46:31 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeClass
{
public function run(string $format)
{
- return "Unsupported format {$format}";
+ return sprintf('Unsupported format %s', $format);
}
}
2020-11-16 17:50:38 +00:00
```
2020-09-11 09:21:48 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2020-11-25 21:34:34 +00:00
### FollowRequireByDirRector
2020-09-11 09:21:48 +00:00
2020-11-25 21:34:34 +00:00
include/require should be followed by absolute path
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Include_\FollowRequireByDirRector` ](../rules/CodingStyle/Rector/Include_/FollowRequireByDirRector.php )
2020-09-11 09:21:48 +00:00
2019-10-15 14:46:31 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run()
2019-10-15 14:46:31 +00:00
{
2020-11-25 21:34:34 +00:00
- require 'autoload.php';
+ require __DIR__ . '/autoload.php';
2019-10-15 14:46:31 +00:00
}
}
```
2020-09-11 09:21:48 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-05-09 00:15:50 +00:00
### FuncGetArgsToVariadicParamRector
Refactor `func_get_args()` in to a variadic param
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector` ](../rules/CodingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector.php )
2021-05-09 00:15:50 +00:00
```diff
-function run()
+function run(...$args)
{
- $args = \func_get_args();
}
```
< br >
2020-11-25 21:34:34 +00:00
### MakeInheritedMethodVisibilitySameAsParentRector
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
Make method visibility same as parent one
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector` ](../rules/CodingStyle/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector.php )
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class ChildClass extends ParentClass
{
- public function run()
+ protected function run()
{
}
}
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
class ParentClass
2019-09-27 22:34:34 +00:00
{
2020-11-25 21:34:34 +00:00
protected function run()
{
}
2019-09-27 22:34:34 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-09-27 22:34:34 +00:00
2021-09-05 00:37:33 +00:00
### NewlineAfterStatementRector
Add new line after statements to tidify code
- class: [`Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector` ](../rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php )
```diff
class SomeClass
{
public function test()
{
}
+
public function test2()
{
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### NewlineBeforeNewAssignSetRector
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
Add extra space before new assign set
2019-08-05 21:10:47 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector` ](../rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php )
2019-08-05 21:10:47 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeClass
2019-08-05 21:10:47 +00:00
{
public function run()
{
2020-11-25 21:34:34 +00:00
$value = new Value;
$value->setValue(5);
+
$value2 = new Value;
$value2->setValue(1);
2019-08-05 21:10:47 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
### NullableCompareToNullRector
2019-01-03 10:54:13 +00:00
2020-11-25 21:34:34 +00:00
Changes negate of empty comparison of nullable value to explicit === or !== compare
2019-01-03 10:54:13 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\If_\NullableCompareToNullRector` ](../rules/CodingStyle/Rector/If_/NullableCompareToNullRector.php )
2019-01-03 10:54:13 +00:00
```diff
2020-11-25 21:34:34 +00:00
/** @var stdClass|null $value */
-if ($value) {
+if ($value !== null) {
}
2019-01-03 10:54:13 +00:00
2020-11-25 21:34:34 +00:00
-if (!$value) {
+if ($value === null) {
2019-01-03 10:54:13 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-07-24 11:46:57 +00:00
2021-07-22 18:40:24 +00:00
### OrderAttributesRector
Order attributes by desired names
:wrench: **configure it!**
- class: [`Rector\CodingStyle\Rector\ClassMethod\OrderAttributesRector` ](../rules/CodingStyle/Rector/ClassMethod/OrderAttributesRector.php )
```php
use Rector\CodingStyle\Rector\ClassMethod\OrderAttributesRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(OrderAttributesRector::class)
2021-12-10 07:56:16 +00:00
->configure(['First', 'Second']);
2021-07-22 18:40:24 +00:00
};
```
↓
```diff
+#[First]
#[Second]
-#[First]
class Someclass
{
}
```
< br >
2021-01-01 18:59:23 +00:00
### PHPStormVarAnnotationRector
Change various `@var` annotation formats to one PHPStorm understands
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Assign\PHPStormVarAnnotationRector` ](../rules/CodingStyle/Rector/Assign/PHPStormVarAnnotationRector.php )
2021-01-01 18:59:23 +00:00
```diff
-$config = 5;
-/** @var \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterConfig $config */
+/** @var \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterConfig $config */
+$config = 5;
```
< br >
2020-12-22 15:48:25 +00:00
### PostIncDecToPreIncDecRector
Use ++$value or --$value instead of `$value++` or `$value--`
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector` ](../rules/CodingStyle/Rector/PostInc/PostIncDecToPreIncDecRector.php )
2020-12-22 15:48:25 +00:00
```diff
class SomeClass
{
public function run($value = 1)
{
- $value++; echo $value;
- $value--; echo $value;
+ ++$value; echo $value;
+ --$value; echo $value;
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### PreferThisOrSelfMethodCallRector
2020-11-19 01:09:53 +00:00
2020-11-25 21:34:34 +00:00
Changes `$this->...` and static:: to self:: or vise versa for given types
2020-11-19 01:09:53 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector` ](../rules/CodingStyle/Rector/MethodCall/PreferThisOrSelfMethodCallRector.php )
2020-11-25 21:34:34 +00:00
```php
2021-08-08 00:26:40 +00:00
use PHPUnit\Framework\TestCase;
2021-07-22 18:40:24 +00:00
use Rector\CodingStyle\Enum\PreferenceSelfThis;
2020-11-25 21:34:34 +00:00
use Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(PreferThisOrSelfMethodCallRector::class)
2022-02-20 00:35:49 +00:00
->configure([
TestCase::class => PreferenceSelfThis::PREFER_SELF(),
]);
2020-11-25 21:34:34 +00:00
};
```
↓
2020-11-19 01:09:53 +00:00
```diff
2021-12-19 00:36:48 +00:00
use PHPUnit\Framework\TestCase;
final class SomeClass extends TestCase
2020-11-19 01:09:53 +00:00
{
public function run()
{
2020-11-25 21:34:34 +00:00
- $this->assertEquals('a', 'a');
+ self::assertEquals('a', 'a');
2020-11-19 01:09:53 +00:00
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveDoubleUnderscoreInMethodNameRector
2020-09-24 18:54:39 +00:00
2020-11-25 21:34:34 +00:00
Non-magic PHP object methods cannot start with "__"
2019-09-25 08:49:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector` ](../rules/CodingStyle/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector.php )
2019-09-25 08:49:53 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
2019-09-25 08:49:53 +00:00
{
2020-11-25 21:34:34 +00:00
- public function __getName($anotherObject)
+ public function getName($anotherObject)
2019-09-25 08:49:53 +00:00
{
2020-11-25 21:34:34 +00:00
- $anotherObject->__getSurname();
+ $anotherObject->getSurname();
2019-09-25 08:49:53 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-09-25 08:49:53 +00:00
2022-01-26 18:39:23 +00:00
### RemoveFinalFromConstRector
Remove final from constants in classes defined as final
- class: [`Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector` ](../rules/CodingStyle/Rector/ClassConst/RemoveFinalFromConstRector.php )
```diff
final class SomeClass
{
- final public const NAME = 'value';
+ public const NAME = 'value';
}
```
< br >
2020-11-25 21:34:34 +00:00
### ReturnArrayClassMethodToYieldRector
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
Turns array return to yield return in specific type and method
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector` ](../rules/CodingStyle/Rector/ClassMethod/ReturnArrayClassMethodToYieldRector.php )
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
```php
use Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector;
use Rector\CodingStyle\ValueObject\ReturnArrayClassMethodToYield;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(ReturnArrayClassMethodToYieldRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', '*provide*')]);
2020-11-25 21:34:34 +00:00
};
```
2019-05-26 11:47:23 +00:00
2020-11-25 21:34:34 +00:00
↓
2019-06-02 07:45:37 +00:00
2020-11-16 17:50:38 +00:00
```diff
2021-08-02 17:48:07 +00:00
use PHPUnit\Framework\TestCase;
final class SomeTest implements TestCase
2020-11-16 17:50:38 +00:00
{
2021-08-02 17:48:07 +00:00
public static function provideData()
2020-11-16 17:50:38 +00:00
{
2021-08-02 17:48:07 +00:00
- return [['some text']];
+ yield ['some text'];
2020-11-16 17:50:38 +00:00
}
}
```
2020-09-24 18:54:39 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-07-24 11:46:57 +00:00
2021-05-09 00:15:50 +00:00
### SeparateMultiUseImportsRector
Split multi use imports and trait statements to standalone lines
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Use_\SeparateMultiUseImportsRector` ](../rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php )
2021-05-09 00:15:50 +00:00
```diff
-use A, B;
+use A;
+use B;
class SomeClass
{
- use SomeTrait, AnotherTrait;
+ use SomeTrait;
+ use AnotherTrait;
}
```
< br >
2020-11-25 21:34:34 +00:00
### SplitDoubleAssignRector
2020-09-24 18:54:39 +00:00
2021-03-15 17:58:04 +00:00
Split multiple inline assigns to each own lines default value, to prevent undefined array issues
2019-06-02 07:45:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector` ](../rules/CodingStyle/Rector/Assign/SplitDoubleAssignRector.php )
2019-05-26 11:47:23 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
2019-05-26 11:47:23 +00:00
{
2020-11-16 17:50:38 +00:00
public function run()
2019-05-26 11:47:23 +00:00
{
2020-11-25 21:34:34 +00:00
- $one = $two = 1;
+ $one = 1;
+ $two = 1;
2019-05-26 11:47:23 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-26 11:47:23 +00:00
2020-11-25 21:34:34 +00:00
### SplitGroupedConstantsAndPropertiesRector
2019-05-29 13:40:20 +00:00
2020-11-25 21:34:34 +00:00
Separate constant and properties to own lines
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\ClassConst\SplitGroupedConstantsAndPropertiesRector` ](../rules/CodingStyle/Rector/ClassConst/SplitGroupedConstantsAndPropertiesRector.php )
2019-05-29 13:40:20 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
- const HI = true, AHOJ = 'true';
+ const HI = true;
+ const AHOJ = 'true';
/**
* @var string
*/
- public $isIt, $isIsThough;
+ public $isIt;
+
+ /**
+ * @var string
+ */
+ public $isIsThough;
2019-05-29 13:40:20 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-29 13:40:20 +00:00
2020-11-25 21:34:34 +00:00
### StrictArraySearchRector
2019-05-26 11:47:23 +00:00
2021-04-10 18:18:49 +00:00
Makes array_search search for identical elements
2019-05-26 11:47:23 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector` ](../rules/CodingStyle/Rector/FuncCall/StrictArraySearchRector.php )
2019-05-26 11:47:23 +00:00
```diff
2020-11-25 21:34:34 +00:00
-array_search($value, $items);
+array_search($value, $items, true);
2019-05-26 11:47:23 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-26 11:47:23 +00:00
2020-11-25 21:34:34 +00:00
### SymplifyQuoteEscapeRector
2019-09-27 22:34:34 +00:00
2020-11-25 21:34:34 +00:00
Prefer quote that are not inside the string
2019-09-27 22:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector` ](../rules/CodingStyle/Rector/String_/SymplifyQuoteEscapeRector.php )
2019-09-27 22:34:34 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
- $name = "\" Tom";
- $name = '\' Sara';
+ $name = '" Tom';
+ $name = "' Sara";
2019-06-02 07:45:37 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
### TernaryConditionVariableAssignmentRector
2020-07-16 15:40:04 +00:00
2020-11-25 21:34:34 +00:00
Assign outcome of ternary condition to variable, where applicable
2020-07-16 15:40:04 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Ternary\TernaryConditionVariableAssignmentRector` ](../rules/CodingStyle/Rector/Ternary/TernaryConditionVariableAssignmentRector.php )
2020-07-16 15:40:04 +00:00
```diff
2020-11-25 21:34:34 +00:00
function ternary($value)
2020-07-13 21:13:40 +00:00
{
2020-11-25 21:34:34 +00:00
- $value ? $a = 1 : $a = 0;
+ $a = $value ? 1 : 0;
2020-07-13 21:13:40 +00:00
}
2020-07-16 15:40:04 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-07-16 15:40:04 +00:00
2021-01-01 18:59:23 +00:00
### UnSpreadOperatorRector
Remove spread operator
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector` ](../rules/CodingStyle/Rector/ClassMethod/UnSpreadOperatorRector.php )
2021-01-01 18:59:23 +00:00
```diff
class SomeClass
{
- public function run(...$array)
+ public function run(array $array)
{
}
public function execute(array $data)
{
- $this->run(...$data);
+ $this->run($data);
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### UseClassKeywordForClassNameResolutionRector
2020-06-29 18:36:58 +00:00
2020-11-25 21:34:34 +00:00
Use `class` keyword for class name resolution in string instead of hardcoded string reference
2020-06-29 18:36:58 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector` ](../rules/CodingStyle/Rector/String_/UseClassKeywordForClassNameResolutionRector.php )
2020-06-29 18:36:58 +00:00
```diff
2020-11-25 21:34:34 +00:00
-$value = 'App\SomeClass::someMethod()';
2021-11-25 12:53:25 +00:00
+$value = \App\SomeClass::class . '::someMethod()';
2019-10-04 14:59:42 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-10-04 14:59:42 +00:00
2020-11-25 21:34:34 +00:00
### UseIncrementAssignRector
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
Use ++ increment instead of `$var += 1`
2020-08-30 18:48:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Plus\UseIncrementAssignRector` ](../rules/CodingStyle/Rector/Plus/UseIncrementAssignRector.php )
2020-08-30 18:48:37 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-08-30 18:48:37 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2020-08-30 18:48:37 +00:00
{
2020-11-25 21:34:34 +00:00
- $style += 1;
2020-12-22 15:48:25 +00:00
+ ++$style;
2020-08-30 18:48:37 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
### VarConstantCommentRector
2019-10-15 14:46:31 +00:00
2021-04-10 18:18:49 +00:00
Constant should have a `@var` comment with type
2019-10-15 14:46:31 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector` ](../rules/CodingStyle/Rector/ClassConst/VarConstantCommentRector.php )
2019-10-15 14:46:31 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-10-15 14:46:31 +00:00
{
2020-11-25 21:34:34 +00:00
+ /**
+ * @var string
+ */
const HI = 'hi';
2019-10-15 14:46:31 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-10-15 14:46:31 +00:00
2020-11-25 21:34:34 +00:00
### VersionCompareFuncCallToConstantRector
2020-07-21 17:13:15 +00:00
2020-11-25 21:34:34 +00:00
Changes use of call to version compare function to use of PHP version constant
2020-07-21 17:13:15 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector` ](../rules/CodingStyle/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php )
2020-07-21 17:13:15 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-07-21 17:13:15 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
- version_compare(PHP_VERSION, '5.3.0', '< ');
+ PHP_VERSION_ID < 50300 ;
2020-11-16 17:50:38 +00:00
}
2020-07-21 17:13:15 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-07-21 17:13:15 +00:00
2020-11-25 21:34:34 +00:00
### WrapEncapsedVariableInCurlyBracesRector
2020-09-24 18:54:39 +00:00
2020-11-25 21:34:34 +00:00
Wrap encapsed variables in curly braces
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector` ](../rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php )
2019-01-03 10:54:13 +00:00
```diff
2020-11-25 21:34:34 +00:00
function run($world)
2020-11-16 17:50:38 +00:00
{
2021-07-22 18:40:24 +00:00
- echo "Hello $world!";
+ echo "Hello {$world}!";
2020-11-16 17:50:38 +00:00
}
```
2019-02-04 00:32:53 +00:00
2020-11-16 17:50:38 +00:00
< br >
2019-09-06 10:30:58 +00:00
2021-11-21 00:34:01 +00:00
## Compatibility
### AttributeCompatibleAnnotationRector
Change annotation to attribute compatible form, see https://tomasvotruba.com/blog/doctrine-annotations-and-attributes-living-together-in-peace/
- class: [`Rector\Compatibility\Rector\Class_\AttributeCompatibleAnnotationRector` ](../rules/Compatibility/Rector/Class_/AttributeCompatibleAnnotationRector.php )
```diff
-use Doctrine\Common\Annotations\Annotation\Required;
+use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
/**
* @annotation
+ * @NamedArgumentConstructor
*/
class SomeAnnotation
{
/**
- * @var string[]
- * @Required ()
+ * @param string[] $enum
*/
- public array $enum;
+ public function __construct(
+ public array $enum
+ ) {
+ }
}
```
< br >
2021-01-16 12:23:37 +00:00
## Composer
2021-01-28 18:26:56 +00:00
### AddPackageToRequireComposerRector
2021-01-16 12:23:37 +00:00
2021-01-28 18:26:56 +00:00
Add package to "require" in `composer.json`
2021-01-16 12:23:37 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Composer\Rector\AddPackageToRequireComposerRector` ](../rules/Composer/Rector/AddPackageToRequireComposerRector.php )
2021-01-16 12:23:37 +00:00
```php
2021-01-28 18:26:56 +00:00
use Rector\Composer\Rector\AddPackageToRequireComposerRector;
2021-01-16 12:23:37 +00:00
use Rector\Composer\ValueObject\PackageAndVersion;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-28 18:26:56 +00:00
$services->set(AddPackageToRequireComposerRector::class)
2021-12-07 19:04:47 +00:00
->configure([new PackageAndVersion('symfony/console', '^3.4')]);
2021-01-16 12:23:37 +00:00
};
```
↓
```diff
{
2021-01-28 18:26:56 +00:00
+ "require": {
2021-01-16 12:23:37 +00:00
+ "symfony/console": "^3.4"
+ }
}
```
< br >
2021-01-28 18:26:56 +00:00
### AddPackageToRequireDevComposerRector
2021-01-16 12:23:37 +00:00
2021-01-28 18:26:56 +00:00
Add package to "require-dev" in `composer.json`
2021-01-16 12:23:37 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Composer\Rector\AddPackageToRequireDevComposerRector` ](../rules/Composer/Rector/AddPackageToRequireDevComposerRector.php )
2021-01-16 12:23:37 +00:00
```php
2021-01-28 18:26:56 +00:00
use Rector\Composer\Rector\AddPackageToRequireDevComposerRector;
2021-01-16 12:23:37 +00:00
use Rector\Composer\ValueObject\PackageAndVersion;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-28 18:26:56 +00:00
$services->set(AddPackageToRequireDevComposerRector::class)
2021-12-07 19:04:47 +00:00
->configure([new PackageAndVersion('symfony/console', '^3.4')]);
2021-01-16 12:23:37 +00:00
};
```
↓
```diff
{
2021-01-28 18:26:56 +00:00
+ "require-dev": {
2021-01-16 12:23:37 +00:00
+ "symfony/console": "^3.4"
+ }
}
```
< br >
2021-01-28 18:26:56 +00:00
### ChangePackageVersionComposerRector
2021-01-16 12:23:37 +00:00
Change package version `composer.json`
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Composer\Rector\ChangePackageVersionComposerRector` ](../rules/Composer/Rector/ChangePackageVersionComposerRector.php )
2021-01-16 12:23:37 +00:00
```php
2021-01-26 01:32:23 +00:00
use Rector\Composer\Rector\ChangePackageVersionComposerRector;
2021-01-16 12:23:37 +00:00
use Rector\Composer\ValueObject\PackageAndVersion;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-26 01:32:23 +00:00
$services->set(ChangePackageVersionComposerRector::class)
2021-12-07 19:04:47 +00:00
->configure([new PackageAndVersion('symfony/console', '^4.4')]);
2021-01-16 12:23:37 +00:00
};
```
↓
```diff
{
2022-03-04 11:12:01 +00:00
"require": {
2021-01-16 12:23:37 +00:00
- "symfony/console": "^3.4"
+ "symfony/console": "^4.4"
}
}
```
< br >
2021-01-28 18:26:56 +00:00
### RemovePackageComposerRector
2021-01-16 12:23:37 +00:00
Remove package from "require" and "require-dev" in `composer.json`
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Composer\Rector\RemovePackageComposerRector` ](../rules/Composer/Rector/RemovePackageComposerRector.php )
2021-01-16 12:23:37 +00:00
```php
2021-01-26 01:32:23 +00:00
use Rector\Composer\Rector\RemovePackageComposerRector;
2021-01-16 12:23:37 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-26 01:32:23 +00:00
$services->set(RemovePackageComposerRector::class)
2021-12-07 19:04:47 +00:00
->configure(['symfony/console']);
2021-01-16 12:23:37 +00:00
};
```
↓
```diff
{
- "require": {
- "symfony/console": "^3.4"
- }
}
```
< br >
2021-05-16 08:19:51 +00:00
### RenamePackageComposerRector
Change package name in `composer.json`
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Composer\Rector\RenamePackageComposerRector` ](../rules/Composer/Rector/RenamePackageComposerRector.php )
2021-05-16 08:19:51 +00:00
```php
use Rector\Composer\Rector\RenamePackageComposerRector;
use Rector\Composer\ValueObject\RenamePackage;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RenamePackageComposerRector::class)
2021-12-07 19:04:47 +00:00
->configure([new RenamePackage('rector/rector', 'rector/rector-src')]);
2021-05-16 08:19:51 +00:00
};
```
↓
```diff
{
"require": {
- "rector/rector": "dev-main"
+ "rector/rector-src": "dev-main"
}
}
```
< br >
2021-01-28 18:26:56 +00:00
### ReplacePackageAndVersionComposerRector
2021-01-16 12:23:37 +00:00
Change package name and version `composer.json`
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Composer\Rector\ReplacePackageAndVersionComposerRector` ](../rules/Composer/Rector/ReplacePackageAndVersionComposerRector.php )
2021-01-16 12:23:37 +00:00
```php
2021-01-26 01:32:23 +00:00
use Rector\Composer\Rector\ReplacePackageAndVersionComposerRector;
2021-01-16 12:23:37 +00:00
use Rector\Composer\ValueObject\ReplacePackageAndVersion;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-26 01:32:23 +00:00
$services->set(ReplacePackageAndVersionComposerRector::class)
2021-12-07 19:04:47 +00:00
->configure([new ReplacePackageAndVersion('symfony/console', 'symfony/http-kernel', '^4.4')]);
2021-01-16 12:23:37 +00:00
};
```
↓
```diff
{
"require-dev": {
- "symfony/console": "^3.4"
+ "symfony/http-kernel": "^4.4"
}
}
```
< br >
2020-11-25 21:34:34 +00:00
## DeadCode
2020-03-25 21:51:12 +00:00
2021-01-01 18:59:23 +00:00
### RecastingRemovalRector
Removes recasting of the same type
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Cast\RecastingRemovalRector` ](../rules/DeadCode/Rector/Cast/RecastingRemovalRector.php )
2021-01-01 18:59:23 +00:00
```diff
$string = '';
-$string = (string) $string;
+$string = $string;
$array = [];
-$array = (array) $array;
+$array = $array;
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveAlwaysTrueIfConditionRector
2020-03-25 21:51:12 +00:00
2020-11-25 21:34:34 +00:00
Remove if condition that is always true
2020-03-25 21:51:12 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector` ](../rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php )
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
```diff
final class SomeClass
2020-03-25 21:51:12 +00:00
{
2020-11-25 21:34:34 +00:00
public function go()
2020-03-25 21:51:12 +00:00
{
2020-11-25 21:34:34 +00:00
- if (1 === 1) {
- return 'yes';
- }
+ return 'yes';
2020-03-25 21:51:12 +00:00
2020-11-25 21:34:34 +00:00
return 'no';
2020-03-25 21:51:12 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-03-25 21:51:12 +00:00
2020-11-25 21:34:34 +00:00
### RemoveAndTrueRector
2019-02-04 00:32:53 +00:00
2020-11-25 21:34:34 +00:00
Remove and true that has no added value
2019-02-04 00:32:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector` ](../rules/DeadCode/Rector/BooleanAnd/RemoveAndTrueRector.php )
2019-02-04 00:32:53 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
- return true && 5 === 1;
+ return 5 === 1;
2019-06-02 07:45:37 +00:00
}
}
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
### RemoveAnnotationRector
Remove annotation by names
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector` ](../rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php )
2021-03-23 22:13:35 +00:00
```php
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RemoveAnnotationRector::class)
2021-12-06 21:12:03 +00:00
->configure(['method']);
2021-03-23 22:13:35 +00:00
};
```
↓
```diff
-/**
- * @method getName()
- */
final class SomeClass
{
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveCodeAfterReturnRector
2019-02-04 00:32:53 +00:00
2020-11-25 21:34:34 +00:00
Remove dead code after return statement
2019-02-04 00:32:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector` ](../rules/DeadCode/Rector/FunctionLike/RemoveCodeAfterReturnRector.php )
2019-02-04 00:32:53 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
public function run(int $a)
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
return $a;
- $a++;
2020-11-16 17:50:38 +00:00
}
2019-02-04 00:32:53 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-25 19:55:16 +00:00
2020-11-25 21:34:34 +00:00
### RemoveConcatAutocastRector
2020-03-30 19:08:12 +00:00
2020-11-25 21:34:34 +00:00
Remove (string) casting when it comes to concat, that does this by default
2020-03-30 19:08:12 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector` ](../rules/DeadCode/Rector/Concat/RemoveConcatAutocastRector.php )
2020-03-30 19:08:12 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeConcatingClass
2020-03-30 19:08:12 +00:00
{
2020-11-25 21:34:34 +00:00
public function run($value)
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
- return 'hi ' . (string) $value;
+ return 'hi ' . $value;
2020-11-16 17:50:38 +00:00
}
2020-03-30 19:08:12 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-03-30 19:08:12 +00:00
2021-01-16 12:23:37 +00:00
### RemoveDeadConditionAboveReturnRector
Remove dead condition above return
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Return_\RemoveDeadConditionAboveReturnRector` ](../rules/DeadCode/Rector/Return_/RemoveDeadConditionAboveReturnRector.php )
2021-01-16 12:23:37 +00:00
```diff
final class SomeClass
{
public function go()
{
- if (1 === 1) {
- return 'yes';
- }
-
return 'yes';
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveDeadConstructorRector
2018-12-25 19:55:16 +00:00
2020-11-25 21:34:34 +00:00
Remove empty constructor
2018-12-25 19:55:16 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveDeadConstructorRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveDeadConstructorRector.php )
2018-12-25 19:55:16 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2018-12-25 19:55:16 +00:00
{
2020-11-25 21:34:34 +00:00
- public function __construct()
- {
- }
2018-12-25 19:55:16 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-25 19:55:16 +00:00
2021-11-28 17:29:16 +00:00
### RemoveDeadContinueRector
Remove useless continue at the end of loops
- class: [`Rector\DeadCode\Rector\For_\RemoveDeadContinueRector` ](../rules/DeadCode/Rector/For_/RemoveDeadContinueRector.php )
```diff
while ($i < 10 ) {
++$i;
- continue;
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveDeadIfForeachForRector
2018-12-31 11:50:32 +00:00
2020-11-25 21:34:34 +00:00
Remove if, foreach and for that does not do anything
2018-12-31 11:50:32 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector` ](../rules/DeadCode/Rector/For_/RemoveDeadIfForeachForRector.php )
2018-12-31 11:50:32 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-02-14 10:25:21 +00:00
{
2020-11-25 21:34:34 +00:00
public function run($someObject)
2020-02-14 10:25:21 +00:00
{
2020-11-25 21:34:34 +00:00
$value = 5;
- if ($value) {
- }
-
if ($someObject->run()) {
- }
-
- foreach ($values as $value) {
}
return $value;
2020-02-14 10:25:21 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-02-14 10:25:21 +00:00
2021-02-13 14:57:30 +00:00
### RemoveDeadInstanceOfRector
Remove dead instanceof check on type hinted variable
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector` ](../rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php )
2021-02-13 14:57:30 +00:00
```diff
final class SomeClass
{
public function go(stdClass $stdClass)
{
- if (! $stdClass instanceof stdClass) {
- return false;
- }
-
return true;
}
}
```
< br >
2021-02-20 23:02:05 +00:00
### RemoveDeadLoopRector
Remove loop with no body
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\For_\RemoveDeadLoopRector` ](../rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php )
2021-02-20 23:02:05 +00:00
```diff
class SomeClass
{
public function run($values)
{
- for ($i=1; $i< count ($ values ); ++$ i ) {
- }
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveDeadReturnRector
2019-03-09 13:24:30 +00:00
2020-11-25 21:34:34 +00:00
Remove last return in the functions, since does not do anything
2019-03-09 13:24:30 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector` ](../rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php )
2019-03-09 13:24:30 +00:00
```diff
class SomeClass
{
2020-11-16 17:50:38 +00:00
public function run()
2019-02-21 14:36:16 +00:00
{
2020-11-25 21:34:34 +00:00
$shallWeDoThis = true;
if ($shallWeDoThis) {
return;
}
-
- return;
2019-02-21 14:36:16 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-21 14:36:16 +00:00
2020-11-25 21:34:34 +00:00
### RemoveDeadStmtRector
2019-07-06 19:51:36 +00:00
2020-11-25 21:34:34 +00:00
Removes dead code statements
2019-07-06 19:51:36 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Expression\RemoveDeadStmtRector` ](../rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php )
2020-06-16 11:39:38 +00:00
2019-07-06 19:51:36 +00:00
```diff
2020-11-25 21:34:34 +00:00
-$value = 5;
-$value;
+$value = 5;
2019-07-06 19:51:36 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-07-06 19:51:36 +00:00
2020-11-25 21:34:34 +00:00
### RemoveDeadTryCatchRector
2019-02-18 15:51:24 +00:00
2020-11-25 21:34:34 +00:00
Remove dead try/catch
2019-02-18 15:51:24 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector` ](../rules/DeadCode/Rector/TryCatch/RemoveDeadTryCatchRector.php )
2019-02-18 15:51:24 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run()
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
- try {
- // some code
- }
- catch (Throwable $throwable) {
- throw $throwable;
- }
2020-11-16 17:50:38 +00:00
}
}
2019-05-19 08:27:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-19 08:27:38 +00:00
2020-11-25 21:34:34 +00:00
### RemoveDeadZeroAndOneOperationRector
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
Remove operation with 1 and 0, that have no effect on the value
2019-08-05 21:10:47 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector` ](../rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php )
2019-08-05 21:10:47 +00:00
```diff
class SomeClass
{
public function run()
{
2020-11-25 21:34:34 +00:00
- $value = 5 * 1;
- $value = 5 + 0;
+ $value = 5;
+ $value = 5;
2019-08-05 21:10:47 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
### RemoveDelegatingParentCallRector
2019-07-06 19:51:36 +00:00
2020-11-25 21:34:34 +00:00
Removed dead parent call, that does not change anything
2019-07-06 19:51:36 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveDelegatingParentCallRector.php )
2020-06-16 11:39:38 +00:00
2020-11-25 21:34:34 +00:00
```diff
class SomeClass
{
- public function prettyPrint(array $stmts): string
- {
- return parent::prettyPrint($stmts);
- }
}
```
2019-07-06 19:51:36 +00:00
2020-11-25 21:34:34 +00:00
< br >
2019-07-06 19:51:36 +00:00
2020-11-25 21:34:34 +00:00
### RemoveDoubleAssignRector
2019-03-31 12:25:39 +00:00
2020-11-25 21:34:34 +00:00
Simplify useless double assigns
2019-03-31 12:25:39 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector` ](../rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php )
2019-03-31 12:25:39 +00:00
```diff
2020-11-25 21:34:34 +00:00
-$value = 1;
$value = 1;
```
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
< br >
### RemoveDuplicatedArrayKeyRector
2021-04-10 18:18:49 +00:00
Remove duplicated key in defined arrays.
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector` ](../rules/DeadCode/Rector/Array_/RemoveDuplicatedArrayKeyRector.php )
2020-11-25 21:34:34 +00:00
```diff
$item = [
- 1 => 'A',
1 => 'B'
];
2019-03-31 12:25:39 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-03-31 12:25:39 +00:00
2020-11-25 21:34:34 +00:00
### RemoveDuplicatedCaseInSwitchRector
2020-10-18 16:29:35 +00:00
2020-11-25 21:34:34 +00:00
2 following switch keys with identical will be reduced to one result
2020-10-18 16:29:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector` ](../rules/DeadCode/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php )
2020-10-18 16:29:35 +00:00
```diff
class SomeClass
{
2020-11-16 17:50:38 +00:00
public function run()
2020-10-18 16:29:35 +00:00
{
2020-11-25 21:34:34 +00:00
switch ($name) {
case 'clearHeader':
return $this->modifyHeader($node, 'remove');
case 'clearAllHeaders':
- return $this->modifyHeader($node, 'replace');
case 'clearRawHeaders':
return $this->modifyHeader($node, 'replace');
case '...':
return 5;
2020-11-16 17:50:38 +00:00
}
2020-10-18 16:29:35 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-18 16:29:35 +00:00
2020-11-25 21:34:34 +00:00
### RemoveDuplicatedIfReturnRector
2019-08-24 11:08:59 +00:00
2020-11-25 21:34:34 +00:00
Remove duplicated if stmt with return in function/method body
2019-08-24 11:08:59 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector` ](../rules/DeadCode/Rector/FunctionLike/RemoveDuplicatedIfReturnRector.php )
2019-08-24 11:08:59 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
2019-08-24 11:08:59 +00:00
{
2020-11-25 21:34:34 +00:00
public function run($value)
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
if ($value) {
return true;
2020-11-16 17:50:38 +00:00
}
2020-11-25 21:34:34 +00:00
$value2 = 100;
-
- if ($value) {
- return true;
- }
2020-11-16 17:50:38 +00:00
}
2019-08-24 11:08:59 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-08-24 11:08:59 +00:00
2020-11-25 21:34:34 +00:00
### RemoveDuplicatedInstanceOfRector
2018-12-25 19:55:16 +00:00
2020-11-25 21:34:34 +00:00
Remove duplicated instanceof in one call
2018-12-25 19:55:16 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\BinaryOp\RemoveDuplicatedInstanceOfRector` ](../rules/DeadCode/Rector/BinaryOp/RemoveDuplicatedInstanceOfRector.php )
2018-12-25 19:55:16 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2018-12-25 19:55:16 +00:00
{
2020-11-25 21:34:34 +00:00
- public function run($value)
+ public function run($value): void
2019-05-29 13:40:20 +00:00
{
2020-11-25 21:34:34 +00:00
- $isIt = $value instanceof A || $value instanceof A;
- $isIt = $value instanceof A && $value instanceof A;
+ $isIt = $value instanceof A;
+ $isIt = $value instanceof A;
2020-11-16 17:50:38 +00:00
}
2020-11-25 21:34:34 +00:00
}
```
2018-12-25 19:55:16 +00:00
2020-11-16 17:50:38 +00:00
< br >
2018-12-25 19:55:16 +00:00
2020-11-25 21:34:34 +00:00
### RemoveEmptyClassMethodRector
2018-12-25 19:55:16 +00:00
2021-04-10 18:18:49 +00:00
Remove empty class methods not required by parents
2018-12-25 19:55:16 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php )
2018-12-25 19:55:16 +00:00
```diff
2020-11-25 21:34:34 +00:00
class OrphanClass
2019-05-29 13:40:20 +00:00
{
2020-11-25 21:34:34 +00:00
- public function __construct()
- {
- }
2019-05-29 13:40:20 +00:00
}
2018-12-25 19:55:16 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-25 19:55:16 +00:00
2020-11-25 21:34:34 +00:00
### RemoveEmptyMethodCallRector
2019-08-17 13:06:02 +00:00
2020-11-25 21:34:34 +00:00
Remove empty method call
2019-08-17 13:06:02 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector` ](../rules/DeadCode/Rector/MethodCall/RemoveEmptyMethodCallRector.php )
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
```diff
class SomeClass
{
public function callThis()
{
}
}
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
-$some = new SomeClass();
-$some->callThis();
+$some = new SomeClass();
```
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
< br >
2020-11-16 17:50:38 +00:00
2021-07-22 18:40:24 +00:00
### RemoveLastReturnRector
Remove very last `return` that has no meaning
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveLastReturnRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveLastReturnRector.php )
```diff
function some_function($value)
{
if ($value === 1000) {
return;
}
if ($value) {
- return;
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveNonExistingVarAnnotationRector
Removes non-existing `@var` annotations above the code
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector` ](../rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php )
2021-03-23 22:13:35 +00:00
```diff
class SomeClass
{
public function get()
{
- /** @var Training[] $trainings */
return $this->getData();
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveNullPropertyInitializationRector
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
Remove initialization with null value from property declarations
2019-08-17 13:06:02 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\PropertyProperty\RemoveNullPropertyInitializationRector` ](../rules/DeadCode/Rector/PropertyProperty/RemoveNullPropertyInitializationRector.php )
2019-08-17 13:06:02 +00:00
2020-11-25 21:34:34 +00:00
```diff
class SunshineCommand extends ParentClassWithNewConstructor
2019-08-17 13:06:02 +00:00
{
2020-11-25 21:34:34 +00:00
- private $myVar = null;
+ private $myVar;
2019-08-17 13:06:02 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-08-17 13:06:02 +00:00
2020-11-25 21:34:34 +00:00
### RemoveOverriddenValuesRector
2019-10-04 14:59:42 +00:00
2020-11-25 21:34:34 +00:00
Remove initial assigns of overridden values
2019-10-04 14:59:42 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveOverriddenValuesRector` ](../rules/DeadCode/Rector/FunctionLike/RemoveOverriddenValuesRector.php )
2019-10-04 14:59:42 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeController
2019-10-04 14:59:42 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2019-10-04 14:59:42 +00:00
{
2020-11-25 21:34:34 +00:00
- $directories = [];
$possibleDirectories = [];
$directories = array_filter($possibleDirectories, 'file_exists');
2019-10-04 14:59:42 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-10-04 14:59:42 +00:00
2020-11-25 21:34:34 +00:00
### RemoveParentCallWithoutParentRector
2020-04-13 21:44:44 +00:00
2020-11-25 21:34:34 +00:00
Remove unused parent call with no parent class
2020-04-13 21:44:44 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector` ](../rules/DeadCode/Rector/StaticCall/RemoveParentCallWithoutParentRector.php )
2020-04-13 21:44:44 +00:00
```diff
2020-11-25 21:34:34 +00:00
class OrphanClass
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
public function __construct()
2020-04-13 21:44:44 +00:00
{
2020-11-25 21:34:34 +00:00
- parent::__construct();
2020-04-13 21:44:44 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-04-13 21:44:44 +00:00
2021-06-27 14:06:45 +00:00
### RemovePhpVersionIdCheckRector
2022-01-16 09:58:52 +00:00
Remove unneeded PHP_VERSION_ID check
2021-06-27 14:06:45 +00:00
:wrench: **configure it!**
- class: [`Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector` ](../rules/DeadCode/Rector/ConstFetch/RemovePhpVersionIdCheckRector.php )
```php
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemovePhpVersionIdCheckRector::class)
2022-02-18 01:47:01 +00:00
->configure([80000]);
2021-06-27 14:06:45 +00:00
};
```
↓
```diff
class SomeClass
{
public function run()
{
- if (PHP_VERSION_ID < 80000 ) {
- return;
- }
echo 'do something';
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveUnreachableStatementRector
2020-01-04 23:18:57 +00:00
2020-11-25 21:34:34 +00:00
Remove unreachable statements
2020-01-04 23:18:57 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector` ](../rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php )
2020-01-04 23:18:57 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
2020-01-04 23:18:57 +00:00
{
2020-11-16 17:50:38 +00:00
public function run()
2020-01-04 23:18:57 +00:00
{
2020-11-25 21:34:34 +00:00
return 5;
2020-01-04 23:18:57 +00:00
-
2020-11-25 21:34:34 +00:00
- $removeMe = 10;
2020-11-16 17:50:38 +00:00
}
2020-01-04 23:18:57 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-04 23:18:57 +00:00
2021-02-13 14:57:30 +00:00
### RemoveUnusedConstructorParamRector
Remove unused parameter in constructor
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php )
2021-02-13 14:57:30 +00:00
```diff
final class SomeClass
{
private $hey;
- public function __construct($hey, $man)
+ public function __construct($hey)
{
$this->hey = $hey;
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveUnusedForeachKeyRector
2020-03-25 18:00:26 +00:00
2021-04-10 18:18:49 +00:00
Remove unused key in foreach
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector` ](../rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php )
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
```diff
$items = [];
-foreach ($items as $key => $value) {
+foreach ($items as $value) {
$result = $value;
2020-03-25 18:00:26 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-03-25 18:00:26 +00:00
2020-11-25 21:34:34 +00:00
### RemoveUnusedNonEmptyArrayBeforeForeachRector
2019-02-21 14:36:16 +00:00
2020-11-25 21:34:34 +00:00
Remove unused if check to non-empty array before foreach of the array
2019-02-21 14:36:16 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector` ](../rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php )
2019-02-21 14:36:16 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-02-21 14:36:16 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2019-02-21 14:36:16 +00:00
{
2020-11-25 21:34:34 +00:00
$values = [];
- if ($values !== []) {
- foreach ($values as $value) {
- echo $value;
- }
+ foreach ($values as $value) {
+ echo $value;
}
2019-02-21 14:36:16 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-21 14:36:16 +00:00
2021-11-25 12:53:25 +00:00
### RemoveUnusedParamInRequiredAutowireRector
Remove unused parameter in required autowire method
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParamInRequiredAutowireRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveUnusedParamInRequiredAutowireRector.php )
```diff
use Symfony\Contracts\Service\Attribute\Required;
final class SomeService
{
private $visibilityManipulator;
#[Required]
2021-11-28 19:07:44 +00:00
- public function autowire(VisibilityManipulator $visibilityManipulator)
+ public function autowire()
2021-11-25 12:53:25 +00:00
{
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveUnusedPrivateClassConstantRector
2019-03-31 12:25:39 +00:00
2021-03-23 22:13:35 +00:00
Remove unused class constants
2019-03-31 12:25:39 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector` ](../rules/DeadCode/Rector/ClassConst/RemoveUnusedPrivateClassConstantRector.php )
2019-03-31 12:25:39 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
2019-03-31 12:25:39 +00:00
{
2021-03-23 22:13:35 +00:00
- private const SOME_CONST = 'dead';
-
public function run()
2019-06-02 07:45:37 +00:00
{
2020-11-16 17:50:38 +00:00
}
2019-03-31 12:25:39 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-03-31 12:25:39 +00:00
2021-04-25 09:44:34 +00:00
### RemoveUnusedPrivateMethodParameterRector
Remove unused parameter, if not required by interface or parent class
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodParameterRector.php )
2021-04-25 09:44:34 +00:00
```diff
class SomeClass
{
- private function run($value, $value2)
+ private function run($value)
{
$this->value = $value;
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### RemoveUnusedPrivateMethodRector
2020-02-23 21:14:24 +00:00
2020-11-25 21:34:34 +00:00
Remove unused private method
2020-02-23 21:14:24 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php )
2020-02-23 21:14:24 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeController
2020-02-23 21:14:24 +00:00
{
public function run()
{
2020-11-25 21:34:34 +00:00
return 5;
2020-02-23 21:14:24 +00:00
}
2020-11-25 21:34:34 +00:00
-
- private function skip()
- {
- return 10;
- }
2020-02-23 21:14:24 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-02-23 21:14:24 +00:00
2020-11-25 21:34:34 +00:00
### RemoveUnusedPrivatePropertyRector
2019-10-15 14:46:31 +00:00
2020-11-25 21:34:34 +00:00
Remove unused private properties
2019-10-15 14:46:31 +00:00
2022-03-26 19:22:15 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector` ](../rules/DeadCode/Rector/Property/RemoveUnusedPrivatePropertyRector.php )
2019-10-15 14:46:31 +00:00
2022-03-26 19:22:15 +00:00
```php
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveUnusedPrivatePropertyRector::class)
->configure([
RemoveUnusedPrivatePropertyRector::REMOVE_ASSIGN_SIDE_EFFECT => true,
]);
};
```
↓
2019-10-15 14:46:31 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
- private $property;
2019-10-15 14:46:31 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-10-15 14:46:31 +00:00
2021-06-27 14:06:45 +00:00
### RemoveUnusedPromotedPropertyRector
Remove unused promoted property
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php )
```diff
class SomeClass
{
public function __construct(
- private $someUnusedDependency,
private $usedDependency
) {
}
public function getUsedDependency()
{
return $this->usedDependency;
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveUnusedVariableAssignRector
2021-01-13 01:25:45 +00:00
2021-03-23 22:13:35 +00:00
Remove unused assigns to variables
2021-01-13 01:25:45 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector` ](../rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php )
2021-01-13 01:25:45 +00:00
```diff
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function run()
2021-01-13 01:25:45 +00:00
{
2021-03-23 22:13:35 +00:00
- $value = 5;
2021-01-13 01:25:45 +00:00
}
2021-03-23 22:13:35 +00:00
}
```
2021-01-13 01:25:45 +00:00
2021-03-23 22:13:35 +00:00
< br >
### RemoveUselessParamTagRector
Remove `@param` docblock with same type as parameter type
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveUselessParamTagRector.php )
2021-03-23 22:13:35 +00:00
```diff
class SomeClass
{
/**
- * @param string $a
* @param string $b description
*/
public function foo(string $a, string $b)
2021-01-13 01:25:45 +00:00
{
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveUselessReturnTagRector
2019-06-02 07:45:37 +00:00
2021-03-23 22:13:35 +00:00
Remove `@return` docblock with same type as defined in PHP
2019-06-02 07:45:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector` ](../rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php )
2019-06-02 07:45:37 +00:00
```diff
2021-03-23 22:13:35 +00:00
use stdClass;
2020-11-16 17:50:38 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
- /**
- * @return stdClass
- */
public function foo(): stdClass
2020-11-16 17:50:38 +00:00
{
}
}
```
2019-06-02 07:45:37 +00:00
2020-11-16 17:50:38 +00:00
< br >
2019-12-27 17:50:00 +00:00
2021-03-23 22:13:35 +00:00
### RemoveUselessVarTagRector
Remove unused `@var` annotation for properties
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector` ](../rules/DeadCode/Rector/Property/RemoveUselessVarTagRector.php )
2021-03-23 22:13:35 +00:00
```diff
final class SomeClass
{
- /**
- * @var string
- */
public string $name = 'name';
}
```
< br >
2020-11-25 21:34:34 +00:00
### SimplifyIfElseWithSameContentRector
2019-12-27 17:50:00 +00:00
2020-11-25 21:34:34 +00:00
Remove if/else if they have same content
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector` ](../rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php )
2019-12-27 17:50:00 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
public function run()
{
- if (true) {
- return 1;
- } else {
- return 1;
- }
+ return 1;
}
2019-12-27 17:50:00 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-14 04:24:29 +00:00
2020-11-25 21:34:34 +00:00
### SimplifyMirrorAssignRector
2020-10-14 04:24:29 +00:00
2020-11-25 21:34:34 +00:00
Removes unneeded $a = $a assigns
2020-10-14 04:24:29 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Expression\SimplifyMirrorAssignRector` ](../rules/DeadCode/Rector/Expression/SimplifyMirrorAssignRector.php )
2020-10-14 04:24:29 +00:00
```diff
2021-08-26 04:53:55 +00:00
function run() {
- $a = $a;
}
2020-10-14 04:24:29 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-14 04:24:29 +00:00
2020-11-25 21:34:34 +00:00
### TernaryToBooleanOrFalseToBooleanAndRector
2020-10-14 04:24:29 +00:00
2020-11-25 21:34:34 +00:00
Change ternary of bool : false to & & bool
2020-10-14 04:24:29 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector` ](../rules/DeadCode/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector.php )
2020-10-14 04:24:29 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
2020-10-14 04:24:29 +00:00
{
2020-11-25 21:34:34 +00:00
public function go()
2020-10-14 04:24:29 +00:00
{
2020-11-25 21:34:34 +00:00
- return $value ? $this->getBool() : false;
+ return $value & & $this->getBool();
}
private function getBool(): bool
{
return (bool) 5;
2020-10-14 04:24:29 +00:00
}
2020-11-16 17:50:38 +00:00
}
2020-10-14 04:24:29 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-14 04:24:29 +00:00
2021-01-30 20:30:37 +00:00
### UnwrapFutureCompatibleIfFunctionExistsRector
Remove functions exists if with else for always existing
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfFunctionExistsRector` ](../rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php )
2021-01-30 20:30:37 +00:00
```diff
class SomeClass
{
public function run()
{
// session locking trough other addons
- if (function_exists('session_abort')) {
- session_abort();
- } else {
- session_write_close();
- }
+ session_abort();
}
}
```
< br >
### UnwrapFutureCompatibleIfPhpVersionRector
Remove php version checks if they are passed
2021-05-23 09:33:26 +00:00
- class: [`Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector` ](../rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php )
2021-01-30 20:30:37 +00:00
```diff
// current PHP: 7.2
-if (version_compare(PHP_VERSION, '7.2', '< ')) {
- return 'is PHP 7.1-';
-} else {
- return 'is PHP 7.2+';
-}
+return 'is PHP 7.2+';
```
< br >
2021-01-01 18:59:23 +00:00
## DependencyInjection
2019-09-21 22:14:49 +00:00
2021-02-13 14:57:30 +00:00
### ActionInjectionToConstructorInjectionRector
Turns action injection in Controllers to constructor injection
2021-05-23 09:33:26 +00:00
- class: [`Rector\DependencyInjection\Rector\Class_\ActionInjectionToConstructorInjectionRector` ](../rules/DependencyInjection/Rector/Class_/ActionInjectionToConstructorInjectionRector.php )
2021-02-13 14:57:30 +00:00
```diff
final class SomeController
{
- public function default(ProductRepository $productRepository)
2021-11-25 18:41:12 +00:00
+ public function __construct(
+ private ProductRepository $productRepository
+ ) {
2021-02-13 14:57:30 +00:00
+ }
+
+ public function default()
2021-11-25 18:41:12 +00:00
{
- $products = $productRepository->fetchAll();
2021-02-13 14:57:30 +00:00
+ $products = $this->productRepository->fetchAll();
}
}
```
< br >
2021-02-20 23:21:19 +00:00
### AddMethodParentCallRector
Add method parent call, in case new parent method is added
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\DependencyInjection\Rector\ClassMethod\AddMethodParentCallRector` ](../rules/DependencyInjection/Rector/ClassMethod/AddMethodParentCallRector.php )
2021-02-20 23:21:19 +00:00
```php
use Rector\DependencyInjection\Rector\ClassMethod\AddMethodParentCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-20 23:21:19 +00:00
$services->set(AddMethodParentCallRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'ParentClassWithNewConstructor' => '__construct',
]);
2021-02-20 23:21:19 +00:00
};
```
↓
```diff
class SunshineCommand extends ParentClassWithNewConstructor
{
public function __construct()
{
$value = 5;
+
+ parent::__construct();
}
}
```
< br >
2021-05-09 00:15:50 +00:00
## DowngradePhp53
2021-04-18 13:01:56 +00:00
2021-05-09 00:15:50 +00:00
### DirConstToFileConstRector
2021-04-18 13:01:56 +00:00
2021-05-09 00:15:50 +00:00
Refactor __DIR__ to dirname(__FILE__)
2021-04-18 13:01:56 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp53\Rector\Dir\DirConstToFileConstRector` ](../rules/DowngradePhp53/Rector/Dir/DirConstToFileConstRector.php )
2021-04-18 13:01:56 +00:00
```diff
2021-05-09 00:15:50 +00:00
final class SomeClass
2021-04-18 13:01:56 +00:00
{
2021-05-09 00:15:50 +00:00
public function run()
2021-04-18 13:01:56 +00:00
{
2021-05-09 00:15:50 +00:00
- return __DIR__ ;
+ return dirname(__FILE__);
2021-04-18 13:01:56 +00:00
}
}
```
< br >
2021-10-24 00:34:27 +00:00
## DowngradePhp54
2021-11-14 00:36:15 +00:00
### DowngradeBinaryNotationRector
Downgrade binary notation for integers
- class: [`Rector\DowngradePhp54\Rector\LNumber\DowngradeBinaryNotationRector` ](../rules/DowngradePhp54/Rector/LNumber/DowngradeBinaryNotationRector.php )
```diff
-$a = 0b11111100101;
+$a = 2021;
```
< br >
### DowngradeCallableTypeDeclarationRector
Remove the "callable" param type, add a `@param` tag instead
- class: [`Rector\DowngradePhp54\Rector\FunctionLike\DowngradeCallableTypeDeclarationRector` ](../rules/DowngradePhp54/Rector/FunctionLike/DowngradeCallableTypeDeclarationRector.php )
```diff
class SomeClass
{
- public function someFunction(callable $callback)
+ /**
+ * @param callable $callback
+ */
+ public function someFunction($callback)
{
}
}
```
< br >
2021-11-21 00:34:01 +00:00
### DowngradeIndirectCallByArrayRector
Downgrade indirect method call by array variable
- class: [`Rector\DowngradePhp54\Rector\FuncCall\DowngradeIndirectCallByArrayRector` ](../rules/DowngradePhp54/Rector/FuncCall/DowngradeIndirectCallByArrayRector.php )
```diff
class Hello {
public static function world($x) {
echo "Hello, $x\n";
}
}
$func = array('Hello','world');
-$func('you');
+call_user_func($func, 'you');
```
< br >
2021-11-14 00:36:15 +00:00
### DowngradeInstanceMethodCallRector
Downgrade instance and method call/property access
- class: [`Rector\DowngradePhp54\Rector\MethodCall\DowngradeInstanceMethodCallRector` ](../rules/DowngradePhp54/Rector/MethodCall/DowngradeInstanceMethodCallRector.php )
```diff
-echo (new \ReflectionClass('\\stdClass'))->getName();
+$object = new \ReflectionClass('\\stdClass');
+echo $object->getName();
```
< br >
2021-10-24 00:34:27 +00:00
### DowngradeStaticClosureRector
Remove static from closure
- class: [`Rector\DowngradePhp54\Rector\Closure\DowngradeStaticClosureRector` ](../rules/DowngradePhp54/Rector/Closure/DowngradeStaticClosureRector.php )
```diff
final class SomeClass
{
public function run()
{
- return static function () {
+ return function () {
return true;
};
}
}
```
< br >
2022-04-02 15:46:27 +00:00
### 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 >
2021-11-14 00:36:15 +00:00
### ShortArrayToLongArrayRector
Replace short arrays by long arrays
- class: [`Rector\DowngradePhp54\Rector\Array_\ShortArrayToLongArrayRector` ](../rules/DowngradePhp54/Rector/Array_/ShortArrayToLongArrayRector.php )
```diff
-$a = [1, 2, 3];
+$a = array(1, 2, 3);
```
< br >
## DowngradePhp55
2021-11-21 00:34:01 +00:00
### DowngradeArbitraryExpressionArgsToEmptyAndIssetRector
Downgrade arbitrary expression arguments to `empty()` and `isset()`
- class: [`Rector\DowngradePhp55\Rector\Isset_\DowngradeArbitraryExpressionArgsToEmptyAndIssetRector` ](../rules/DowngradePhp55/Rector/Isset_/DowngradeArbitraryExpressionArgsToEmptyAndIssetRector.php )
```diff
-if (isset(some_function())) {
+if (some_function() !== null) {
// ...
}
```
< br >
2021-11-25 12:53:25 +00:00
### DowngradeBoolvalRector
Replace `boolval()` by type casting to boolean
- class: [`Rector\DowngradePhp55\Rector\FuncCall\DowngradeBoolvalRector` ](../rules/DowngradePhp55/Rector/FuncCall/DowngradeBoolvalRector.php )
```diff
-$bool = boolval($value);
+$bool = (bool) $value;
```
< br >
2021-11-14 00:36:15 +00:00
### DowngradeClassConstantToStringRector
Replace < class > ::class constant by string class names
- class: [`Rector\DowngradePhp55\Rector\ClassConstFetch\DowngradeClassConstantToStringRector` ](../rules/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector.php )
```diff
class AnotherClass
{
}
class SomeClass
{
public function run()
{
- return \AnotherClass::class;
+ return 'AnotherClass';
}
}
```
< br >
2021-11-21 00:34:01 +00:00
### DowngradeForeachListRector
Downgrade `list()` support in foreach constructs
- class: [`Rector\DowngradePhp55\Rector\Foreach_\DowngradeForeachListRector` ](../rules/DowngradePhp55/Rector/Foreach_/DowngradeForeachListRector.php )
```diff
-foreach ($array as $key => list($item1, $item2)) {
+foreach ($array as $key => arrayItem) {
+ list($item1, $item2) = $arrayItem;
var_dump($item1, $item2);
}
```
< br >
2021-11-08 19:40:36 +00:00
## DowngradePhp56
2021-11-14 00:36:15 +00:00
### DowngradeArgumentUnpackingRector
Replace argument unpacking by `call_user_func_array()`
- class: [`Rector\DowngradePhp56\Rector\CallLike\DowngradeArgumentUnpackingRector` ](../rules/DowngradePhp56/Rector/CallLike/DowngradeArgumentUnpackingRector.php )
```diff
class SomeClass
{
public function run(array $items)
{
- some_function(...$items);
+ call_user_func_array('some_function', $items);
}
}
```
< br >
2022-01-02 00:31:07 +00:00
### DowngradeArrayFilterUseConstantRector
Replace use ARRAY_FILTER_USE_BOTH and ARRAY_FILTER_USE_KEY to loop to filter it
- class: [`Rector\DowngradePhp56\Rector\FuncCall\DowngradeArrayFilterUseConstantRector` ](../rules/DowngradePhp56/Rector/FuncCall/DowngradeArrayFilterUseConstantRector.php )
```diff
$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
-var_dump(array_filter($arr, function($v, $k) {
- return $k == 'b' || $v == 4;
-}, ARRAY_FILTER_USE_BOTH));
+$result = [];
+foreach ($arr as $k => $v) {
+ if ($v === 4 || $k === 'b') {
+ $result[$k] = $v;
+ }
+}
+
+var_dump($result);
```
< br >
2021-11-08 19:40:36 +00:00
### DowngradeExponentialAssignmentOperatorRector
Remove exponential assignment operator ** =
- class: [`Rector\DowngradePhp56\Rector\Pow\DowngradeExponentialAssignmentOperatorRector` ](../rules/DowngradePhp56/Rector/Pow/DowngradeExponentialAssignmentOperatorRector.php )
```diff
-$a ** = 3;
+$a = pow($a, 3);
```
< br >
### DowngradeExponentialOperatorRector
Changes ** (exp) operator to pow(val, val2)
- class: [`Rector\DowngradePhp56\Rector\Pow\DowngradeExponentialOperatorRector` ](../rules/DowngradePhp56/Rector/Pow/DowngradeExponentialOperatorRector.php )
```diff
-1**2;
+pow(1, 2);
```
< br >
2021-11-25 12:53:25 +00:00
### DowngradeUseFunctionRector
Replace imports of functions and constants
- class: [`Rector\DowngradePhp56\Rector\Use_\DowngradeUseFunctionRector` ](../rules/DowngradePhp56/Rector/Use_/DowngradeUseFunctionRector.php )
```diff
-use function Foo\Bar\baz;
-
-$var = baz();
+$var = \Foo\Bar\baz();
```
< br >
2021-03-23 22:13:35 +00:00
## DowngradePhp70
2021-01-01 18:59:23 +00:00
2021-04-18 13:01:56 +00:00
### DowngradeAnonymousClassRector
Remove anonymous class
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp70\Rector\New_\DowngradeAnonymousClassRector` ](../rules/DowngradePhp70/Rector/New_/DowngradeAnonymousClassRector.php )
2021-04-18 13:01:56 +00:00
```diff
+class Anonymous
+{
+ public function execute()
+ {
+ }
+}
class SomeClass
{
public function run()
{
- return new class {
- public function execute()
- {
- }
- };
+ return new Anonymous();
}
}
```
< br >
2022-01-09 10:54:09 +00:00
### DowngradeCatchThrowableRector
Make catch clauses catching `Throwable` also catch `Exception` to support exception hierarchies in PHP 5.
- class: [`Rector\DowngradePhp70\Rector\TryCatch\DowngradeCatchThrowableRector` ](../rules/DowngradePhp70/Rector/TryCatch/DowngradeCatchThrowableRector.php )
```diff
try {
// Some code...
} catch (\Throwable $exception) {
handle();
+} catch (\Exception $exception) {
+ handle();
}
```
< br >
2021-11-26 20:07:54 +00:00
### DowngradeClosureCallRector
Replace `Closure::call()` by `Closure::bindTo()`
- class: [`Rector\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector` ](../rules/DowngradePhp70/Rector/MethodCall/DowngradeClosureCallRector.php )
```diff
-$closure->call($newObj, ...$args);
+call_user_func($closure->bindTo($newObj, $newObj), ...$args);
```
< br >
2021-05-02 08:26:23 +00:00
### DowngradeDefineArrayConstantRector
Change array contant definition via define to const
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp70\Rector\Expression\DowngradeDefineArrayConstantRector` ](../rules/DowngradePhp70/Rector/Expression/DowngradeDefineArrayConstantRector.php )
2021-05-02 08:26:23 +00:00
```diff
-define('ANIMALS', [
+const ANIMALS = [
'dog',
'cat',
'bird'
-]);
+];
```
< br >
2021-11-14 00:36:15 +00:00
### DowngradeDirnameLevelsRector
Replace the 2nd argument of `dirname()`
- class: [`Rector\DowngradePhp70\Rector\FuncCall\DowngradeDirnameLevelsRector` ](../rules/DowngradePhp70/Rector/FuncCall/DowngradeDirnameLevelsRector.php )
```diff
-return dirname($path, 2);
+return dirname(dirname($path));
```
< br >
2021-05-09 00:15:50 +00:00
### DowngradeGeneratedScalarTypesRector
Refactor scalar types in PHP code in string snippets, e.g. generated container code from symfony/dependency-injection
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp70\Rector\String_\DowngradeGeneratedScalarTypesRector` ](../rules/DowngradePhp70/Rector/String_/DowngradeGeneratedScalarTypesRector.php )
2021-05-09 00:15:50 +00:00
```diff
$code = < < < 'EOF'
- public function getParameter(string $name)
+ /**
2021-08-01 00:25:55 +00:00
+ * @param string $name
2021-05-09 00:15:50 +00:00
+ */
+ public function getParameter($name)
{
return $name;
}
EOF;
```
< br >
2022-01-09 10:54:09 +00:00
### DowngradeInstanceofThrowableRector
Add `instanceof Exception` check as a fallback to `instanceof Throwable` to support exception hierarchies in PHP 5
- class: [`Rector\DowngradePhp70\Rector\Instanceof_\DowngradeInstanceofThrowableRector` ](../rules/DowngradePhp70/Rector/Instanceof_/DowngradeInstanceofThrowableRector.php )
```diff
-return $e instanceof \Throwable;
+return ($throwable = $e) instanceof \Throwable || $throwable instanceof \Exception;
```
< br >
2021-12-26 00:38:36 +00:00
### DowngradeMethodCallOnCloneRector
Replace (clone `$obj)->call()` to object assign and call
- class: [`Rector\DowngradePhp70\Rector\MethodCall\DowngradeMethodCallOnCloneRector` ](../rules/DowngradePhp70/Rector/MethodCall/DowngradeMethodCallOnCloneRector.php )
```diff
-(clone $this)->execute();
+$object = (clone $this);
+$object->execute();
```
< br >
2021-04-25 09:44:34 +00:00
### DowngradeNullCoalesceRector
Change null coalesce to isset ternary check
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp70\Rector\Coalesce\DowngradeNullCoalesceRector` ](../rules/DowngradePhp70/Rector/Coalesce/DowngradeNullCoalesceRector.php )
2021-04-25 09:44:34 +00:00
```diff
-$username = $_GET['user'] ?? 'nobody';
+$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
```
< br >
2021-08-26 04:53:55 +00:00
### DowngradeParentTypeDeclarationRector
Remove "parent" return type, add a `"@return` parent" tag instead
- class: [`Rector\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector` ](../rules/DowngradePhp70/Rector/ClassMethod/DowngradeParentTypeDeclarationRector.php )
```diff
class ParentClass
{
}
class SomeClass extends ParentClass
{
- public function foo(): parent
+ /**
+ * @return parent
+ */
+ public function foo()
{
return $this;
}
}
```
< br >
2021-05-09 00:15:50 +00:00
### DowngradeScalarTypeDeclarationRector
Remove the type params and return type, add `@param` and `@return` tags instead
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRector` ](../rules/DowngradePhp70/Rector/FunctionLike/DowngradeScalarTypeDeclarationRector.php )
2021-05-09 00:15:50 +00:00
```diff
class SomeClass
{
- public function run(string $input): string
+ /**
+ * @param string $input
+ * @return string
+ */
+ public function run($input)
{
}
}
```
< br >
2021-08-08 20:04:42 +00:00
### DowngradeSelfTypeDeclarationRector
2022-01-02 00:31:07 +00:00
Remove "self" return type, add a `"@return` `$this"` tag instead
2021-08-08 20:04:42 +00:00
- class: [`Rector\DowngradePhp70\Rector\ClassMethod\DowngradeSelfTypeDeclarationRector` ](../rules/DowngradePhp70/Rector/ClassMethod/DowngradeSelfTypeDeclarationRector.php )
```diff
class SomeClass
{
- public function foo(): self
2022-01-02 00:31:07 +00:00
+ /**
+ * @return $this
+ */
2021-08-08 20:04:42 +00:00
+ public function foo()
{
return $this;
}
}
```
< br >
2021-05-09 00:15:50 +00:00
### DowngradeSessionStartArrayOptionsRector
Move array option of session_start($options) to before statement's `ini_set()`
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp70\Rector\FuncCall\DowngradeSessionStartArrayOptionsRector` ](../rules/DowngradePhp70/Rector/FuncCall/DowngradeSessionStartArrayOptionsRector.php )
2021-05-09 00:15:50 +00:00
```diff
-session_start([
- 'cache_limiter' => 'private',
-]);
+ini_set('session.cache_limiter', 'private');
+session_start();
```
< br >
2021-05-02 08:26:23 +00:00
### DowngradeSpaceshipRector
Change spaceship with check equal, and ternary to result 0, -1, 1
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp70\Rector\Spaceship\DowngradeSpaceshipRector` ](../rules/DowngradePhp70/Rector/Spaceship/DowngradeSpaceshipRector.php )
2021-05-02 08:26:23 +00:00
```diff
-return $a < => $b;
+$battleShipcompare = function ($left, $right) {
+ if ($left === $right) {
+ return 0;
+ }
+ return $left < $right ? -1 : 1;
+};
+return $battleShipcompare($a, $b);
```
< br >
2021-04-18 13:01:56 +00:00
### DowngradeStrictTypeDeclarationRector
Remove the declare(strict_types=1)
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector` ](../rules/DowngradePhp70/Rector/Declare_/DowngradeStrictTypeDeclarationRector.php )
2021-04-18 13:01:56 +00:00
```diff
-declare(strict_types=1);
2021-08-26 04:53:55 +00:00
echo 'something';
2021-04-18 13:01:56 +00:00
```
< br >
2022-01-02 00:31:07 +00:00
### DowngradeThrowableTypeDeclarationRector
Replace `Throwable` type hints by PHPDoc tags
- class: [`Rector\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector` ](../rules/DowngradePhp70/Rector/FunctionLike/DowngradeThrowableTypeDeclarationRector.php )
```diff
class SomeClass
{
- public function foo(\Throwable $e): ?\Throwable
+ /**
+ * @param \Throwable $e
+ * @return \Throwable|null
+ */
+ public function foo($e)
{
return new \Exception("Troubles");
}
}
```
< br >
2022-01-09 10:54:09 +00:00
### DowngradeUncallableValueCallToCallUserFuncRector
Downgrade calling a value that is not directly callable in PHP 5 (property, static property, closure, …) to call_user_func.
- class: [`Rector\DowngradePhp70\Rector\FuncCall\DowngradeUncallableValueCallToCallUserFuncRector` ](../rules/DowngradePhp70/Rector/FuncCall/DowngradeUncallableValueCallToCallUserFuncRector.php )
```diff
final class Foo
{
/** @var callable */
public $handler;
/** @var callable */
public static $staticHandler;
}
$foo = new Foo;
-($foo->handler)(/* args */);
-($foo::$staticHandler)(41);
+call_user_func($foo->handler, /* args */);
+call_user_func($foo::$staticHandler, 41);
-(function() { /* … */ })();
+call_user_func(function() { /* … */ });
```
< br >
2022-01-26 10:10:51 +00:00
### DowngradeUnnecessarilyParenthesizedExpressionRector
Remove parentheses around expressions allowed by Uniform variable syntax RFC where they are not necessary to prevent parse errors on PHP 5.
- class: [`Rector\DowngradePhp70\Rector\Expr\DowngradeUnnecessarilyParenthesizedExpressionRector` ](../rules/DowngradePhp70/Rector/Expr/DowngradeUnnecessarilyParenthesizedExpressionRector.php )
```diff
-($f)['foo'];
-($f)->foo;
-($f)->foo();
-($f)::$foo;
-($f)::foo();
-($f)();
+$f['foo'];
+$f->foo;
+$f->foo();
+$f::$foo;
+$f::foo();
+$f();
```
< br >
2021-05-09 00:15:50 +00:00
### SplitGroupedUseImportsRector
2021-01-01 18:59:23 +00:00
2021-05-09 00:15:50 +00:00
Refactor grouped use imports to standalone lines
2021-01-01 18:59:23 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp70\Rector\GroupUse\SplitGroupedUseImportsRector` ](../rules/DowngradePhp70/Rector/GroupUse/SplitGroupedUseImportsRector.php )
2021-01-01 18:59:23 +00:00
```diff
2021-05-09 00:15:50 +00:00
-use SomeNamespace\{
- First,
- Second
-};
+use SomeNamespace\First;
+use SomeNamespace\Second;
2021-01-01 18:59:23 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
## DowngradePhp71
2020-02-09 20:57:40 +00:00
2021-03-23 22:13:35 +00:00
### DowngradeClassConstantVisibilityRector
2021-03-12 23:52:38 +00:00
2021-03-23 22:13:35 +00:00
Downgrade class constant visibility
2020-02-09 20:57:40 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp71\Rector\ClassConst\DowngradeClassConstantVisibilityRector` ](../rules/DowngradePhp71/Rector/ClassConst/DowngradeClassConstantVisibilityRector.php )
2020-02-09 20:57:40 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
- public const PUBLIC_CONST_B = 2;
- protected const PROTECTED_CONST = 3;
- private const PRIVATE_CONST = 4;
+ const PUBLIC_CONST_B = 2;
+ const PROTECTED_CONST = 3;
+ const PRIVATE_CONST = 4;
}
2020-02-09 20:57:40 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-02-09 20:57:40 +00:00
2021-09-05 00:37:33 +00:00
### DowngradeClosureFromCallableRector
Converts `Closure::fromCallable()` to compatible alternative.
- class: [`Rector\DowngradePhp71\Rector\StaticCall\DowngradeClosureFromCallableRector` ](../rules/DowngradePhp71/Rector/StaticCall/DowngradeClosureFromCallableRector.php )
```diff
-\Closure::fromCallable('callable');
+$callable = 'callable';
+function () use ($callable) {
+ return $callable(...func_get_args());
+};
```
< br >
2021-04-25 09:44:34 +00:00
### DowngradeIsIterableRector
Change is_iterable with array and Traversable object type check
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp71\Rector\FuncCall\DowngradeIsIterableRector` ](../rules/DowngradePhp71/Rector/FuncCall/DowngradeIsIterableRector.php )
2021-04-25 09:44:34 +00:00
```diff
class SomeClass
{
public function run($obj)
{
- is_iterable($obj);
+ is_array($obj) || $obj instanceof \Traversable;
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeIterablePseudoTypeDeclarationRector
2020-08-16 12:39:59 +00:00
2021-03-23 22:13:35 +00:00
Remove the iterable pseudo type params and returns, add `@param` and `@return` tags instead
2020-08-16 12:39:59 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeIterablePseudoTypeDeclarationRector` ](../rules/DowngradePhp71/Rector/FunctionLike/DowngradeIterablePseudoTypeDeclarationRector.php )
2020-08-16 12:39:59 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-08-16 12:39:59 +00:00
{
2021-03-23 22:13:35 +00:00
- public function run(iterable $iterator): iterable
+ /**
+ * @param mixed[]|\Traversable $iterator
+ * @return mixed[]|\Traversable
+ */
+ public function run($iterator)
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
// do something
2020-11-16 17:50:38 +00:00
}
2020-08-16 12:39:59 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-16 12:39:59 +00:00
2021-04-25 09:44:34 +00:00
### DowngradeKeysInListRector
Extract keys in list to its own variable assignment
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp71\Rector\List_\DowngradeKeysInListRector` ](../rules/DowngradePhp71/Rector/List_/DowngradeKeysInListRector.php )
2021-04-25 09:44:34 +00:00
```diff
class SomeClass
{
public function run(): void
{
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
- list("id" => $id1, "name" => $name1) = $data[0];
+ $id1 = $data[0]["id"];
+ $name1 = $data[0]["name"];
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeNegativeStringOffsetToStrlenRector
2020-10-18 16:29:35 +00:00
2021-04-10 18:18:49 +00:00
Downgrade negative string offset to strlen
2020-10-18 16:29:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp71\Rector\String_\DowngradeNegativeStringOffsetToStrlenRector` ](../rules/DowngradePhp71/Rector/String_/DowngradeNegativeStringOffsetToStrlenRector.php )
2020-10-18 16:29:35 +00:00
```diff
2021-03-23 22:13:35 +00:00
-echo 'abcdef'[-2];
-echo strpos('aabbcc', 'b', -3);
-echo strpos($var, 'b', -3);
+echo 'abcdef'[strlen('abcdef') - 2];
+echo strpos('aabbcc', 'b', strlen('aabbcc') - 3);
+echo strpos($var, 'b', strlen($var) - 3);
2020-10-18 16:29:35 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-18 16:29:35 +00:00
2021-03-23 22:13:35 +00:00
### DowngradeNullableTypeDeclarationRector
2021-01-30 20:30:37 +00:00
2021-03-23 22:13:35 +00:00
Remove the nullable type params, add `@param` tags instead
2021-01-30 20:30:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeDeclarationRector` ](../rules/DowngradePhp71/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector.php )
2021-01-30 20:30:37 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2021-01-30 20:30:37 +00:00
{
2021-03-23 22:13:35 +00:00
- public function run(?string $input): ?string
+ /**
+ * @param string|null $input
2021-08-01 00:25:55 +00:00
+ * @return string|null
2021-03-23 22:13:35 +00:00
+ */
+ public function run($input)
2021-01-30 20:30:37 +00:00
{
}
}
```
< br >
2022-02-13 00:29:38 +00:00
### DowngradePhp71JsonConstRector
Remove Json constant that available only in php 7.1
- class: [`Rector\DowngradePhp71\Rector\ConstFetch\DowngradePhp71JsonConstRector` ](../rules/DowngradePhp71/Rector/ConstFetch/DowngradePhp71JsonConstRector.php )
```diff
-json_encode($content, JSON_UNESCAPED_LINE_TERMINATORS);
+json_encode($content, 0);
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradePipeToMultiCatchExceptionRector
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
Downgrade single one | separated to multi catch exception
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp71\Rector\TryCatch\DowngradePipeToMultiCatchExceptionRector` ](../rules/DowngradePhp71/Rector/TryCatch/DowngradePipeToMultiCatchExceptionRector.php )
2020-11-16 17:50:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
try {
// Some code...
2021-08-01 00:25:55 +00:00
-} catch (ExceptionType1 | ExceptionType2 $exception) {
2021-03-23 22:13:35 +00:00
+} catch (ExceptionType1 $exception) {
+ $sameCode;
2021-08-01 00:25:55 +00:00
+} catch (ExceptionType2 $exception) {
$sameCode;
}
2021-03-23 22:13:35 +00:00
```
< br >
### DowngradeVoidTypeDeclarationRector
Remove "void" return type, add a `"@return` void" tag instead
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeDeclarationRector` ](../rules/DowngradePhp71/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector.php )
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
```diff
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- public function run(): void
2020-11-25 21:34:34 +00:00
+ /**
2021-03-23 22:13:35 +00:00
+ * @return void
2020-11-25 21:34:34 +00:00
+ */
2021-03-23 22:13:35 +00:00
+ public function run()
2020-11-25 21:34:34 +00:00
{
}
}
2020-08-16 12:39:59 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-16 12:39:59 +00:00
2021-03-23 22:13:35 +00:00
### SymmetricArrayDestructuringToListRector
2020-08-16 12:39:59 +00:00
2021-03-23 22:13:35 +00:00
Downgrade Symmetric array destructuring to `list()` function
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp71\Rector\Array_\SymmetricArrayDestructuringToListRector` ](../rules/DowngradePhp71/Rector/Array_/SymmetricArrayDestructuringToListRector.php )
2020-08-16 12:39:59 +00:00
```diff
2021-03-23 22:13:35 +00:00
-[$id1, $name1] = $data;
+list($id1, $name1) = $data;
2020-08-16 12:39:59 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-16 12:39:59 +00:00
2021-03-23 22:13:35 +00:00
## DowngradePhp72
2020-08-16 12:39:59 +00:00
2022-01-26 10:10:51 +00:00
### DowngradeJsonDecodeNullAssociativeArgRector
Downgrade `json_decode()` with null associative argument function
- class: [`Rector\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector` ](../rules/DowngradePhp72/Rector/FuncCall/DowngradeJsonDecodeNullAssociativeArgRector.php )
```diff
function exactlyNull(string $json)
{
- $value = json_decode($json, null);
+ $value = json_decode($json, true);
}
function possiblyNull(string $json, ?bool $associative)
{
2022-02-13 00:29:38 +00:00
- $value = json_decode($json, $associative);
+ $value = json_decode($json, $associative === null ?: $associative);
2022-01-26 10:10:51 +00:00
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeObjectTypeDeclarationRector
2020-08-16 12:39:59 +00:00
2021-03-23 22:13:35 +00:00
Remove the "object" param and return type, add a `@param` and `@return` tags instead
2020-08-16 12:39:59 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector` ](../rules/DowngradePhp72/Rector/FunctionLike/DowngradeObjectTypeDeclarationRector.php )
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
```diff
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- public function someFunction(object $someObject): object
+ /**
+ * @param object $someObject
+ * @return object
+ */
+ public function someFunction($someObject)
2020-11-16 17:50:38 +00:00
{
}
2020-08-16 12:39:59 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-16 12:39:59 +00:00
2021-03-23 22:13:35 +00:00
### DowngradeParameterTypeWideningRector
2020-09-08 10:00:38 +00:00
2021-03-23 22:13:35 +00:00
Change param type to match the lowest type in whole family tree
2020-09-08 10:00:38 +00:00
2021-07-22 18:40:24 +00:00
:wrench: **configure it!**
- class: [`Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector` ](../rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php )
```php
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeParameterTypeWideningRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'ContainerInterface' => ['set', 'get', 'has', 'initialized'],
'SomeContainerInterface' => ['set', 'has'],
]);
2021-07-22 18:40:24 +00:00
};
```
↓
2020-09-08 10:00:38 +00:00
```diff
2021-04-25 09:44:34 +00:00
interface SomeInterface
2020-11-25 21:34:34 +00:00
{
2021-04-25 09:44:34 +00:00
- public function test(array $input);
+ /**
+ * @param mixed[] $input
+ */
+ public function test($input);
2020-09-08 10:00:38 +00:00
}
2020-11-18 01:11:35 +00:00
2021-04-25 09:44:34 +00:00
final class SomeClass implements SomeInterface
2020-11-25 21:34:34 +00:00
{
2021-04-25 09:44:34 +00:00
public function test($input)
{
}
2020-11-25 21:34:34 +00:00
}
```
2020-11-18 01:11:35 +00:00
2020-11-25 21:34:34 +00:00
< br >
2020-11-18 01:11:35 +00:00
2022-02-06 08:25:55 +00:00
### DowngradePhp72JsonConstRector
2022-02-13 00:29:38 +00:00
Remove Json constant that available only in php 7.2
2022-02-06 08:25:55 +00:00
- class: [`Rector\DowngradePhp72\Rector\ConstFetch\DowngradePhp72JsonConstRector` ](../rules/DowngradePhp72/Rector/ConstFetch/DowngradePhp72JsonConstRector.php )
```diff
-$inDecoder = new Decoder($connection, true, 512, \JSON_INVALID_UTF8_IGNORE);
2022-02-13 00:29:38 +00:00
-$inDecoder = new Decoder($connection, true, 512, \JSON_INVALID_UTF8_SUBSTITUTE);
+$inDecoder = new Decoder($connection, true, 512, 0);
2022-02-06 08:25:55 +00:00
+$inDecoder = new Decoder($connection, true, 512, 0);
```
< br >
2021-04-10 18:18:49 +00:00
### DowngradePregUnmatchedAsNullConstantRector
Remove PREG_UNMATCHED_AS_NULL from preg_match and set null value on empty string matched on each match
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector` ](../rules/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector.php )
2021-04-10 18:18:49 +00:00
```diff
class SomeClass
{
public function run()
{
- preg_match('/(a)(b)*(c)/', 'ac', $matches, PREG_UNMATCHED_AS_NULL);
+ preg_match('/(a)(b)*(c)/', 'ac', $matches);
+ array_walk_recursive($matches, function (& $value) {
+ if ($value === '') {
+ $value = null;
+ }
+ });
}
}
```
< br >
2021-05-09 00:15:50 +00:00
### DowngradeStreamIsattyRector
Downgrade `stream_isatty()` function
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector` ](../rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php )
2021-05-09 00:15:50 +00:00
```diff
class SomeClass
{
public function run($stream)
{
- $isStream = stream_isatty($stream);
+ $streamIsatty = function ($stream) {
2021-05-23 09:33:26 +00:00
+ if (\function_exists('stream_isatty')) {
+ return stream_isatty($stream);
+ }
+
+ if (!\is_resource($stream)) {
+ trigger_error('stream_isatty() expects parameter 1 to be resource, '.\gettype($stream).' given', \E_USER_WARNING);
+
+ return false;
+ }
+
2021-05-09 00:15:50 +00:00
+ if ('\\' === \DIRECTORY_SEPARATOR) {
+ $stat = @fstat ($stream);
2021-05-23 09:33:26 +00:00
+ // Check if formatted mode is S_IFCHR
+ return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
2021-05-09 00:15:50 +00:00
+ }
2021-05-23 09:33:26 +00:00
+
+ return \function_exists('posix_isatty') && @posix_isatty ($stream);
2021-05-09 00:15:50 +00:00
+ };
+ $isStream = $streamIsatty($stream);
}
}
```
< br >
2021-03-23 22:13:35 +00:00
## DowngradePhp73
2020-11-18 01:11:35 +00:00
2021-05-09 00:15:50 +00:00
### DowngradeArrayKeyFirstLastRector
Downgrade `array_key_first()` and `array_key_last()` functions
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector` ](../rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php )
2021-05-09 00:15:50 +00:00
```diff
class SomeClass
{
public function run($items)
{
- $firstItemKey = array_key_first($items);
+ reset($items);
+ $firstItemKey = key($items);
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeFlexibleHeredocSyntaxRector
2020-11-25 21:34:34 +00:00
2021-06-06 07:59:41 +00:00
Remove indentation from heredoc/nowdoc
2020-11-18 01:11:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp73\Rector\String_\DowngradeFlexibleHeredocSyntaxRector` ](../rules/DowngradePhp73/Rector/String_/DowngradeFlexibleHeredocSyntaxRector.php )
2020-11-17 10:45:31 +00:00
2021-03-23 22:13:35 +00:00
```diff
$query = < < < SQL
- SELECT *
- FROM `table`
- WHERE `column` = true;
- SQL;
+SELECT *
+FROM `table`
+WHERE `column` = true;
+SQL;
2020-11-17 10:45:31 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-08 10:00:38 +00:00
2021-06-06 07:59:41 +00:00
### DowngradeIsCountableRector
Downgrade `is_countable()` to former version
- class: [`Rector\DowngradePhp73\Rector\FuncCall\DowngradeIsCountableRector` ](../rules/DowngradePhp73/Rector/FuncCall/DowngradeIsCountableRector.php )
```diff
$items = [];
-return is_countable($items);
+return is_array($items) || $items instanceof Countable;
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeListReferenceAssignmentRector
2020-11-04 21:51:11 +00:00
2021-03-23 22:13:35 +00:00
Convert the list reference assignment to its equivalent PHP 7.2 code
2020-11-04 21:51:11 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp73\Rector\List_\DowngradeListReferenceAssignmentRector` ](../rules/DowngradePhp73/Rector/List_/DowngradeListReferenceAssignmentRector.php )
2020-11-04 21:51:11 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($string)
2020-11-25 21:34:34 +00:00
{
2021-11-21 00:34:01 +00:00
$array = [1, 2, 3];
2021-03-23 22:13:35 +00:00
- list($a, & $b) = $array;
+ list($a) = $array;
+ $b =& $array[1];
- [& $c, $d, & $e] = $array;
+ [$c, $d, $e] = $array;
+ $c =& $array[0];
+ $e =& $array[2];
- list(& $a, & $b) = $array;
+ $a =& $array[0];
+ $b =& $array[1];
2020-11-25 21:34:34 +00:00
}
}
2020-11-04 21:51:11 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-11-04 21:51:11 +00:00
2022-02-13 00:29:38 +00:00
### DowngradePhp73JsonConstRector
Remove Json constant that available only in php 7.3
- class: [`Rector\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector` ](../rules/DowngradePhp73/Rector/ConstFetch/DowngradePhp73JsonConstRector.php )
```diff
-json_encode($content, JSON_THROW_ON_ERROR);
+json_encode($content, 0);
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeTrailingCommasInFunctionCallsRector
2020-11-04 21:51:11 +00:00
2021-03-23 22:13:35 +00:00
Remove trailing commas in function calls
2020-11-04 21:51:11 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector` ](../rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php )
2020-11-04 21:51:11 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function __construct(string $value)
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
$compacted = compact(
'posts',
- 'units',
+ 'units'
);
2020-11-16 17:50:38 +00:00
}
2020-11-04 21:51:11 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-12-26 10:21:09 +00:00
2021-03-23 22:13:35 +00:00
### SetCookieOptionsArrayToArgumentsRector
2020-01-08 00:05:45 +00:00
2021-04-10 18:18:49 +00:00
Convert setcookie option array to arguments
2020-01-08 00:05:45 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp73\Rector\FuncCall\SetCookieOptionsArrayToArgumentsRector` ](../rules/DowngradePhp73/Rector/FuncCall/SetCookieOptionsArrayToArgumentsRector.php )
2020-01-08 00:05:45 +00:00
```diff
2021-03-23 22:13:35 +00:00
-setcookie('name', $value, ['expires' => 360]);
+setcookie('name', $value, 360);
2020-01-08 00:05:45 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-08 00:05:45 +00:00
2021-03-23 22:13:35 +00:00
## DowngradePhp74
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
### ArrowFunctionToAnonymousFunctionRector
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
Replace arrow functions with anonymous functions
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector` ](../rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php )
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
```diff
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
{
$delimiter = ",";
- $callable = fn($matches) => $delimiter . strtolower($matches[1]);
+ $callable = function ($matches) use ($delimiter) {
+ return $delimiter . strtolower($matches[1]);
+ };
}
2020-11-25 21:34:34 +00:00
}
2020-11-16 17:50:38 +00:00
```
2020-11-25 21:34:34 +00:00
< br >
2021-03-23 22:13:35 +00:00
### DowngradeArrayMergeCallWithoutArgumentsRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Add missing param to `array_merge` and `array_merge_recursive`
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector` ](../rules/DowngradePhp74/Rector/FuncCall/DowngradeArrayMergeCallWithoutArgumentsRector.php )
2020-01-08 00:05:45 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
2020-01-08 00:05:45 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
{
- array_merge();
- array_merge_recursive();
+ array_merge([]);
+ array_merge_recursive([]);
}
2020-01-08 00:05:45 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-08 00:05:45 +00:00
2021-03-23 22:13:35 +00:00
### DowngradeArraySpreadRector
2019-12-26 10:21:09 +00:00
2021-04-10 18:18:49 +00:00
Replace array spread with array_merge function
2019-12-26 10:21:09 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector` ](../rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php )
2020-01-08 00:05:45 +00:00
```diff
2020-11-16 17:50:38 +00:00
class SomeClass
2020-01-08 00:05:45 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
{
$parts = ['apple', 'pear'];
- $fruits = ['banana', 'orange', ...$parts, 'watermelon'];
+ $fruits = array_merge(['banana', 'orange'], $parts, ['watermelon']);
}
public function runWithIterable()
{
- $fruits = ['banana', 'orange', ...new ArrayIterator(['durian', 'kiwi']), 'watermelon'];
+ $item0Unpacked = new ArrayIterator(['durian', 'kiwi']);
+ $fruits = array_merge(['banana', 'orange'], is_array($item0Unpacked) ? $item0Unpacked : iterator_to_array($item0Unpacked), ['watermelon']);
}
2020-01-08 00:05:45 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-08 00:05:45 +00:00
2021-03-23 22:13:35 +00:00
### DowngradeContravariantArgumentTypeRector
2019-12-26 10:21:09 +00:00
2021-03-23 22:13:35 +00:00
Remove contravariant argument type declarations
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\ClassMethod\DowngradeContravariantArgumentTypeRector` ](../rules/DowngradePhp74/Rector/ClassMethod/DowngradeContravariantArgumentTypeRector.php )
2019-12-26 10:21:09 +00:00
```diff
2021-03-23 22:13:35 +00:00
class ParentType {}
class ChildType extends ParentType {}
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
class A
2019-12-26 10:21:09 +00:00
{
2021-03-23 22:13:35 +00:00
public function contraVariantArguments(ChildType $type)
2021-04-25 09:44:34 +00:00
{
}
2019-12-26 10:21:09 +00:00
}
2021-03-23 22:13:35 +00:00
class B extends A
2019-12-26 10:21:09 +00:00
{
2021-03-23 22:13:35 +00:00
- public function contraVariantArguments(ParentType $type)
+ /**
+ * @param ParentType $type
+ */
+ public function contraVariantArguments($type)
{
}
2019-12-26 10:21:09 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-12-26 10:21:09 +00:00
2021-03-23 22:13:35 +00:00
### DowngradeCovariantReturnTypeRector
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
Make method return same type as parent
2020-09-13 10:36:29 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\ClassMethod\DowngradeCovariantReturnTypeRector` ](../rules/DowngradePhp74/Rector/ClassMethod/DowngradeCovariantReturnTypeRector.php )
2020-09-13 10:36:29 +00:00
```diff
2021-03-23 22:13:35 +00:00
class ParentType {}
class ChildType extends ParentType {}
2020-09-13 10:36:29 +00:00
2021-03-23 22:13:35 +00:00
class A
2020-09-13 10:36:29 +00:00
{
2021-03-23 22:13:35 +00:00
public function covariantReturnTypes(): ParentType
{
}
}
class B extends A
{
- public function covariantReturnTypes(): ChildType
+ /**
+ * @return ChildType
+ */
+ public function covariantReturnTypes(): ParentType
2020-09-13 10:36:29 +00:00
{
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-13 10:36:29 +00:00
2021-03-23 22:13:35 +00:00
### DowngradeFreadFwriteFalsyToNegationRector
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
Changes `fread()` or `fwrite()` compare to false to negation check
2020-09-13 10:36:29 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\Identical\DowngradeFreadFwriteFalsyToNegationRector` ](../rules/DowngradePhp74/Rector/Identical/DowngradeFreadFwriteFalsyToNegationRector.php )
2020-09-13 10:36:29 +00:00
```diff
2021-03-23 22:13:35 +00:00
-fread($handle, $length) === false;
-fwrite($fp, '1') === false;
+!fread($handle, $length);
+!fwrite($fp, '1');
2020-09-13 10:36:29 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-13 10:36:29 +00:00
2021-03-23 22:13:35 +00:00
### DowngradeNullCoalescingOperatorRector
2020-09-13 10:36:29 +00:00
2021-03-23 22:13:35 +00:00
Remove null coalescing operator ??=
2020-09-13 10:36:29 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\Coalesce\DowngradeNullCoalescingOperatorRector` ](../rules/DowngradePhp74/Rector/Coalesce/DowngradeNullCoalescingOperatorRector.php )
2020-09-13 10:36:29 +00:00
```diff
2021-03-23 22:13:35 +00:00
$array = [];
-$array['user_id'] ??= 'value';
+$array['user_id'] = $array['user_id'] ?? 'value';
2020-09-13 10:36:29 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-13 10:36:29 +00:00
2021-03-23 22:13:35 +00:00
### DowngradeNumericLiteralSeparatorRector
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
Remove "_" as thousands separator in numbers
2020-09-11 09:21:48 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\LNumber\DowngradeNumericLiteralSeparatorRector` ](../rules/DowngradePhp74/Rector/LNumber/DowngradeNumericLiteralSeparatorRector.php )
2020-09-11 09:21:48 +00:00
2020-09-10 07:27:03 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-09-10 07:27:03 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
{
- $int = 1_000;
- $float = 1_000_500.001;
+ $int = 1000;
+ $float = 1000500.001;
}
2020-09-10 07:27:03 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-10 07:27:03 +00:00
2021-11-05 22:37:42 +00:00
### DowngradePreviouslyImplementedInterfaceRector
Downgrade previously implemented interface
- class: [`Rector\DowngradePhp74\Rector\Interface_\DowngradePreviouslyImplementedInterfaceRector` ](../rules/DowngradePhp74/Rector/Interface_/DowngradePreviouslyImplementedInterfaceRector.php )
```diff
interface ContainerExceptionInterface extends Throwable
{
}
-interface ExceptionInterface extends ContainerExceptionInterface, Throwable
+interface ExceptionInterface extends ContainerExceptionInterface
{
}
```
< br >
2021-12-06 19:42:49 +00:00
### DowngradeReflectionGetTypeRector
Downgrade reflection `$refleciton->getType()` method call
- class: [`Rector\DowngradePhp74\Rector\MethodCall\DowngradeReflectionGetTypeRector` ](../rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php )
```diff
class SomeClass
{
public function run(ReflectionProperty $reflectionProperty)
{
- if ($reflectionProperty->getType()) {
+ if (null) {
return true;
}
return false;
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeStripTagsCallWithArrayRector
2020-11-28 18:06:05 +00:00
2021-03-23 22:13:35 +00:00
Convert 2nd param to `strip_tags` from array to string
2020-11-28 18:06:05 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\FuncCall\DowngradeStripTagsCallWithArrayRector` ](../rules/DowngradePhp74/Rector/FuncCall/DowngradeStripTagsCallWithArrayRector.php )
2020-11-28 18:06:05 +00:00
2020-12-22 15:48:25 +00:00
```diff
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function run($string)
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
// Arrays: change to string
- strip_tags($string, ['a', 'p']);
+ strip_tags($string, '< ' . implode('>< ', ['a', 'p']) . '>');
// Variables/consts/properties: if array, change to string
$tags = ['a', 'p'];
- strip_tags($string, $tags);
+ strip_tags($string, $tags !== null & & is_array($tags) ? '< ' . implode('>< ', $tags) . '>' : $tags);
// Default case (eg: function call): externalize to var, then if array, change to string
- strip_tags($string, getTags());
+ $expr = getTags();
+ strip_tags($string, is_array($expr) ? '< ' . implode('>< ', $expr) . '>' : $expr);
2020-12-22 15:48:25 +00:00
}
2020-11-28 18:06:05 +00:00
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeTypedPropertyRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Changes property type definition from type definitions to `@var` annotations.
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector` ](../rules/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector.php )
2020-11-16 17:50:38 +00:00
2020-09-10 07:27:03 +00:00
```diff
class SomeClass
{
2021-03-23 22:13:35 +00:00
- private string $property;
+ /**
2021-08-01 00:25:55 +00:00
+ * @var string
+ */
2021-03-23 22:13:35 +00:00
+ private $property;
2020-09-10 07:27:03 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-09 08:11:22 +00:00
2021-03-23 22:13:35 +00:00
## DowngradePhp80
2020-11-27 12:00:56 +00:00
2021-08-08 20:04:42 +00:00
### DowngradeAbstractPrivateMethodInTraitRector
Remove "abstract" from private methods in traits and adds an empty function body
- class: [`Rector\DowngradePhp80\Rector\ClassMethod\DowngradeAbstractPrivateMethodInTraitRector` ](../rules/DowngradePhp80/Rector/ClassMethod/DowngradeAbstractPrivateMethodInTraitRector.php )
```diff
trait SomeTrait
{
- abstract private function someAbstractPrivateFunction();
+ private function someAbstractPrivateFunction() {}
}
```
< br >
2021-12-05 00:35:40 +00:00
### DowngradeArbitraryExpressionsSupportRector
Replace arbitrary expressions used with new or instanceof.
- class: [`Rector\DowngradePhp80\Rector\New_\DowngradeArbitraryExpressionsSupportRector` ](../rules/DowngradePhp80/Rector/New_/DowngradeArbitraryExpressionsSupportRector.php )
```diff
function getObjectClassName() {
return stdClass::class;
}
-$object = new (getObjectClassName());
+$className = getObjectClassName();
+$object = new $className();
```
< br >
2022-01-02 00:31:07 +00:00
### DowngradeArrayFilterNullableCallbackRector
Unset nullable callback on array_filter
- class: [`Rector\DowngradePhp80\Rector\FuncCall\DowngradeArrayFilterNullableCallbackRector` ](../rules/DowngradePhp80/Rector/FuncCall/DowngradeArrayFilterNullableCallbackRector.php )
```diff
class SomeClass
{
public function run($callback = null)
{
$data = [[]];
- var_dump(array_filter($data, null));
+ var_dump(array_filter($data));
}
}
```
< br >
2021-05-30 07:54:06 +00:00
### DowngradeAttributeToAnnotationRector
Refactor PHP attribute markers to annotations notation
:wrench: **configure it!**
- class: [`Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector` ](../rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php )
```php
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeAttributeToAnnotationRector::class)
2021-12-06 21:12:03 +00:00
->configure([new DowngradeAttributeToAnnotation('Symfony\Component\Routing\Annotation\Route')]);
2021-05-30 07:54:06 +00:00
};
```
↓
```diff
use Symfony\Component\Routing\Annotation\Route;
class SymfonyRoute
{
- #[Route(path: '/path', name: 'action')]
+ /**
+ * @Route ("/path", name="action")
+ */
public function action()
{
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeClassOnObjectToGetClassRector
2020-11-27 12:00:56 +00:00
2021-03-23 22:13:35 +00:00
Change `$object::class` to get_class($object)
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\ClassConstFetch\DowngradeClassOnObjectToGetClassRector` ](../rules/DowngradePhp80/Rector/ClassConstFetch/DowngradeClassOnObjectToGetClassRector.php )
2020-11-27 12:00:56 +00:00
```diff
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function run($object)
2020-11-27 12:00:56 +00:00
{
2021-03-23 22:13:35 +00:00
- return $object::class;
+ return get_class($object);
2020-11-27 12:00:56 +00:00
}
}
```
< br >
2021-12-10 07:56:16 +00:00
### DowngradeDereferenceableOperationRector
Add parentheses around non-dereferenceable expressions.
- class: [`Rector\DowngradePhp80\Rector\ArrayDimFetch\DowngradeDereferenceableOperationRector` ](../rules/DowngradePhp80/Rector/ArrayDimFetch/DowngradeDereferenceableOperationRector.php )
```diff
function getFirstChar(string $str, string $suffix = '')
{
- return "$str$suffix"[0];
+ return ("$str$suffix")[0];
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeMatchToSwitchRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Downgrade `match()` to `switch()`
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector` ](../rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php )
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
public function run()
{
- $message = match ($statusCode) {
- 200, 300 => null,
- 400 => 'not found',
- default => 'unknown status code',
- };
+ switch ($statusCode) {
+ case 200:
+ case 300:
+ $message = null;
+ break;
+ case 400:
+ $message = 'not found';
+ break;
+ default:
+ $message = 'unknown status code';
+ break;
+ }
}
}
2020-12-22 15:48:25 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeMixedTypeDeclarationRector
2020-10-31 12:59:40 +00:00
2021-03-23 22:13:35 +00:00
Remove the "mixed" param and return type, add a `@param` and `@return` tag instead
2020-10-31 12:59:40 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector` ](../rules/DowngradePhp80/Rector/FunctionLike/DowngradeMixedTypeDeclarationRector.php )
2020-10-31 12:59:40 +00:00
2020-10-09 08:11:22 +00:00
```diff
class SomeClass
{
2021-03-23 22:13:35 +00:00
- public function someFunction(mixed $anything): mixed
2020-11-16 17:50:38 +00:00
+ /**
2021-03-23 22:13:35 +00:00
+ * @param mixed $anything
+ * @return mixed
2020-11-16 17:50:38 +00:00
+ */
2021-03-23 22:13:35 +00:00
+ public function someFunction($anything)
2020-10-09 08:11:22 +00:00
{
2020-09-08 10:00:38 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-08 10:00:38 +00:00
2021-06-06 07:59:41 +00:00
### DowngradeNamedArgumentRector
Remove named argument
- class: [`Rector\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector` ](../rules/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector.php )
```diff
class SomeClass
{
2021-07-22 18:40:24 +00:00
public function run()
2021-06-06 07:59:41 +00:00
{
2021-07-22 18:40:24 +00:00
- $this->execute(b: 100);
2021-08-01 00:25:55 +00:00
+ $this->execute(null, 100);
2021-06-06 07:59:41 +00:00
}
2021-07-22 18:40:24 +00:00
private function execute($a = null, $b = null)
2021-06-06 07:59:41 +00:00
{
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeNonCapturingCatchesRector
2020-12-05 10:08:31 +00:00
2021-03-23 22:13:35 +00:00
Downgrade catch () without variable to one
2020-12-05 10:08:31 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector` ](../rules/DowngradePhp80/Rector/Catch_/DowngradeNonCapturingCatchesRector.php )
2020-12-05 10:08:31 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
public function run()
{
try {
// code
- } catch (\Exception) {
+ } catch (\Exception $exception) {
// error
}
}
}
2020-12-05 10:08:31 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeNullsafeToTernaryOperatorRector
2020-10-09 20:01:37 +00:00
2021-03-23 22:13:35 +00:00
Change nullsafe operator to ternary operator rector
2020-10-09 20:01:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector` ](../rules/DowngradePhp80/Rector/NullsafeMethodCall/DowngradeNullsafeToTernaryOperatorRector.php )
2020-11-16 17:50:38 +00:00
2020-10-07 13:41:19 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$dateAsString = $booking->getStartDate()?->asDateTimeString();
-$dateAsString = $booking->startDate?->dateTimeString;
2021-04-18 13:01:56 +00:00
+$dateAsString = ($bookingGetStartDate = $booking->getStartDate()) ? $bookingGetStartDate->asDateTimeString() : null;
+$dateAsString = ($bookingGetStartDate = $booking->startDate) ? $bookingGetStartDate->dateTimeString : null;
2020-10-07 13:41:19 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-08 10:00:38 +00:00
2022-01-09 10:54:09 +00:00
### DowngradeNumberFormatNoFourthArgRector
Downgrade number_format arg to fill 4th arg when only 3rd arg filled
- class: [`Rector\DowngradePhp80\Rector\FuncCall\DowngradeNumberFormatNoFourthArgRector` ](../rules/DowngradePhp80/Rector/FuncCall/DowngradeNumberFormatNoFourthArgRector.php )
```diff
class SomeClass
{
public function run()
{
- return number_format(1000, 2, ',');
+ return number_format(1000, 2, ',', ',');
}
}
```
< br >
2021-11-08 19:40:36 +00:00
### DowngradePhp80ResourceReturnToObjectRector
change instanceof Object to is_resource
- class: [`Rector\DowngradePhp80\Rector\Instanceof_\DowngradePhp80ResourceReturnToObjectRector` ](../rules/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector.php )
```diff
class SomeClass
{
public function run($obj)
{
- $obj instanceof \CurlHandle;
2021-11-08 20:33:54 +00:00
+ is_resource($obj) || $obj instanceof \CurlHandle;
2021-11-08 19:40:36 +00:00
}
}
```
< br >
2021-07-22 18:40:24 +00:00
### DowngradePhpTokenRector
`"something()"` will be renamed to `"somethingElse()"`
- class: [`Rector\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector` ](../rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.php )
```diff
-$tokens = \PhpToken::tokenize($code);
+$tokens = token_get_all($code);
-foreach ($tokens as $phpToken) {
- $name = $phpToken->getTokenName();
- $text = $phpToken->text;
+foreach ($tokens as $token) {
+ $name = is_array($token) ? token_name($token[0]) : null;
+ $text = is_array($token) ? $token[1] : $token;
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradePropertyPromotionRector
2020-12-22 15:48:25 +00:00
2022-01-16 09:58:52 +00:00
Change constructor property promotion to property assign
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector` ](../rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php )
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
- public function __construct(public float $value = 0.0)
+ public float $value;
+
+ public function __construct(float $value = 0.0)
{
+ $this->value = $value;
}
}
2020-12-22 15:48:25 +00:00
```
< br >
2021-12-10 07:56:16 +00:00
### DowngradeRecursiveDirectoryIteratorHasChildrenRector
Remove bool type hint on child of RecursiveDirectoryIterator hasChildren allowLinks parameter
- class: [`Rector\DowngradePhp80\Rector\ClassMethod\DowngradeRecursiveDirectoryIteratorHasChildrenRector` ](../rules/DowngradePhp80/Rector/ClassMethod/DowngradeRecursiveDirectoryIteratorHasChildrenRector.php )
```diff
class RecursiveDirectoryIteratorChild extends \RecursiveDirectoryIterator
{
- public function hasChildren(bool $allowLinks = false): bool
+ public function hasChildren($allowLinks = false): bool
{
return true;
}
}
```
< br >
2021-12-26 00:38:36 +00:00
### DowngradeReflectionClassGetConstantsFilterRector
Downgrade ReflectionClass->getConstants(ReflectionClassConstant::IS_*)
- class: [`Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionClassGetConstantsFilterRector` ](../rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionClassGetConstantsFilterRector.php )
```diff
$reflectionClass = new ReflectionClass('SomeClass');
-$constants = $reflectionClass->getConstants(ReflectionClassConstant::IS_PUBLIC));
+$reflectionClassConstants = $reflectionClass->getReflectionConstants();
+$result = [];
+array_walk($reflectionClassConstants, function ($value) use (& $result) {
+ if ($value->isPublic()) {
+ $result[$value->getName()] = $value->getValue();
+ }
+});
+$constants = $result;
```
< br >
2021-12-06 19:42:49 +00:00
### DowngradeReflectionGetAttributesRector
Remove reflection `getAttributes()` class method code
- class: [`Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionGetAttributesRector` ](../rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.php )
```diff
class SomeClass
{
public function run(ReflectionClass $reflectionClass)
{
- if ($reflectionClass->getAttributes()) {
+ if ([]) {
return true;
}
return false;
}
}
```
< br >
2021-12-14 17:04:36 +00:00
### DowngradeReflectionPropertyGetDefaultValueRector
Downgrade `ReflectionProperty->getDefaultValue()`
- class: [`Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionPropertyGetDefaultValueRector` ](../rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionPropertyGetDefaultValueRector.php )
```diff
class SomeClass
{
public function run(ReflectionProperty $reflectionProperty)
{
- return $reflectionProperty->getDefaultValue();
+ return $reflectionProperty->getDeclaringClass()->getDefaultProperties()[$reflectionProperty->getName()] ?? null;
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeStaticTypeDeclarationRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Remove "static" return and param type, add a `"@param` `$this"` and `"@return` `$this"` tag instead
2020-09-10 07:27:03 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\ClassMethod\DowngradeStaticTypeDeclarationRector` ](../rules/DowngradePhp80/Rector/ClassMethod/DowngradeStaticTypeDeclarationRector.php )
2020-09-24 18:54:39 +00:00
2020-09-10 07:27:03 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
- public function getStatic(): static
2020-11-25 21:34:34 +00:00
+ /**
2021-03-23 22:13:35 +00:00
+ * @return static
2020-11-25 21:34:34 +00:00
+ */
2021-03-23 22:13:35 +00:00
+ public function getStatic()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
return new static();
2020-11-25 21:34:34 +00:00
}
}
2020-09-10 07:27:03 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-10 07:27:03 +00:00
2021-05-23 09:33:26 +00:00
### DowngradeStrContainsRector
Replace `str_contains()` with `strpos()` !== false
- class: [`Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrContainsRector` ](../rules/DowngradePhp80/Rector/FuncCall/DowngradeStrContainsRector.php )
```diff
class SomeClass
{
public function run()
{
- return str_contains('abc', 'a');
+ return strpos('abc', 'a') !== false;
}
}
```
< br >
2021-06-06 07:59:41 +00:00
### DowngradeStrEndsWithRector
Downgrade `str_ends_with()` to `strncmp()` version
- class: [`Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrEndsWithRector` ](../rules/DowngradePhp80/Rector/FuncCall/DowngradeStrEndsWithRector.php )
```diff
-str_ends_with($haystack, $needle);
+"" === $needle || ("" !== $haystack && 0 === substr_compare($haystack, $needle, -\strlen($needle)));
```
< br >
### DowngradeStrStartsWithRector
Downgrade `str_starts_with()` to `strncmp()` version
- class: [`Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrStartsWithRector` ](../rules/DowngradePhp80/Rector/FuncCall/DowngradeStrStartsWithRector.php )
```diff
-str_starts_with($haystack, $needle);
+strncmp($haystack, $needle, strlen($needle)) === 0;
```
< br >
2022-02-02 21:14:38 +00:00
### DowngradeStringReturnTypeOnToStringRector
Add "string" return on current `__toString()` method when parent method has string return on `__toString()` method
- class: [`Rector\DowngradePhp80\Rector\ClassMethod\DowngradeStringReturnTypeOnToStringRector` ](../rules/DowngradePhp80/Rector/ClassMethod/DowngradeStringReturnTypeOnToStringRector.php )
```diff
abstract class ParentClass
{
public function __toString(): string
{
return 'value';
}
}
class ChildClass extends ParentClass
{
- public function __toString()
+ public function __toString(): string
{
return 'value';
}
}
```
< br >
2021-08-01 00:25:55 +00:00
### DowngradeThrowExprRector
2021-12-07 19:04:47 +00:00
Downgrade throw expression
2021-08-01 00:25:55 +00:00
- class: [`Rector\DowngradePhp80\Rector\Expression\DowngradeThrowExprRector` ](../rules/DowngradePhp80/Rector/Expression/DowngradeThrowExprRector.php )
```diff
2021-12-07 19:04:47 +00:00
-echo $variable ?? throw new RuntimeException();
+if (! isset($variable)) {
+ throw new RuntimeException();
+}
+
+echo $variable;
2021-08-01 00:25:55 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeTrailingCommasInParamUseRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Remove trailing commas in param or use list
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\ClassMethod\DowngradeTrailingCommasInParamUseRector` ](../rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php )
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
- public function __construct(string $value1, string $value2,)
+ public function __construct(string $value1, string $value2)
{
- function (string $value1, string $value2,) {
+ function (string $value1, string $value2) {
};
- function () use ($value1, $value2,) {
+ function () use ($value1, $value2) {
};
}
2020-12-22 15:48:25 +00:00
}
2021-03-23 22:13:35 +00:00
-function inFunction(string $value1, string $value2,)
+function inFunction(string $value1, string $value2)
2020-12-22 15:48:25 +00:00
{
}
2021-03-23 22:13:35 +00:00
```
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
< br >
### DowngradeUnionTypeDeclarationRector
Remove the union type params and returns, add `@param/@return` tags instead
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector` ](../rules/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector.php )
2021-03-23 22:13:35 +00:00
```diff
class SomeClass
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
- public function echoInput(string|int $input): int|bool
2020-12-22 15:48:25 +00:00
+ /**
2021-03-23 22:13:35 +00:00
+ * @param string|int $input
+ * @return int|bool
2020-12-22 15:48:25 +00:00
+ */
2021-03-23 22:13:35 +00:00
+ public function echoInput($input)
{
echo $input;
}
2020-12-22 15:48:25 +00:00
}
```
< br >
2021-03-23 22:13:35 +00:00
### DowngradeUnionTypeTypedPropertyRector
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
Removes union type property type definition, adding `@var` annotations instead.
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector` ](../rules/DowngradePhp80/Rector/Property/DowngradeUnionTypeTypedPropertyRector.php )
2020-11-16 17:50:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
- private string|int $property;
+ /**
2021-08-01 00:25:55 +00:00
+ * @var string|int
+ */
2021-03-23 22:13:35 +00:00
+ private $property;
}
2020-09-11 09:21:48 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-11 09:21:48 +00:00
2021-07-23 16:35:16 +00:00
## DowngradePhp81
2022-04-02 15:46:27 +00:00
### 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 >
2021-12-05 00:35:40 +00:00
### DowngradeArraySpreadStringKeyRector
Replace array spread with string key to array_merge function
- class: [`Rector\DowngradePhp81\Rector\Array_\DowngradeArraySpreadStringKeyRector` ](../rules/DowngradePhp81/Rector/Array_/DowngradeArraySpreadStringKeyRector.php )
```diff
class SomeClass
{
public function run()
{
$parts = ['a' => 'b'];
$parts2 = ['c' => 'd'];
- $result = [...$parts, ...$parts2];
+ $result = array_merge($parts, $parts2);
}
}
```
< br >
2021-07-23 16:35:16 +00:00
### DowngradeFinalizePublicClassConstantRector
Remove final from class constants
- class: [`Rector\DowngradePhp81\Rector\ClassConst\DowngradeFinalizePublicClassConstantRector` ](../rules/DowngradePhp81/Rector/ClassConst/DowngradeFinalizePublicClassConstantRector.php )
```diff
class SomeClass
{
- final public const NAME = 'value';
+ public const NAME = 'value';
}
```
< br >
2021-11-21 00:34:01 +00:00
### DowngradeFirstClassCallableSyntaxRector
Replace variadic placeholders usage by `Closure::fromCallable()`
- class: [`Rector\DowngradePhp81\Rector\FuncCall\DowngradeFirstClassCallableSyntaxRector` ](../rules/DowngradePhp81/Rector/FuncCall/DowngradeFirstClassCallableSyntaxRector.php )
```diff
-$cb = strlen(...);
+$cb = \Closure::fromCallable('strlen');
```
< br >
### DowngradeNeverTypeDeclarationRector
Remove "never" return type, add a `"@return` never" tag instead
- class: [`Rector\DowngradePhp81\Rector\FunctionLike\DowngradeNeverTypeDeclarationRector` ](../rules/DowngradePhp81/Rector/FunctionLike/DowngradeNeverTypeDeclarationRector.php )
```diff
-function someFunction(): never
+/**
+ * @return never
+ */
+function someFunction()
{
}
```
< br >
### DowngradeNewInInitializerRector
Replace New in initializers
- class: [`Rector\DowngradePhp81\Rector\FunctionLike\DowngradeNewInInitializerRector` ](../rules/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector.php )
```diff
class SomeClass
{
public function __construct(
- private Logger $logger = new NullLogger,
+ private ?Logger $logger = null,
) {
+ $this->logger = $logger ?? new NullLogger;
}
}
```
< br >
2021-11-08 19:40:36 +00:00
### DowngradePhp81ResourceReturnToObjectRector
change instanceof Object to is_resource
- class: [`Rector\DowngradePhp81\Rector\Instanceof_\DowngradePhp81ResourceReturnToObjectRector` ](../rules/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector.php )
```diff
class SomeClass
{
public function run($obj)
{
- $obj instanceof \finfo;
2021-11-08 20:33:54 +00:00
+ is_resource($obj) || $obj instanceof \finfo;
2021-11-08 19:40:36 +00:00
}
}
```
< br >
2021-11-25 12:53:25 +00:00
### DowngradePureIntersectionTypeRector
Remove the intersection type params and returns, add `@param/@return` tags instead
- class: [`Rector\DowngradePhp81\Rector\FunctionLike\DowngradePureIntersectionTypeRector` ](../rules/DowngradePhp81/Rector/FunctionLike/DowngradePureIntersectionTypeRector.php )
```diff
-function someFunction(): Foo& Bar
+/**
+ * @return Foo& Bar
+ */
+function someFunction()
{
}
```
< br >
2021-11-21 00:34:01 +00:00
### DowngradeReadonlyPropertyRector
Remove "readonly" property type, add a "@readonly" tag instead
- class: [`Rector\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector` ](../rules/DowngradePhp81/Rector/Property/DowngradeReadonlyPropertyRector.php )
```diff
class SomeClass
{
- public readonly string $foo;
+ /**
+ * @readonly
+ */
+ public string $foo;
public function __construct()
{
$this->foo = 'foo';
}
}
```
< br >
2021-03-23 22:13:35 +00:00
## EarlyReturn
2020-09-08 10:00:38 +00:00
2021-03-23 22:13:35 +00:00
### ChangeAndIfToEarlyReturnRector
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
Changes if & & to early return
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector` ](../rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php )
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-09-08 10:00:38 +00:00
{
2021-03-23 22:13:35 +00:00
public function canDrive(Car $car)
2020-09-08 10:00:38 +00:00
{
2021-03-23 22:13:35 +00:00
- if ($car->hasWheels && $car->hasFuel) {
- return true;
+ if (!$car->hasWheels) {
+ return false;
}
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
- return false;
+ if (!$car->hasFuel) {
+ return false;
+ }
+
+ return true;
2020-09-08 10:00:38 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-08 10:00:38 +00:00
2021-03-23 22:13:35 +00:00
### ChangeIfElseValueAssignToEarlyReturnRector
2020-12-05 22:33:27 +00:00
2021-03-23 22:13:35 +00:00
Change if/else value to early return
2020-12-05 22:33:27 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector` ](../rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php )
2020-12-05 22:33:27 +00:00
```diff
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function run()
2020-12-05 22:33:27 +00:00
{
2021-03-23 22:13:35 +00:00
if ($this->hasDocBlock($tokens, $index)) {
- $docToken = $tokens[$this->getDocBlockIndex($tokens, $index)];
- } else {
- $docToken = null;
+ return $tokens[$this->getDocBlockIndex($tokens, $index)];
}
-
- return $docToken;
+ return null;
2020-12-05 22:33:27 +00:00
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### ChangeNestedForeachIfsToEarlyContinueRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Change nested ifs to foreach with continue
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector` ](../rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php )
2020-07-10 20:15:52 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
$items = [];
foreach ($values as $value) {
- if ($value === 5) {
- if ($value2 === 10) {
- $items[] = 'maybe';
- }
+ if ($value !== 5) {
+ continue;
}
+ if ($value2 !== 10) {
+ continue;
+ }
+
+ $items[] = 'maybe';
}
2020-11-16 17:50:38 +00:00
}
}
```
2020-09-11 09:21:48 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
### ChangeNestedIfsToEarlyReturnRector
2020-09-11 09:21:48 +00:00
2021-03-23 22:13:35 +00:00
Change nested ifs to early return
2020-09-24 18:54:39 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\If_\ChangeNestedIfsToEarlyReturnRector` ](../rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php )
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run()
{
2021-03-23 22:13:35 +00:00
- if ($value === 5) {
- if ($value2 === 10) {
- return 'yes';
- }
+ if ($value !== 5) {
+ return 'no';
+ }
+
+ if ($value2 === 10) {
+ return 'yes';
}
return 'no';
2020-11-25 21:34:34 +00:00
}
}
2020-09-11 09:21:48 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-11 09:21:48 +00:00
2021-03-23 22:13:35 +00:00
### ChangeOrIfContinueToMultiContinueRector
2020-11-16 17:50:38 +00:00
2021-06-06 07:59:41 +00:00
Changes if || to early return
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\If_\ChangeOrIfContinueToMultiContinueRector` ](../rules/EarlyReturn/Rector/If_/ChangeOrIfContinueToMultiContinueRector.php )
2020-09-10 07:27:03 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-08-01 11:41:16 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function canDrive(Car $newCar)
2020-09-10 07:27:03 +00:00
{
2021-03-23 22:13:35 +00:00
foreach ($cars as $car) {
- if ($car->hasWheels() || $car->hasFuel()) {
+ if ($car->hasWheels()) {
+ continue;
+ }
+ if ($car->hasFuel()) {
continue;
}
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
$car->setWheel($newCar->wheel);
$car->setFuel($newCar->fuel);
}
2020-09-10 07:27:03 +00:00
}
2020-07-10 20:15:52 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-07-10 20:15:52 +00:00
2021-03-23 22:13:35 +00:00
### ChangeOrIfReturnToEarlyReturnRector
2020-12-05 10:08:31 +00:00
2021-03-23 22:13:35 +00:00
Changes if || with return to early return
2020-12-05 10:08:31 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector` ](../rules/EarlyReturn/Rector/If_/ChangeOrIfReturnToEarlyReturnRector.php )
2020-12-05 10:08:31 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-12-05 10:08:31 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($a, $b)
2021-03-01 12:10:50 +00:00
{
2021-03-23 22:13:35 +00:00
- if ($a || $b) {
+ if ($a) {
+ return null;
+ }
+ if ($b) {
return null;
}
return 'another';
2021-03-01 12:10:50 +00:00
}
2020-12-05 10:08:31 +00:00
}
```
< br >
2021-03-23 22:13:35 +00:00
### PreparedValueToEarlyReturnRector
2020-12-03 08:32:04 +00:00
2021-03-23 22:13:35 +00:00
Return early prepared value in ifs
2020-12-03 08:32:04 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector` ](../rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php )
2020-12-03 08:32:04 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-12-03 08:32:04 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2021-03-01 12:10:50 +00:00
{
2021-03-23 22:13:35 +00:00
- $var = null;
-
if (rand(0,1)) {
- $var = 1;
+ return 1;
}
2020-12-03 08:32:04 +00:00
2021-03-23 22:13:35 +00:00
if (rand(0,1)) {
- $var = 2;
+ return 2;
}
- return $var;
+ return null;
2021-03-01 12:10:50 +00:00
}
2020-12-03 08:32:04 +00:00
}
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveAlwaysElseRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Split if statement, when if condition always break execution flow
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector` ](../rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php )
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
public function run($value)
{
if ($value) {
throw new \InvalidStateException;
- } else {
- return 10;
}
+
+ return 10;
}
}
2020-12-22 15:48:25 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### ReturnAfterToEarlyOnBreakRector
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
Change return after foreach to early return in foreach on break
2020-09-11 09:21:48 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\Foreach_\ReturnAfterToEarlyOnBreakRector` ](../rules/EarlyReturn/Rector/Foreach_/ReturnAfterToEarlyOnBreakRector.php )
2020-09-11 09:21:48 +00:00
2020-09-08 10:00:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
public function run(array $pathConstants, string $allowedPath)
{
- $pathOK = false;
-
foreach ($pathConstants as $allowedPath) {
if ($dirPath == $allowedPath) {
- $pathOK = true;
- break;
+ return true;
}
}
- return $pathOK;
+ return false;
}
}
2020-09-10 07:27:03 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-10 07:27:03 +00:00
2021-03-23 22:13:35 +00:00
### ReturnBinaryAndToEarlyReturnRector
2020-09-10 07:27:03 +00:00
2021-03-23 22:13:35 +00:00
Changes Single return of & & to early returns
2020-09-08 10:00:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector` ](../rules/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php )
2020-09-10 07:27:03 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
public function accept()
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
- return $this->something() && $this->somethingelse();
+ if (!$this->something()) {
+ return false;
+ }
+ return (bool) $this->somethingelse();
2020-11-16 17:50:38 +00:00
}
}
```
2020-09-24 18:54:39 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-11 09:21:48 +00:00
2021-03-23 22:13:35 +00:00
### ReturnBinaryOrToEarlyReturnRector
2021-01-12 11:52:10 +00:00
2021-03-23 22:13:35 +00:00
Changes Single return of || to early returns
2021-01-12 11:52:10 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector` ](../rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php )
2021-01-12 11:52:10 +00:00
```diff
2021-03-01 12:10:50 +00:00
class SomeClass
2021-01-12 11:52:10 +00:00
{
2021-03-23 22:13:35 +00:00
public function accept()
2021-01-12 11:52:10 +00:00
{
2021-03-23 22:13:35 +00:00
- return $this->something() || $this->somethingElse();
+ if ($this->something()) {
+ return true;
+ }
+ return (bool) $this->somethingElse();
2021-01-12 11:52:10 +00:00
}
}
```
< br >
2021-03-23 22:13:35 +00:00
## MysqlToMysqli
### MysqlAssignToMysqliRector
Converts more complex mysql functions to mysqli
2021-05-23 09:33:26 +00:00
- class: [`Rector\MysqlToMysqli\Rector\Assign\MysqlAssignToMysqliRector` ](../rules/MysqlToMysqli/Rector/Assign/MysqlAssignToMysqliRector.php )
2020-12-05 10:08:31 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$data = mysql_db_name($result, $row);
+mysqli_data_seek($result, $row);
+$fetch = mysql_fetch_row($result);
+$data = $fetch[0];
2020-12-05 10:08:31 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### MysqlFuncCallToMysqliRector
2019-11-16 15:36:58 +00:00
2021-03-23 22:13:35 +00:00
Converts more complex mysql functions to mysqli
2020-09-11 09:21:48 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\MysqlToMysqli\Rector\FuncCall\MysqlFuncCallToMysqliRector` ](../rules/MysqlToMysqli/Rector/FuncCall/MysqlFuncCallToMysqliRector.php )
2020-09-24 18:54:39 +00:00
2020-12-05 22:33:27 +00:00
```diff
2021-03-23 22:13:35 +00:00
-mysql_drop_db($database);
+mysqli_query('DROP DATABASE ' . $database);
2020-12-05 22:33:27 +00:00
```
2020-09-11 09:21:48 +00:00
2020-12-05 22:33:27 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
### MysqlPConnectToMysqliConnectRector
2021-03-01 12:10:50 +00:00
2021-03-23 22:13:35 +00:00
Replace `mysql_pconnect()` with `mysqli_connect()` with host p: prefix
2021-03-01 12:10:50 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\MysqlToMysqli\Rector\FuncCall\MysqlPConnectToMysqliConnectRector` ](../rules/MysqlToMysqli/Rector/FuncCall/MysqlPConnectToMysqliConnectRector.php )
2021-03-01 12:10:50 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
2021-03-01 12:10:50 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($host, $username, $password)
2021-03-01 12:10:50 +00:00
{
2021-03-23 22:13:35 +00:00
- return mysql_pconnect($host, $username, $password);
+ return mysqli_connect('p:' . $host, $username, $password);
2021-03-01 12:10:50 +00:00
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### MysqlQueryMysqlErrorWithLinkRector
2020-09-11 09:21:48 +00:00
2021-03-23 22:13:35 +00:00
Add mysql_query and mysql_error with connection
2020-12-05 22:33:27 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\MysqlToMysqli\Rector\FuncCall\MysqlQueryMysqlErrorWithLinkRector` ](../rules/MysqlToMysqli/Rector/FuncCall/MysqlQueryMysqlErrorWithLinkRector.php )
2020-12-05 22:33:27 +00:00
```diff
class SomeClass
{
public function run()
{
2021-03-23 22:13:35 +00:00
$conn = mysqli_connect('host', 'user', 'pass');
- mysql_error();
+ mysqli_error($conn);
$sql = 'SELECT';
- return mysql_query($sql);
+ return mysqli_query($conn, $sql);
2020-12-05 22:33:27 +00:00
}
}
2020-11-25 21:34:34 +00:00
```
2020-12-05 22:33:27 +00:00
< br >
2021-03-23 22:13:35 +00:00
## Naming
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
### RenameForeachValueVariableToMatchExprVariableRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Renames value variable name in foreach loop to match expression variable
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector` ](../rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchExprVariableRector.php )
2020-12-22 15:48:25 +00:00
```diff
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function run()
{
$array = [];
2021-08-01 00:25:55 +00:00
- foreach ($variables as $property) {
2021-03-23 22:13:35 +00:00
- $array[] = $property;
+ foreach ($variables as $variable) {
+ $array[] = $variable;
2020-12-22 15:48:25 +00:00
}
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RenameForeachValueVariableToMatchMethodCallReturnTypeRector
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
Renames value variable name in foreach loop to match method type
2020-08-30 12:45:03 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector` ](../rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php )
2020-02-23 17:20:00 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function run()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
$array = [];
- foreach ($object->getMethods() as $property) {
- $array[] = $property;
+ foreach ($object->getMethods() as $method) {
+ $array[] = $method;
}
2020-11-25 21:34:34 +00:00
}
2020-11-16 17:50:38 +00:00
}
```
2020-08-12 09:44:34 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-08-12 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
### RenameParamToMatchTypeRector
2019-09-19 09:27:29 +00:00
2021-06-06 07:59:41 +00:00
Rename param to match ClassType
2019-09-19 09:27:29 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector` ](../rules/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector.php )
2019-09-19 09:27:29 +00:00
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
- public function run(Apple $pie)
+ public function run(Apple $apple)
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
- $food = $pie;
+ $food = $apple;
2020-12-22 15:48:25 +00:00
}
}
```
2020-11-25 21:34:34 +00:00
2020-12-22 15:48:25 +00:00
< br >
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
### RenamePropertyToMatchTypeRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Rename property and method param to match its type
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector` ](../rules/Naming/Rector/Class_/RenamePropertyToMatchTypeRector.php )
2020-11-25 21:34:34 +00:00
```diff
2020-12-22 15:48:25 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
/**
* @var EntityManager
*/
- private $eventManager;
+ private $entityManager;
- public function __construct(EntityManager $eventManager)
+ public function __construct(EntityManager $entityManager)
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
- $this->eventManager = $eventManager;
+ $this->entityManager = $entityManager;
2020-12-22 15:48:25 +00:00
}
}
2020-11-16 17:50:38 +00:00
```
2020-09-24 18:54:39 +00:00
2020-11-16 17:50:38 +00:00
< br >
2019-06-02 07:45:37 +00:00
2021-03-23 22:13:35 +00:00
### RenameVariableToMatchMethodCallReturnTypeRector
2019-06-02 07:45:37 +00:00
2021-03-23 22:13:35 +00:00
Rename variable to match method return type
2019-06-02 07:45:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector` ](../rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php )
2019-03-09 13:24:30 +00:00
2020-11-25 21:34:34 +00:00
```diff
2020-12-22 15:48:25 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
{
- $a = $this->getRunner();
+ $runner = $this->getRunner();
}
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
public function getRunner(): Runner
{
return new Runner();
}
2020-11-16 17:50:38 +00:00
}
```
2020-09-24 18:54:39 +00:00
2020-11-16 17:50:38 +00:00
< br >
2019-03-09 13:24:30 +00:00
2021-03-23 22:13:35 +00:00
### RenameVariableToMatchNewTypeRector
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
Rename variable to match new ClassType
2019-03-09 13:24:30 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector` ](../rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php )
2019-03-09 13:24:30 +00:00
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
2020-12-22 15:48:25 +00:00
{
public function run()
{
2021-03-23 22:13:35 +00:00
- $search = new DreamSearch();
- $search->advance();
+ $dreamSearch = new DreamSearch();
+ $dreamSearch->advance();
2021-02-20 23:21:19 +00:00
}
}
2020-05-29 10:41:25 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-05-24 08:46:34 +00:00
2021-03-23 22:13:35 +00:00
## PSR4
2018-12-06 00:13:34 +00:00
2021-03-23 22:13:35 +00:00
### MultipleClassFileToPsr4ClassesRector
2018-12-06 00:13:34 +00:00
2021-03-23 22:13:35 +00:00
Change multiple classes in one file to standalone PSR-4 classes.
2018-12-06 00:13:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\PSR4\Rector\Namespace_\MultipleClassFileToPsr4ClassesRector` ](../rules/PSR4/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector.php )
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
+// new file: "app/Exceptions/FirstException.php"
namespace App\Exceptions;
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
use Exception;
final class FirstException extends Exception
{
}
+
+// new file: "app/Exceptions/SecondException.php"
+namespace App\Exceptions;
+
+use Exception;
final class SecondException extends Exception
2020-07-28 23:41:20 +00:00
{
}
2018-12-06 00:13:34 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-06 00:13:34 +00:00
2021-03-23 22:13:35 +00:00
### NormalizeNamespaceByPSR4ComposerAutoloadRector
Adds namespace to namespace-less files or correct namespace to match PSR-4 in `composer.json` autoload section. Run with combination with "Rector\PSR4\Rector\Namespace_\MultipleClassFileToPsr4ClassesRector"
2021-05-23 09:33:26 +00:00
- class: [`Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector` ](../rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php )
2021-03-23 22:13:35 +00:00
- with `composer.json` :
2020-08-03 07:26:01 +00:00
2021-03-23 22:13:35 +00:00
```json
{
"autoload": {
"psr-4": {
"App\\CustomNamespace\\": "src"
}
}
}
```
2020-08-03 07:26:01 +00:00
2021-03-23 22:13:35 +00:00
↓
2020-08-03 07:26:01 +00:00
```diff
2021-03-23 22:13:35 +00:00
// src/SomeClass.php
2020-08-03 07:26:01 +00:00
2021-03-23 22:13:35 +00:00
+namespace App\CustomNamespace;
+
class SomeClass
2020-11-25 21:34:34 +00:00
{
2020-08-03 07:26:01 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-03 07:26:01 +00:00
2021-03-23 22:13:35 +00:00
## Php52
### ContinueToBreakInSwitchRector
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
Use break instead of continue in switch statements
2018-11-11 12:22:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php52\Rector\Switch_\ContinueToBreakInSwitchRector` ](../rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php )
2018-11-11 12:22:35 +00:00
```diff
2021-03-23 22:13:35 +00:00
function some_run($value)
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
switch ($value) {
case 1:
echo 'Hi';
- continue;
+ break;
case 2:
echo 'Hello';
break;
2020-11-25 21:34:34 +00:00
}
2020-07-28 23:41:20 +00:00
}
2018-11-11 12:22:35 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2021-03-23 22:13:35 +00:00
### VarToPublicPropertyRector
2020-11-16 17:50:38 +00:00
2021-06-06 07:59:41 +00:00
Change property modifier from `var` to `public`
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php52\Rector\Property\VarToPublicPropertyRector` ](../rules/Php52/Rector/Property/VarToPublicPropertyRector.php )
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeController
{
- var $name = 'Tom';
+ public $name = 'Tom';
}
2020-11-25 21:34:34 +00:00
```
2020-11-04 04:45:10 +00:00
2020-11-25 21:34:34 +00:00
< br >
2020-11-04 04:45:10 +00:00
2021-03-23 22:13:35 +00:00
## Php53
### DirNameFileConstantToDirConstantRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Convert dirname(__FILE__) to __DIR__
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector` ](../rules/Php53/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php )
2020-11-25 21:34:34 +00:00
```diff
class SomeClass
2019-08-05 21:10:47 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
{
2021-03-23 22:13:35 +00:00
- return dirname(__FILE__);
+ return __DIR__ ;
2020-11-25 21:34:34 +00:00
}
2019-08-05 21:10:47 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-10-15 14:46:31 +00:00
2021-03-23 22:13:35 +00:00
### ReplaceHttpServerVarsByServerRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Rename old `$HTTP_*` variable names to new replacements
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php53\Rector\Variable\ReplaceHttpServerVarsByServerRector` ](../rules/Php53/Rector/Variable/ReplaceHttpServerVarsByServerRector.php )
2020-04-01 00:05:51 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$serverVars = $HTTP_SERVER_VARS;
+$serverVars = $_SERVER;
2020-04-01 00:05:51 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-04-01 00:05:51 +00:00
2021-03-23 22:13:35 +00:00
### TernaryToElvisRector
2019-09-06 10:30:58 +00:00
2021-03-23 22:13:35 +00:00
Use ?: instead of ?, where useful
2019-09-06 10:30:58 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php53\Rector\Ternary\TernaryToElvisRector` ](../rules/Php53/Rector/Ternary/TernaryToElvisRector.php )
2019-09-06 10:30:58 +00:00
```diff
2021-03-23 22:13:35 +00:00
function elvis()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- $value = $a ? $a : false;
+ $value = $a ?: false;
2019-09-06 10:30:58 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-09-06 10:30:58 +00:00
2021-03-23 22:13:35 +00:00
## Php54
### RemoveReferenceFromCallRector
2021-01-01 18:59:23 +00:00
2021-03-23 22:13:35 +00:00
Remove & from function and method calls
2021-01-01 18:59:23 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php54\Rector\FuncCall\RemoveReferenceFromCallRector` ](../rules/Php54/Rector/FuncCall/RemoveReferenceFromCallRector.php )
2021-01-01 18:59:23 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
2021-01-01 18:59:23 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($one)
2021-01-01 18:59:23 +00:00
{
2021-03-23 22:13:35 +00:00
- return strlen(&$one);
+ return strlen($one);
2021-01-01 18:59:23 +00:00
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveZeroBreakContinueRector
2020-08-03 22:21:59 +00:00
2021-03-23 22:13:35 +00:00
Remove 0 from break and continue
2020-08-03 22:21:59 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php54\Rector\Break_\RemoveZeroBreakContinueRector` ](../rules/Php54/Rector/Break_/RemoveZeroBreakContinueRector.php )
2020-08-03 22:21:59 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($random)
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- continue 0;
- break 0;
+ continue;
+ break;
$five = 5;
- continue $five;
+ continue 5;
- break $random;
+ break;
2020-11-25 21:34:34 +00:00
}
}
2020-08-03 22:21:59 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-03 22:21:59 +00:00
2021-03-23 22:13:35 +00:00
## Php55
### ClassConstantToSelfClassRector
2020-10-31 00:43:54 +00:00
2021-03-23 22:13:35 +00:00
Change `__CLASS__` to self::class
2020-10-31 00:43:54 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php55\Rector\Class_\ClassConstantToSelfClassRector` ](../rules/Php55/Rector/Class_/ClassConstantToSelfClassRector.php )
2020-10-31 00:43:54 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function callOnMe()
{
- var_dump(__CLASS__);
+ var_dump(self::class);
}
2020-10-31 00:43:54 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-31 00:43:54 +00:00
2022-03-28 09:17:08 +00:00
### GetCalledClassToSelfClassRector
Change `get_called_class()` to self::class on final class
- class: [`Rector\Php55\Rector\FuncCall\GetCalledClassToSelfClassRector` ](../rules/Php55/Rector/FuncCall/GetCalledClassToSelfClassRector.php )
```diff
final class SomeClass
{
public function callOnMe()
{
- var_dump(get_called_class());
+ var_dump(self::class);
}
}
```
< br >
### GetCalledClassToStaticClassRector
Change `get_called_class()` to static::class on non-final class
- class: [`Rector\Php55\Rector\FuncCall\GetCalledClassToStaticClassRector` ](../rules/Php55/Rector/FuncCall/GetCalledClassToStaticClassRector.php )
```diff
class SomeClass
{
public function callOnMe()
{
- var_dump(get_called_class());
+ var_dump(static::class);
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### PregReplaceEModifierRector
2018-10-21 22:26:45 +00:00
2021-04-10 18:18:49 +00:00
The /e modifier is no longer supported, use preg_replace_callback instead
2018-10-21 22:26:45 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php55\Rector\FuncCall\PregReplaceEModifierRector` ](../rules/Php55/Rector/FuncCall/PregReplaceEModifierRector.php )
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
{
2021-03-23 22:13:35 +00:00
- $comment = preg_replace('~\b(\w)(\w+)~e', '"$1".strtolower("$2")', $comment);
+ $comment = preg_replace_callback('~\b(\w)(\w+)~', function ($matches) {
+ return($matches[1].strtolower($matches[2]));
2021-08-01 00:25:55 +00:00
+ }, $comment);
2020-11-25 21:34:34 +00:00
}
2020-07-28 23:41:20 +00:00
}
2018-12-14 19:35:35 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-14 19:35:35 +00:00
2021-03-23 22:13:35 +00:00
### StringClassNameToClassConstantRector
2020-10-30 17:10:17 +00:00
2021-03-23 22:13:35 +00:00
Replace string class names by < class > ::class constant
2020-10-30 17:10:17 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-10-30 17:10:17 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php55\Rector\String_\StringClassNameToClassConstantRector` ](../rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php )
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-10-30 17:10:17 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(StringClassNameToClassConstantRector::class)
2021-12-10 07:56:16 +00:00
->configure(['ClassName', 'AnotherClassName']);
2021-03-23 22:13:35 +00:00
};
```
2021-03-12 23:52:38 +00:00
2021-03-23 22:13:35 +00:00
↓
2021-03-12 23:52:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
class AnotherClass
2021-03-12 23:52:38 +00:00
{
}
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function run()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- return 'AnotherClass';
+ return \AnotherClass::class;
2020-11-25 21:34:34 +00:00
}
}
2020-10-09 20:01:37 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-09 20:01:37 +00:00
2021-03-23 22:13:35 +00:00
## Php56
### AddDefaultValueForUndefinedVariableRector
2018-12-14 19:35:35 +00:00
2021-03-23 22:13:35 +00:00
Adds default value for undefined variable
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector` ](../rules/Php56/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php )
2019-05-29 13:40:20 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function run()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
+ $a = null;
if (rand(0, 1)) {
$a = 5;
}
echo $a;
2020-11-25 21:34:34 +00:00
}
}
2019-05-29 13:40:20 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
### PowToExpRector
2019-05-29 13:40:20 +00:00
2021-04-10 18:18:49 +00:00
Changes pow(val, val2) to ** (exp) parameter
2018-12-14 19:35:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php56\Rector\FuncCall\PowToExpRector` ](../rules/Php56/Rector/FuncCall/PowToExpRector.php )
2018-12-14 19:35:35 +00:00
```diff
2021-03-23 22:13:35 +00:00
-pow(1, 2);
+1**2;
2019-03-16 20:31:46 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-03-16 20:31:46 +00:00
2021-03-23 22:13:35 +00:00
## Php70
### BreakNotInLoopOrSwitchToReturnRector
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
Convert break outside for/foreach/switch context to return
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\Break_\BreakNotInLoopOrSwitchToReturnRector` ](../rules/Php70/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php )
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
```diff
2020-07-28 23:41:20 +00:00
class SomeClass
{
public function run()
{
2021-03-23 22:13:35 +00:00
if ($isphp5)
return 1;
else
return 2;
- break;
+ return;
2020-07-28 23:41:20 +00:00
}
}
2019-05-29 13:40:20 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
### CallUserMethodRector
2019-03-16 20:31:46 +00:00
2021-03-23 22:13:35 +00:00
Changes `call_user_method()/call_user_method_array()` to `call_user_func()/call_user_func_array()`
2019-03-16 20:31:46 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\FuncCall\CallUserMethodRector` ](../rules/Php70/Rector/FuncCall/CallUserMethodRector.php )
2019-02-04 00:32:53 +00:00
2018-08-01 20:09:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
-call_user_method($method, $obj, "arg1", "arg2");
+call_user_func(array(& $obj, "method"), "arg1", "arg2");
2018-07-31 12:50:39 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-07-31 19:43:39 +00:00
2021-03-23 22:13:35 +00:00
### EmptyListRector
2018-07-31 12:50:39 +00:00
2021-03-23 22:13:35 +00:00
`list()` cannot be empty
2018-07-31 12:50:39 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\List_\EmptyListRector` ](../rules/Php70/Rector/List_/EmptyListRector.php )
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
-'list() = $values;'
+'list($unusedGenerated) = $values;'
2018-10-12 23:15:00 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-07-31 12:50:39 +00:00
2021-03-23 22:13:35 +00:00
### EregToPregMatchRector
2020-07-29 08:33:10 +00:00
2021-03-23 22:13:35 +00:00
Changes ereg*() to preg*() calls
2020-07-29 08:33:10 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\FuncCall\EregToPregMatchRector` ](../rules/Php70/Rector/FuncCall/EregToPregMatchRector.php )
2020-07-29 08:33:10 +00:00
```diff
2021-03-23 22:13:35 +00:00
-ereg("hi")
+preg_match("#hi#");
2020-07-29 08:33:10 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-07-29 08:33:10 +00:00
2021-03-23 22:13:35 +00:00
### ExceptionHandlerTypehintRector
2020-08-01 22:18:40 +00:00
2021-06-06 07:59:41 +00:00
Change typehint from `Exception` to `Throwable` .
2020-08-01 22:18:40 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\FunctionLike\ExceptionHandlerTypehintRector` ](../rules/Php70/Rector/FunctionLike/ExceptionHandlerTypehintRector.php )
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
-function handler(Exception $exception) { ... }
+function handler(Throwable $exception) { ... }
set_exception_handler('handler');
2020-08-01 22:18:40 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-01 22:18:40 +00:00
2021-03-23 22:13:35 +00:00
### IfToSpaceshipRector
2020-08-01 22:18:40 +00:00
2021-03-23 22:13:35 +00:00
Changes if/else to spaceship < => where useful
2020-08-01 22:18:40 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\If_\IfToSpaceshipRector` ](../rules/Php70/Rector/If_/IfToSpaceshipRector.php )
2020-08-01 22:18:40 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-08-01 22:18:40 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2020-08-01 22:18:40 +00:00
{
2021-03-23 22:13:35 +00:00
usort($languages, function ($a, $b) {
- if ($a[0] === $b[0]) {
- return 0;
- }
-
- return ($a[0] < $b[0]) ? 1 : -1;
+ return $b[0] < => $a[0];
});
2020-08-01 22:18:40 +00:00
}
2020-11-25 21:34:34 +00:00
}
2020-08-01 22:18:40 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-01 22:18:40 +00:00
2021-03-23 22:13:35 +00:00
### ListSplitStringRector
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
`list()` cannot split string directly anymore, use `str_split()`
2018-10-12 23:15:00 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\Assign\ListSplitStringRector` ](../rules/Php70/Rector/Assign/ListSplitStringRector.php )
2018-10-12 23:15:00 +00:00
2018-11-11 12:22:35 +00:00
```diff
2021-03-23 22:13:35 +00:00
-list($foo) = "string";
+list($foo) = str_split("string");
2018-11-11 12:22:35 +00:00
```
2018-10-12 23:15:00 +00:00
2020-11-16 17:50:38 +00:00
< br >
2018-10-21 22:26:45 +00:00
2021-03-23 22:13:35 +00:00
### ListSwapArrayOrderRector
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
`list()` assigns variables in reverse order - relevant in array assign
2018-11-11 12:22:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\Assign\ListSwapArrayOrderRector` ](../rules/Php70/Rector/Assign/ListSwapArrayOrderRector.php )
2018-10-12 23:15:00 +00:00
```diff
2021-03-23 22:13:35 +00:00
-list($a[], $a[]) = [1, 2];
+list($a[], $a[]) = array_reverse([1, 2]);
2020-11-25 21:34:34 +00:00
```
2018-10-12 23:15:00 +00:00
2020-11-25 21:34:34 +00:00
< br >
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
### MultiDirnameRector
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
Changes multiple `dirname()` calls to one with nesting level
2018-10-12 23:15:00 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\FuncCall\MultiDirnameRector` ](../rules/Php70/Rector/FuncCall/MultiDirnameRector.php )
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
-dirname(dirname($path));
+dirname($path, 2);
```
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
< br >
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
### NonVariableToVariableOnFunctionCallRector
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
Transform non variable like arguments to variable where a function or method expects an argument passed by reference
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\FuncCall\NonVariableToVariableOnFunctionCallRector` ](../rules/Php70/Rector/FuncCall/NonVariableToVariableOnFunctionCallRector.php )
2021-03-23 22:13:35 +00:00
```diff
-reset(a());
+$a = a(); reset($a);
2018-10-12 23:15:00 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
### Php4ConstructorRector
2021-02-13 14:57:30 +00:00
2021-03-23 22:13:35 +00:00
Changes PHP 4 style constructor to __construct.
2021-02-13 14:57:30 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\ClassMethod\Php4ConstructorRector` ](../rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php )
2021-02-13 14:57:30 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2021-02-13 14:57:30 +00:00
{
2021-03-23 22:13:35 +00:00
- public function SomeClass()
+ public function __construct()
2021-02-13 14:57:30 +00:00
{
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RandomFunctionRector
2018-11-11 12:22:35 +00:00
2021-04-10 18:18:49 +00:00
Changes rand, srand and getrandmax by new mt_* alternatives.
2018-10-12 23:15:00 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\FuncCall\RandomFunctionRector` ](../rules/Php70/Rector/FuncCall/RandomFunctionRector.php )
2018-10-12 23:15:00 +00:00
```diff
2021-03-23 22:13:35 +00:00
-rand();
+mt_rand();
2018-11-11 12:22:35 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
### ReduceMultipleDefaultSwitchRector
2020-11-27 12:00:56 +00:00
2021-03-23 22:13:35 +00:00
Remove first default switch, that is ignored
2020-11-27 12:00:56 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\Switch_\ReduceMultipleDefaultSwitchRector` ](../rules/Php70/Rector/Switch_/ReduceMultipleDefaultSwitchRector.php )
2020-11-27 12:00:56 +00:00
```diff
2021-03-23 22:13:35 +00:00
switch ($expr) {
default:
- echo "Hello World";
-
- default:
echo "Goodbye Moon!";
break;
}
2020-11-27 12:00:56 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### RenameMktimeWithoutArgsToTimeRector
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
Renames `mktime()` without arguments to `time()`
2018-11-11 12:22:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\FuncCall\RenameMktimeWithoutArgsToTimeRector` ](../rules/Php70/Rector/FuncCall/RenameMktimeWithoutArgsToTimeRector.php )
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
$time = mktime(1, 2, 3);
- $nextTime = mktime();
+ $nextTime = time();
2020-11-25 21:34:34 +00:00
}
}
2020-11-16 17:50:38 +00:00
```
2020-11-25 21:34:34 +00:00
< br >
2021-03-23 22:13:35 +00:00
### StaticCallOnNonStaticToInstanceCallRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Changes static call to instance call, where not useful
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector` ](../rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php )
2020-07-28 23:41:20 +00:00
2020-11-16 17:50:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
class Something
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
public function doWork()
2020-11-25 21:34:34 +00:00
{
}
2021-03-23 22:13:35 +00:00
}
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
class Another
{
2020-07-28 23:41:20 +00:00
public function run()
{
2021-03-23 22:13:35 +00:00
- return Something::doWork();
+ return (new Something)->doWork();
2020-07-28 23:41:20 +00:00
}
}
2018-10-12 23:15:00 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-03-23 16:13:04 +00:00
2021-03-23 22:13:35 +00:00
### TernaryToNullCoalescingRector
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
Changes unneeded null check to ?? operator
2018-10-12 23:15:00 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\Ternary\TernaryToNullCoalescingRector` ](../rules/Php70/Rector/Ternary/TernaryToNullCoalescingRector.php )
2019-05-29 13:40:20 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$value === null ? 10 : $value;
+$value ?? 10;
2019-10-15 14:46:31 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-10-15 14:46:31 +00:00
2021-03-23 22:13:35 +00:00
```diff
-isset($value) ? $value : 10;
+$value ?? 10;
```
2021-01-01 18:59:23 +00:00
2021-03-23 22:13:35 +00:00
< br >
2021-01-01 18:59:23 +00:00
2021-03-23 22:13:35 +00:00
### TernaryToSpaceshipRector
2021-01-01 18:59:23 +00:00
2021-03-23 22:13:35 +00:00
Use < => spaceship instead of ternary with same effect
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\Ternary\TernaryToSpaceshipRector` ](../rules/Php70/Rector/Ternary/TernaryToSpaceshipRector.php )
2021-03-23 22:13:35 +00:00
```diff
function order_func($a, $b) {
- return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
+ return $a < => $b;
}
```
< br >
### ThisCallOnStaticMethodToStaticCallRector
Changes `$this->call()` to static method to static call
2021-01-01 18:59:23 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector` ](../rules/Php70/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php )
2021-03-23 22:13:35 +00:00
```diff
class SomeClass
2021-01-01 18:59:23 +00:00
{
2021-03-23 22:13:35 +00:00
public static function run()
2021-01-01 18:59:23 +00:00
{
2021-03-23 22:13:35 +00:00
- $this->eat();
+ static::eat();
2021-01-01 18:59:23 +00:00
}
2021-03-23 22:13:35 +00:00
public static function eat()
2021-01-01 18:59:23 +00:00
{
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### WrapVariableVariableNameInCurlyBracesRector
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
Ensure variable variables are wrapped in curly braces
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php70\Rector\Variable\WrapVariableVariableNameInCurlyBracesRector` ](../rules/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector.php )
2019-05-29 13:40:20 +00:00
```diff
2021-03-23 22:13:35 +00:00
function run($foo)
2020-01-31 14:26:54 +00:00
{
2021-03-23 22:13:35 +00:00
- global $$foo->bar;
+ global ${$foo->bar};
2020-11-16 17:50:38 +00:00
}
2020-01-31 14:26:54 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-31 14:26:54 +00:00
2021-03-23 22:13:35 +00:00
## Php71
2020-01-17 00:16:38 +00:00
2021-03-23 22:13:35 +00:00
### AssignArrayToStringRector
2020-01-17 00:16:38 +00:00
2021-03-23 22:13:35 +00:00
String cannot be turned into array by assignment anymore
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php71\Rector\Assign\AssignArrayToStringRector` ](../rules/Php71/Rector/Assign/AssignArrayToStringRector.php )
2020-01-17 00:16:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$string = '';
+$string = [];
$string[] = 1;
```
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
< br >
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
### BinaryOpBetweenNumberAndStringRector
Change binary operation between some number + string to PHP 7.1 compatible version
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php71\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector` ](../rules/Php71/Rector/BinaryOp/BinaryOpBetweenNumberAndStringRector.php )
2021-03-23 22:13:35 +00:00
```diff
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- $value = 5 + '';
- $value = 5.0 + 'hi';
+ $value = 5 + 0;
2021-08-01 00:25:55 +00:00
+ $value = 5.0 + 0;
2020-11-25 21:34:34 +00:00
}
2021-03-23 22:13:35 +00:00
}
2020-01-17 00:16:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-01-17 00:16:38 +00:00
2021-03-23 22:13:35 +00:00
### CountOnNullRector
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
Changes `count()` on null to safe ternary check
2018-10-12 23:15:00 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php71\Rector\FuncCall\CountOnNullRector` ](../rules/Php71/Rector/FuncCall/CountOnNullRector.php )
2018-10-12 23:15:00 +00:00
```diff
2021-03-23 22:13:35 +00:00
$values = null;
-$count = count($values);
+$count = count((array) $values);
2018-10-12 23:15:00 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-10-23 18:58:57 +00:00
2021-03-23 22:13:35 +00:00
### IsIterableRector
2019-10-15 14:46:31 +00:00
2021-04-10 18:18:49 +00:00
Changes is_array + Traversable check to is_iterable
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php71\Rector\BooleanOr\IsIterableRector` ](../rules/Php71/Rector/BooleanOr/IsIterableRector.php )
2019-10-15 14:46:31 +00:00
```diff
2021-03-23 22:13:35 +00:00
-is_array($foo) || $foo instanceof Traversable;
+is_iterable($foo);
2019-10-15 14:46:31 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-01 16:55:10 +00:00
2021-03-23 22:13:35 +00:00
### ListToArrayDestructRector
2020-01-17 00:16:38 +00:00
2021-05-30 07:54:06 +00:00
Change `list()` to array destruct
2020-01-17 00:16:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php71\Rector\List_\ListToArrayDestructRector` ](../rules/Php71/Rector/List_/ListToArrayDestructRector.php )
2020-11-16 17:50:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- list($id1, $name1) = $data;
+ [$id1, $name1] = $data;
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
- foreach ($data as list($id, $name)) {
+ foreach ($data as [$id, $name]) {
}
2020-11-25 21:34:34 +00:00
}
}
2020-11-16 17:50:38 +00:00
```
2020-01-17 00:16:38 +00:00
2021-03-23 22:13:35 +00:00
< br >
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
### MultiExceptionCatchRector
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
Changes multi catch of same exception to single one | separated.
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php71\Rector\TryCatch\MultiExceptionCatchRector` ](../rules/Php71/Rector/TryCatch/MultiExceptionCatchRector.php )
2021-03-23 22:13:35 +00:00
```diff
try {
2021-08-01 00:25:55 +00:00
// Some code...
2021-03-23 22:13:35 +00:00
-} catch (ExceptionType1 $exception) {
- $sameCode;
-} catch (ExceptionType2 $exception) {
+} catch (ExceptionType1 | ExceptionType2 $exception) {
2021-08-01 00:25:55 +00:00
$sameCode;
2021-03-23 22:13:35 +00:00
}
2020-11-25 21:34:34 +00:00
```
2019-05-29 13:40:20 +00:00
2020-11-25 21:34:34 +00:00
< br >
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
### PublicConstantVisibilityRector
2019-03-31 12:25:39 +00:00
2021-03-23 22:13:35 +00:00
Add explicit public constant visibility.
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php71\Rector\ClassConst\PublicConstantVisibilityRector` ](../rules/Php71/Rector/ClassConst/PublicConstantVisibilityRector.php )
2019-05-29 13:40:20 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- const HEY = 'you';
+ public const HEY = 'you';
2020-11-25 21:34:34 +00:00
}
2020-11-16 17:50:38 +00:00
```
2019-05-29 13:40:20 +00:00
2020-11-25 21:34:34 +00:00
< br >
2021-03-23 22:13:35 +00:00
### RemoveExtraParametersRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Remove extra parameters
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector` ](../rules/Php71/Rector/FuncCall/RemoveExtraParametersRector.php )
2019-05-29 13:40:20 +00:00
```diff
2021-03-23 22:13:35 +00:00
-strlen("asdf", 1);
+strlen("asdf");
2019-03-31 12:25:39 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-03-31 12:25:39 +00:00
2021-03-23 22:13:35 +00:00
### ReservedObjectRector
2019-11-14 16:48:46 +00:00
2021-03-23 22:13:35 +00:00
Changes reserved "Object" name to "< Smart > Object" where < Smart > can be configured
2019-11-14 16:48:46 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2019-11-14 16:48:46 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php71\Rector\Name\ReservedObjectRector` ](../rules/Php71/Rector/Name/ReservedObjectRector.php )
2019-11-14 16:48:46 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Php71\Rector\Name\ReservedObjectRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(ReservedObjectRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'ReservedObject' => 'SmartObject',
'Object' => 'AnotherSmartObject',
]);
2021-03-23 22:13:35 +00:00
};
```
↓
```diff
-class Object
+class SmartObject
2020-11-25 21:34:34 +00:00
{
}
2019-11-14 16:48:46 +00:00
```
2020-11-25 21:34:34 +00:00
< br >
2021-03-23 22:13:35 +00:00
## Php72
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
### CreateFunctionToAnonymousFunctionRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Use anonymous functions instead of deprecated `create_function()`
2019-11-29 13:19:49 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector` ](../rules/Php72/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php )
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
```diff
class ClassWithCreateFunction
2020-07-28 23:41:20 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- $callable = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
+ $callable = function($matches) use ($delimiter) {
+ return $delimiter . strtolower($matches[1]);
+ };
2020-11-25 21:34:34 +00:00
}
2019-11-29 13:19:49 +00:00
}
2018-10-12 23:15:00 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
### GetClassOnNullRector
2019-01-22 20:34:38 +00:00
2021-03-23 22:13:35 +00:00
Null is no more allowed in `get_class()`
2019-01-22 20:34:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\FuncCall\GetClassOnNullRector` ](../rules/Php72/Rector/FuncCall/GetClassOnNullRector.php )
2019-01-22 20:34:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function getItem()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
$value = null;
- return get_class($value);
+ return $value !== null ? get_class($value) : self::class;
2020-11-25 21:34:34 +00:00
}
}
2019-01-22 20:34:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-01-22 20:34:38 +00:00
2021-03-23 22:13:35 +00:00
### IsObjectOnIncompleteClassRector
2020-03-15 09:16:55 +00:00
2021-03-23 22:13:35 +00:00
Incomplete class returns inverted bool on `is_object()`
2020-03-15 09:16:55 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\FuncCall\IsObjectOnIncompleteClassRector` ](../rules/Php72/Rector/FuncCall/IsObjectOnIncompleteClassRector.php )
2020-03-15 09:16:55 +00:00
```diff
2021-03-23 22:13:35 +00:00
$incompleteObject = new __PHP_Incomplete_Class;
-$isObject = is_object($incompleteObject);
+$isObject = ! is_object($incompleteObject);
2020-03-15 09:16:55 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-03-15 09:16:55 +00:00
2021-03-23 22:13:35 +00:00
### ListEachRector
2018-12-14 19:35:35 +00:00
2021-03-23 22:13:35 +00:00
`each()` function is deprecated, use `key()` and `current()` instead
2018-12-14 19:35:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\Assign\ListEachRector` ](../rules/Php72/Rector/Assign/ListEachRector.php )
2018-12-14 19:35:35 +00:00
```diff
2021-03-23 22:13:35 +00:00
-list($key, $callback) = each($callbacks);
+$key = key($callbacks);
+$callback = current($callbacks);
+next($callbacks);
2018-12-14 19:35:35 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
### ParseStrWithResultArgumentRector
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
Use `$result` argument in `parse_str()` function
2018-10-12 23:15:00 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\FuncCall\ParseStrWithResultArgumentRector` ](../rules/Php72/Rector/FuncCall/ParseStrWithResultArgumentRector.php )
2020-11-16 17:50:38 +00:00
2018-10-12 23:15:00 +00:00
```diff
2021-03-23 22:13:35 +00:00
-parse_str($this->query);
-$data = get_defined_vars();
+parse_str($this->query, $result);
+$data = $result;
2018-10-12 23:15:00 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
### ReplaceEachAssignmentWithKeyCurrentRector
2020-08-10 21:59:40 +00:00
2021-03-23 22:13:35 +00:00
Replace `each()` assign outside loop
2020-08-10 21:59:40 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\Assign\ReplaceEachAssignmentWithKeyCurrentRector` ](../rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php )
2020-11-16 17:50:38 +00:00
2020-08-10 21:59:40 +00:00
```diff
2021-03-23 22:13:35 +00:00
$array = ['b' => 1, 'a' => 2];
-$eachedArray = each($array);
+$eachedArray[1] = current($array);
+$eachedArray['value'] = current($array);
+$eachedArray[0] = key($array);
+$eachedArray['key'] = key($array);
+next($array);
2020-08-10 21:59:40 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-10 21:59:40 +00:00
2021-03-23 22:13:35 +00:00
### StringifyDefineRector
2020-08-16 11:42:22 +00:00
2021-03-23 22:13:35 +00:00
Make first argument of `define()` string
2020-08-16 11:42:22 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\FuncCall\StringifyDefineRector` ](../rules/Php72/Rector/FuncCall/StringifyDefineRector.php )
2020-11-16 17:50:38 +00:00
2020-08-16 11:42:22 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-08-16 11:42:22 +00:00
{
2021-03-23 22:13:35 +00:00
public function run(int $a)
{
- define(CONSTANT_2, 'value');
+ define('CONSTANT_2', 'value');
define('CONSTANT', 'value');
}
2020-08-16 11:42:22 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-07 22:21:17 +00:00
2021-03-23 22:13:35 +00:00
### StringsAssertNakedRector
2020-08-07 22:21:17 +00:00
2021-03-23 22:13:35 +00:00
String asserts must be passed directly to `assert()`
2020-08-07 22:21:17 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\FuncCall\StringsAssertNakedRector` ](../rules/Php72/Rector/FuncCall/StringsAssertNakedRector.php )
2020-11-16 17:50:38 +00:00
2020-08-07 22:21:17 +00:00
```diff
2021-03-23 22:13:35 +00:00
function nakedAssert()
2020-08-07 22:21:17 +00:00
{
2021-03-23 22:13:35 +00:00
- assert('true === true');
- assert("true === true");
+ assert(true === true);
+ assert(true === true);
2020-08-07 22:21:17 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-11-06 23:52:19 +00:00
2021-03-23 22:13:35 +00:00
### UnsetCastRector
2019-11-06 23:52:19 +00:00
2021-03-23 22:13:35 +00:00
Removes (unset) cast
2019-11-06 23:52:19 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\Unset_\UnsetCastRector` ](../rules/Php72/Rector/Unset_/UnsetCastRector.php )
2020-11-16 17:50:38 +00:00
2019-11-06 23:52:19 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$different = (unset) $value;
+$different = null;
2019-08-05 21:10:47 +00:00
2021-03-23 22:13:35 +00:00
-$value = (unset) $value;
+unset($value);
2019-08-05 21:10:47 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-07 22:21:17 +00:00
2021-03-23 22:13:35 +00:00
### WhileEachToForeachRector
2020-08-07 22:21:17 +00:00
2021-03-23 22:13:35 +00:00
`each()` function is deprecated, use `foreach()` instead.
2020-08-07 22:21:17 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php72\Rector\While_\WhileEachToForeachRector` ](../rules/Php72/Rector/While_/WhileEachToForeachRector.php )
2020-11-16 17:50:38 +00:00
2020-08-07 22:21:17 +00:00
```diff
2021-03-23 22:13:35 +00:00
-while (list($key, $callback) = each($callbacks)) {
+foreach ($callbacks as $key => $callback) {
// ...
2020-08-07 22:21:17 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-05-09 22:05:46 +00:00
2020-05-31 15:26:08 +00:00
```diff
2021-03-23 22:13:35 +00:00
-while (list($key) = each($callbacks)) {
+foreach (array_keys($callbacks) as $key) {
// ...
2019-09-27 22:34:34 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-09-27 22:34:34 +00:00
2021-03-23 22:13:35 +00:00
## Php73
2020-11-19 01:09:53 +00:00
2021-03-23 22:13:35 +00:00
### ArrayKeyFirstLastRector
2020-11-19 01:09:53 +00:00
2021-03-23 22:13:35 +00:00
Make use of `array_key_first()` and `array_key_last()`
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector` ](../rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php )
2020-11-19 01:09:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
-reset($items);
-$firstKey = key($items);
+$firstKey = array_key_first($items);
2019-05-19 08:27:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-19 08:27:38 +00:00
2019-02-04 00:32:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
-end($items);
-$lastKey = key($items);
+$lastKey = array_key_last($items);
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
### IsCountableRector
2019-09-27 22:34:34 +00:00
2021-04-10 18:18:49 +00:00
Changes is_array + Countable check to is_countable
2019-09-27 22:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php73\Rector\BooleanOr\IsCountableRector` ](../rules/Php73/Rector/BooleanOr/IsCountableRector.php )
2019-09-27 22:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
-is_array($foo) || $foo instanceof Countable;
+is_countable($foo);
2019-09-27 22:34:34 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-21 14:36:16 +00:00
2021-03-23 22:13:35 +00:00
### JsonThrowOnErrorRector
2019-02-21 14:36:16 +00:00
2021-03-23 22:13:35 +00:00
Adds JSON_THROW_ON_ERROR to `json_encode()` and `json_decode()` to throw JsonException on error
2019-09-25 08:49:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector` ](../rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php )
2019-02-21 14:36:16 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
-json_encode($content);
-json_decode($json);
+json_encode($content, JSON_THROW_ON_ERROR);
2021-04-18 13:01:56 +00:00
+json_decode($json, null, 512, JSON_THROW_ON_ERROR);
2020-11-25 21:34:34 +00:00
```
2019-05-01 23:56:58 +00:00
2020-11-25 21:34:34 +00:00
< br >
2019-05-01 23:56:58 +00:00
2021-03-23 22:13:35 +00:00
### RegexDashEscapeRector
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
Escape - in some cases
2019-02-04 00:32:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php73\Rector\FuncCall\RegexDashEscapeRector` ](../rules/Php73/Rector/FuncCall/RegexDashEscapeRector.php )
2019-02-04 00:32:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
-preg_match("#[\w-()]#", 'some text');
+preg_match("#[\w\-()]#", 'some text');
2020-11-25 21:34:34 +00:00
```
2019-09-25 08:49:53 +00:00
2020-11-25 21:34:34 +00:00
< br >
2021-03-23 22:13:35 +00:00
### SensitiveConstantNameRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Changes case insensitive constants to sensitive ones.
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector` ](../rules/Php73/Rector/ConstFetch/SensitiveConstantNameRector.php )
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
define('FOO', 42, true);
var_dump(FOO);
-var_dump(foo);
+var_dump(FOO);
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
### SensitiveDefineRector
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
Changes case insensitive constants to sensitive ones.
2019-09-25 08:49:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php73\Rector\FuncCall\SensitiveDefineRector` ](../rules/Php73/Rector/FuncCall/SensitiveDefineRector.php )
2019-02-04 00:32:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
-define('FOO', 42, true);
+define('FOO', 42);
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
### SensitiveHereNowDocRector
2019-02-21 14:36:16 +00:00
2021-03-23 22:13:35 +00:00
Changes heredoc/nowdoc that contains closing word to safe wrapper name
2020-07-28 23:41:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php73\Rector\String_\SensitiveHereNowDocRector` ](../rules/Php73/Rector/String_/SensitiveHereNowDocRector.php )
2019-02-21 14:36:16 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$value = < < < A
+$value = < < < A_WRAP
A
-A
+A_WRAP
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
### SetCookieRector
2019-05-29 13:40:20 +00:00
2021-04-10 18:18:49 +00:00
Convert setcookie argument to PHP7.3 option array
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php73\Rector\FuncCall\SetCookieRector` ](../rules/Php73/Rector/FuncCall/SetCookieRector.php )
2019-02-04 00:32:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
-setcookie('name', $value, 360);
+setcookie('name', $value, ['expires' => 360]);
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
```diff
-setcookie('name', $name, 0, '', '', true, true);
+setcookie('name', $name, ['expires' => 0, 'path' => '', 'domain' => '', 'secure' => true, 'httponly' => true]);
```
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
### StringifyStrNeedlesRector
Makes needles explicit strings
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector` ](../rules/Php73/Rector/FuncCall/StringifyStrNeedlesRector.php )
2019-02-04 00:32:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
$needle = 5;
-$fivePosition = strpos('725', $needle);
+$fivePosition = strpos('725', (string) $needle);
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
## Php74
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
### AddLiteralSeparatorToNumberRector
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
Add "_" as thousands separator in numbers for higher or equals to limitValue config
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector` ](../rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php )
2021-03-23 22:13:35 +00:00
```php
use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(AddLiteralSeparatorToNumberRector::class)
2022-02-20 00:35:49 +00:00
->configure([
AddLiteralSeparatorToNumberRector::LIMIT_VALUE => 1000000,
]);
2021-03-23 22:13:35 +00:00
};
```
↓
2019-03-09 13:24:30 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-07-28 23:41:20 +00:00
{
2020-11-25 21:34:34 +00:00
public function run()
2020-07-28 23:41:20 +00:00
{
2021-03-23 22:13:35 +00:00
- $int = 500000;
- $float = 1000500.001;
+ $int = 500_000;
+ $float = 1_000_500.001;
2020-11-25 21:34:34 +00:00
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### ArrayKeyExistsOnPropertyRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Change `array_key_exists()` on property to `property_exists()`
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector` ](../rules/Php74/Rector/FuncCall/ArrayKeyExistsOnPropertyRector.php )
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public $value;
2020-07-28 23:41:20 +00:00
}
2021-03-23 22:13:35 +00:00
$someClass = new SomeClass;
-array_key_exists('value', $someClass);
+property_exists($someClass, 'value');
2019-03-09 13:24:30 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-03-09 13:24:30 +00:00
2021-03-23 22:13:35 +00:00
### ArraySpreadInsteadOfArrayMergeRector
2018-12-22 11:22:28 +00:00
2021-12-05 00:35:40 +00:00
Change `array_merge()` to spread operator
2018-12-22 11:22:28 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector` ](../rules/Php74/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php )
2018-12-22 11:22:28 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($iter1, $iter2)
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- $values = array_merge(iterator_to_array($iter1), iterator_to_array($iter2));
+ $values = [...$iter1, ...$iter2];
// Or to generalize to all iterables
- $anotherValues = array_merge(
- is_array($iter1) ? $iter1 : iterator_to_array($iter1),
- is_array($iter2) ? $iter2 : iterator_to_array($iter2)
- );
+ $anotherValues = [...$iter1, ...$iter2];
2020-11-25 21:34:34 +00:00
}
}
2018-12-22 11:22:28 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-22 11:22:28 +00:00
2021-03-23 22:13:35 +00:00
### ChangeReflectionTypeToStringToGetNameRector
2018-12-31 11:50:32 +00:00
2021-03-23 22:13:35 +00:00
Change string calls on ReflectionType
2018-12-31 11:50:32 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector` ](../rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php )
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2021-01-01 18:59:23 +00:00
{
2021-03-23 22:13:35 +00:00
public function go(ReflectionFunction $reflectionFunction)
2021-01-01 18:59:23 +00:00
{
2021-03-23 22:13:35 +00:00
$parameterReflection = $reflectionFunction->getParameters()[0];
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
- $paramType = (string) $parameterReflection->getType();
+ $paramType = (string) ($parameterReflection->getType() ? $parameterReflection->getType()->getName() : null);
2018-10-21 22:26:45 +00:00
2021-03-23 22:13:35 +00:00
- $stringValue = 'hey' . $reflectionFunction->getReturnType();
+ $stringValue = 'hey' . ($reflectionFunction->getReturnType() ? $reflectionFunction->getReturnType()->getName() : null);
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
// keep
return $reflectionFunction->getReturnType();
2020-11-16 17:50:38 +00:00
}
}
2018-10-23 18:58:57 +00:00
```
2018-10-21 22:26:45 +00:00
2020-11-16 17:50:38 +00:00
< br >
2018-10-21 22:26:45 +00:00
2021-03-23 22:13:35 +00:00
### ClosureToArrowFunctionRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Change closure to arrow function
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector` ](../rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php )
2020-08-30 12:45:03 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($meetups)
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
- return array_filter($meetups, function (Meetup $meetup) {
- return is_object($meetup);
- });
+ return array_filter($meetups, fn(Meetup $meetup) => is_object($meetup));
2020-11-16 17:50:38 +00:00
}
}
2020-08-30 12:45:03 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-30 12:45:03 +00:00
2021-09-12 00:30:25 +00:00
### CurlyToSquareBracketArrayStringRector
Change curly based array and string to square bracket
- class: [`Rector\Php74\Rector\ArrayDimFetch\CurlyToSquareBracketArrayStringRector` ](../rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php )
```diff
$string = 'test';
-echo $string{0};
+echo $string[0];
$array = ['test'];
-echo $array{0};
+echo $array[0];
```
< br >
2021-03-23 22:13:35 +00:00
### ExportToReflectionFunctionRector
2019-02-21 14:36:16 +00:00
2021-03-23 22:13:35 +00:00
Change `export()` to ReflectionFunction alternatives
2019-06-02 07:45:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\StaticCall\ExportToReflectionFunctionRector` ](../rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php )
2019-06-02 07:45:37 +00:00
2020-11-16 00:40:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$reflectionFunction = ReflectionFunction::export('foo');
-$reflectionFunctionAsString = ReflectionFunction::export('foo', true);
+$reflectionFunction = new ReflectionFunction('foo');
+$reflectionFunctionAsString = (string) new ReflectionFunction('foo');
2020-11-16 00:40:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-11-16 00:40:38 +00:00
2021-03-23 22:13:35 +00:00
### FilterVarToAddSlashesRector
2019-06-02 07:45:37 +00:00
2021-03-23 22:13:35 +00:00
Change `filter_var()` with slash escaping to `addslashes()`
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector` ](../rules/Php74/Rector/FuncCall/FilterVarToAddSlashesRector.php )
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
$var= "Satya's here!";
-filter_var($var, FILTER_SANITIZE_MAGIC_QUOTES);
+addslashes($var);
2019-02-21 14:36:16 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-21 14:36:16 +00:00
2021-03-23 22:13:35 +00:00
### MbStrrposEncodingArgumentPositionRector
2020-11-14 01:27:55 +00:00
2021-03-23 22:13:35 +00:00
Change `mb_strrpos()` encoding argument position
2020-11-14 01:27:55 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector` ](../rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php )
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
-mb_strrpos($text, "abc", "UTF-8");
+mb_strrpos($text, "abc", 0, "UTF-8");
2020-11-14 01:27:55 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-11-14 01:27:55 +00:00
2021-03-23 22:13:35 +00:00
### NullCoalescingOperatorRector
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
Use null coalescing operator ??=
2020-07-21 09:23:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\Assign\NullCoalescingOperatorRector` ](../rules/Php74/Rector/Assign/NullCoalescingOperatorRector.php )
2020-07-21 09:23:25 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
$array = [];
-$array['user_id'] = $array['user_id'] ?? 'value';
+$array['user_id'] ??= 'value';
2020-11-25 21:34:34 +00:00
```
< br >
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
### RealToFloatTypeCastRector
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
Change deprecated (real) to (float)
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\Double\RealToFloatTypeCastRector` ](../rules/Php74/Rector/Double/RealToFloatTypeCastRector.php )
2019-09-25 08:49:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
public function run()
{
- $number = (real) 5;
+ $number = (float) 5;
$number = (float) 5;
$number = (double) 5;
}
}
2020-11-25 21:34:34 +00:00
```
< br >
2022-01-02 00:31:07 +00:00
### ReservedFnFunctionRector
Change `fn()` function name to `f()` , since it will be reserved keyword
- class: [`Rector\Php74\Rector\Function_\ReservedFnFunctionRector` ](../rules/Php74/Rector/Function_/ReservedFnFunctionRector.php )
```diff
class SomeClass
{
public function run()
{
- function fn($value)
+ function f($value)
{
return $value;
}
- fn(5);
+ f(5);
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RestoreDefaultNullToNullableTypePropertyRector
2020-11-19 01:09:53 +00:00
2021-03-23 22:13:35 +00:00
Add null default to properties with PHP 7.4 property nullable type
2020-11-19 01:09:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector` ](../rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php )
2020-11-19 01:09:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
- public ?string $name;
+ public ?string $name = null;
}
2020-11-19 01:09:53 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### TypedPropertyRector
2019-09-27 22:34:34 +00:00
2022-02-13 00:29:38 +00:00
Changes property type by `@var` annotations or default value.
2020-11-16 17:50:38 +00:00
2022-02-02 21:14:38 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php74\Rector\Property\TypedPropertyRector` ](../rules/Php74/Rector/Property/TypedPropertyRector.php )
2020-11-16 17:50:38 +00:00
2022-02-02 21:14:38 +00:00
```php
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(TypedPropertyRector::class)
2022-02-20 00:35:49 +00:00
->configure([
TypedPropertyRector::INLINE_PUBLIC => false,
]);
2022-02-02 21:14:38 +00:00
};
```
↓
2019-09-27 22:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
{
- /**
- * @var int
- */
2021-07-22 18:40:24 +00:00
- private $count;
+ private int $count;
2022-02-13 00:29:38 +00:00
- private $isDone = false;
+ private bool $isDone = false;
2021-03-23 22:13:35 +00:00
}
2019-09-27 22:34:34 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-09-27 22:34:34 +00:00
2021-03-23 22:13:35 +00:00
## Php80
2018-07-31 12:50:39 +00:00
2021-12-10 07:56:16 +00:00
### AddParamBasedOnParentClassMethodRector
Add missing parameter based on parent class method
- class: [`Rector\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector` ](../rules/Php80/Rector/ClassMethod/AddParamBasedOnParentClassMethodRector.php )
```diff
class A
{
public function execute($foo)
{
}
}
class B extends A{
- public function execute()
+ public function execute($foo)
{
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### AnnotationToAttributeRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Change annotation to attribute
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\Class_\AnnotationToAttributeRector` ](../rules/Php80/Rector/Class_/AnnotationToAttributeRector.php )
2018-12-22 11:22:28 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2018-12-22 11:22:28 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(AnnotationToAttributeRector::class)
2021-12-06 21:12:03 +00:00
->configure([new AnnotationToAttribute('Symfony\Component\Routing\Annotation\Route')]);
2021-03-23 22:13:35 +00:00
};
```
2018-07-31 12:50:39 +00:00
2021-03-23 22:13:35 +00:00
↓
2018-07-31 12:50:39 +00:00
```diff
2021-03-23 22:13:35 +00:00
use Symfony\Component\Routing\Annotation\Route;
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
class SymfonyRoute
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- /**
- * @Route ("/path", name="action")
- */
+ #[Route(path: '/path', name: 'action')]
public function action()
2020-07-28 23:41:20 +00:00
{
}
}
2018-08-01 20:09:34 +00:00
```
2018-08-01 13:34:55 +00:00
2020-11-16 17:50:38 +00:00
< br >
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
### ChangeSwitchToMatchRector
2019-09-25 08:49:53 +00:00
2021-03-23 22:13:35 +00:00
Change `switch()` to `match()`
2019-03-09 13:24:30 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector` ](../rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php )
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-06-13 10:00:46 +00:00
-switch ($input) {
- case Lexer::T_SELECT:
- $statement = 'select';
- break;
- case Lexer::T_UPDATE:
- $statement = 'update';
- break;
- default:
- $statement = 'error';
-}
+$statement = match ($input) {
+ Lexer::T_SELECT => 'select',
+ Lexer::T_UPDATE => 'update',
+ default => 'error',
+};
2018-12-31 11:50:32 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-31 11:50:32 +00:00
2021-03-23 22:13:35 +00:00
### ClassOnObjectRector
2018-10-12 23:15:00 +00:00
2021-03-23 22:13:35 +00:00
Change get_class($object) to faster `$object::class`
2018-11-11 12:22:35 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\FuncCall\ClassOnObjectRector` ](../rules/Php80/Rector/FuncCall/ClassOnObjectRector.php )
2018-10-12 23:15:00 +00:00
2018-08-01 20:09:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-08-30 12:45:03 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($object)
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- return get_class($object);
+ return $object::class;
2020-11-25 21:34:34 +00:00
}
2020-08-30 12:45:03 +00:00
}
2018-05-05 12:48:33 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
### ClassPropertyAssignToConstructorPromotionRector
2019-05-01 23:56:58 +00:00
2021-03-23 22:13:35 +00:00
Change simple property init and assign to constructor promotion
2019-05-01 23:56:58 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector` ](../rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php )
2019-05-01 23:56:58 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- public float $someVariable;
-
2021-11-25 19:16:58 +00:00
public function __construct(
- float $someVariable = 0.0
+ private float $someVariable = 0.0
) {
2021-03-23 22:13:35 +00:00
- $this->someVariable = $someVariable;
2020-11-25 21:34:34 +00:00
}
}
2019-05-01 23:56:58 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-01 23:56:58 +00:00
2021-05-23 09:33:26 +00:00
### DoctrineAnnotationClassToAttributeRector
Refactor Doctrine `@annotation` annotated class to a PHP 8.0 attribute class
2021-06-06 07:59:41 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\Class_\DoctrineAnnotationClassToAttributeRector` ](../rules/Php80/Rector/Class_/DoctrineAnnotationClassToAttributeRector.php )
2021-06-06 07:59:41 +00:00
```php
use Rector\Php80\Rector\Class_\DoctrineAnnotationClassToAttributeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DoctrineAnnotationClassToAttributeRector::class)
2022-02-20 00:35:49 +00:00
->configure([
DoctrineAnnotationClassToAttributeRector::REMOVE_ANNOTATIONS => true,
]);
2021-06-06 07:59:41 +00:00
};
```
↓
2021-05-23 09:33:26 +00:00
```diff
-use Doctrine\Common\Annotations\Annotation\Target;
+use Attribute;
-/**
- * @Annotation
- * @Target ({"METHOD"})
- */
+#[Attribute(Attribute::TARGET_METHOD)]
class SomeAnnotation
{
}
```
< br >
2021-03-23 22:13:35 +00:00
### FinalPrivateToPrivateVisibilityRector
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
Changes method visibility from final private to only private
2019-02-04 00:32:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector` ](../rules/Php80/Rector/ClassMethod/FinalPrivateToPrivateVisibilityRector.php )
2019-02-04 00:32:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
- final private function getter() {
+ private function getter() {
return $this;
}
}
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
### GetDebugTypeRector
2020-06-17 12:12:29 +00:00
2021-03-23 22:13:35 +00:00
Change ternary type resolve to `get_debug_type()`
2020-06-17 12:12:29 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\Ternary\GetDebugTypeRector` ](../rules/Php80/Rector/Ternary/GetDebugTypeRector.php )
2020-06-17 12:12:29 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-07-28 23:41:20 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($value)
{
- return is_object($value) ? get_class($value) : gettype($value);
+ return get_debug_type($value);
}
2020-07-28 23:41:20 +00:00
}
2020-06-17 12:12:29 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-06-17 12:12:29 +00:00
2021-10-28 22:22:07 +00:00
### Php8ResourceReturnToObjectRector
Change `is_resource()` to instanceof Object
- class: [`Rector\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector` ](../rules/Php80/Rector/FuncCall/Php8ResourceReturnToObjectRector.php )
```diff
class SomeClass
{
public function run()
{
$ch = curl_init();
- is_resource($ch);
+ $ch instanceof \CurlHandle;
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveUnusedVariableInCatchRector
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
Remove unused variable in `catch()`
2019-02-04 00:32:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector` ](../rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php )
2019-02-04 00:32:53 +00:00
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
{
public function run()
{
try {
- } catch (Throwable $notUsedThrowable) {
+ } catch (Throwable) {
}
}
}
2020-11-25 21:34:34 +00:00
```
2019-02-04 00:32:53 +00:00
2020-11-25 21:34:34 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
### SetStateToStaticRector
2021-03-17 23:11:23 +00:00
2021-03-23 22:13:35 +00:00
Adds static visibility to `__set_state()` methods
2021-03-17 23:11:23 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\ClassMethod\SetStateToStaticRector` ](../rules/Php80/Rector/ClassMethod/SetStateToStaticRector.php )
2021-03-17 23:11:23 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
- public function __set_state($properties) {
+ public static function __set_state($properties) {
}
}
2021-03-17 23:11:23 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### StrContainsRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Replace `strpos()` !== false and `strstr()` with `str_contains()`
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\NotIdentical\StrContainsRector` ](../rules/Php80/Rector/NotIdentical/StrContainsRector.php )
2019-02-04 00:32:53 +00:00
2018-07-31 19:43:39 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- return strpos('abc', 'a') !== false;
+ return str_contains('abc', 'a');
2020-11-25 21:34:34 +00:00
}
2019-06-02 07:45:37 +00:00
}
2018-07-31 19:43:39 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
### StrEndsWithRector
2018-08-01 20:09:34 +00:00
2021-03-23 22:13:35 +00:00
Change helper functions to `str_ends_with()`
2019-09-25 08:49:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\Identical\StrEndsWithRector` ](../rules/Php80/Rector/Identical/StrEndsWithRector.php )
2018-08-01 20:09:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
{
- $isMatch = substr($haystack, -strlen($needle)) === $needle;
+ $isMatch = str_ends_with($haystack, $needle);
2021-07-22 18:40:24 +00:00
- $isNotMatch = substr($haystack, -strlen($needle)) !== $needle;
+ $isNotMatch = !str_ends_with($haystack, $needle);
}
}
```
< br >
```diff
class SomeClass
{
public function run()
{
- $isMatch = substr($haystack, -9) === 'hardcoded;
+ $isMatch = str_ends_with($haystack, 'hardcoded');
- $isNotMatch = substr($haystack, -9) !== 'hardcoded';
+ $isNotMatch = !str_ends_with($haystack, 'hardcoded');
2021-03-23 22:13:35 +00:00
}
2020-11-25 21:34:34 +00:00
}
2020-11-16 17:50:38 +00:00
```
2019-09-25 08:49:53 +00:00
2020-11-16 17:50:38 +00:00
< br >
2021-03-23 22:13:35 +00:00
### StrStartsWithRector
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
Change helper functions to `str_starts_with()`
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\Identical\StrStartsWithRector` ](../rules/Php80/Rector/Identical/StrStartsWithRector.php )
2020-11-16 17:50:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-07-28 23:41:20 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2020-07-28 23:41:20 +00:00
{
2021-03-23 22:13:35 +00:00
- $isMatch = substr($haystack, 0, strlen($needle)) === $needle;
+ $isMatch = str_starts_with($haystack, $needle);
- $isNotMatch = substr($haystack, 0, strlen($needle)) !== $needle;
+ $isNotMatch = ! str_starts_with($haystack, $needle);
2020-07-28 23:41:20 +00:00
}
}
2018-08-01 20:09:34 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
### StringableForToStringRector
2019-05-01 23:56:58 +00:00
2021-03-23 22:13:35 +00:00
Add `Stringable` interface to classes with `__toString()` method
2020-07-28 23:41:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\Class_\StringableForToStringRector` ](../rules/Php80/Rector/Class_/StringableForToStringRector.php )
2019-05-01 23:56:58 +00:00
```diff
2021-03-23 22:13:35 +00:00
-class SomeClass
+class SomeClass implements Stringable
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- public function __toString()
+ public function __toString(): string
2020-07-28 23:41:20 +00:00
{
2021-03-23 22:13:35 +00:00
return 'I can stringz';
2020-11-16 17:50:38 +00:00
}
2020-11-25 21:34:34 +00:00
}
```
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
< br >
2021-03-23 22:13:35 +00:00
### TokenGetAllToObjectRector
2020-11-25 21:34:34 +00:00
2021-07-22 18:40:24 +00:00
Convert `token_get_all` to `PhpToken::tokenize`
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector` ](../rules/Php80/Rector/FuncCall/TokenGetAllToObjectRector.php )
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
{
public function run()
{
- $tokens = token_get_all($code);
- foreach ($tokens as $token) {
- if (is_array($token)) {
- $name = token_name($token[0]);
- $text = $token[1];
- } else {
- $name = null;
- $text = $token;
- }
2021-07-22 18:40:24 +00:00
+ $tokens = \PhpToken::tokenize($code);
2021-03-23 22:13:35 +00:00
+ foreach ($tokens as $phpToken) {
+ $name = $phpToken->getTokenName();
+ $text = $phpToken->text;
}
}
}
2019-05-01 23:56:58 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-01 23:56:58 +00:00
2021-03-23 22:13:35 +00:00
### UnionTypesRector
2018-12-31 19:29:12 +00:00
2021-03-23 22:13:35 +00:00
Change docs types to union types, where possible (properties are covered by TypedPropertiesRector)
2020-07-28 23:41:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php80\Rector\FunctionLike\UnionTypesRector` ](../rules/Php80/Rector/FunctionLike/UnionTypesRector.php )
2019-02-04 00:32:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-07-28 23:41:20 +00:00
{
2021-03-23 22:13:35 +00:00
- /**
- * @param array|int $number
- * @return bool|float
- */
- public function go($number)
+ public function go(array|int $number): bool|float
2020-11-16 17:50:38 +00:00
{
}
2020-07-28 23:41:20 +00:00
}
2018-08-01 20:09:34 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-12-31 11:50:32 +00:00
2021-05-16 08:19:51 +00:00
## Php81
2021-07-23 16:35:16 +00:00
### FinalizePublicClassConstantRector
2022-01-30 00:27:52 +00:00
Add final to constants that does not have children
2021-07-23 16:35:16 +00:00
- class: [`Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector` ](../rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php )
```diff
class SomeClass
{
- public const NAME = 'value';
+ public final const NAME = 'value';
}
```
< br >
2021-11-21 00:34:01 +00:00
### IntersectionTypesRector
Change docs to intersection types, where possible (properties are covered by TypedPropertyRector (@todo))
- class: [`Rector\Php81\Rector\FunctionLike\IntersectionTypesRector` ](../rules/Php81/Rector/FunctionLike/IntersectionTypesRector.php )
```diff
final class SomeClass
{
- /**
2021-12-14 17:04:36 +00:00
- * @param Foo& Bar $types
2021-11-21 00:34:01 +00:00
- */
- public function process($types)
2021-12-14 17:04:36 +00:00
+ public function process(Foo& Bar $types)
2021-11-21 00:34:01 +00:00
{
}
}
```
< br >
2021-05-16 08:19:51 +00:00
### MyCLabsClassToEnumRector
Refactor MyCLabs enum class to native Enum
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector` ](../rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php )
2021-05-16 08:19:51 +00:00
```diff
-use MyCLabs\Enum\Enum;
-
-final class Action extends Enum
2021-12-05 00:35:40 +00:00
+enum Action : string
2021-05-16 08:19:51 +00:00
{
- private const VIEW = 'view';
- private const EDIT = 'edit';
+ case VIEW = 'view';
+ case EDIT = 'edit';
}
```
< br >
### MyCLabsMethodCallToEnumConstRector
Refactor MyCLabs enum fetch to Enum const
2021-05-23 09:33:26 +00:00
- class: [`Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector` ](../rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php )
2021-05-16 08:19:51 +00:00
```diff
-$name = SomeEnum::VALUE()->getKey();
+$name = SomeEnum::VALUE;
```
< br >
2021-11-14 00:36:15 +00:00
### NewInInitializerRector
Replace property declaration of new state with direct new
- class: [`Rector\Php81\Rector\ClassMethod\NewInInitializerRector` ](../rules/Php81/Rector/ClassMethod/NewInInitializerRector.php )
```diff
class SomeClass
{
2021-11-25 12:53:25 +00:00
- private Logger $logger;
-
2021-11-14 00:36:15 +00:00
public function __construct(
- ?Logger $logger = null,
2021-11-25 12:53:25 +00:00
+ private Logger $logger = new NullLogger,
2021-11-14 00:36:15 +00:00
) {
- $this->logger = $logger ?? new NullLogger;
}
}
```
< br >
2022-01-16 01:52:41 +00:00
### NullToStrictStringFuncCallArgRector
Change null to strict string defined function call args
- class: [`Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector` ](../rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php )
```diff
class SomeClass
{
public function run()
{
- preg_split("#a#", null);
+ preg_split("#a#", '');
}
}
```
< br >
2021-11-05 14:38:48 +00:00
### Php81ResourceReturnToObjectRector
Change `is_resource()` to instanceof Object
- class: [`Rector\Php81\Rector\FuncCall\Php81ResourceReturnToObjectRector` ](../rules/Php81/Rector/FuncCall/Php81ResourceReturnToObjectRector.php )
```diff
class SomeClass
{
public function run()
{
$f = finfo_open();
- is_resource($f);
+ $f instanceof \finfo;
}
}
```
< br >
2021-07-23 16:35:16 +00:00
### ReadOnlyPropertyRector
Decorate read-only property with `readonly` attribute
- class: [`Rector\Php81\Rector\Property\ReadOnlyPropertyRector` ](../rules/Php81/Rector/Property/ReadOnlyPropertyRector.php )
```diff
class SomeClass
{
public function __construct(
- private string $name
+ private readonly string $name
) {
}
public function getName()
{
return $this->name;
}
}
```
< br >
2021-08-15 00:30:09 +00:00
### SpatieEnumClassToEnumRector
Refactor Spatie enum class to native Enum
- class: [`Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector` ](../rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php )
```diff
-use \Spatie\Enum\Enum;
-
-/**
- * @method static self draft()
- * @method static self published()
- * @method static self archived()
- */
-class StatusEnum extends Enum
2021-12-05 00:35:40 +00:00
+enum StatusEnum : string
2021-08-15 00:30:09 +00:00
{
+ case draft = 'draft';
+ case published = 'published';
+ case archived = 'archived';
}
```
< br >
2021-03-23 22:13:35 +00:00
## PostRector
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
### ClassRenamingPostRector
Rename references for classes that were renamed during Rector run
2021-05-23 09:33:26 +00:00
- class: [`Rector\PostRector\Rector\ClassRenamingPostRector` ](../packages/PostRector/Rector/ClassRenamingPostRector.php )
2021-03-23 22:13:35 +00:00
```diff
-function (OriginalClass $someClass)
+function (RenamedClass $someClass)
2020-11-16 17:50:38 +00:00
{
}
2020-07-28 23:41:20 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
### NameImportingPostRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Imports fully qualified names
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\PostRector\Rector\NameImportingPostRector` ](../packages/PostRector/Rector/NameImportingPostRector.php )
2018-12-31 19:29:12 +00:00
```diff
2021-03-23 22:13:35 +00:00
+use App\AnotherClass;
2021-01-30 20:30:37 +00:00
+
class SomeClass
{
2021-03-23 22:13:35 +00:00
- public function run(App\AnotherClass $anotherClass)
+ public function run(AnotherClass $anotherClass)
{
}
2020-07-28 23:41:20 +00:00
}
2018-12-31 19:29:12 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-03-31 12:25:39 +00:00
2021-03-23 22:13:35 +00:00
### NodeAddingPostRector
2020-04-24 22:57:13 +00:00
2021-03-23 22:13:35 +00:00
Add nodes on weird positions
2020-04-24 22:57:13 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\PostRector\Rector\NodeAddingPostRector` ](../packages/PostRector/Rector/NodeAddingPostRector.php )
2020-04-24 22:57:13 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-04-24 22:57:13 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($value)
{
- return 1;
+ if ($value) {
+ return 1;
+ }
2020-07-28 23:41:20 +00:00
}
2020-04-24 22:57:13 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-04-24 22:57:13 +00:00
2021-03-23 22:13:35 +00:00
### NodeRemovingPostRector
2020-04-29 20:01:42 +00:00
2021-03-23 22:13:35 +00:00
Remove nodes from weird positions
2020-04-29 20:01:42 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\PostRector\Rector\NodeRemovingPostRector` ](../packages/PostRector/Rector/NodeRemovingPostRector.php )
2020-04-29 20:01:42 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
public function run($value)
{
- if ($value) {
- return 1;
- }
+ return 1;
}
2020-11-16 17:50:38 +00:00
}
2020-06-08 09:28:03 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-06-08 09:28:03 +00:00
2021-03-23 22:13:35 +00:00
### NodeToReplacePostRector
2020-04-29 21:23:14 +00:00
2021-03-23 22:13:35 +00:00
Replaces nodes on weird positions
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\PostRector\Rector\NodeToReplacePostRector` ](../packages/PostRector/Rector/NodeToReplacePostRector.php )
2020-04-29 21:23:14 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
public function run($value)
{
- return 1;
+ return $value;
}
}
2020-04-29 21:23:14 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-04-29 21:23:14 +00:00
2021-03-23 22:13:35 +00:00
### PropertyAddingPostRector
2020-06-05 10:33:30 +00:00
2021-03-23 22:13:35 +00:00
Add dependency properties
2020-06-05 10:33:30 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\PostRector\Rector\PropertyAddingPostRector` ](../packages/PostRector/Rector/PropertyAddingPostRector.php )
2020-06-05 10:33:30 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
+ private $value;
2020-11-25 21:34:34 +00:00
public function run()
{
2021-03-23 22:13:35 +00:00
return $this->value;
2020-11-25 21:34:34 +00:00
}
2020-11-16 17:50:38 +00:00
}
2020-06-05 10:33:30 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-06-05 10:33:30 +00:00
2021-03-23 22:13:35 +00:00
### UseAddingPostRector
2020-03-26 22:22:29 +00:00
2021-03-23 22:13:35 +00:00
Add unique use imports collected during Rector run
2020-03-26 22:22:29 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\PostRector\Rector\UseAddingPostRector` ](../packages/PostRector/Rector/UseAddingPostRector.php )
2020-04-24 12:00:49 +00:00
```diff
2021-03-23 22:13:35 +00:00
+use App\AnotherClass;
+
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
public function run(AnotherClass $anotherClass)
2020-11-25 21:34:34 +00:00
{
}
}
2020-04-24 12:00:49 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-04-24 12:00:49 +00:00
2021-03-23 22:13:35 +00:00
## Privatization
2020-06-05 10:33:30 +00:00
2021-03-23 22:13:35 +00:00
### ChangeGlobalVariablesToPropertiesRector
2021-01-28 18:26:56 +00:00
2021-03-23 22:13:35 +00:00
Change global `$variables` to private properties
2021-01-28 18:26:56 +00:00
2021-11-25 18:41:12 +00:00
- class: [`Rector\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector` ](../rules/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector.php )
2021-01-28 18:26:56 +00:00
```diff
class SomeClass
{
2021-03-23 22:13:35 +00:00
+ private $variable;
public function go()
{
- global $variable;
- $variable = 5;
+ $this->variable = 5;
}
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
public function run()
{
2021-03-23 22:13:35 +00:00
- global $variable;
- var_dump($variable);
+ var_dump($this->variable);
2020-11-25 21:34:34 +00:00
}
2020-11-16 17:50:38 +00:00
}
2020-06-05 10:33:30 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-06-05 10:33:30 +00:00
2021-03-23 22:13:35 +00:00
### ChangeLocalPropertyToVariableRector
2019-11-29 13:19:49 +00:00
2021-03-23 22:13:35 +00:00
Change local property used in single method to local variable
2019-11-29 13:19:49 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Privatization\Rector\Class_\ChangeLocalPropertyToVariableRector` ](../rules/Privatization/Rector/Class_/ChangeLocalPropertyToVariableRector.php )
2019-11-29 13:19:49 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
- private $count;
2020-11-25 21:34:34 +00:00
public function run()
{
2021-03-23 22:13:35 +00:00
- $this->count = 5;
- return $this->count;
+ $count = 5;
+ return $count;
2020-11-25 21:34:34 +00:00
}
}
2019-11-29 13:19:49 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-11-29 13:19:49 +00:00
2021-03-23 22:13:35 +00:00
### ChangeReadOnlyPropertyWithDefaultValueToConstantRector
2019-06-02 07:45:37 +00:00
2021-03-23 22:13:35 +00:00
Change property with read only status with default value to constant
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector` ](../rules/Privatization/Rector/Property/ChangeReadOnlyPropertyWithDefaultValueToConstantRector.php )
2019-06-02 07:45:37 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
/**
* @var string[]
*/
- private $magicMethods = [
+ private const MAGIC_METHODS = [
'__toString',
'__wakeup',
];
2020-11-25 21:34:34 +00:00
public function run()
{
2021-03-23 22:13:35 +00:00
- foreach ($this->magicMethods as $magicMethod) {
+ foreach (self::MAGIC_METHODS as $magicMethod) {
echo $magicMethod;
2020-11-25 21:34:34 +00:00
}
}
}
2019-06-02 07:45:37 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-06-02 07:45:37 +00:00
2021-03-23 22:13:35 +00:00
### ChangeReadOnlyVariableWithDefaultValueToConstantRector
2019-03-31 12:25:39 +00:00
2021-03-23 22:13:35 +00:00
Change variable with read only status with default value to constant
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector` ](../rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php )
2019-03-31 12:25:39 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
2021-03-23 22:13:35 +00:00
+ /**
+ * @var string[]
+ */
+ private const REPLACEMENTS = [
+ 'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
+ 'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
+ ];
+
2020-11-25 21:34:34 +00:00
public function run()
{
2021-03-23 22:13:35 +00:00
- $replacements = [
- 'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
- 'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
- ];
-
- foreach ($replacements as $class => $method) {
+ foreach (self::REPLACEMENTS as $class => $method) {
}
2020-11-25 21:34:34 +00:00
}
}
2019-03-31 12:25:39 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-03-31 12:25:39 +00:00
2021-03-23 22:13:35 +00:00
### FinalizeClassesWithoutChildrenRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Finalize every class that has no children
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector` ](../rules/Privatization/Rector/Class_/FinalizeClassesWithoutChildrenRector.php )
2019-03-16 20:31:46 +00:00
```diff
2021-03-23 22:13:35 +00:00
-class FirstClass
+final class FirstClass
{
}
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
class SecondClass
{
}
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
-class ThirdClass extends SecondClass
+final class ThirdClass extends SecondClass
{
2020-11-25 21:34:34 +00:00
}
2020-11-16 17:50:38 +00:00
```
2019-07-06 19:51:36 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
### PrivatizeFinalClassMethodRector
2021-03-12 23:52:38 +00:00
2021-03-23 22:13:35 +00:00
Change protected class method to private if possible
2020-10-21 21:06:24 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector` ](../rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php )
2020-10-21 21:06:24 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeClass
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
- protected function someMethod()
+ private function someMethod()
2020-11-16 17:50:38 +00:00
{
}
}
```
2020-10-21 21:06:24 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-10-21 21:06:24 +00:00
2021-03-23 22:13:35 +00:00
### PrivatizeFinalClassPropertyRector
2021-03-12 23:52:38 +00:00
2021-03-23 22:13:35 +00:00
Change property to private if possible
2020-07-22 21:31:42 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector` ](../rules/Privatization/Rector/Property/PrivatizeFinalClassPropertyRector.php )
2020-07-22 21:31:42 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
{
- protected $value;
+ private $value;
}
2020-11-16 17:50:38 +00:00
```
2020-07-22 21:31:42 +00:00
2020-11-16 17:50:38 +00:00
< br >
2021-03-23 22:13:35 +00:00
### PrivatizeLocalGetterToPropertyRector
2018-09-28 16:33:35 +00:00
2021-03-23 22:13:35 +00:00
Privatize getter of local property to property
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector` ](../rules/Privatization/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php )
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
```diff
class SomeClass
{
private $some;
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
public function run()
{
- return $this->getSome() + 5;
+ return $this->some + 5;
}
2018-09-28 16:33:35 +00:00
2021-03-23 22:13:35 +00:00
private function getSome()
{
return $this->some;
}
}
2018-09-28 16:33:35 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
### RepeatedLiteralToClassConstantRector
2020-10-01 19:45:29 +00:00
2021-03-23 22:13:35 +00:00
Replace repeated strings with constant
2020-10-01 19:45:29 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Privatization\Rector\Class_\RepeatedLiteralToClassConstantRector` ](../rules/Privatization/Rector/Class_/RepeatedLiteralToClassConstantRector.php )
2020-10-01 19:45:29 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
+ /**
+ * @var string
+ */
+ private const REQUIRES = 'requires';
public function run($key, $items)
{
- if ($key === 'requires') {
- return $items['requires'];
+ if ($key === self::REQUIRES) {
+ return $items[self::REQUIRES];
}
}
}
2020-10-01 19:45:29 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-01 19:45:29 +00:00
2021-03-23 22:13:35 +00:00
### ReplaceStringWithClassConstantRector
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
Replace string values in specific method call by constant of provided class
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-07-28 23:41:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Privatization\Rector\MethodCall\ReplaceStringWithClassConstantRector` ](../rules/Privatization/Rector/MethodCall/ReplaceStringWithClassConstantRector.php )
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
```php
2021-03-23 22:13:35 +00:00
use Rector\Privatization\Rector\MethodCall\ReplaceStringWithClassConstantRector;
use Rector\Privatization\ValueObject\ReplaceStringWithClassConstant;
2020-11-25 21:34:34 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-05-19 08:27:38 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(ReplaceStringWithClassConstantRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ReplaceStringWithClassConstant('SomeClass', 'call', 0, 'Placeholder', false)]);
2020-11-25 21:34:34 +00:00
};
```
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
↓
2019-05-19 08:27:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
{
public function run()
{
- $this->call('name');
+ $this->call(Placeholder::NAME);
}
}
2019-09-25 08:49:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-09-25 08:49:53 +00:00
2021-03-23 22:13:35 +00:00
## Removing
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
### ArgumentRemoverRector
2021-01-28 18:26:56 +00:00
2021-03-23 22:13:35 +00:00
Removes defined arguments in defined methods and their calls.
2021-01-28 18:26:56 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Removing\Rector\ClassMethod\ArgumentRemoverRector` ](../rules/Removing/Rector/ClassMethod/ArgumentRemoverRector.php )
2021-01-28 18:26:56 +00:00
```php
2021-03-23 22:13:35 +00:00
use Rector\Removing\Rector\ClassMethod\ArgumentRemoverRector;
use Rector\Removing\ValueObject\ArgumentRemover;
2021-01-28 18:26:56 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(ArgumentRemoverRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ArgumentRemover('ExampleClass', 'someMethod', 0, [true])]);
2021-01-28 18:26:56 +00:00
};
```
↓
```diff
2021-03-23 22:13:35 +00:00
$someObject = new SomeClass;
-$someObject->someMethod(true);
2021-08-01 00:25:55 +00:00
+$someObject->someMethod();
2021-01-28 18:26:56 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveFuncCallArgRector
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
Remove argument by position by function name
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-10-10 16:27:13 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector` ](../rules/Removing/Rector/FuncCall/RemoveFuncCallArgRector.php )
2020-10-10 16:27:13 +00:00
2020-11-25 21:34:34 +00:00
```php
2021-03-23 22:13:35 +00:00
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
use Rector\Removing\ValueObject\RemoveFuncCallArg;
2020-11-25 21:34:34 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-09-25 08:49:53 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RemoveFuncCallArgRector::class)
2021-12-06 21:12:03 +00:00
->configure([new RemoveFuncCallArg('remove_last_arg', 1)]);
2020-11-25 21:34:34 +00:00
};
```
↓
2019-09-25 08:49:53 +00:00
```diff
2021-03-23 22:13:35 +00:00
-remove_last_arg(1, 2);
+remove_last_arg(1);
2019-09-25 08:49:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-09-25 08:49:53 +00:00
2021-03-23 22:13:35 +00:00
### RemoveFuncCallRector
2020-07-24 11:46:57 +00:00
2021-04-10 18:18:49 +00:00
Remove ini_get by configuration
2019-12-18 09:53:46 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Removing\Rector\FuncCall\RemoveFuncCallRector` ](../rules/Removing/Rector/FuncCall/RemoveFuncCallRector.php )
2020-11-25 21:34:34 +00:00
```php
2021-03-23 22:13:35 +00:00
use Rector\Removing\Rector\FuncCall\RemoveFuncCallRector;
use Rector\Removing\ValueObject\RemoveFuncCall;
2020-11-25 21:34:34 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RemoveFuncCallRector::class)
2021-12-07 19:04:47 +00:00
->configure([new RemoveFuncCall('ini_get', [['y2k_compliance']])]);
2020-11-25 21:34:34 +00:00
};
```
↓
2019-12-18 09:53:46 +00:00
```diff
2021-03-23 22:13:35 +00:00
-ini_get('y2k_compliance');
ini_get('keep_me');
2019-05-19 08:27:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-19 08:27:38 +00:00
2021-03-23 22:13:35 +00:00
### RemoveInterfacesRector
2019-05-19 08:27:38 +00:00
2021-03-23 22:13:35 +00:00
Removes interfaces usage from class.
2019-05-19 08:27:38 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Removing\Rector\Class_\RemoveInterfacesRector` ](../rules/Removing/Rector/Class_/RemoveInterfacesRector.php )
2020-11-25 21:34:34 +00:00
```php
2021-03-23 22:13:35 +00:00
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
2020-11-25 21:34:34 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RemoveInterfacesRector::class)
2021-12-06 21:12:03 +00:00
->configure(['SomeInterface']);
2020-11-25 21:34:34 +00:00
};
```
↓
2019-05-19 08:27:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
-class SomeClass implements SomeInterface
+class SomeClass
2020-11-25 21:34:34 +00:00
{
}
2019-05-19 08:27:38 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-19 08:27:38 +00:00
2022-04-06 12:39:03 +00:00
### RemoveNamespaceRector
Remove namespace by configured namespace names
:wrench: **configure it!**
- class: [`Rector\Removing\Rector\Namespace_\RemoveNamespaceRector` ](../rules/Removing/Rector/Namespace_/RemoveNamespaceRector.php )
```php
use Rector\Removing\Rector\Namespace_\RemoveNamespaceRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveNamespaceRector::class)
->configure(['App']);
};
```
↓
```diff
-namespace App;
class SomeClass
{
}
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveParentRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Removes extends class by name
2020-06-16 14:27:04 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2019-08-05 21:10:47 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Removing\Rector\Class_\RemoveParentRector` ](../rules/Removing/Rector/Class_/RemoveParentRector.php )
2018-05-04 22:30:32 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Removing\Rector\Class_\RemoveParentRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2018-08-01 20:09:34 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RemoveParentRector::class)
2021-12-06 21:12:03 +00:00
->configure(['SomeParentClass']);
2021-03-23 22:13:35 +00:00
};
2020-11-16 17:50:38 +00:00
```
2019-06-02 07:45:37 +00:00
2021-03-23 22:13:35 +00:00
↓
2020-11-16 17:50:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
-final class SomeClass extends SomeParentClass
+final class SomeClass
2020-11-25 21:34:34 +00:00
{
}
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-02-04 00:32:53 +00:00
2021-07-22 18:40:24 +00:00
### RemoveTraitUseRector
2020-10-30 13:25:24 +00:00
2021-03-23 22:13:35 +00:00
Remove specific traits from code
2020-10-30 13:25:24 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-11-25 21:34:34 +00:00
2021-07-22 18:40:24 +00:00
- class: [`Rector\Removing\Rector\Class_\RemoveTraitUseRector` ](../rules/Removing/Rector/Class_/RemoveTraitUseRector.php )
2020-10-30 13:25:24 +00:00
2021-03-23 22:13:35 +00:00
```php
2021-07-05 09:49:56 +00:00
use Rector\Removing\Rector\Class_\RemoveTraitUseRector;
2021-03-23 22:13:35 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-10-30 13:25:24 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-07-05 09:49:56 +00:00
$services->set(RemoveTraitUseRector::class)
2021-12-06 21:12:03 +00:00
->configure(['TraitNameToRemove']);
2021-03-23 22:13:35 +00:00
};
```
2019-12-19 22:07:06 +00:00
2021-03-23 22:13:35 +00:00
↓
2019-12-19 22:07:06 +00:00
2020-11-16 17:50:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-25 21:34:34 +00:00
{
2021-03-23 22:13:35 +00:00
- use SomeTrait;
2020-11-25 21:34:34 +00:00
}
2020-11-16 17:50:38 +00:00
```
2020-07-24 11:46:57 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
## RemovingStatic
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
### LocallyCalledStaticMethodToNonStaticRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Change static method and local-only calls to non-static
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\RemovingStatic\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector` ](../rules/RemovingStatic/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php )
2020-11-25 21:34:34 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
{
- self::someStatic();
+ $this->someStatic();
}
- private static function someStatic()
+ private function someStatic()
2020-12-22 15:48:25 +00:00
{
}
}
```
2020-11-25 21:34:34 +00:00
2020-12-22 15:48:25 +00:00
< br >
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
## Renaming
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
### PseudoNamespaceToNamespaceRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Replaces defined Pseudo_Namespaces by Namespace\Ones.
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-09-11 09:21:48 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\FileWithoutNamespace\PseudoNamespaceToNamespaceRector` ](../rules/Renaming/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector.php )
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\FileWithoutNamespace\PseudoNamespaceToNamespaceRector;
use Rector\Renaming\ValueObject\PseudoNamespaceToNamespace;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(PseudoNamespaceToNamespaceRector::class)
2021-12-06 21:12:03 +00:00
->configure([new PseudoNamespaceToNamespace('Some_', ['Some_Class_To_Keep'])]);
2021-03-23 22:13:35 +00:00
};
```
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
↓
2020-09-11 09:21:48 +00:00
2019-11-06 23:52:19 +00:00
```diff
2021-03-23 22:13:35 +00:00
-/** @var Some_Chicken $someService */
-$someService = new Some_Chicken;
+/** @var Some\Chicken $someService */
+$someService = new Some\Chicken;
$someClassToKeep = new Some_Class_To_Keep;
2020-12-22 15:48:25 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### RenameAnnotationRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Turns defined annotations above properties and methods to their new values.
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\ClassMethod\RenameAnnotationRector` ](../rules/Renaming/Rector/ClassMethod/RenameAnnotationRector.php )
2019-11-06 23:52:19 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\ClassMethod\RenameAnnotationRector;
2021-11-29 10:27:24 +00:00
use Rector\Renaming\ValueObject\RenameAnnotationByType;
2021-03-23 22:13:35 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-11-06 23:52:19 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenameAnnotationRector::class)
2021-12-06 21:12:03 +00:00
->configure([new RenameAnnotationByType('PHPUnit\Framework\TestCase', 'test', 'scenario')]);
2021-03-23 22:13:35 +00:00
};
```
2020-07-28 15:15:30 +00:00
2021-03-23 22:13:35 +00:00
↓
2020-07-28 15:15:30 +00:00
```diff
2021-12-05 00:35:40 +00:00
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
/**
- * @test
+ * @scenario
*/
public function someMethod()
2020-12-22 15:48:25 +00:00
{
}
2020-07-28 15:15:30 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-07-28 15:15:30 +00:00
2021-03-23 22:13:35 +00:00
### RenameClassConstFetchRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Replaces defined class constants in their calls.
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector` ](../rules/Renaming/Rector/ClassConstFetch/RenameClassConstFetchRector.php )
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
use Rector\Renaming\ValueObject\RenameClassAndConstFetch;
2021-12-07 19:04:47 +00:00
use Rector\Renaming\ValueObject\RenameClassConstFetch;
2021-03-23 22:13:35 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenameClassConstFetchRector::class)
2022-02-20 00:35:49 +00:00
->configure(
[new RenameClassConstFetch('SomeClass', 'OLD_CONSTANT', 'NEW_CONSTANT'), new RenameClassAndConstFetch(
'SomeClass',
'OTHER_OLD_CONSTANT',
'DifferentClass',
'NEW_CONSTANT'
)]
);
2021-03-23 22:13:35 +00:00
};
2020-12-22 15:48:25 +00:00
```
2021-03-23 22:13:35 +00:00
↓
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$value = SomeClass::OLD_CONSTANT;
-$value = SomeClass::OTHER_OLD_CONSTANT;
+$value = SomeClass::NEW_CONSTANT;
+$value = DifferentClass::NEW_CONSTANT;
2020-12-22 15:48:25 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### RenameClassRector
2020-07-24 11:46:57 +00:00
2021-03-23 22:13:35 +00:00
Replaces defined classes by new ones.
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2019-02-04 00:32:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\Name\RenameClassRector` ](../rules/Renaming/Rector/Name/RenameClassRector.php )
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\Name\RenameClassRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-02-04 00:32:53 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenameClassRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'App\SomeOldClass' => 'App\SomeNewClass',
]);
2021-03-23 22:13:35 +00:00
};
2020-07-28 23:41:20 +00:00
```
2020-07-24 11:46:57 +00:00
2021-03-23 22:13:35 +00:00
↓
2018-05-04 22:30:32 +00:00
2021-03-23 22:13:35 +00:00
```diff
namespace App;
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
-use SomeOldClass;
+use SomeNewClass;
2018-08-01 20:09:34 +00:00
2021-03-23 22:13:35 +00:00
-function someFunction(SomeOldClass $someOldClass): SomeOldClass
+function someFunction(SomeNewClass $someOldClass): SomeNewClass
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
- if ($someOldClass instanceof SomeOldClass) {
- return new SomeOldClass;
+ if ($someOldClass instanceof SomeNewClass) {
+ return new SomeNewClass;
2020-11-16 17:50:38 +00:00
}
}
2018-08-01 13:34:55 +00:00
```
2018-07-31 19:43:39 +00:00
2020-11-16 17:50:38 +00:00
< br >
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
### RenameConstantRector
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
Replace constant by new ones
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2019-05-29 13:40:20 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\ConstFetch\RenameConstantRector` ](../rules/Renaming/Rector/ConstFetch/RenameConstantRector.php )
2020-07-24 11:46:57 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\ConstFetch\RenameConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenameConstantRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'MYSQL_ASSOC' => 'MYSQLI_ASSOC',
'OLD_CONSTANT' => 'NEW_CONSTANT',
]);
2021-03-23 22:13:35 +00:00
};
```
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
↓
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
```diff
final class SomeClass
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2020-12-22 15:48:25 +00:00
{
2021-03-23 22:13:35 +00:00
- return MYSQL_ASSOC;
+ return MYSQLI_ASSOC;
2020-12-22 15:48:25 +00:00
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RenameFunctionRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Turns defined function call new one.
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\FuncCall\RenameFunctionRector` ](../rules/Renaming/Rector/FuncCall/RenameFunctionRector.php )
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenameFunctionRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'view' => 'Laravel\Templating\render',
]);
2021-03-23 22:13:35 +00:00
};
2020-12-22 15:48:25 +00:00
```
2021-03-23 22:13:35 +00:00
↓
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
-view("...", []);
+Laravel\Templating\render("...", []);
2020-12-22 15:48:25 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### RenameMethodRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Turns method names to new ones.
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\MethodCall\RenameMethodRector` ](../rules/Renaming/Rector/MethodCall/RenameMethodRector.php )
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenameMethodRector::class)
2021-12-06 21:12:03 +00:00
->configure([new MethodCallRename('SomeExampleClass', 'oldMethod', 'newMethod')]);
2021-03-23 22:13:35 +00:00
};
```
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
↓
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
$someObject = new SomeExampleClass;
-$someObject->oldMethod();
+$someObject->newMethod();
2020-12-22 15:48:25 +00:00
```
< br >
2021-03-23 22:13:35 +00:00
### RenameNamespaceRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Replaces old namespace by new one.
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\Namespace_\RenameNamespaceRector` ](../rules/Renaming/Rector/Namespace_/RenameNamespaceRector.php )
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\Namespace_\RenameNamespaceRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenameNamespaceRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'SomeOldNamespace' => 'SomeNewNamespace',
]);
2021-03-23 22:13:35 +00:00
};
```
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
↓
2018-09-28 16:33:35 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$someObject = new SomeOldNamespace\SomeClass;
+$someObject = new SomeNewNamespace\SomeClass;
2018-09-28 16:33:35 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2021-03-23 22:13:35 +00:00
### RenamePropertyRector
2018-11-11 12:22:35 +00:00
2021-03-23 22:13:35 +00:00
Replaces defined old properties by new ones.
2019-02-04 00:32:53 +00:00
2020-11-16 17:50:38 +00:00
:wrench: **configure it!**
2019-02-04 00:32:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector` ](../rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php )
2020-11-16 17:50:38 +00:00
```php
2021-03-23 22:13:35 +00:00
use Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector;
use Rector\Renaming\ValueObject\RenameProperty;
2020-11-16 17:50:38 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenamePropertyRector::class)
2021-12-06 21:12:03 +00:00
->configure([new RenameProperty('SomeClass', 'someOldProperty', 'someNewProperty')]);
2020-11-16 17:50:38 +00:00
};
```
↓
2019-02-04 00:32:53 +00:00
2020-07-28 23:41:20 +00:00
```diff
2021-03-23 22:13:35 +00:00
-$someObject->someOldProperty;
+$someObject->someNewProperty;
2020-07-28 23:41:20 +00:00
```
2020-07-24 11:46:57 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-07-24 11:46:57 +00:00
2021-03-23 22:13:35 +00:00
### RenameStaticMethodRector
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
Turns method names to new ones.
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\StaticCall\RenameStaticMethodRector` ](../rules/Renaming/Rector/StaticCall/RenameStaticMethodRector.php )
2020-11-16 17:50:38 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\StaticCall\RenameStaticMethodRector;
use Rector\Renaming\ValueObject\RenameStaticMethod;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-28 23:41:20 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenameStaticMethodRector::class)
2021-12-07 19:04:47 +00:00
->configure([new RenameStaticMethod('SomeClass', 'oldMethod', 'AnotherExampleClass', 'newStaticMethod')]);
2021-03-23 22:13:35 +00:00
};
2019-02-04 00:32:53 +00:00
```
2021-03-23 22:13:35 +00:00
↓
2019-06-02 07:45:37 +00:00
```diff
2021-03-23 22:13:35 +00:00
-SomeClass::oldStaticMethod();
+AnotherExampleClass::newStaticMethod();
2019-05-29 13:40:20 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2019-05-29 13:40:20 +00:00
2021-03-23 22:13:35 +00:00
### RenameStringRector
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
Change string value
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-11-25 21:34:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Renaming\Rector\String_\RenameStringRector` ](../rules/Renaming/Rector/String_/RenameStringRector.php )
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Renaming\Rector\String_\RenameStringRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-11-25 21:34:34 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(RenameStringRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'ROLE_PREVIOUS_ADMIN' => 'IS_IMPERSONATOR',
]);
2021-03-23 22:13:35 +00:00
};
```
2020-10-25 13:43:05 +00:00
2021-03-23 22:13:35 +00:00
↓
2020-10-25 13:43:05 +00:00
```diff
2021-03-23 22:13:35 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
public function run()
2020-11-16 17:50:38 +00:00
{
2021-03-23 22:13:35 +00:00
- return 'ROLE_PREVIOUS_ADMIN';
+ return 'IS_IMPERSONATOR';
2020-11-16 17:50:38 +00:00
}
}
```
2020-09-24 18:54:39 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-07-29 23:39:41 +00:00
2021-03-23 22:13:35 +00:00
## Restoration
2020-08-24 22:26:14 +00:00
2021-03-23 22:13:35 +00:00
### CompleteImportForPartialAnnotationRector
2020-08-24 22:26:14 +00:00
2021-03-23 22:13:35 +00:00
In case you have accidentally removed use imports but code still contains partial use statements, this will save you
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
:wrench: **configure it!**
2020-08-24 22:26:14 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Restoration\Rector\Namespace_\CompleteImportForPartialAnnotationRector` ](../rules/Restoration/Rector/Namespace_/CompleteImportForPartialAnnotationRector.php )
2020-09-24 18:54:39 +00:00
2021-03-23 22:13:35 +00:00
```php
use Rector\Restoration\Rector\Namespace_\CompleteImportForPartialAnnotationRector;
use Rector\Restoration\ValueObject\CompleteImportForPartialAnnotation;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-08-24 22:26:14 +00:00
2021-03-23 22:13:35 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-03-23 22:13:35 +00:00
$services->set(CompleteImportForPartialAnnotationRector::class)
2021-12-06 21:12:03 +00:00
->configure([new CompleteImportForPartialAnnotation('Doctrine\ORM\Mapping', 'ORM')]);
2021-03-23 22:13:35 +00:00
};
2020-11-16 17:50:38 +00:00
```
2020-08-24 22:26:14 +00:00
2021-03-23 22:13:35 +00:00
↓
2020-08-25 00:21:00 +00:00
2020-11-16 17:50:38 +00:00
```diff
2021-03-23 22:13:35 +00:00
+use Doctrine\ORM\Mapping as ORM;
+
class SomeClass
{
/**
* @ORM \Id
*/
public $id;
}
2020-08-24 22:26:14 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-24 22:26:14 +00:00
2021-03-23 22:13:35 +00:00
### MakeTypedPropertyNullableIfCheckedRector
2021-01-30 20:30:37 +00:00
2021-03-23 22:13:35 +00:00
Make typed property nullable if checked
2021-01-30 20:30:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Restoration\Rector\Property\MakeTypedPropertyNullableIfCheckedRector` ](../rules/Restoration/Rector/Property/MakeTypedPropertyNullableIfCheckedRector.php )
2021-01-30 20:30:37 +00:00
```diff
2021-03-23 22:13:35 +00:00
final class SomeClass
2021-01-30 20:30:37 +00:00
{
2021-03-23 22:13:35 +00:00
- private AnotherClass $anotherClass;
+ private ?AnotherClass $anotherClass = null;
2021-01-30 20:30:37 +00:00
2021-03-23 22:13:35 +00:00
public function run()
2021-01-30 20:30:37 +00:00
{
2021-03-23 22:13:35 +00:00
if ($this->anotherClass === null) {
$this->anotherClass = new AnotherClass;
}
2021-01-30 20:30:37 +00:00
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### MissingClassConstantReferenceToStringRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Convert missing class reference to string
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Restoration\Rector\ClassConstFetch\MissingClassConstantReferenceToStringRector` ](../rules/Restoration/Rector/ClassConstFetch/MissingClassConstantReferenceToStringRector.php )
2020-12-22 15:48:25 +00:00
```diff
class SomeClass
{
public function run()
{
2021-03-23 22:13:35 +00:00
- return NonExistingClass::class;
+ return 'NonExistingClass';
2020-12-22 15:48:25 +00:00
}
}
```
< br >
2021-03-23 22:13:35 +00:00
### RemoveFinalFromEntityRector
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
Remove final from Doctrine entities
2020-12-22 15:48:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Restoration\Rector\Class_\RemoveFinalFromEntityRector` ](../rules/Restoration/Rector/Class_/RemoveFinalFromEntityRector.php )
2020-12-22 15:48:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
use Doctrine\ORM\Mapping as ORM;
2020-12-22 15:48:25 +00:00
2021-03-23 22:13:35 +00:00
/**
* @ORM \Entity
*/
-final class SomeClass
+class SomeClass
2020-12-22 15:48:25 +00:00
{
}
```
< br >
2021-03-23 22:13:35 +00:00
### UpdateFileNameByClassNameFileSystemRector
2021-03-14 00:55:25 +00:00
2021-03-23 22:13:35 +00:00
Rename file to respect class name
2021-03-14 00:55:25 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Restoration\Rector\ClassLike\UpdateFileNameByClassNameFileSystemRector` ](../rules/Restoration/Rector/ClassLike/UpdateFileNameByClassNameFileSystemRector.php )
2021-03-14 00:55:25 +00:00
```diff
2021-03-23 22:13:35 +00:00
-// app/SomeClass.php
+// app/AnotherClass.php
class AnotherClass
2021-03-14 00:55:25 +00:00
{
2021-03-23 22:13:35 +00:00
}
2021-03-14 00:55:25 +00:00
```
< br >
2021-10-05 08:51:30 +00:00
## Strict
### BooleanInBooleanNotRuleFixerRector
Fixer for PHPStan reports by strict type rule - "PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule"
2021-10-05 15:59:39 +00:00
:wrench: **configure it!**
2021-10-05 08:51:30 +00:00
- class: [`Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector` ](../rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php )
2021-10-05 15:59:39 +00:00
```php
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(BooleanInBooleanNotRuleFixerRector::class)
2022-02-20 00:35:49 +00:00
->configure([
BooleanInBooleanNotRuleFixerRector::TREAT_AS_NON_EMPTY => true,
]);
2021-10-05 15:59:39 +00:00
};
```
↓
2021-10-05 08:51:30 +00:00
```diff
class SomeClass
{
2022-04-10 06:08:51 +00:00
public function run(string|null $name)
2021-10-05 08:51:30 +00:00
{
- if (! $name) {
2021-10-05 15:59:39 +00:00
+ if ($name === null) {
2021-10-05 11:12:46 +00:00
return 'no name';
2021-10-05 08:51:30 +00:00
}
2021-10-05 11:12:46 +00:00
return 'name';
2021-10-05 08:51:30 +00:00
}
}
```
< br >
### BooleanInIfConditionRuleFixerRector
Fixer for PHPStan reports by strict type rule - "PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule"
2021-10-05 15:59:39 +00:00
:wrench: **configure it!**
2021-10-05 08:51:30 +00:00
- class: [`Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector` ](../rules/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector.php )
2021-10-05 15:59:39 +00:00
```php
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(BooleanInIfConditionRuleFixerRector::class)
2022-02-20 00:35:49 +00:00
->configure([
BooleanInIfConditionRuleFixerRector::TREAT_AS_NON_EMPTY => false,
]);
2021-10-05 15:59:39 +00:00
};
```
↓
2021-10-05 08:51:30 +00:00
```diff
final class NegatedString
{
public function run(string $name)
{
- if ($name) {
+ if ($name !== '') {
return 'name';
}
return 'no name';
}
}
```
< br >
### BooleanInTernaryOperatorRuleFixerRector
Fixer for PHPStan reports by strict type rule - "PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule"
2021-10-05 15:59:39 +00:00
:wrench: **configure it!**
2021-10-05 08:51:30 +00:00
- class: [`Rector\Strict\Rector\Ternary\BooleanInTernaryOperatorRuleFixerRector` ](../rules/Strict/Rector/Ternary/BooleanInTernaryOperatorRuleFixerRector.php )
2021-10-05 15:59:39 +00:00
```php
use Rector\Strict\Rector\Ternary\BooleanInTernaryOperatorRuleFixerRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(BooleanInTernaryOperatorRuleFixerRector::class)
2022-02-20 00:35:49 +00:00
->configure([
BooleanInTernaryOperatorRuleFixerRector::TREAT_AS_NON_EMPTY => false,
]);
2021-10-05 15:59:39 +00:00
};
```
↓
2021-10-05 08:51:30 +00:00
```diff
final class ArrayCompare
{
public function run(array $data)
{
- return $data ? 1 : 2;
+ return $data !== [] ? 1 : 2;
}
}
```
< br >
### DisallowedEmptyRuleFixerRector
Fixer for PHPStan reports by strict type rule - "PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule"
2021-10-05 15:59:39 +00:00
:wrench: **configure it!**
2021-10-05 08:51:30 +00:00
- class: [`Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector` ](../rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php )
2021-10-05 15:59:39 +00:00
```php
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DisallowedEmptyRuleFixerRector::class)
2022-02-20 00:35:49 +00:00
->configure([
DisallowedEmptyRuleFixerRector::TREAT_AS_NON_EMPTY => false,
]);
2021-10-05 15:59:39 +00:00
};
```
↓
2021-10-05 08:51:30 +00:00
```diff
final class SomeEmptyArray
{
public function run(array $items)
{
- return empty($items);
+ return $items === [];
}
}
```
< br >
### DisallowedShortTernaryRuleFixerRector
Fixer for PHPStan reports by strict type rule - "PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule"
2021-10-05 15:59:39 +00:00
:wrench: **configure it!**
2021-10-05 08:51:30 +00:00
- class: [`Rector\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector` ](../rules/Strict/Rector/Ternary/DisallowedShortTernaryRuleFixerRector.php )
2021-10-05 15:59:39 +00:00
```php
use Rector\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DisallowedShortTernaryRuleFixerRector::class)
2022-02-20 00:35:49 +00:00
->configure([
DisallowedShortTernaryRuleFixerRector::TREAT_AS_NON_EMPTY => false,
]);
2021-10-05 15:59:39 +00:00
};
```
↓
2021-10-05 08:51:30 +00:00
```diff
final class ShortTernaryArray
{
public function run(array $array)
{
- return $array ?: 2;
+ return $array !== [] ? $array : 2;
}
}
```
< br >
2021-03-23 22:13:35 +00:00
## Transform
2021-01-30 20:30:37 +00:00
2021-11-26 20:07:54 +00:00
### AddAllowDynamicPropertiesAttributeRector
Add the `AllowDynamicProperties` attribute to all classes
2021-12-10 07:56:16 +00:00
:wrench: **configure it!**
2021-11-26 20:07:54 +00:00
- class: [`Rector\Transform\Rector\Class_\AddAllowDynamicPropertiesAttributeRector` ](../rules/Transform/Rector/Class_/AddAllowDynamicPropertiesAttributeRector.php )
2021-12-10 07:56:16 +00:00
```php
use Rector\Transform\Rector\Class_\AddAllowDynamicPropertiesAttributeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddAllowDynamicPropertiesAttributeRector::class)
->configure(['Example\*']);
};
```
↓
2021-11-26 20:07:54 +00:00
```diff
2021-12-10 07:56:16 +00:00
namespace Example\Domain;
2021-11-26 20:07:54 +00:00
+#[AllowDynamicProperties]
class SomeObject {
public string $someProperty = 'hello world';
}
```
< br >
2021-02-20 23:02:05 +00:00
### AddInterfaceByTraitRector
Add interface by used trait
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\Class_\AddInterfaceByTraitRector` ](../rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php )
2021-02-20 23:02:05 +00:00
```php
use Rector\Transform\Rector\Class_\AddInterfaceByTraitRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-20 23:02:05 +00:00
$services->set(AddInterfaceByTraitRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'SomeTrait' => 'SomeInterface',
]);
2021-02-20 23:02:05 +00:00
};
```
↓
```diff
-class SomeClass
+class SomeClass implements SomeInterface
{
use SomeTrait;
}
```
< br >
2021-01-30 20:30:37 +00:00
### ArgumentFuncCallToMethodCallRector
Move help facade-like function calls to constructor injection
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\FuncCall\ArgumentFuncCallToMethodCallRector` ](../rules/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector.php )
2021-01-30 20:30:37 +00:00
```php
use Rector\Transform\Rector\FuncCall\ArgumentFuncCallToMethodCallRector;
use Rector\Transform\ValueObject\ArgumentFuncCallToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(ArgumentFuncCallToMethodCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ArgumentFuncCallToMethodCall('view', 'Illuminate\Contracts\View\Factory', 'make')]);
2021-01-30 20:30:37 +00:00
};
```
↓
```diff
class SomeController
{
+ /**
+ * @var \Illuminate\Contracts\View\Factory
+ */
+ private $viewFactory;
+
+ public function __construct(\Illuminate\Contracts\View\Factory $viewFactory)
+ {
+ $this->viewFactory = $viewFactory;
+ }
+
public function action()
{
- $template = view('template.blade');
- $viewFactory = view();
+ $template = $this->viewFactory->make('template.blade');
+ $viewFactory = $this->viewFactory;
}
}
```
< br >
2021-10-14 15:36:34 +00:00
### AttributeKeyToClassConstFetchRector
Replace key value on specific attribute to class constant
:wrench: **configure it!**
- class: [`Rector\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector` ](../rules/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector.php )
```php
use Rector\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector;
use Rector\Transform\ValueObject\AttributeKeyToClassConstFetch;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AttributeKeyToClassConstFetchRector::class)
2022-02-20 00:35:49 +00:00
->configure(
[new AttributeKeyToClassConstFetch('Doctrine\ORM\Mapping\Column', 'type', 'Doctrine\DBAL\Types\Types', [
'STRING',
])]
);
2021-10-14 15:36:34 +00:00
};
```
↓
```diff
use Doctrine\ORM\Mapping\Column;
+use Doctrine\DBAL\Types\Types;
class SomeClass
{
- #[Column(type: "string")]
+ #[Column(type: Types::STRING)]
public $name;
}
```
< br >
2021-02-07 10:11:42 +00:00
### CallableInMethodCallToVariableRector
2021-01-30 20:30:37 +00:00
2021-02-07 10:11:42 +00:00
Change a callable in method call to standalone variable assign
2021-01-30 20:30:37 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\MethodCall\CallableInMethodCallToVariableRector` ](../rules/Transform/Rector/MethodCall/CallableInMethodCallToVariableRector.php )
2021-01-30 20:30:37 +00:00
```php
2021-02-07 10:11:42 +00:00
use Rector\Transform\Rector\MethodCall\CallableInMethodCallToVariableRector;
use Rector\Transform\ValueObject\CallableInMethodCallToVariable;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-07 10:11:42 +00:00
$services->set(CallableInMethodCallToVariableRector::class)
2021-12-06 21:12:03 +00:00
->configure([new CallableInMethodCallToVariable('Nette\Caching\Cache', 'save', 1)]);
2021-02-07 10:11:42 +00:00
};
```
↓
```diff
final class SomeClass
{
public function run()
{
/** @var \Nette\Caching\Cache $cache */
- $cache->save($key, function () use ($container) {
- return 100;
- });
+ $result = 100;
+ $cache->save($key, $result);
}
}
```
< br >
2021-03-12 23:52:38 +00:00
### ChangeSingletonToServiceRector
Change singleton class to normal class that can be registered as a service
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\Class_\ChangeSingletonToServiceRector` ](../rules/Transform/Rector/Class_/ChangeSingletonToServiceRector.php )
2021-03-12 23:52:38 +00:00
```diff
class SomeClass
{
- private static $instance;
-
- private function __construct()
+ public function __construct()
{
- }
-
- public static function getInstance()
- {
- if (null === static::$instance) {
- static::$instance = new static();
- }
-
- return static::$instance;
}
}
```
< br >
2021-01-30 20:30:37 +00:00
### DimFetchAssignToMethodCallRector
2020-08-30 13:47:36 +00:00
2021-01-30 20:30:37 +00:00
Change magic array access add to `$list[],` to explicit `$list->addMethod(...)`
2020-08-30 13:47:36 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-08-30 13:47:36 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\Assign\DimFetchAssignToMethodCallRector` ](../rules/Transform/Rector/Assign/DimFetchAssignToMethodCallRector.php )
2020-08-30 13:47:36 +00:00
2020-11-25 21:34:34 +00:00
```php
2021-01-30 20:30:37 +00:00
use Rector\Transform\Rector\Assign\DimFetchAssignToMethodCallRector;
use Rector\Transform\ValueObject\DimFetchAssignToMethodCall;
2020-11-25 21:34:34 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-09-24 18:54:39 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(DimFetchAssignToMethodCallRector::class)
2022-02-20 00:35:49 +00:00
->configure(
[new DimFetchAssignToMethodCall(
'Nette\Application\Routers\RouteList',
'Nette\Application\Routers\Route',
'addRoute'
)]
);
2020-11-25 21:34:34 +00:00
};
```
↓
2020-08-30 13:47:36 +00:00
```diff
2021-01-30 20:30:37 +00:00
-use Nette\Application\Routers\Route;
use Nette\Application\Routers\RouteList;
class RouterFactory
2020-08-30 13:47:36 +00:00
{
2021-01-30 20:30:37 +00:00
public static function createRouter()
2020-08-30 13:47:36 +00:00
{
2021-01-30 20:30:37 +00:00
$routeList = new RouteList();
- $routeList[] = new Route('...');
+ $routeList->addRoute('...');
2020-08-30 13:47:36 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-30 18:48:37 +00:00
2021-02-21 09:40:54 +00:00
### FuncCallToConstFetchRector
Changes use of function calls to use constants
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\FuncCall\FuncCallToConstFetchRector` ](../rules/Transform/Rector/FuncCall/FuncCallToConstFetchRector.php )
2021-02-21 09:40:54 +00:00
```php
use Rector\Transform\Rector\FuncCall\FuncCallToConstFetchRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-21 09:40:54 +00:00
$services->set(FuncCallToConstFetchRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'php_sapi_name' => 'PHP_SAPI',
]);
2021-02-21 09:40:54 +00:00
};
```
↓
```diff
class SomeClass
{
public function run()
{
- $value = php_sapi_name();
+ $value = PHP_SAPI;
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### FuncCallToMethodCallRector
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
Turns defined function calls to local method calls.
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-08-30 18:48:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\FuncCall\FuncCallToMethodCallRector` ](../rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php )
2020-11-25 21:34:34 +00:00
```php
use Rector\Transform\Rector\FuncCall\FuncCallToMethodCallRector;
2021-02-09 14:02:29 +00:00
use Rector\Transform\ValueObject\FuncCallToMethodCall;
2020-11-25 21:34:34 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(FuncCallToMethodCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new FuncCallToMethodCall('view', 'Namespaced\SomeRenderer', 'render')]);
2020-11-25 21:34:34 +00:00
};
2020-11-16 17:50:38 +00:00
```
2020-09-24 18:54:39 +00:00
2020-11-25 21:34:34 +00:00
↓
2020-08-30 18:48:37 +00:00
```diff
class SomeClass
{
2020-11-25 21:34:34 +00:00
+ /**
+ * @var \Namespaced\SomeRenderer
+ */
+ private $someRenderer;
+
+ public function __construct(\Namespaced\SomeRenderer $someRenderer)
+ {
+ $this->someRenderer = $someRenderer;
+ }
+
public function run()
2020-08-30 18:48:37 +00:00
{
2020-11-25 21:34:34 +00:00
- view('...');
+ $this->someRenderer->view('...');
2020-08-30 18:48:37 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-31 08:48:14 +00:00
2021-01-30 20:30:37 +00:00
### FuncCallToNewRector
Change configured function calls to new Instance
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\FuncCall\FuncCallToNewRector` ](../rules/Transform/Rector/FuncCall/FuncCallToNewRector.php )
2021-01-30 20:30:37 +00:00
```php
use Rector\Transform\Rector\FuncCall\FuncCallToNewRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(FuncCallToNewRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'collection' => ['Collection'],
]);
2021-01-30 20:30:37 +00:00
};
```
↓
```diff
class SomeClass
{
public function run()
{
- $array = collection([]);
+ $array = new \Collection([]);
}
}
```
< br >
### FuncCallToStaticCallRector
Turns defined function call to static method call.
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\FuncCall\FuncCallToStaticCallRector` ](../rules/Transform/Rector/FuncCall/FuncCallToStaticCallRector.php )
2021-01-30 20:30:37 +00:00
```php
use Rector\Transform\Rector\FuncCall\FuncCallToStaticCallRector;
use Rector\Transform\ValueObject\FuncCallToStaticCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(FuncCallToStaticCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new FuncCallToStaticCall('view', 'SomeStaticClass', 'render')]);
2021-01-30 20:30:37 +00:00
};
```
↓
```diff
-view("...", []);
+SomeClass::render("...", []);
```
< br >
2021-02-07 10:11:42 +00:00
### GetAndSetToMethodCallRector
Turns defined `__get` /`__set` to specific method calls.
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\Assign\GetAndSetToMethodCallRector` ](../rules/Transform/Rector/Assign/GetAndSetToMethodCallRector.php )
2021-02-07 10:11:42 +00:00
```php
use Rector\Transform\Rector\Assign\GetAndSetToMethodCallRector;
2021-04-18 13:01:56 +00:00
use Rector\Transform\ValueObject\GetAndSetToMethodCall;
2021-02-07 10:11:42 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-07 10:11:42 +00:00
$services->set(GetAndSetToMethodCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new GetAndSetToMethodCall('SomeContainer', 'addService', 'getService')]);
2021-02-07 10:11:42 +00:00
};
```
↓
```diff
$container = new SomeContainer;
-$container->someService = $someService;
+$container->setService("someService", $someService);
```
< br >
2021-02-20 23:02:05 +00:00
### MergeInterfacesRector
Merges old interface to a new one, that already has its methods
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\Class_\MergeInterfacesRector` ](../rules/Transform/Rector/Class_/MergeInterfacesRector.php )
2021-02-20 23:02:05 +00:00
```php
use Rector\Transform\Rector\Class_\MergeInterfacesRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-20 23:02:05 +00:00
$services->set(MergeInterfacesRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'SomeOldInterface' => 'SomeInterface',
]);
2021-02-20 23:02:05 +00:00
};
```
↓
```diff
-class SomeClass implements SomeInterface, SomeOldInterface
+class SomeClass implements SomeInterface
{
}
```
< br >
2020-11-25 21:34:34 +00:00
### MethodCallToAnotherMethodCallWithArgumentsRector
2020-08-31 08:48:14 +00:00
2020-11-25 21:34:34 +00:00
Turns old method call with specific types to new one with arguments
2020-08-31 08:48:14 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector` ](../rules/Transform/Rector/MethodCall/MethodCallToAnotherMethodCallWithArgumentsRector.php )
2020-11-25 21:34:34 +00:00
```php
use Rector\Transform\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector;
2021-08-01 00:25:55 +00:00
use Rector\Transform\ValueObject\MethodCallToAnotherMethodCallWithArguments;
2020-11-25 21:34:34 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(MethodCallToAnotherMethodCallWithArgumentsRector::class)
2022-02-20 00:35:49 +00:00
->configure(
[new MethodCallToAnotherMethodCallWithArguments('Nette\DI\ServiceDefinition', 'setInject', 'addTag', [
'inject',
])]
);
2020-11-25 21:34:34 +00:00
};
```
↓
2020-08-31 08:48:14 +00:00
```diff
2020-11-25 21:34:34 +00:00
$serviceDefinition = new Nette\DI\ServiceDefinition;
-$serviceDefinition->setInject();
+$serviceDefinition->addTag('inject');
2020-08-31 08:48:14 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-07-25 11:10:57 +00:00
### MethodCallToMethodCallRector
Change method one method from one service to a method call to in another service
:wrench: **configure it!**
- class: [`Rector\Transform\Rector\MethodCall\MethodCallToMethodCallRector` ](../rules/Transform/Rector/MethodCall/MethodCallToMethodCallRector.php )
```php
use Rector\Transform\Rector\MethodCall\MethodCallToMethodCallRector;
use Rector\Transform\ValueObject\MethodCallToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(MethodCallToMethodCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new MethodCallToMethodCall('FirstDependency', 'go', 'SecondDependency', 'away')]);
2021-07-25 11:10:57 +00:00
};
```
↓
```diff
class SomeClass
{
public function __construct(
- private FirstDependency $firstDependency
+ private SecondDependency $secondDependency
) {
}
public function run()
{
- $this->firstDependency->go();
+ $this->secondDependency->away();
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### MethodCallToPropertyFetchRector
2020-08-30 13:47:36 +00:00
2020-11-25 21:34:34 +00:00
Turns method call `"$this->something()"` to property fetch "$this->something"
2020-09-24 18:54:39 +00:00
2020-11-16 17:50:38 +00:00
:wrench: **configure it!**
2020-08-30 13:47:36 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector` ](../rules/Transform/Rector/MethodCall/MethodCallToPropertyFetchRector.php )
2020-08-30 13:47:36 +00:00
```php
2020-11-25 21:34:34 +00:00
use Rector\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector;
2020-09-24 18:54:39 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-08-30 13:47:36 +00:00
2020-09-24 18:54:39 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-08-30 13:47:36 +00:00
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(MethodCallToPropertyFetchRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'someMethod' => 'someProperty',
]);
2020-08-30 13:47:36 +00:00
};
```
↓
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run()
{
- $this->someMethod();
+ $this->someProperty;
}
}
2020-08-30 13:47:36 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-27 00:42:04 +00:00
2020-11-25 21:34:34 +00:00
### MethodCallToStaticCallRector
Change method call to desired static call
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\MethodCall\MethodCallToStaticCallRector` ](../rules/Transform/Rector/MethodCall/MethodCallToStaticCallRector.php )
2020-11-25 21:34:34 +00:00
2020-11-16 17:50:38 +00:00
```php
2020-11-25 21:34:34 +00:00
use Rector\Transform\Rector\MethodCall\MethodCallToStaticCallRector;
use Rector\Transform\ValueObject\MethodCallToStaticCall;
2020-10-27 00:42:04 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(MethodCallToStaticCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new MethodCallToStaticCall('AnotherDependency', 'process', 'StaticCaller', 'anotherMethod')]);
2020-10-27 00:42:04 +00:00
};
```
↓
```diff
2020-11-25 21:34:34 +00:00
final class SomeClass
{
private $anotherDependency;
public function __construct(AnotherDependency $anotherDependency)
{
$this->anotherDependency = $anotherDependency;
}
public function loadConfiguration()
{
- return $this->anotherDependency->process('value');
+ return StaticCaller::anotherMethod('value');
}
}
2020-10-27 00:42:04 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-27 00:42:04 +00:00
2021-01-28 18:26:56 +00:00
### NewArgToMethodCallRector
Change new with specific argument to method call
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\New_\NewArgToMethodCallRector` ](../rules/Transform/Rector/New_/NewArgToMethodCallRector.php )
2021-01-28 18:26:56 +00:00
```php
use Rector\Transform\Rector\New_\NewArgToMethodCallRector;
use Rector\Transform\ValueObject\NewArgToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-28 18:26:56 +00:00
$services->set(NewArgToMethodCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new NewArgToMethodCall('Dotenv', true, 'usePutenv')]);
2021-01-28 18:26:56 +00:00
};
```
↓
```diff
class SomeClass
{
public function run()
{
- $dotenv = new Dotenv(true);
+ $dotenv = new Dotenv();
+ $dotenv->usePutenv();
}
}
```
< br >
2021-01-24 09:07:23 +00:00
### NewToConstructorInjectionRector
Change defined new type to constructor injection
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\New_\NewToConstructorInjectionRector` ](../rules/Transform/Rector/New_/NewToConstructorInjectionRector.php )
2021-01-24 09:07:23 +00:00
```php
2021-01-30 20:30:37 +00:00
use Rector\Transform\Rector\New_\NewToConstructorInjectionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(NewToConstructorInjectionRector::class)
2021-12-06 21:12:03 +00:00
->configure(['Validator']);
2021-01-30 20:30:37 +00:00
};
```
↓
```diff
class SomeClass
{
+ /**
+ * @var Validator
+ */
+ private $validator;
+
+ public function __construct(Validator $validator)
+ {
+ $this->validator = $validator;
+ }
+
public function run()
{
- $validator = new Validator();
- $validator->validate(1000);
+ $this->validator->validate(1000);
}
}
```
< br >
### NewToMethodCallRector
Replaces creating object instances with "new" keyword with factory method.
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\New_\NewToMethodCallRector` ](../rules/Transform/Rector/New_/NewToMethodCallRector.php )
2021-01-30 20:30:37 +00:00
```php
use Rector\Transform\Rector\New_\NewToMethodCallRector;
use Rector\Transform\ValueObject\NewToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(NewToMethodCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new NewToMethodCall('MyClass', 'MyClassFactory', 'create')]);
2021-01-30 20:30:37 +00:00
};
```
↓
```diff
class SomeClass
{
+ /**
+ * @var \MyClassFactory
+ */
+ private $myClassFactory;
+
public function example() {
- new MyClass($argument);
+ $this->myClassFactory->create($argument);
}
}
```
< br >
### NewToStaticCallRector
Change new Object to static call
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\New_\NewToStaticCallRector` ](../rules/Transform/Rector/New_/NewToStaticCallRector.php )
2021-01-30 20:30:37 +00:00
```php
use Rector\Transform\Rector\New_\NewToStaticCallRector;
use Rector\Transform\ValueObject\NewToStaticCall;
2021-01-24 09:07:23 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(NewToStaticCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new NewToStaticCall('Cookie', 'Cookie', 'create')]);
2021-01-24 09:07:23 +00:00
};
```
↓
```diff
class SomeClass
{
public function run()
{
2021-01-30 20:30:37 +00:00
- new Cookie($name);
+ Cookie::create($name);
2021-01-24 09:07:23 +00:00
}
}
```
< br >
2021-01-30 20:30:37 +00:00
### ParentClassToTraitsRector
2020-08-30 18:48:37 +00:00
2021-01-30 20:30:37 +00:00
Replaces parent class to specific traits
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-08-30 18:48:37 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\Class_\ParentClassToTraitsRector` ](../rules/Transform/Rector/Class_/ParentClassToTraitsRector.php )
2020-09-24 18:54:39 +00:00
2020-11-25 21:34:34 +00:00
```php
2021-01-30 20:30:37 +00:00
use Rector\Transform\Rector\Class_\ParentClassToTraitsRector;
2021-08-01 00:25:55 +00:00
use Rector\Transform\ValueObject\ParentClassToTraits;
2020-11-25 21:34:34 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(ParentClassToTraitsRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ParentClassToTraits('Nette\Object', ['Nette\SmartObject'])]);
2020-11-25 21:34:34 +00:00
};
```
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
↓
2020-08-30 18:48:37 +00:00
```diff
2021-01-30 20:30:37 +00:00
-class SomeClass extends Nette\Object
+class SomeClass
2020-08-30 18:48:37 +00:00
{
2021-01-30 20:30:37 +00:00
+ use Nette\SmartObject;
2020-11-16 17:50:38 +00:00
}
```
2020-08-30 18:48:37 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-11-25 21:34:34 +00:00
### PropertyAssignToMethodCallRector
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
Turns property assign of specific type and property name to method call
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\Assign\PropertyAssignToMethodCallRector` ](../rules/Transform/Rector/Assign/PropertyAssignToMethodCallRector.php )
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
```php
use Rector\Transform\Rector\Assign\PropertyAssignToMethodCallRector;
use Rector\Transform\ValueObject\PropertyAssignToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(PropertyAssignToMethodCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new PropertyAssignToMethodCall('SomeClass', 'oldProperty', 'newMethodCall')]);
2020-11-25 21:34:34 +00:00
};
```
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
↓
2020-08-30 18:48:37 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
$someObject = new SomeClass;
-$someObject->oldProperty = false;
+$someObject->newMethodCall(false);
2020-11-16 17:50:38 +00:00
```
2020-08-30 18:48:37 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-01-30 20:30:37 +00:00
### PropertyFetchToMethodCallRector
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
Replaces properties assign calls be defined methods.
2020-09-24 18:54:39 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\Assign\PropertyFetchToMethodCallRector` ](../rules/Transform/Rector/Assign/PropertyFetchToMethodCallRector.php )
2020-08-30 18:48:37 +00:00
2020-11-25 21:34:34 +00:00
```php
2021-01-28 23:32:20 +00:00
use Rector\Transform\Rector\Assign\PropertyFetchToMethodCallRector;
use Rector\Transform\ValueObject\PropertyFetchToMethodCall;
2020-11-25 21:34:34 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-28 23:32:20 +00:00
$services->set(PropertyFetchToMethodCallRector::class)
2022-02-20 00:35:49 +00:00
->configure(
[new PropertyFetchToMethodCall(
'SomeObject',
'property',
'getProperty',
'setProperty',
[
]), new PropertyFetchToMethodCall(
'SomeObject',
'bareProperty',
'getConfig',
[
'someArg',
])]
);
2020-11-25 21:34:34 +00:00
};
```
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
↓
2020-08-30 18:48:37 +00:00
```diff
2020-11-25 21:34:34 +00:00
-$result = $object->property;
-$object->property = $value;
+$result = $object->getProperty();
+$object->setProperty($value);
2021-12-07 19:04:47 +00:00
-$bare = $object->bareProperty;
+$bare = $object->getConfig('someArg');
2020-11-16 17:50:38 +00:00
```
2020-08-31 00:26:25 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-12-10 07:56:16 +00:00
### RemoveAllowDynamicPropertiesAttributeRector
Remove the `AllowDynamicProperties` attribute from all classes
:wrench: **configure it!**
- class: [`Rector\Transform\Rector\Class_\RemoveAllowDynamicPropertiesAttributeRector` ](../rules/Transform/Rector/Class_/RemoveAllowDynamicPropertiesAttributeRector.php )
```php
use Rector\Transform\Rector\Class_\RemoveAllowDynamicPropertiesAttributeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveAllowDynamicPropertiesAttributeRector::class)
->configure(['Example\*']);
};
```
↓
```diff
namespace Example\Domain;
-#[AllowDynamicProperties]
class SomeObject {
public string $someProperty = 'hello world';
}
```
< br >
2020-11-25 21:34:34 +00:00
### ReplaceParentCallByPropertyCallRector
2020-08-31 00:26:25 +00:00
2020-11-25 21:34:34 +00:00
Changes method calls in child of specific types to defined property method call
2020-09-24 18:54:39 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\MethodCall\ReplaceParentCallByPropertyCallRector` ](../rules/Transform/Rector/MethodCall/ReplaceParentCallByPropertyCallRector.php )
2020-08-31 00:26:25 +00:00
2020-11-25 21:34:34 +00:00
```php
use Rector\Transform\Rector\MethodCall\ReplaceParentCallByPropertyCallRector;
use Rector\Transform\ValueObject\ReplaceParentCallByPropertyCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-08-31 00:26:25 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(ReplaceParentCallByPropertyCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ReplaceParentCallByPropertyCall('SomeTypeToReplace', 'someMethodCall', 'someProperty')]);
2020-11-25 21:34:34 +00:00
};
```
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
↓
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
final class SomeClass
2020-08-31 00:26:25 +00:00
{
2020-11-25 21:34:34 +00:00
public function run(SomeTypeToReplace $someTypeToReplace)
2020-08-31 00:26:25 +00:00
{
2020-11-25 21:34:34 +00:00
- $someTypeToReplace->someMethodCall();
+ $this->someProperty->someMethodCall();
2020-08-31 00:26:25 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-08-31 00:26:25 +00:00
2022-01-02 00:31:07 +00:00
### ReturnTypeWillChangeRector
2021-12-10 07:56:16 +00:00
2022-01-02 00:31:07 +00:00
Add #[\ReturnTypeWillChange] attribute to configured instanceof class with methods
2021-12-10 07:56:16 +00:00
:wrench: **configure it!**
2022-01-02 00:31:07 +00:00
- class: [`Rector\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector` ](../rules/Transform/Rector/ClassMethod/ReturnTypeWillChangeRector.php )
2021-12-10 07:56:16 +00:00
```php
2022-01-02 00:31:07 +00:00
use Rector\Transform\Rector\ClassMethod\ReturnTypeWillChangeRector;
2021-12-10 07:56:16 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2022-01-02 00:31:07 +00:00
$services->set(ReturnTypeWillChangeRector::class)
2022-02-20 00:35:49 +00:00
->configure([
ArrayAccess::class => ['offsetGet'],
]);
2021-12-10 07:56:16 +00:00
};
```
↓
```diff
2022-01-02 00:31:07 +00:00
class SomeClass implements ArrayAccess
2021-12-10 07:56:16 +00:00
{
2022-01-02 00:31:07 +00:00
+ #[\ReturnTypeWillChange]
public function offsetGet($offset)
2021-12-10 07:56:16 +00:00
{
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### ServiceGetterToConstructorInjectionRector
2020-07-24 11:46:57 +00:00
2020-11-25 21:34:34 +00:00
Get service call to constructor injection
2020-07-24 11:46:57 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\MethodCall\ServiceGetterToConstructorInjectionRector` ](../rules/Transform/Rector/MethodCall/ServiceGetterToConstructorInjectionRector.php )
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
```php
use Rector\Transform\Rector\MethodCall\ServiceGetterToConstructorInjectionRector;
use Rector\Transform\ValueObject\ServiceGetterToConstructorInjection;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(ServiceGetterToConstructorInjectionRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ServiceGetterToConstructorInjection('FirstService', 'getAnotherService', 'AnotherService')]);
2020-11-25 21:34:34 +00:00
};
```
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
↓
2020-11-16 17:50:38 +00:00
```diff
final class SomeClass
{
2020-11-25 21:34:34 +00:00
/**
* @var FirstService
*/
private $firstService;
- public function __construct(FirstService $firstService)
- {
- $this->firstService = $firstService;
- }
-
- public function run()
- {
- $anotherService = $this->firstService->getAnotherService();
- $anotherService->run();
- }
-}
-
-class FirstService
-{
/**
* @var AnotherService
*/
private $anotherService;
- public function __construct(AnotherService $anotherService)
+ public function __construct(FirstService $firstService, AnotherService $anotherService)
2020-07-28 23:41:20 +00:00
{
2020-11-25 21:34:34 +00:00
+ $this->firstService = $firstService;
$this->anotherService = $anotherService;
}
- public function getAnotherService(): AnotherService
+ public function run()
{
- return $this->anotherService;
+ $anotherService = $this->anotherService;
+ $anotherService->run();
2020-11-16 17:50:38 +00:00
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### StaticCallToFuncCallRector
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
Turns static call to function call.
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector` ](../rules/Transform/Rector/StaticCall/StaticCallToFuncCallRector.php )
2019-06-02 07:45:37 +00:00
2020-11-25 21:34:34 +00:00
```php
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
use Rector\Transform\ValueObject\StaticCallToFuncCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(StaticCallToFuncCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new StaticCallToFuncCall('OldClass', 'oldMethod', 'new_function')]);
2020-11-25 21:34:34 +00:00
};
```
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
↓
2019-05-29 13:40:20 +00:00
```diff
2020-11-25 21:34:34 +00:00
-OldClass::oldMethod("args");
+new_function("args");
2018-07-31 12:50:39 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
### StaticCallToMethodCallRector
2018-07-31 19:43:39 +00:00
2020-11-25 21:34:34 +00:00
Change static call to service method via constructor injection
2018-05-04 22:30:32 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2018-08-01 20:09:34 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\StaticCall\StaticCallToMethodCallRector` ](../rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php )
2020-11-25 21:34:34 +00:00
```php
use Rector\Transform\Rector\StaticCall\StaticCallToMethodCallRector;
use Rector\Transform\ValueObject\StaticCallToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(StaticCallToMethodCallRector::class)
2022-02-20 00:35:49 +00:00
->configure(
[new StaticCallToMethodCall(
'Nette\Utils\FileSystem',
'write',
'Symplify\SmartFileSystem\SmartFileSystem',
'dumpFile'
)]
);
2020-11-25 21:34:34 +00:00
};
2020-11-16 17:50:38 +00:00
```
2018-05-04 22:30:32 +00:00
2020-11-25 21:34:34 +00:00
↓
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
-use Nette\Utils\FileSystem;
+use Symplify\SmartFileSystem\SmartFileSystem;
class SomeClass
{
+ /**
+ * @var SmartFileSystem
+ */
+ private $smartFileSystem;
+
+ public function __construct(SmartFileSystem $smartFileSystem)
+ {
+ $this->smartFileSystem = $smartFileSystem;
+ }
+
public function run()
{
- return FileSystem::write('file', 'content');
+ return $this->smartFileSystem->dumpFile('file', 'content');
}
}
2018-08-01 20:09:34 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2021-01-28 18:26:56 +00:00
### StaticCallToNewRector
Change static call to new instance
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\StaticCall\StaticCallToNewRector` ](../rules/Transform/Rector/StaticCall/StaticCallToNewRector.php )
2021-01-28 18:26:56 +00:00
```php
use Rector\Transform\Rector\StaticCall\StaticCallToNewRector;
use Rector\Transform\ValueObject\StaticCallToNew;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-28 18:26:56 +00:00
$services->set(StaticCallToNewRector::class)
2021-12-06 21:12:03 +00:00
->configure([new StaticCallToNew('JsonResponse', 'create')]);
2021-01-28 18:26:56 +00:00
};
```
↓
```diff
class SomeClass
{
public function run()
{
2021-11-28 17:29:16 +00:00
- $dotenv = JsonResponse::create(['foo' => 'bar'], Response::HTTP_OK);
+ $dotenv = new JsonResponse(['foo' => 'bar'], Response::HTTP_OK);
2021-01-28 18:26:56 +00:00
}
}
```
< br >
2021-01-30 20:30:37 +00:00
### StringToClassConstantRector
Changes strings to specific constants
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\String_\StringToClassConstantRector` ](../rules/Transform/Rector/String_/StringToClassConstantRector.php )
2021-01-30 20:30:37 +00:00
```php
use Rector\Transform\Rector\String_\StringToClassConstantRector;
use Rector\Transform\ValueObject\StringToClassConstant;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(StringToClassConstantRector::class)
2021-12-06 21:12:03 +00:00
->configure([new StringToClassConstant('compiler.post_dump', 'Yet\AnotherClass', 'CONSTANT')]);
2021-01-30 20:30:37 +00:00
};
```
↓
```diff
final class SomeSubscriber
{
public static function getSubscribedEvents()
{
- return ['compiler.post_dump' => 'compile'];
+ return [\Yet\AnotherClass::CONSTANT => 'compile'];
}
}
```
< br >
2021-02-07 10:11:42 +00:00
### ToStringToMethodCallRector
Turns defined code uses of `"__toString()"` method to specific method calls.
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\String_\ToStringToMethodCallRector` ](../rules/Transform/Rector/String_/ToStringToMethodCallRector.php )
2021-02-07 10:11:42 +00:00
```php
use Rector\Transform\Rector\String_\ToStringToMethodCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-07 10:11:42 +00:00
$services->set(ToStringToMethodCallRector::class)
2022-02-20 00:35:49 +00:00
->configure([
'SomeObject' => 'getPath',
]);
2021-02-07 10:11:42 +00:00
};
```
↓
```diff
$someValue = new SomeObject;
-$result = (string) $someValue;
-$result = $someValue->__toString();
+$result = $someValue->getPath();
+$result = $someValue->getPath();
```
< br >
### UnsetAndIssetToMethodCallRector
Turns defined `__isset` /`__unset` calls to specific method calls.
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\Isset_\UnsetAndIssetToMethodCallRector` ](../rules/Transform/Rector/Isset_/UnsetAndIssetToMethodCallRector.php )
2021-02-07 10:11:42 +00:00
```php
use Rector\Transform\Rector\Isset_\UnsetAndIssetToMethodCallRector;
2021-02-09 14:02:29 +00:00
use Rector\Transform\ValueObject\UnsetAndIssetToMethodCall;
2021-02-07 10:11:42 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-07 10:11:42 +00:00
$services->set(UnsetAndIssetToMethodCallRector::class)
2021-12-06 21:12:03 +00:00
->configure([new UnsetAndIssetToMethodCall('SomeContainer', 'hasService', 'removeService')]);
2021-02-07 10:11:42 +00:00
};
```
↓
```diff
$container = new SomeContainer;
-isset($container["someKey"]);
-unset($container["someKey"]);
2021-03-07 11:24:31 +00:00
+$container->hasService("someKey");
2021-02-07 10:11:42 +00:00
+$container->removeService("someKey");
```
< br >
2021-02-20 23:21:19 +00:00
### WrapReturnRector
Wrap return value of specific method
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Transform\Rector\ClassMethod\WrapReturnRector` ](../rules/Transform/Rector/ClassMethod/WrapReturnRector.php )
2021-02-20 23:21:19 +00:00
```php
use Rector\Transform\Rector\ClassMethod\WrapReturnRector;
use Rector\Transform\ValueObject\WrapReturn;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-02-20 23:21:19 +00:00
$services->set(WrapReturnRector::class)
2021-12-06 21:12:03 +00:00
->configure([new WrapReturn('SomeClass', 'getItem', true)]);
2021-02-20 23:21:19 +00:00
};
```
↓
```diff
final class SomeClass
{
public function getItem()
{
- return 1;
+ return [1];
}
}
```
< br >
2020-11-25 21:34:34 +00:00
## TypeDeclaration
2020-07-24 11:46:57 +00:00
2020-11-25 21:34:34 +00:00
### AddArrayParamDocTypeRector
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
Adds `@param` annotation to array parameters inferred from the rest of the code
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector` ](../rules/TypeDeclaration/Rector/ClassMethod/AddArrayParamDocTypeRector.php )
2019-08-05 21:10:47 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-08-05 21:10:47 +00:00
{
2020-11-25 21:34:34 +00:00
/**
* @var int[]
*/
private $values;
+ /**
+ * @param int[] $values
+ */
public function __construct(array $values)
{
$this->values = $values;
}
2020-07-28 23:41:20 +00:00
}
2020-11-16 17:50:38 +00:00
```
2019-08-05 21:10:47 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-11-25 21:34:34 +00:00
### AddArrayReturnDocTypeRector
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
Adds `@return` annotation to array parameters inferred from the rest of the code
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector` ](../rules/TypeDeclaration/Rector/ClassMethod/AddArrayReturnDocTypeRector.php )
2020-11-16 17:50:38 +00:00
```diff
class SomeClass
2020-07-28 23:41:20 +00:00
{
2020-11-25 21:34:34 +00:00
/**
* @var int[]
*/
private $values;
+ /**
+ * @return int[]
+ */
public function getValues(): array
2019-08-05 21:10:47 +00:00
{
2020-11-25 21:34:34 +00:00
return $this->values;
2019-08-05 21:10:47 +00:00
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-08-05 21:10:47 +00:00
2020-11-25 21:34:34 +00:00
### AddClosureReturnTypeRector
2018-05-04 22:30:32 +00:00
2020-11-25 21:34:34 +00:00
Add known return type to functions
2020-07-24 11:46:57 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector` ](../rules/TypeDeclaration/Rector/Closure/AddClosureReturnTypeRector.php )
2020-09-24 18:54:39 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
{
public function run($meetups)
{
- return array_filter($meetups, function (Meetup $meetup) {
+ return array_filter($meetups, function (Meetup $meetup): bool {
return is_object($meetup);
});
}
2020-11-16 17:50:38 +00:00
}
```
2020-07-24 11:46:57 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-09-24 18:54:39 +00:00
2021-02-20 23:02:05 +00:00
### AddMethodCallBasedStrictParamTypeRector
2018-08-01 20:09:34 +00:00
2021-10-28 22:22:07 +00:00
Change private method param type to strict type, based on passed strict types
2020-11-16 17:50:38 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector` ](../rules/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector.php )
2018-08-01 20:09:34 +00:00
2018-05-04 22:30:32 +00:00
```diff
2021-10-28 22:22:07 +00:00
final class SomeClass
2020-11-16 17:50:38 +00:00
{
2021-10-28 22:22:07 +00:00
public function run(int $value)
2020-11-16 17:50:38 +00:00
{
2021-10-28 22:22:07 +00:00
$this->resolve($value);
2021-02-20 23:02:05 +00:00
}
2021-10-28 22:22:07 +00:00
- private function resolve($value)
+ private function resolve(int $value)
2021-02-20 23:02:05 +00:00
{
2020-11-16 17:50:38 +00:00
}
}
2018-07-31 06:38:48 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2018-11-11 12:22:35 +00:00
2020-11-25 21:34:34 +00:00
### AddParamTypeDeclarationRector
2020-10-30 11:58:35 +00:00
2020-11-25 21:34:34 +00:00
Add param types where needed
2020-10-30 11:58:35 +00:00
2020-11-25 21:34:34 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector` ](../rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php )
2020-11-25 21:34:34 +00:00
```php
use PHPStan\Type\StringType;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddParamTypeDeclaration;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(AddParamTypeDeclarationRector::class)
2021-12-07 19:04:47 +00:00
->configure([new AddParamTypeDeclaration('SomeClass', 'process', 0, new StringType())]);
2020-11-25 21:34:34 +00:00
};
```
↓
2020-10-30 11:58:35 +00:00
2020-11-16 17:50:38 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-11-16 17:50:38 +00:00
{
2020-11-25 21:34:34 +00:00
- public function process($name)
+ public function process(string $name)
{
}
2020-11-16 17:50:38 +00:00
}
```
2020-10-30 11:58:35 +00:00
2020-11-16 17:50:38 +00:00
< br >
2020-10-30 11:58:35 +00:00
2021-11-26 20:07:54 +00:00
### AddPropertyTypeDeclarationRector
Add type to property by added rules, mostly public/property by parent type
:wrench: **configure it!**
- class: [`Rector\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector` ](../rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php )
```php
use PHPStan\Type\StringType;
use Rector\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddPropertyTypeDeclarationRector::class)
2021-12-06 21:12:03 +00:00
->configure([new AddPropertyTypeDeclaration('ParentClass', 'name', new StringType())]);
2021-11-26 20:07:54 +00:00
};
```
↓
```diff
class SomeClass extends ParentClass
{
- public $name;
+ public string $name;
}
```
< br >
2020-11-25 21:34:34 +00:00
### AddReturnTypeDeclarationRector
2020-11-16 17:50:38 +00:00
2020-11-25 21:34:34 +00:00
Changes defined return typehint of method and class.
2020-11-16 17:50:38 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector` ](../rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php )
2020-11-16 17:50:38 +00:00
```php
2020-11-25 21:34:34 +00:00
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddReturnTypeDeclaration;
2020-10-30 11:58:35 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2020-11-25 21:34:34 +00:00
$services->set(AddReturnTypeDeclarationRector::class)
2022-02-20 00:35:49 +00:00
->configure(
[new AddReturnTypeDeclaration('SomeClass', 'getData', new ArrayType(new MixedType(), new MixedType()))]
);
2020-10-30 11:58:35 +00:00
};
```
↓
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2020-10-30 11:58:35 +00:00
{
2021-07-22 18:40:24 +00:00
- public function getData()
+ public function getData(): array
2020-10-30 11:58:35 +00:00
{
}
}
```
2020-11-16 17:50:38 +00:00
< br >
2020-10-30 11:58:35 +00:00
2021-02-20 23:02:05 +00:00
### AddVoidReturnTypeWhereNoReturnRector
Add return type void to function like without any return
2021-10-10 00:30:12 +00:00
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector` ](../rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php )
2021-02-20 23:02:05 +00:00
2021-10-10 00:30:12 +00:00
```php
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddVoidReturnTypeWhereNoReturnRector::class)
2022-02-20 00:35:49 +00:00
->configure([
AddVoidReturnTypeWhereNoReturnRector::USE_PHPDOC => false,
]);
2021-10-10 00:30:12 +00:00
};
```
↓
2021-02-20 23:02:05 +00:00
```diff
final class SomeClass
{
- public function getValues()
+ public function getValues(): void
{
$value = 1000;
return;
}
}
```
< br >
2022-03-13 00:30:25 +00:00
### ArrayShapeFromConstantArrayReturnRector
Add array shape exact types based on constant keys of array
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector` ](../rules/TypeDeclaration/Rector/ClassMethod/ArrayShapeFromConstantArrayReturnRector.php )
```diff
final class SomeClass
{
+ /**
+ * @return array{name: string}
+ */
public function run(string $name)
{
return ['name' => $name];
}
}
```
< br >
2021-01-30 20:30:37 +00:00
### FormerNullableArgumentToScalarTypedRector
Change null in argument, that is now not nullable anymore
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\MethodCall\FormerNullableArgumentToScalarTypedRector` ](../rules/TypeDeclaration/Rector/MethodCall/FormerNullableArgumentToScalarTypedRector.php )
2021-01-30 20:30:37 +00:00
```diff
final class SomeClass
{
public function run()
{
- $this->setValue(null);
+ $this->setValue('');
}
public function setValue(string $value)
{
}
}
```
< br >
2021-08-01 00:25:55 +00:00
### ParamTypeByMethodCallTypeRector
Change param type based on passed method call type
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector` ](../rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php )
```diff
class SomeTypedService
{
public function run(string $name)
{
}
}
final class UseDependency
{
public function __construct(
private SomeTypedService $someTypedService
) {
}
- public function go($value)
+ public function go(string $value)
{
$this->someTypedService->run($value);
}
}
```
< br >
### ParamTypeByParentCallTypeRector
Change param type based on parent param type
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector` ](../rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php )
```diff
class SomeControl
{
public function __construct(string $name)
{
}
}
class VideoControl extends SomeControl
{
- public function __construct($name)
+ public function __construct(string $name)
{
parent::__construct($name);
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### ParamTypeDeclarationRector
2019-12-18 09:53:46 +00:00
2020-11-25 21:34:34 +00:00
Change `@param` types to type declarations if not a BC-break
2020-07-24 11:46:57 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector` ](../rules/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector.php )
2020-07-24 11:46:57 +00:00
2020-07-28 23:41:20 +00:00
```diff
2021-08-08 00:26:40 +00:00
abstract class VendorParentClass
2019-12-18 09:53:46 +00:00
{
2020-11-25 21:34:34 +00:00
/**
* @param int $number
*/
public function keep($number)
{
}
}
2021-08-08 00:26:40 +00:00
final class ChildClass extends VendorParentClass
2020-11-25 21:34:34 +00:00
{
/**
* @param int $number
*/
public function keep($number)
{
}
2021-01-30 20:30:37 +00:00
- /**
- * @param int $number
- */
2020-11-25 21:34:34 +00:00
- public function change($number)
+ public function change(int $number)
{
}
2019-12-18 09:53:46 +00:00
}
```
2020-11-16 17:50:38 +00:00
< br >
2019-12-18 09:53:46 +00:00
2021-02-20 23:02:05 +00:00
### ParamTypeFromStrictTypedPropertyRector
Add param type from `$param` set to typed property
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector` ](../rules/TypeDeclaration/Rector/Param/ParamTypeFromStrictTypedPropertyRector.php )
2021-02-20 23:02:05 +00:00
```diff
final class SomeClass
{
private int $age;
- public function setAge($age)
+ public function setAge(int $age)
{
$this->age = $age;
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### PropertyTypeDeclarationRector
2019-09-25 08:49:53 +00:00
2020-11-25 21:34:34 +00:00
Add `@var` to properties that are missing it
2019-09-25 08:49:53 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\Property\PropertyTypeDeclarationRector` ](../rules/TypeDeclaration/Rector/Property/PropertyTypeDeclarationRector.php )
2020-07-28 23:41:20 +00:00
2020-11-25 21:34:34 +00:00
```diff
class SomeClass
{
+ /**
+ * @var int
+ */
private $value;
2019-09-25 08:49:53 +00:00
2020-11-25 21:34:34 +00:00
public function run()
{
$this->value = 123;
}
}
```
2019-09-25 08:49:53 +00:00
2020-11-25 21:34:34 +00:00
< br >
2020-07-24 11:46:57 +00:00
2021-05-09 00:15:50 +00:00
### ReturnNeverTypeRector
2021-07-22 18:40:24 +00:00
Add "never" return-type for methods that never return anything
2021-05-09 00:15:50 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector` ](../rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php )
2021-05-09 00:15:50 +00:00
```diff
final class SomeClass
{
2021-07-22 18:40:24 +00:00
+ /**
+ * @return never
+ */
public function run()
2021-05-09 00:15:50 +00:00
{
throw new InvalidException();
}
}
```
< br >
2020-11-25 21:34:34 +00:00
### ReturnTypeDeclarationRector
2019-02-04 00:32:53 +00:00
2020-11-25 21:34:34 +00:00
Change `@return` types and type from static analysis to type declarations if not a BC-break
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector` ](../rules/TypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector.php )
2019-02-04 00:32:53 +00:00
```diff
2020-11-25 21:34:34 +00:00
class SomeClass
2019-06-02 07:45:37 +00:00
{
2020-11-25 21:34:34 +00:00
- /**
- * @return int
- */
- public function getCount()
+ public function getCount(): int
2019-06-02 07:45:37 +00:00
{
}
}
2019-02-04 00:32:53 +00:00
```
2020-11-16 17:50:38 +00:00
< br >
2021-01-30 20:30:37 +00:00
2021-02-20 23:02:05 +00:00
### ReturnTypeFromReturnNewRector
2021-06-06 07:59:41 +00:00
Add return type to function like with return new
2021-02-20 23:02:05 +00:00
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector` ](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php )
2021-02-20 23:02:05 +00:00
```diff
final class SomeClass
{
- public function action()
2021-06-21 19:24:28 +00:00
+ public function action(): Response
2021-02-20 23:02:05 +00:00
{
return new Response();
}
}
```
< br >
### ReturnTypeFromStrictTypedCallRector
Add return type from strict return type of call
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector` ](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php )
2021-02-20 23:02:05 +00:00
```diff
final class SomeClass
{
- public function getData()
+ public function getData(): int
{
return $this->getNumber();
}
private function getNumber(): int
{
return 1000;
}
}
```
< br >
2021-02-07 10:11:42 +00:00
### ReturnTypeFromStrictTypedPropertyRector
Add return method return type based on strict typed property
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector` ](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php )
2021-02-07 10:11:42 +00:00
```diff
final class SomeClass
{
private int $age = 100;
- public function getAge()
+ public function getAge(): int
{
return $this->age;
}
}
```
< br >
2021-12-15 00:48:30 +00:00
### TypedPropertyFromAssignsRector
Add typed property from assigned types
- class: [`Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector` ](../rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php )
```diff
final class SomeClass
{
- private $name;
+ private string|null $name = null;
public function run()
{
$this->name = 'string';
}
}
```
< br >
2021-02-07 10:11:42 +00:00
### TypedPropertyFromStrictConstructorRector
Add typed properties based only on strict constructor types
2021-05-23 09:33:26 +00:00
- class: [`Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector` ](../rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php )
2021-02-07 10:11:42 +00:00
```diff
class SomeObject
{
- private $name;
+ private string $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
```
< br >
2021-10-24 00:34:27 +00:00
### TypedPropertyFromStrictGetterMethodReturnTypeRector
Complete property type based on getter strict types
- class: [`Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector` ](../rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector.php )
```diff
final class SomeClass
{
- public $name;
+ public ?string $name;
public function getName(): string|null
{
return $this->name;
}
}
```
< br >
2021-01-30 20:30:37 +00:00
## Visibility
### ChangeConstantVisibilityRector
Change visibility of constant from parent class.
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Visibility\Rector\ClassConst\ChangeConstantVisibilityRector` ](../rules/Visibility/Rector/ClassConst/ChangeConstantVisibilityRector.php )
2021-01-30 20:30:37 +00:00
```php
use Rector\Visibility\Rector\ClassConst\ChangeConstantVisibilityRector;
2021-02-09 14:02:29 +00:00
use Rector\Visibility\ValueObject\ChangeConstantVisibility;
2021-01-30 20:30:37 +00:00
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(ChangeConstantVisibilityRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ChangeConstantVisibility('ParentObject', 'SOME_CONSTANT', 2)]);
2021-01-30 20:30:37 +00:00
};
```
↓
```diff
class FrameworkClass
{
protected const SOME_CONSTANT = 1;
}
class MyClass extends FrameworkClass
{
- public const SOME_CONSTANT = 1;
+ protected const SOME_CONSTANT = 1;
}
```
< br >
### ChangeMethodVisibilityRector
Change visibility of method from parent class.
:wrench: **configure it!**
2021-05-23 09:33:26 +00:00
- class: [`Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector` ](../rules/Visibility/Rector/ClassMethod/ChangeMethodVisibilityRector.php )
2021-01-30 20:30:37 +00:00
```php
use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
2021-04-25 09:44:34 +00:00
2021-01-30 20:30:37 +00:00
$services->set(ChangeMethodVisibilityRector::class)
2021-12-06 21:12:03 +00:00
->configure([new ChangeMethodVisibility('FrameworkClass', 'someMethod', 2)]);
2021-01-30 20:30:37 +00:00
};
```
↓
```diff
class FrameworkClass
{
2021-07-22 18:40:24 +00:00
protected function someMethod()
2021-01-30 20:30:37 +00:00
{
}
}
class MyClass extends FrameworkClass
{
2021-07-22 18:40:24 +00:00
- public function someMethod()
+ protected function someMethod()
2021-01-30 20:30:37 +00:00
{
}
}
```
< br >
2022-04-10 00:31:45 +00:00
### ExplicitPublicClassMethodRector
Add explicit public method visibility.
- class: [`Rector\Visibility\Rector\ClassMethod\ExplicitPublicClassMethodRector` ](../rules/Visibility/Rector/ClassMethod/ExplicitPublicClassMethodRector.php )
```diff
class SomeClass
{
- function foo()
+ public function foo()
{
}
}
```
< br >