2019-08-24 11:08:59 +00:00
# All 336 Rectors Overview
2018-04-29 09:03:47 +00:00
2018-08-01 20:09:34 +00:00
- [Projects ](#projects )
- [General ](#general )
## Projects
2019-08-05 21:10:47 +00:00
- [Architecture ](#architecture )
2019-02-04 00:32:53 +00:00
- [CakePHP ](#cakephp )
2019-03-09 13:24:30 +00:00
- [Celebrity ](#celebrity )
2019-02-04 00:32:53 +00:00
- [CodeQuality ](#codequality )
- [CodingStyle ](#codingstyle )
- [DeadCode ](#deadcode )
2018-08-01 20:09:34 +00:00
- [Doctrine ](#doctrine )
2019-02-04 00:32:53 +00:00
- [DomainDrivenDesign ](#domaindrivendesign )
2019-07-06 19:51:36 +00:00
- [ElasticSearchDSL ](#elasticsearchdsl )
2019-02-04 00:32:53 +00:00
- [Guzzle ](#guzzle )
2019-03-09 13:24:30 +00:00
- [Laravel ](#laravel )
2019-05-26 16:38:09 +00:00
- [Legacy ](#legacy )
2019-02-21 14:36:16 +00:00
- [MysqlToMysqli ](#mysqltomysqli )
2019-04-02 13:35:35 +00:00
- [Nette ](#nette )
2019-03-16 20:31:46 +00:00
- [NetteTesterToPHPUnit ](#nettetestertophpunit )
2019-02-02 16:22:15 +00:00
- [NetteToSymfony ](#nettetosymfony )
2019-02-04 00:32:53 +00:00
- [PHPStan ](#phpstan )
2018-07-31 21:47:59 +00:00
- [PHPUnit ](#phpunit )
2019-08-05 21:10:47 +00:00
- [PHPUnitSymfony ](#phpunitsymfony )
2019-08-18 10:56:56 +00:00
- [PSR4 ](#psr4 )
2019-02-04 00:32:53 +00:00
- [Php ](#php )
2018-08-01 20:09:34 +00:00
- [PhpParser ](#phpparser )
2019-03-16 20:31:46 +00:00
- [PhpSpecToPHPUnit ](#phpspectophpunit )
2019-05-01 23:56:58 +00:00
- [RemovingStatic ](#removingstatic )
2019-08-05 21:10:47 +00:00
- [Restoration ](#restoration )
2019-05-01 23:56:58 +00:00
- [SOLID ](#solid )
2019-02-04 00:32:53 +00:00
- [Sensio ](#sensio )
2019-03-16 20:31:46 +00:00
- [Shopware ](#shopware )
2018-09-28 16:33:35 +00:00
- [Silverstripe ](#silverstripe )
2019-02-04 00:32:53 +00:00
- [Sylius ](#sylius )
- [Symfony ](#symfony )
2019-08-05 21:10:47 +00:00
- [SymfonyCodeQuality ](#symfonycodequality )
2019-07-06 19:51:36 +00:00
- [SymfonyPHPUnit ](#symfonyphpunit )
2018-09-28 16:33:35 +00:00
- [Twig ](#twig )
2019-05-19 08:27:38 +00:00
- [TypeDeclaration ](#typedeclaration )
2018-09-28 16:33:35 +00:00
2019-08-05 21:10:47 +00:00
## Architecture
### `ConstructorInjectionToActionInjectionRector`
- class: `Rector\Architecture\Rector\Class_\ConstructorInjectionToActionInjectionRector`
```diff
final class SomeController
{
- /**
- * @var ProductRepository
- */
- private $productRepository;
-
- public function __construct(ProductRepository $productRepository)
+ public function default(ProductRepository $productRepository)
{
- $this->productRepository = $productRepository;
- }
-
- public function default()
- {
- $products = $this->productRepository->fetchAll();
+ $products = $productRepository->fetchAll();
}
}
```
< br >
### `RemoveRepositoryFromEntityAnnotationRector`
- class: `Rector\Architecture\Rector\Class_\RemoveRepositoryFromEntityAnnotationRector`
Removes repository class from @Entity annotation
```diff
use Doctrine\ORM\Mapping as ORM;
/**
- * @ORM \Entity(repositoryClass="ProductRepository")
+ * @ORM \Entity
*/
class Product
{
}
```
< br >
2019-02-04 00:32:53 +00:00
## CakePHP
2018-09-28 16:33:35 +00:00
2019-06-02 07:45:37 +00:00
### `ChangeSnakedFixtureNameToCamelRector`
- class: `Rector\CakePHP\Rector\Name\ChangeSnakedFixtureNameToCamelRector`
Changes $fixtues style from snake_case to CamelCase.
```diff
class SomeTest
{
protected $fixtures = [
- 'app.posts',
- 'app.users',
- 'some_plugin.posts/special_posts',
+ 'app.Posts',
+ 'app.Users',
+ 'some_plugin.Posts/SpeectialPosts',
];
```
< br >
2018-09-28 16:33:35 +00:00
### `ModalToGetSetRector`
- class: `Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector`
Changes combined set/get `value()` to specific `getValue()` or `setValue(x)` .
```diff
$object = new InstanceConfigTrait;
-$config = $object->config();
-$config = $object->config('key');
+$config = $object->getConfig();
+$config = $object->getConfig('key');
-$object->config('key', 'value');
-$object->config(['key' => 'value']);
+$object->setConfig('key', 'value');
+$object->setConfig(['key' => 'value']);
```
2018-08-01 20:09:34 +00:00
2018-11-11 12:22:35 +00:00
< br >
2019-03-09 13:24:30 +00:00
## Celebrity
### `CommonNotEqualRector`
- class: `Rector\Celebrity\Rector\NotEqual\CommonNotEqualRector`
Use common != instead of less known < > with same meaning
```diff
final class SomeClass
{
public function run($one, $two)
{
- return $one < > $two;
+ return $one != $two;
}
}
```
< br >
### `LogicalToBooleanRector`
- class: `Rector\Celebrity\Rector\BooleanOp\LogicalToBooleanRector`
Change OR, AND to ||, & & with more common understanding
```diff
-if ($f = false or true) {
+if (($f = false) || true) {
return $f;
}
```
< br >
2019-05-29 13:40:20 +00:00
### `SetTypeToCastRector`
2019-02-21 14:36:16 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Celebrity\Rector\FuncCall\SetTypeToCastRector`
2019-02-21 14:36:16 +00:00
2019-05-29 13:40:20 +00:00
Changes settype() to (type) where possible
2019-02-21 14:36:16 +00:00
```diff
class SomeClass
{
2019-05-29 13:40:20 +00:00
- public function run($foo)
+ public function run(array $items)
2019-02-21 14:36:16 +00:00
{
2019-05-29 13:40:20 +00:00
- settype($foo, 'string');
+ $foo = (string) $foo;
- return settype($foo, 'integer');
+ return (int) $foo;
2019-02-21 14:36:16 +00:00
}
}
```
< br >
2019-05-29 13:40:20 +00:00
## CodeQuality
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
### `AndAssignsToSeparateLinesRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\LogicalAnd\AndAssignsToSeparateLinesRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Split 2 assigns ands to separate line
2019-03-09 13:24:30 +00:00
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function run()
2019-03-09 13:24:30 +00:00
{
2019-06-02 07:45:37 +00:00
$tokens = [];
- $token = 4 and $tokens[] = $token;
+ $token = 4;
+ $tokens[] = $token;
2019-03-09 13:24:30 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `BooleanNotIdenticalToNotIdenticalRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +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
```diff
2019-05-29 13:40:20 +00:00
class SomeClass
2019-02-04 00:32:53 +00:00
{
2019-05-29 13:40:20 +00:00
public function run()
{
2019-06-02 07:45:37 +00:00
$a = true;
$b = false;
- 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
2019-05-29 13:40:20 +00:00
}
2019-02-04 00:32:53 +00:00
}
2018-12-22 11:22:28 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `CallableThisArrayToAnonymousFunctionRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
Convert [$this, "method"] to proper anonymous function
2018-12-31 11:50:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
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)
{
return $first < => $second;
}
}
2018-12-31 11:50:32 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `CombinedAssignRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Assign\CombinedAssignRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
Simplify $value = $value + 5; assignments to shorter ones
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$value = $value + 5;
+$value += 5;
2018-10-21 22:26:45 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `CompactToVariablesRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
Change compact() call to own array
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
public function run()
{
$checkout = 'one';
$form = 'two';
- return compact('checkout', 'form');
+ return ['checkout' => $checkout, 'form' => $form];
}
}
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `CompleteDynamicPropertiesRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Add missing dynamic properties
```diff
class SomeClass
{
+ /**
+ * @var int
+ */
+ public $value;
public function set()
{
$this->value = 5;
}
}
```
< br >
### `ConsecutiveNullCompareReturnsToNullCoalesceQueueRector`
- class: `Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector`
Change multiple null compares to ?? queue
2019-05-01 23:56:58 +00:00
```diff
class SomeClass
{
public function run()
{
2019-06-02 07:45:37 +00:00
- if (null !== $this->orderItem) {
- return $this->orderItem;
2019-05-29 13:40:20 +00:00
- }
2019-06-02 07:45:37 +00:00
-
- if (null !== $this->orderItemUnit) {
- return $this->orderItemUnit;
- }
-
- return null;
+ return $this->orderItem ?? $this->orderItemUnit;
2019-05-01 23:56:58 +00:00
}
}
```
< br >
2019-05-29 13:40:20 +00:00
### `ExplicitBoolCompareRector`
2019-05-19 08:27:38 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector`
2019-05-19 08:27:38 +00:00
2019-05-29 13:40:20 +00:00
Make if conditions more explicit
2019-05-19 08:27:38 +00:00
```diff
2019-05-29 13:40:20 +00:00
final class SomeController
2019-05-19 08:27:38 +00:00
{
2019-05-29 13:40:20 +00:00
public function run($items)
2019-05-19 08:27:38 +00:00
{
2019-05-29 13:40:20 +00:00
- if (!count($items)) {
+ if (count($items) === 0) {
return 'no items';
2019-05-19 08:27:38 +00:00
}
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `ForToForeachRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\For_\ForToForeachRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Change for() to foreach() where useful
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function run($tokens)
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- 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') {
$previousNonSpaceToken = $this->getPreviousNonSpaceToken($tokens, $i);
if ($previousNonSpaceToken !== null & & $previousNonSpaceToken[0] === T_OBJECT_OPERATOR) {
continue;
}
$tokens[$i][0] = self::T_FN;
}
}
2019-05-29 13:40:20 +00:00
}
}
2019-02-04 00:32:53 +00:00
```
< br >
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
### `ForeachToInArrayRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
Simplify `foreach` loops into `in_array` when possible
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
-foreach ($items as $item) {
- if ($item === "something") {
- return true;
- }
2019-05-29 13:40:20 +00:00
-}
-
-return false;
2019-06-02 07:45:37 +00:00
+in_array("something", $items, true);
2018-10-21 22:26:45 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-23 20:13:13 +00:00
2019-06-02 07:45:37 +00:00
### `GetClassToInstanceOfRector`
2018-10-23 20:13:13 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Identical\GetClassToInstanceOfRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Changes comparison with get_class to instanceof
2018-10-23 20:13:13 +00:00
```diff
2019-06-02 07:45:37 +00:00
-if (EventsListener::class === get_class($event->job)) { }
+if ($event->job instanceof EventsListener) { }
2018-10-23 20:13:13 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `InArrayAndArrayKeysToArrayKeyExistsRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
Simplify `in_array` and `array_keys` functions combination into `array_key_exists` when `array_keys` has one argument only
2019-05-19 08:27:38 +00:00
```diff
2019-06-02 07:45:37 +00:00
-in_array("key", array_keys($array), true);
+array_key_exists("key", $array);
```
< br >
2019-08-05 21:10:47 +00:00
### `IsAWithStringWithThirdArgumentRector`
- class: `Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector`
```diff
class SomeClass
{
public function __construct(string $value)
{
- return is_a($value, 'stdClass');
+ return is_a($value, 'stdClass', true);
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `JoinStringConcatRector`
- class: `Rector\CodeQuality\Rector\Concat\JoinStringConcatRector`
Joins concat of 2 strings
```diff
class SomeClass
{
2019-05-19 08:27:38 +00:00
public function run()
{
2019-06-02 07:45:37 +00:00
- $name = 'Hi' . ' Tom';
+ $name = 'Hi Tom';
2019-05-19 08:27:38 +00:00
}
}
```
< br >
2019-08-05 21:10:47 +00:00
### `RemoveAlwaysTrueConditionSetInConstructorRector`
- class: `Rector\CodeQuality\Rector\If_\RemoveAlwaysTrueConditionSetInConstructorRector`
If conditions is always true, perform the content right away
```diff
final class SomeClass
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function go()
{
- if ($this->value) {
- return 'yes';
- }
+ return 'yes';
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyArraySearchRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Identical\SimplifyArraySearchRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Simplify array_search to in_array
2018-11-11 12:22:35 +00:00
```diff
2019-06-02 07:45:37 +00:00
-array_search("searching", $array) !== false;
+in_array("searching", $array, true);
```
```diff
-array_search("searching", $array) != false;
+in_array("searching", $array);
2018-11-11 12:22:35 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyBoolIdenticalTrueRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
Symplify bool value compare to true or false
2018-12-31 11:50:32 +00:00
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function run(bool $value, string $items)
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +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
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyConditionsRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Identical\SimplifyConditionsRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
Simplify conditions
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
-if (! ($foo !== 'bar')) {...
+if ($foo === 'bar') {...
```
< br >
### `SimplifyDeMorganBinaryRector`
- class: `Rector\CodeQuality\Rector\BinaryOp\SimplifyDeMorganBinaryRector`
Simplify negated conditions with de Morgan theorem
```diff
< ?php
$a = 5;
$b = 10;
-$result = !($a > 20 || $b < = 50);
+$result = $a < = 20 & & $b > 50;
2018-10-21 22:26:45 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-21 22:26:45 +00:00
2019-05-29 13:40:20 +00:00
### `SimplifyDuplicatedTernaryRector`
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector`
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
Remove ternary that duplicated return value of true : false
2019-03-31 12:25:39 +00:00
```diff
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function run(bool $value, string $name)
2019-03-31 12:25:39 +00:00
{
2019-05-29 13:40:20 +00:00
- $isTrue = $value ? true : false;
+ $isTrue = $value;
$isName = $name ? true : false;
2019-03-31 12:25:39 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyEmptyArrayCheckRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Simplify `is_array` and `empty` functions combination into a simple identical check for an empty array
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
-is_array($values) & & empty($values)
+$values === []
2018-10-21 22:26:45 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
### `SimplifyForeachToArrayFilterRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToArrayFilterRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
Simplify foreach with function filtering to array filter
2019-02-18 15:51:24 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$directories = [];
$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
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyForeachToCoalescingRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Changes foreach that returns set value to ??
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +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
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyFuncGetArgsCountRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\FuncCall\SimplifyFuncGetArgsCountRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
Simplify count of func_get_args() to fun_num_args()
```diff
-count(func_get_args());
+func_num_args();
```
< br >
### `SimplifyIfElseToTernaryRector`
- class: `Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector`
Changes if/else for same value as assign to ternary
2018-12-31 11:50:32 +00:00
```diff
class SomeClass
{
public function run()
{
2019-06-02 07:45:37 +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
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyIfIssetToNullCoalescingRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Simplify binary if to null coalesce
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeController
{
public function run($possibleStatieYamlFile)
{
- if (isset($possibleStatieYamlFile['import'])) {
- $possibleStatieYamlFile['import'] = array_merge($possibleStatieYamlFile['import'], $filesToImport);
- } else {
- $possibleStatieYamlFile['import'] = $filesToImport;
- }
+ $possibleStatieYamlFile['import'] = array_merge($possibleStatieYamlFile['import'] ?? [], $filesToImport);
}
}
2018-10-21 22:26:45 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
### `SimplifyIfNotNullReturnRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\If_\SimplifyIfNotNullReturnRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
Changes redundant null check to instant return
2019-02-18 15:51:24 +00:00
```diff
2019-06-02 07:45:37 +00:00
$newNode = 'something ;
-if ($newNode !== null) {
- return $newNode;
-}
-
-return null;
+return $newNode;
2019-02-18 15:51:24 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyIfReturnBoolRector`
2019-01-22 20:34:38 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector`
2019-01-22 20:34:38 +00:00
2019-06-02 07:45:37 +00:00
Shortens if return false/true to direct return
2019-01-22 20:34:38 +00:00
```diff
2019-06-02 07:45:37 +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
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyInArrayValuesRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Removes unneeded array_values() in in_array() call
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-in_array("key", array_values($array), true);
+in_array("key", $array, true);
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyRegexPatternRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Simplify regex pattern to known ranges
2019-05-19 08:27:38 +00:00
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function run($value)
2019-05-19 08:27:38 +00:00
{
2019-06-02 07:45:37 +00:00
- preg_match('#[a-zA-Z0-9+]#', $value);
+ preg_match('#[\w\d+]#', $value);
2019-05-19 08:27:38 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyStrposLowerRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Simplify strpos(strtolower(), "...") calls
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
-strpos(strtolower($var), "...")"
+stripos($var, "...")"
2019-05-29 13:40:20 +00:00
```
2019-02-04 00:32:53 +00:00
< br >
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
### `SimplifyTautologyTernaryRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
Simplify tautology ternary to value
2018-12-22 11:22:28 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$value = ($fullyQualifiedTypeHint !== $typeHint) ? $fullyQualifiedTypeHint : $typeHint;
+$value = $fullyQualifiedTypeHint;
2018-12-22 11:22:28 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyUselessVariableRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Removes useless variable assigns
2018-10-23 18:58:57 +00:00
```diff
2019-06-02 07:45:37 +00:00
function () {
- $a = true;
- return $a;
+ return true;
};
2018-10-23 18:58:57 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
### `SingleInArrayToCompareRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
Changes in_array() with single element to ===
2019-05-01 23:56:58 +00:00
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function run()
2019-05-01 23:56:58 +00:00
{
2019-06-02 07:45:37 +00:00
- if (in_array(strtolower($type), ['$this'], true)) {
+ if (strtolower($type) === '$this') {
return strtolower($type);
}
2019-05-01 23:56:58 +00:00
}
}
```
< br >
2019-08-05 21:10:47 +00:00
### `StrlenZeroToIdenticalEmptyStringRector`
- class: `Rector\CodeQuality\Rector\FuncCall\StrlenZeroToIdenticalEmptyStringRector`
```diff
class SomeClass
{
public function run($value)
{
- $empty = strlen($value) === 0;
+ $empty = $value === '';
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `TernaryToElvisRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Ternary\TernaryToElvisRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
Use ?: instead of ?, where useful
2018-12-22 11:22:28 +00:00
```diff
2019-06-02 07:45:37 +00:00
function elvis()
{
- $value = $a ? $a : false;
+ $value = $a ?: false;
}
2018-12-22 11:22:28 +00:00
```
< br >
2019-08-05 21:10:47 +00:00
### `ThrowWithPreviousExceptionRector`
- class: `Rector\CodeQuality\Rector\Catch_\ThrowWithPreviousExceptionRector`
When throwing into a catch block, checks that the previous exception is passed to the new throw clause
```diff
class SomeClass
{
public function run()
{
try {
$someCode = 1;
} catch (Throwable $throwable) {
- throw new AnotherException('ups');
+ throw new AnotherException('ups', $throwable->getCode(), $throwable);
}
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `UnnecessaryTernaryExpressionRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
Remove unnecessary ternary expressions.
2018-12-25 19:55:16 +00:00
2018-11-11 12:22:35 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$foo === $bar ? true : false;
+$foo === $bar;
2018-11-11 12:22:35 +00:00
```
< br >
2019-05-29 13:40:20 +00:00
### `UseIdenticalOverEqualWithSameTypeRector`
2018-12-22 14:15:11 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector`
2018-12-22 14:15:11 +00:00
2019-05-29 13:40:20 +00:00
Use ===/!== over ==/!=, it values have the same type
2018-12-22 14:15:11 +00:00
```diff
2019-02-04 00:32:53 +00:00
class SomeClass
2018-12-22 14:15:11 +00:00
{
2019-05-29 13:40:20 +00:00
public function run(int $firstValue, int $secondValue)
2019-02-04 00:32:53 +00:00
{
2019-05-29 13:40:20 +00:00
- $isSame = $firstValue == $secondValue;
- $isDiffernt = $firstValue != $secondValue;
+ $isSame = $firstValue === $secondValue;
+ $isDiffernt = $firstValue !== $secondValue;
2019-02-04 00:32:53 +00:00
}
2018-12-22 14:15:11 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
## CodingStyle
2019-05-26 19:26:33 +00:00
2019-08-17 13:06:02 +00:00
### `AddArrayDefaultToArrayPropertyRector`
2019-05-26 19:26:33 +00:00
2019-08-17 13:06:02 +00:00
- class: `Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector`
2019-06-02 07:45:37 +00:00
2019-08-17 13:06:02 +00:00
Adds array default value to property to prevent foreach over null error
2019-05-26 19:26:33 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
/**
* @var int[]
*/
2019-08-17 13:06:02 +00:00
- private $values;
+ private $values = [];
2019-05-29 13:40:20 +00:00
2019-08-17 13:06:02 +00:00
public function isEmpty()
2019-06-02 07:45:37 +00:00
{
2019-08-17 13:06:02 +00:00
- return $this->values === null;
+ return $this->values === [];
2019-06-02 07:45:37 +00:00
}
}
2019-05-26 19:26:33 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `BinarySwitchToIfElseRector`
2019-05-26 11:47:23 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector`
2019-05-26 11:47:23 +00:00
2019-06-02 07:45:37 +00:00
Changes switch with 2 options to if-else
2019-05-26 11:47:23 +00:00
```diff
2019-06-02 07:45:37 +00:00
-switch ($foo) {
- case 'my string':
- $result = 'ok';
- break;
-
- default:
- $result = 'not ok';
+if ($foo == 'my string') {
+ $result = 'ok;
+} else {
+ $result = 'not ok';
}
2019-05-26 11:47:23 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `CatchExceptionNameMatchingTypeRector`
2019-01-03 10:54:13 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector`
2019-01-03 10:54:13 +00:00
2019-06-02 07:45:37 +00:00
Type and name of catch exception should match
2019-01-03 10:54:13 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeClass
2019-01-03 10:54:13 +00:00
{
2019-06-02 07:45:37 +00:00
public function run()
2019-01-03 10:54:13 +00:00
{
2019-06-02 07:45:37 +00:00
try {
// ...
- } catch (SomeException $typoException) {
- $typoException->getMessage();
+ } catch (SomeException $someException) {
+ $someException->getMessage();
2019-05-29 13:40:20 +00:00
}
2019-01-03 10:54:13 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `ConsistentImplodeRector`
2019-05-26 11:47:23 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\FuncCall\ConsistentImplodeRector`
2019-05-26 11:47:23 +00:00
2019-06-02 07:45:37 +00:00
Changes various implode forms to consistent one
2019-05-26 11:47:23 +00:00
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function run(array $items)
2019-05-26 11:47:23 +00:00
{
2019-06-02 07:45:37 +00:00
- $itemsAsStrings = implode($items);
- $itemsAsStrings = implode($items, '|');
+ $itemsAsStrings = implode('', $items);
+ $itemsAsStrings = implode('|', $items);
$itemsAsStrings = implode('|', $items);
2019-05-26 11:47:23 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `ConsistentPregDelimiterRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Replace PREG delimiter with configured one
2019-02-04 00:32:53 +00:00
```diff
2019-03-09 13:24:30 +00:00
class SomeClass
2019-02-04 00:32:53 +00:00
{
2019-03-09 13:24:30 +00:00
public function run()
{
2019-06-02 07:45:37 +00:00
- preg_match('~value~', $value);
- preg_match_all('~value~im', $value);
+ preg_match('#value#', $value);
+ preg_match_all('#value#im', $value);
2019-03-09 13:24:30 +00:00
}
2019-02-04 00:32:53 +00:00
}
```
< br >
2018-11-11 12:22:35 +00:00
2019-08-05 21:10:47 +00:00
### `EncapsedStringsToSprintfRector`
- class: `Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector`
Convert enscaped {$string} to more readable sprintf
```diff
final class SomeClass
{
public function run(string $format)
{
- return "Unsupported format {$format}";
+ return sprintf('Unsupported format %s', $format);
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `FollowRequireByDirRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\Include_\FollowRequireByDirRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
include/require should be followed by absolute path
2019-05-01 23:56:58 +00:00
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function run()
2019-05-01 23:56:58 +00:00
{
2019-06-02 07:45:37 +00:00
- require 'autoload.php';
+ require __DIR__ . '/autoload.php';
2019-05-01 23:56:58 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `IdenticalFalseToBooleanNotRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\Identical\IdenticalFalseToBooleanNotRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
Changes === false to negate !
2019-01-03 10:54:13 +00:00
```diff
2019-05-29 13:40:20 +00:00
-if ($something === false) {}
+if (! $something) {}
2019-01-03 10:54:13 +00:00
```
< br >
2019-05-29 13:40:20 +00:00
### `ImportFullyQualifiedNamesRector`
2019-05-26 11:47:23 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector`
2019-05-26 11:47:23 +00:00
2019-05-29 13:40:20 +00:00
Import fully qualified names to use statements
2019-05-26 11:47:23 +00:00
```diff
2019-05-29 13:40:20 +00:00
+use SomeAnother\AnotherClass;
+
2019-05-26 11:47:23 +00:00
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function create()
2019-05-26 11:47:23 +00:00
{
2019-05-29 13:40:20 +00:00
- return SomeAnother\AnotherClass;
+ return AnotherClass;
2019-05-26 11:47:23 +00:00
}
}
```
< br >
2019-08-05 21:10:47 +00:00
### `ManualJsonStringToJsonEncodeArrayRector`
- class: `Rector\CodingStyle\Rector\String_\ManualJsonStringToJsonEncodeArrayRector`
Add extra space before new assign set
```diff
final class SomeClass
{
public function run()
{
- $someJsonAsString = '{"role_name":"admin","numberz":{"id":"10"}}';
+ $data = [
+ 'role_name' => 'admin',
+ 'numberz' => ['id' => 10]
+ ];
+
2019-08-17 13:06:02 +00:00
+ $someJsonAsString = Nette\Utils\Json::encode($data);
2019-08-05 21:10:47 +00:00
}
}
```
< br >
### `NewlineBeforeNewAssignSetRector`
- class: `Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector`
Add extra space before new assign set
```diff
final class SomeClass
{
public function run()
{
$value = new Value;
$value->setValue(5);
+
$value2 = new Value;
$value2->setValue(1);
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `NullableCompareToNullRector`
2019-01-03 10:54:13 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\If_\NullableCompareToNullRector`
2019-01-03 10:54:13 +00:00
2019-06-02 07:45:37 +00:00
Changes negate of empty comparison of nullable value to explicit === or !== compare
2019-01-03 10:54:13 +00:00
```diff
2019-06-02 07:45:37 +00:00
/** @var stdClass|null $value */
-if ($value) {
+if ($value !== null) {
}
2019-01-03 10:54:13 +00:00
2019-06-02 07:45:37 +00:00
-if (!$value) {
+if ($value === null) {
2019-01-03 10:54:13 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveUnusedAliasRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\Use_\RemoveUnusedAliasRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Removes unused use aliases
2018-11-11 12:22:35 +00:00
```diff
2019-06-02 07:45:37 +00:00
-use Symfony\Kernel as BaseKernel;
+use Symfony\Kernel;
-class SomeClass extends BaseKernel
+class SomeClass extends Kernel
{
}
2018-11-11 12:22:35 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ReturnArrayClassMethodToYieldRector`
2019-05-26 11:47:23 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector`
2019-05-26 11:47:23 +00:00
2019-06-02 07:45:37 +00:00
Turns yield return to array return in specific type and method
```yaml
services:
Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector:
EventSubscriberInterface:
- getSubscribedEvents
```
↓
2019-05-26 11:47:23 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeEventSubscriber implements EventSubscriberInterface
2019-05-26 11:47:23 +00:00
{
2019-06-02 07:45:37 +00:00
public static function getSubscribedEvents()
2019-05-26 11:47:23 +00:00
{
2019-06-02 07:45:37 +00:00
- yield 'event' => 'callback';
+ return ['event' => 'callback'];
2019-05-26 11:47:23 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SimpleArrayCallableToStringRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\FuncCall\SimpleArrayCallableToStringRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Changes redundant anonymous bool functions to simple calls
2018-11-11 12:22:35 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$paths = array_filter($paths, function ($path): bool {
- return is_dir($path);
-});
+array_filter($paths, "is_dir");
2019-05-29 13:40:20 +00:00
```
2018-11-11 12:22:35 +00:00
2019-05-29 13:40:20 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `SplitDoubleAssignRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Split multiple inline assigns to each own lines default value, to prevent undefined array issues
2019-05-29 13:40:20 +00:00
```diff
class SomeClass
{
public function run()
{
2019-06-02 07:45:37 +00:00
- $one = $two = 1;
+ $one = 1;
+ $two = 1;
2019-05-29 13:40:20 +00:00
}
}
```
< br >
2019-05-01 23:56:58 +00:00
### `SplitGroupedConstantsAndPropertiesRector`
- class: `Rector\CodingStyle\Rector\ClassConst\SplitGroupedConstantsAndPropertiesRector`
Separate constant and properties to own lines
```diff
class SomeClass
{
- const HI = true, AHOJ = 'true';
+ const HI = true;
+ const AHOJ = 'true';
/**
* @var string
*/
- public $isIt, $isIsThough;
+ public $isIt;
+
+ /**
+ * @var string
+ */
+ public $isIsThough;
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SplitStringClassConstantToClassConstFetchRector`
2019-05-26 11:47:23 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector`
2019-05-26 11:47:23 +00:00
2019-06-02 07:45:37 +00:00
Separate class constant in a string to class constant fetch and string
2019-05-26 11:47:23 +00:00
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
const HI = true;
}
class AnotherClass
{
public function get()
{
- return 'SomeClass::HI';
+ return SomeClass::class . '::HI';
}
2019-05-26 11:47:23 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SymplifyQuoteEscapeRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
Prefer quote that not inside the string
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
public function run()
{
- $name = "\" Tom";
- $name = '\' Sara';
+ $name = '" Tom';
+ $name = "' Sara";
}
}
```
< br >
### `VarConstantCommentRector`
- class: `Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector`
Constant should have a @var comment with type
```diff
class SomeClass
2019-02-04 00:32:53 +00:00
{
+ /**
2019-06-02 07:45:37 +00:00
+ * @var string
2019-02-04 00:32:53 +00:00
+ */
2019-06-02 07:45:37 +00:00
const HI = 'hi';
2019-02-04 00:32:53 +00:00
}
2018-10-21 22:26:45 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-21 22:26:45 +00:00
2019-05-29 13:40:20 +00:00
### `YieldClassMethodToArrayClassMethodRector`
2019-01-03 10:54:13 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector`
2019-01-03 10:54:13 +00:00
2019-05-29 13:40:20 +00:00
Turns yield return to array return in specific type and method
```yaml
services:
Rector\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector:
EventSubscriberInterface:
- getSubscribedEvents
```
↓
2019-01-03 10:54:13 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
- yield 'event' => 'callback';
+ return ['event' => 'callback'];
}
}
2019-02-04 00:32:53 +00:00
```
2019-01-03 10:54:13 +00:00
2019-02-04 00:32:53 +00:00
< br >
2019-06-02 07:45:37 +00:00
## DeadCode
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
### `RemoveAndTrueRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Remove and true that has no added value
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function run()
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- return true && 5 === 1;
+ return 5 === 1;
2019-05-29 13:40:20 +00:00
}
2019-01-03 10:54:13 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveCodeAfterReturnRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Remove dead code after return statement
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
public function run(int $a)
{
return $a;
- $a++;
}
}
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-06 13:01:53 +00:00
### `RemoveConcatAutocastRector`
- class: `Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector`
Remove (string) casting when it comes to concat, that does this by default
```diff
class SomeConcatingClass
{
public function run($value)
{
- return 'hi ' . (string) $value;
+ return 'hi ' . $value;
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveDeadConstructorRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\ClassMethod\RemoveDeadConstructorRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Remove empty constructor
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
- public function __construct()
- {
- }
2019-02-04 00:32:53 +00:00
}
```
< br >
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
### `RemoveDeadIfForeachForRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
Remove if, foreach and for that does not do anything
2018-12-25 19:55:16 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeClass
2019-02-04 00:32:53 +00:00
{
2019-06-02 07:45:37 +00:00
public function run($someObject)
2019-02-04 00:32:53 +00:00
{
2019-06-02 07:45:37 +00:00
$value = 5;
- if ($value) {
- }
-
if ($someObject->run()) {
- }
-
- foreach ($values as $value) {
}
return $value;
2019-02-04 00:32:53 +00:00
}
}
2018-12-25 19:55:16 +00:00
```
< br >
2019-05-29 13:40:20 +00:00
### `RemoveDeadReturnRector`
2018-12-25 19:55:16 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector`
2018-12-25 19:55:16 +00:00
2019-05-29 13:40:20 +00:00
Remove last return in the functions, since does not do anything
2018-12-25 19:55:16 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeClass
2018-12-25 19:55:16 +00:00
{
2019-05-29 13:40:20 +00:00
public function run()
{
$shallWeDoThis = true;
if ($shallWeDoThis) {
return;
}
-
- return;
}
2018-12-25 19:55:16 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveDeadStmtRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\Stmt\RemoveDeadStmtRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
Removes dead code statements
2018-12-31 11:50:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$value = 5;
-$value;
+$value = 5;
2018-12-31 11:50:32 +00:00
```
< br >
2019-07-06 19:51:36 +00:00
### `RemoveDeadZeroAndOneOperationRector`
- class: `Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector`
```diff
class SomeClass
{
public function run()
{
- $value = 5 * 1;
- $value = 5 + 0;
+ $value = 5;
+ $value = 5;
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveDefaultArgumentValueRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\MethodCall\RemoveDefaultArgumentValueRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
Remove argument value, if it is the same as default value
2019-03-09 13:24:30 +00:00
```diff
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function run()
{
2019-06-02 07:45:37 +00:00
- $this->runWithDefault([]);
- $card = self::runWithStaticDefault([]);
+ $this->runWithDefault();
+ $card = self::runWithStaticDefault();
2019-05-29 13:40:20 +00:00
}
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
public function runWithDefault($items = [])
{
return $items;
}
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
public function runStaticWithDefault($cards = [])
2019-02-21 14:36:16 +00:00
{
2019-06-02 07:45:37 +00:00
return $cards;
2019-02-21 14:36:16 +00:00
}
}
```
< br >
2019-07-06 19:51:36 +00:00
### `RemoveDelegatingParentCallRector`
- class: `Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector`
```diff
class SomeClass
{
- public function prettyPrint(array $stmts): string
- {
- return parent::prettyPrint($stmts);
- }
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveDoubleAssignRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
Simplify useless double assigns
2019-02-18 15:51:24 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$value = 1;
$value = 1;
2019-02-18 15:51:24 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveDuplicatedArrayKeyRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
Remove duplicated key in defined arrays.
2019-05-19 08:27:38 +00:00
```diff
2019-06-02 07:45:37 +00:00
$item = [
- 1 => 'A',
1 => 'B'
];
2019-05-19 08:27:38 +00:00
```
< br >
2019-08-05 21:10:47 +00:00
### `RemoveDuplicatedCaseInSwitchRector`
- class: `Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector`
2 following switch keys with identical will be reduced to one result
```diff
class SomeClass
{
public function run()
{
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;
}
}
}
```
< br >
2019-07-06 19:51:36 +00:00
### `RemoveDuplicatedInstanceOfRector`
- class: `Rector\DeadCode\Rector\Instanceof_\RemoveDuplicatedInstanceOfRector`
```diff
class SomeClass
{
public function run($value)
{
- $isIt = $value instanceof A || $value instanceof A;
- $isIt = $value instanceof A && $value instanceof A;
+ $isIt = $value instanceof A;
+ $isIt = $value instanceof A;
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveEmptyClassMethodRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
Remove empty method calls not required by parents
2019-03-31 12:25:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
class OrphanClass
{
- public function __construct()
- {
- }
}
2019-03-31 12:25:39 +00:00
```
< br >
2019-08-24 11:08:59 +00:00
### `RemoveNullPropertyInitializationRector`
- class: `Rector\DeadCode\Rector\Property\RemoveNullPropertyInitializationRector`
Remove initialization with null value from property declarations
```diff
class SunshineCommand extends ParentClassWithNewConstructor
{
- private $myVar = null;
+ private $myVar;
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveOverriddenValuesRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\ClassMethod\RemoveOverriddenValuesRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
Remove initial assigns of overridden values
2018-12-25 19:55:16 +00:00
```diff
2019-05-29 13:40:20 +00:00
final class SomeController
2018-12-25 19:55:16 +00:00
{
2019-05-29 13:40:20 +00:00
public function run()
{
2019-06-02 07:45:37 +00:00
- $directories = [];
$possibleDirectories = [];
$directories = array_filter($possibleDirectories, 'file_exists');
2019-05-29 13:40:20 +00:00
}
2018-12-25 19:55:16 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveParentCallWithoutParentRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
Remove unused parent call with no parent class
2018-12-25 19:55:16 +00:00
```diff
2019-06-02 07:45:37 +00:00
class OrphanClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function __construct()
{
- parent::__construct();
}
2019-05-29 13:40:20 +00:00
}
2018-12-25 19:55:16 +00:00
```
< br >
2019-08-17 13:06:02 +00:00
### `RemoveSetterOnlyPropertyAndMethodCallRector`
- class: `Rector\DeadCode\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector`
Removes method that set values that are never used
```diff
class SomeClass
{
- private $name;
-
- public function setName($name)
- {
- $this->name = $name;
- }
}
class ActiveOnlySetter
{
public function run()
{
$someClass = new SomeClass();
- $someClass->setName('Tom');
}
}
```
< br >
2019-08-05 21:10:47 +00:00
### `RemoveUnusedDoctrineEntityMethodAndPropertyRector`
- class: `Rector\DeadCode\Rector\Class_\RemoveUnusedDoctrineEntityMethodAndPropertyRector`
Removes unused methods and properties from Doctrine entity classes
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM \Entity
*/
class UserEntity
{
- /**
- * @ORM \Column
- */
- private $name;
-
- public function getName()
- {
- return $this->name;
- }
-
- public function setName($name)
- {
- $this->name = $name;
- }
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveUnusedForeachKeyRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
Remove unused key in foreach
2019-05-19 08:27:38 +00:00
```diff
2019-06-02 07:45:37 +00:00
$items = [];
-foreach ($items as $key => $value) {
+foreach ($items as $value) {
$result = $value;
2019-05-19 08:27:38 +00:00
}
```
< br >
2019-05-29 13:40:20 +00:00
### `RemoveUnusedParameterRector`
2019-02-21 14:36:16 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParameterRector`
2019-02-21 14:36:16 +00:00
2019-05-29 13:40:20 +00:00
Remove unused parameter, if not required by interface or parent class
2019-02-21 14:36:16 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeClass
2019-02-21 14:36:16 +00:00
{
2019-05-29 13:40:20 +00:00
- public function __construct($value, $value2)
+ public function __construct($value)
2019-02-21 14:36:16 +00:00
{
2019-05-29 13:40:20 +00:00
$this->value = $value;
2019-02-21 14:36:16 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveUnusedPrivateConstantRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
Remove unused private constant
2019-02-21 14:36:16 +00:00
```diff
2019-05-29 13:40:20 +00:00
final class SomeController
2019-02-21 14:36:16 +00:00
{
2019-06-02 07:45:37 +00:00
- private const SOME_CONSTANT = 5;
2019-05-29 13:40:20 +00:00
public function run()
2019-02-21 14:36:16 +00:00
{
2019-06-02 07:45:37 +00:00
return 5;
2019-02-21 14:36:16 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveUnusedPrivateMethodRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
Remove unused private method
2019-03-31 12:25:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeController
2019-03-31 12:25:39 +00:00
{
2019-06-02 07:45:37 +00:00
public function run()
{
return 5;
}
-
- private function skip()
2019-05-29 13:40:20 +00:00
- {
2019-06-02 07:45:37 +00:00
- return 10;
2019-05-29 13:40:20 +00:00
- }
2019-03-31 12:25:39 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveUnusedPrivatePropertyRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector`
2018-12-25 19:55:16 +00:00
2019-06-02 07:45:37 +00:00
Remove unused private properties
2018-12-25 19:55:16 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeClass
{
2019-06-02 07:45:37 +00:00
- private $property;
2019-05-29 13:40:20 +00:00
}
2018-12-25 19:55:16 +00:00
```
< br >
2019-07-06 19:51:36 +00:00
### `RemoveZeroAndOneBinaryRector`
- class: `Rector\DeadCode\Rector\Plus\RemoveZeroAndOneBinaryRector`
```diff
class SomeClass
{
public function run()
{
- $value = 5 * 1;
- $value = 5 + 0;
+ $value = 5;
+ $value = 5;
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyMirrorAssignRector`
- class: `Rector\DeadCode\Rector\Expression\SimplifyMirrorAssignRector`
Removes unneeded $a = $a assigns
```diff
-$a = $a;
```
< br >
2018-08-01 20:09:34 +00:00
## Doctrine
### `AliasToClassRector`
- class: `Rector\Doctrine\Rector\AliasToClassRector`
Replaces doctrine alias with class.
```diff
2018-08-10 19:07:08 +00:00
$entityManager = new Doctrine\ORM\EntityManager();
-$entityManager->getRepository("AppBundle:Post");
+$entityManager->getRepository(\App\Entity\Post::class);
2018-08-01 20:09:34 +00:00
```
2018-07-31 21:47:59 +00:00
2018-11-11 12:22:35 +00:00
< br >
2019-07-06 19:51:36 +00:00
### `ManagerRegistryGetManagerToEntityManagerRector`
- class: `Rector\Doctrine\Rector\Class_\ManagerRegistryGetManagerToEntityManagerRector`
```diff
-use Doctrine\Common\Persistence\ManagerRegistry;
+use Doctrine\ORM\EntityManagerInterface;
class CustomRepository
{
/**
- * @var ManagerRegistry
+ * @var EntityManagerInterface
*/
- private $managerRegistry;
+ private $entityManager;
- public function __construct(ManagerRegistry $managerRegistry)
+ public function __construct(EntityManagerInterface $entityManager)
{
- $this->managerRegistry = $managerRegistry;
+ $this->entityManager = $entityManager;
}
public function run()
{
- $entityManager = $this->managerRegistry->getManager();
- $someRepository = $entityManager->getRepository('Some');
+ $someRepository = $this->entityManager->getRepository('Some');
}
}
```
< br >
2019-02-04 00:32:53 +00:00
## DomainDrivenDesign
2018-07-31 19:43:39 +00:00
2018-12-14 19:35:35 +00:00
### `ObjectToScalarDocBlockRector`
2018-07-31 19:43:39 +00:00
2018-12-14 19:35:35 +00:00
- class: `Rector\DomainDrivenDesign\Rector\ObjectToScalar\ObjectToScalarDocBlockRector`
2018-05-05 12:48:33 +00:00
2018-10-21 22:26:45 +00:00
Turns defined value object to simple types in doc blocks
```yaml
services:
2018-12-14 19:35:35 +00:00
Rector\DomainDrivenDesign\Rector\ObjectToScalar\ObjectToScalarDocBlockRector:
2018-10-21 22:26:45 +00:00
$valueObjectsToSimpleTypes:
ValueObject: string
```
↓
2018-05-05 12:48:33 +00:00
```diff
2018-10-12 23:15:00 +00:00
/**
2018-10-21 22:26:45 +00:00
- * @var ValueObject|null
+ * @var string|null
2018-10-12 23:15:00 +00:00
*/
2018-10-21 22:26:45 +00:00
private $name;
2018-07-31 19:43:39 +00:00
2018-10-21 22:26:45 +00:00
-/** @var ValueObject|null */
+/** @var string|null */
$name;
```
2018-11-11 12:22:35 +00:00
< br >
2018-12-14 19:35:35 +00:00
### `ObjectToScalarRector`
2018-10-21 22:26:45 +00:00
2018-12-14 19:35:35 +00:00
- class: `Rector\DomainDrivenDesign\Rector\ObjectToScalar\ObjectToScalarRector`
2018-10-21 22:26:45 +00:00
Remove values objects and use directly the value.
```yaml
services:
2018-12-14 19:35:35 +00:00
Rector\DomainDrivenDesign\Rector\ObjectToScalar\ObjectToScalarRector:
2018-10-21 22:26:45 +00:00
$valueObjectsToSimpleTypes:
ValueObject: string
```
↓
2018-07-31 12:50:39 +00:00
```diff
2018-10-21 22:26:45 +00:00
-$name = new ValueObject("name");
+$name = "name";
2018-10-12 23:15:00 +00:00
2018-12-14 19:35:35 +00:00
-function someFunction(ValueObject $name): ?ValueObject {
+function someFunction(string $name): ?string {
}
2018-07-31 12:50:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-07-06 19:51:36 +00:00
## ElasticSearchDSL
### `MigrateFilterToQueryRector`
- class: `Rector\ElasticSearchDSL\Rector\MethodCall\MigrateFilterToQueryRector`
Migrates addFilter to addQuery
```diff
class SomeClass
{
public function run()
{
$search = new \ONGR\ElasticsearchDSL\Search();
- $search->addFilter(
- new \ONGR\ElasticsearchDSL\Query\TermsQuery('categoryIds', [1, 2])
+ $search->addQuery(
+ new \ONGR\ElasticsearchDSL\Query\TermsQuery('categoryIds', [1, 2]),
+ \ONGR\ElasticsearchDSL\Query\Compound\BoolQuery::FILTER
);
}
}
```
< br >
2019-02-04 00:32:53 +00:00
## Guzzle
2018-11-11 12:22:35 +00:00
### `MessageAsArrayRector`
- class: `Rector\Guzzle\Rector\MethodCall\MessageAsArrayRector`
Changes getMessage(..., true) to getMessageAsArray()
```diff
/** @var GuzzleHttp\Message\MessageInterface */
-$value = $message->getMessage('key', true);
+$value = $message->getMessageAsArray('key');
```
< br >
2019-03-09 13:24:30 +00:00
## Laravel
2019-06-02 07:45:37 +00:00
### `FacadeStaticCallToConstructorInjectionRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Laravel\Rector\StaticCall\FacadeStaticCallToConstructorInjectionRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
Move Illuminate\Support\Facades\* static calls to constructor injection
2019-03-09 13:24:30 +00:00
```diff
2019-06-02 07:45:37 +00:00
use Illuminate\Support\Facades\Response;
class ExampleController extends Controller
2019-03-09 13:24:30 +00:00
{
2019-06-02 07:45:37 +00:00
+ /**
+ * @var \Illuminate\Contracts\Routing\ResponseFactory
+ */
+ private $responseFactory;
+
+ public function __construct(\Illuminate\Contracts\Routing\ResponseFactory $responseFactory)
+ {
+ $this->responseFactory = $responseFactory;
+ }
+
public function store()
2019-03-09 13:24:30 +00:00
{
2019-06-02 07:45:37 +00:00
- return Response::view('example', ['new_example' => 123]);
+ return $this->responseFactory->view('example', ['new_example' => 123]);
2019-03-09 13:24:30 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `HelperFunctionToConstructorInjectionRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Laravel\Rector\FuncCall\HelperFunctionToConstructorInjectionRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
Move help facade-like function calls to constructor injection
2019-03-09 13:24:30 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeController
2019-03-09 13:24:30 +00:00
{
+ /**
2019-06-02 07:45:37 +00:00
+ * @var \Illuminate\Contracts\View\Factory
2019-03-09 13:24:30 +00:00
+ */
2019-06-02 07:45:37 +00:00
+ private $viewFactory;
2019-03-09 13:24:30 +00:00
+
2019-06-02 07:45:37 +00:00
+ public function __construct(\Illuminate\Contracts\View\Factory $viewFactory)
2019-03-09 13:24:30 +00:00
+ {
2019-06-02 07:45:37 +00:00
+ $this->viewFactory = $viewFactory;
2019-03-09 13:24:30 +00:00
+ }
+
2019-06-02 07:45:37 +00:00
public function action()
2019-03-09 13:24:30 +00:00
{
2019-06-02 07:45:37 +00:00
- $template = view('template.blade');
- $viewFactory = view();
+ $template = $this->viewFactory->make('template.blade');
+ $viewFactory = $this->viewFactory;
}
}
```
< br >
### `MinutesToSecondsInCacheRector`
- class: `Rector\Laravel\Rector\StaticCall\MinutesToSecondsInCacheRector`
Change minutes argument to seconds in Illuminate\Contracts\Cache\Store and Illuminate\Support\Facades\Cache
```diff
class SomeClass
{
public function run()
{
- Illuminate\Support\Facades\Cache::put('key', 'value', 60);
+ Illuminate\Support\Facades\Cache::put('key', 'value', 60 * 60);
2019-03-09 13:24:30 +00:00
}
}
```
< br >
### `Redirect301ToPermanentRedirectRector`
- class: `Rector\Laravel\Rector\StaticCall\Redirect301ToPermanentRedirectRector`
Change "redirect" call with 301 to "permanentRedirect"
```diff
class SomeClass
{
public function run()
{
- Illuminate\Routing\Route::redirect('/foo', '/bar', 301);
+ Illuminate\Routing\Route::permanentRedirect('/foo', '/bar');
}
}
```
< br >
### `RequestStaticValidateToInjectRector`
- class: `Rector\Laravel\Rector\StaticCall\RequestStaticValidateToInjectRector`
Change static validate() method to $request->validate()
```diff
use Illuminate\Http\Request;
class SomeClass
{
- public function store()
+ public function store(\Illuminate\Http\Request $request)
{
- $validatedData = Request::validate(['some_attribute' => 'required']);
+ $validatedData = $request->validate(['some_attribute' => 'required']);
}
}
```
< br >
2019-05-26 16:38:09 +00:00
## Legacy
### `ChangeSingletonToServiceRector`
- class: `Rector\Legacy\Rector\ClassMethod\ChangeSingletonToServiceRector`
Change singleton class to normal class that can be registered as a service
```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 >
2019-02-21 14:36:16 +00:00
## MysqlToMysqli
### `MysqlAssignToMysqliRector`
- class: `Rector\MysqlToMysqli\Rector\Assign\MysqlAssignToMysqliRector`
Converts more complex mysql functions to mysqli
```diff
-$data = mysql_db_name($result, $row);
+mysqli_data_seek($result, $row);
+$fetch = mysql_fetch_row($result);
+$data = $fetch[0];
```
< br >
### `MysqlFuncCallToMysqliRector`
- class: `Rector\MysqlToMysqli\Rector\FuncCall\MysqlFuncCallToMysqliRector`
Converts more complex mysql functions to mysqli
```diff
-mysql_drop_db($database);
+mysqli_query('DROP DATABASE ' . $database);
```
< br >
### `MysqlPConnectToMysqliConnectRector`
- class: `Rector\MysqlToMysqli\Rector\FuncCall\MysqlPConnectToMysqliConnectRector`
Replace mysql_pconnect() with mysqli_connect() with host p: prefix
```diff
final class SomeClass
{
public function run($host, $username, $password)
{
- return mysql_pconnect($host, $username, $password);
+ return mysqli_connect('p:' . $host, $username, $password);
}
}
```
< br >
2019-04-02 13:35:35 +00:00
## Nette
2019-06-02 07:45:37 +00:00
### `EndsWithFunctionToNetteUtilsStringsRector`
2019-04-02 13:35:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Nette\Rector\Identical\EndsWithFunctionToNetteUtilsStringsRector`
2019-04-02 13:35:35 +00:00
Use Nette\Utils\Strings over bare string-functions
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function end($needle)
2019-04-02 13:35:35 +00:00
{
2019-05-29 13:40:20 +00:00
$content = 'Hi, my name is Tom';
2019-06-02 07:45:37 +00:00
- $yes = substr($content, -strlen($needle)) === $needle;
- $no = $needle !== substr($content, -strlen($needle));
+ $yes = \Nette\Utils\Strings::endsWith($content, $needle);
+ $no = !\Nette\Utils\Strings::endsWith($content, $needle);
2019-04-02 13:35:35 +00:00
}
}
```
< br >
2019-08-17 13:06:02 +00:00
### `JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector`
- class: `Rector\Nette\Rector\FuncCall\JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector`
Changes json_encode()/json_decode() to safer and more verbose Nette\Utils\Json::encode()/decode() calls
```diff
class SomeClass
{
public function decodeJson(string $jsonString)
{
- $stdClass = json_decode($jsonString);
+ $stdClass = \Nette\Utils\Json::decode($jsonString);
- $array = json_decode($jsonString, true);
- $array = json_decode($jsonString, false);
+ $array = \Nette\Utils\Json::decode($jsonString, \Nette\Utils\Json::FORCE_ARRAY);
+ $array = \Nette\Utils\Json::decode($jsonString);
}
public function encodeJson(array $data)
{
- $jsonString = json_encode($data);
+ $jsonString = \Nette\Utils\Json::encode($data);
- $prettyJsonString = json_encode($data, JSON_PRETTY_PRINT);
+ $prettyJsonString = \Nette\Utils\Json::encode($data, \Nette\Utils\Json::PRETTY);
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `PregFunctionToNetteUtilsStringsRector`
2019-04-02 13:35:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Nette\Rector\FuncCall\PregFunctionToNetteUtilsStringsRector`
2019-04-02 13:35:35 +00:00
2019-06-02 07:45:37 +00:00
Use Nette\Utils\Strings over bare preg_* functions
2019-04-02 13:35:35 +00:00
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function run()
2019-04-02 13:35:35 +00:00
{
2019-06-02 07:45:37 +00:00
$content = 'Hi my name is Tom';
- preg_match('#Hi#', $content);
+ \Nette\Utils\Strings::match($content, '#Hi#');
2019-04-02 13:35:35 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `StartsWithFunctionToNetteUtilsStringsRector`
2019-04-02 13:35:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Nette\Rector\Identical\StartsWithFunctionToNetteUtilsStringsRector`
2019-04-02 13:35:35 +00:00
Use Nette\Utils\Strings over bare string-functions
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function start($needle)
2019-04-02 13:35:35 +00:00
{
2019-06-02 07:45:37 +00:00
$content = 'Hi, my name is Tom';
- $yes = substr($content, 0, strlen($needle)) === $needle;
- $no = $needle !== substr($content, 0, strlen($needle));
+ $yes = \Nette\Utils\Strings::startwith($content, $needle);
+ $no = !\Nette\Utils\Strings::startwith($content, $needle);
2019-04-02 13:35:35 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `StrposToStringsContainsRector`
2019-04-02 13:35:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Nette\Rector\NotIdentical\StrposToStringsContainsRector`
2019-04-02 13:35:35 +00:00
2019-06-02 07:45:37 +00:00
Use Nette\Utils\Strings over bare string-functions
2019-04-02 13:35:35 +00:00
```diff
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function run()
2019-04-02 13:35:35 +00:00
{
2019-06-02 07:45:37 +00:00
$name = 'Hi, my name is Tom';
- return strpos($name, 'Hi') !== false;
+ return \Nette\Utils\Strings::contains($name, 'Hi');
2019-04-02 13:35:35 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SubstrStrlenFunctionToNetteUtilsStringsRector`
2019-04-02 13:35:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Nette\Rector\FuncCall\SubstrStrlenFunctionToNetteUtilsStringsRector`
2019-04-02 13:35:35 +00:00
Use Nette\Utils\Strings over bare string-functions
```diff
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function run()
2019-04-02 13:35:35 +00:00
{
2019-06-02 07:45:37 +00:00
- return substr($value, 0, 3);
+ return \Nette\Utils\Strings::substring($value, 0, 3);
2019-04-02 13:35:35 +00:00
}
}
```
< br >
2019-03-16 20:31:46 +00:00
## NetteTesterToPHPUnit
2019-06-02 07:45:37 +00:00
### `NetteAssertToPHPUnitAssertRector`
- class: `Rector\NetteTesterToPHPUnit\Rector\StaticCall\NetteAssertToPHPUnitAssertRector`
Migrate Nette/Assert calls to PHPUnit
```diff
use Tester\Assert;
function someStaticFunctions()
{
- Assert::true(10 == 5);
+ \PHPUnit\Framework\Assert::assertTrue(10 == 5);
}
```
< br >
2019-03-16 20:31:46 +00:00
### `NetteTesterClassToPHPUnitClassRector`
- class: `Rector\NetteTesterToPHPUnit\Rector\Class_\NetteTesterClassToPHPUnitClassRector`
Migrate Nette Tester test case to PHPUnit
```diff
namespace KdybyTests\Doctrine;
use Tester\TestCase;
use Tester\Assert;
-require_once __DIR__ . '/../bootstrap.php';
-
-class ExtensionTest extends TestCase
+class ExtensionTest extends \PHPUnit\Framework\TestCase
{
public function testFunctionality()
{
- Assert::true($default instanceof Kdyby\Doctrine\EntityManager);
- Assert::true(5);
- Assert::same($container->getService('kdyby.doctrine.default.entityManager'), $default);
2019-03-31 12:25:39 +00:00
+ $this->assertInstanceOf(\Kdyby\Doctrine\EntityManager::cllass, $default);
+ $this->assertTrue(5);
+ $this->same($container->getService('kdyby.doctrine.default.entityManager'), $default);
2019-03-16 20:31:46 +00:00
}
-}
-
-(new \ExtensionTest())->run();
+}
```
< br >
2019-06-02 07:45:37 +00:00
### `RenameTesterTestToPHPUnitToTestFileRector`
2019-02-02 16:22:15 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\NetteTesterToPHPUnit\Rector\RenameTesterTestToPHPUnitToTestFileRector`
2019-02-02 16:22:15 +00:00
2019-06-02 07:45:37 +00:00
Rename "*.phpt" file to "*Test.php" file
2019-02-02 16:22:15 +00:00
2019-06-02 07:45:37 +00:00
< br >
2019-02-02 16:22:15 +00:00
2019-06-02 07:45:37 +00:00
## NetteToSymfony
2019-02-02 16:22:15 +00:00
2019-06-02 07:45:37 +00:00
### `FromHttpRequestGetHeaderToHeadersGetRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\NetteToSymfony\Rector\MethodCall\FromHttpRequestGetHeaderToHeadersGetRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Changes getHeader() to $request->headers->get()
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
```diff
use Nette\Request;
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
final class SomeController
{
public static function someAction(Request $request)
{
- $header = $this->httpRequest->getHeader('x');
+ $header = $request->headers->get('x');
}
}
```
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
< br >
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
### `FromRequestGetParameterToAttributesGetRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\NetteToSymfony\Rector\MethodCall\FromRequestGetParameterToAttributesGetRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Changes "getParameter()" to "attributes->get()" from Nette to Symfony
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
use Nette\Request;
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
final class SomeController
2019-02-02 16:22:15 +00:00
{
2019-06-02 07:45:37 +00:00
public static function someAction(Request $request)
2019-02-02 16:22:15 +00:00
{
2019-06-02 07:45:37 +00:00
- $value = $request->getParameter('abz');
+ $value = $request->attribute->get('abz');
2019-02-02 16:22:15 +00:00
}
}
```
< br >
2019-05-29 13:40:20 +00:00
### `NetteControlToSymfonyControllerRector`
2019-02-18 15:51:24 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\NetteToSymfony\Rector\Class_\NetteControlToSymfonyControllerRector`
2019-02-18 15:51:24 +00:00
2019-05-29 13:40:20 +00:00
Migrate Nette Component to Symfony Controller
2019-02-18 15:51:24 +00:00
```diff
2019-05-29 13:40:20 +00:00
use Nette\Application\UI\Control;
2019-02-18 15:51:24 +00:00
2019-05-29 13:40:20 +00:00
-class SomeControl extends Control
+class SomeController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
2019-02-18 15:51:24 +00:00
{
2019-05-29 13:40:20 +00:00
- public function render()
- {
- $this->template->param = 'some value';
- $this->template->render(__DIR__ . '/poll.latte');
- }
+ public function some()
+ {
+ $this->render(__DIR__ . '/poll.latte', ['param' => 'some value']);
+ }
2019-02-18 15:51:24 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `NetteFormToSymfonyFormRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\NetteToSymfony\Rector\Class_\NetteFormToSymfonyFormRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
Migrate Nette\Forms in Presenter to Symfony
2019-02-18 15:51:24 +00:00
```diff
2019-06-02 07:45:37 +00:00
use Nette\Application\UI;
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
class SomePresenter extends UI\Presenter
2019-02-18 15:51:24 +00:00
{
2019-06-02 07:45:37 +00:00
public function someAction()
2019-02-18 15:51:24 +00:00
{
2019-06-02 07:45:37 +00:00
- $form = new UI\Form;
- $form->addText('name', 'Name:');
- $form->addPassword('password', 'Password:');
- $form->addSubmit('login', 'Sign up');
+ $form = $this->createFormBuilder();
+ $form->add('name', \Symfony\Component\Form\Extension\Core\Type\TextType::class, [
+ 'label' => 'Name:'
+ ]);
+ $form->add('password', \Symfony\Component\Form\Extension\Core\Type\PasswordType::class, [
+ 'label' => 'Password:'
+ ]);
+ $form->add('login', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class, [
+ 'label' => 'Sign up'
+ ]);
2019-02-18 15:51:24 +00:00
}
}
```
< br >
2019-05-29 13:40:20 +00:00
### `RenameEventNamesInEventSubscriberRector`
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\NetteToSymfony\Rector\ClassMethod\RenameEventNamesInEventSubscriberRector`
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
Changes event names from Nette ones to Symfony ones
2019-03-31 12:25:39 +00:00
```diff
2019-05-29 13:40:20 +00:00
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
final class SomeClass implements EventSubscriberInterface
2019-03-31 12:25:39 +00:00
{
2019-05-29 13:40:20 +00:00
public static function getSubscribedEvents()
{
- return ['nette.application' => 'someMethod'];
+ return [\SymfonyEvents::KERNEL => 'someMethod'];
}
2019-03-31 12:25:39 +00:00
}
```
< br >
2019-05-29 13:40:20 +00:00
### `RouterListToControllerAnnotationsRector`
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\NetteToSymfony\Rector\ClassMethod\RouterListToControllerAnnotationsRector`
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
Change new Route() from RouteFactory to @Route annotation above controller method
2019-03-31 12:25:39 +00:00
```diff
2019-05-29 13:40:20 +00:00
final class RouterFactory
{
public function create(): RouteList
{
$routeList = new RouteList();
+
+ // case of single action controller, usually get() or __invoke() method
$routeList[] = new Route('some-path', SomePresenter::class);
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
return $routeList;
}
}
final class SomePresenter
2019-03-31 12:25:39 +00:00
{
2019-05-29 13:40:20 +00:00
+ /**
+ * @Symfony \Component\Routing\Annotation\Route(path="some-path")
+ */
public function run()
2019-03-31 12:25:39 +00:00
{
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `WrapTransParameterNameRector`
- class: `Rector\NetteToSymfony\Rector\MethodCall\WrapTransParameterNameRector`
Adds %% to placeholder name of trans() method if missing
```diff
use Symfony\Component\Translation\Translator;
final class SomeController
{
public function run()
{
$translator = new Translator('');
$translated = $translator->trans(
'Hello %name%',
- ['name' => $name]
+ ['%name%' => $name]
);
}
}
```
< br >
2019-02-04 00:32:53 +00:00
## PHPStan
2018-12-06 00:13:34 +00:00
### `PHPStormVarAnnotationRector`
- class: `Rector\PHPStan\Rector\Assign\PHPStormVarAnnotationRector`
Change various @var annotation formats to one PHPStorm understands
```diff
-$config = 5;
-/** @var \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterConfig $config */
+/** @var \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterConfig $config */
+$config = 5;
```
< br >
2018-11-11 12:22:35 +00:00
### `RecastingRemovalRector`
- class: `Rector\PHPStan\Rector\Cast\RecastingRemovalRector`
Removes recasting of the same type
```diff
$string = '';
-$string = (string) $string;
+$string = $string;
$array = [];
-$array = (array) $array;
+$array = $array;
```
< br >
2019-08-05 21:10:47 +00:00
### `RemoveNonExistingVarAnnotationRector`
- class: `Rector\PHPStan\Rector\Node\RemoveNonExistingVarAnnotationRector`
Removes non-existing @var annotations above the code
```diff
class SomeClass
{
public function get()
{
- /** @var Training[] $trainings */
return $this->getData();
}
}
```
< br >
2018-10-21 22:26:45 +00:00
## PHPUnit
2019-06-02 07:45:37 +00:00
### `AssertCompareToSpecificMethodRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertCompareToSpecificMethodRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
Turns vague php-only method in PHPUnit TestCase to more specific
2018-10-21 22:26:45 +00:00
2018-10-12 23:15:00 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");
2018-10-12 23:15:00 +00:00
```
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
```diff
-$this->assertSame($value, {function}($anything), "message");
+$this->assert{function}($value, $anything, "message\");
```
2018-12-14 19:35:35 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertEquals($value, {function}($anything), "message");
+$this->assert{function}($value, $anything, "message\");
```
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
```diff
-$this->assertNotSame($value, {function}($anything), "message");
+$this->assertNot{function}($value, $anything, "message")
```
2018-12-14 19:35:35 +00:00
2019-06-02 07:45:37 +00:00
```diff
-$this->assertNotEquals($value, {function}($anything), "message");
+$this->assertNot{function}($value, $anything, "message")
2018-12-14 19:35:35 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `AssertComparisonToSpecificMethodRector`
2018-12-14 19:35:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertComparisonToSpecificMethodRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Turns comparison operations to their method name alternatives in PHPUnit TestCase
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertTrue($foo === $bar, "message");
+$this->assertSame($bar, $foo, "message");
2019-05-29 13:40:20 +00:00
```
```diff
2019-06-02 07:45:37 +00:00
-$this->assertFalse($foo >= $bar, "message");
+$this->assertLessThanOrEqual($bar, $foo, "message");
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `AssertEqualsParameterToSpecificMethodsTypeRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\MethodCall\AssertEqualsParameterToSpecificMethodsTypeRector`
2018-12-14 19:35:35 +00:00
2019-06-02 07:45:37 +00:00
Change assertEquals()/assertNotEquals() method parameters to new specific alternatives
2018-12-14 19:35:35 +00:00
```diff
final class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
2019-06-02 07:45:37 +00:00
$value = 'value';
- $this->assertEquals('string', $value, 'message', 5.0);
+ $this->assertEqualsWithDelta('string', $value, 5.0, 'message');
- $this->assertEquals('string', $value, 'message', 0.0, 20);
+ $this->assertEquals('string', $value, 'message', 0.0);
- $this->assertEquals('string', $value, 'message', 0.0, 10, true);
+ $this->assertEqualsCanonicalizing('string', $value, 'message');
- $this->assertEquals('string', $value, 'message', 0.0, 10, false, true);
+ $this->assertEqualsIgnoringCase('string', $value, 'message');
2019-03-16 20:31:46 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `AssertFalseStrposToContainsRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertFalseStrposToContainsRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Turns `strpos` /`stripos` comparisons to their method name alternatives in PHPUnit TestCase
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertFalse(strpos($anything, "foo"), "message");
+$this->assertNotContains("foo", $anything, "message");
```
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
```diff
-$this->assertNotFalse(stripos($anything, "foo"), "message");
+$this->assertContains("foo", $anything, "message");
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `AssertInstanceOfComparisonRector`
2019-03-16 20:31:46 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertInstanceOfComparisonRector`
2019-03-16 20:31:46 +00:00
2019-06-02 07:45:37 +00:00
Turns instanceof comparisons to their method name alternatives in PHPUnit TestCase
2019-03-16 20:31:46 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertTrue($foo instanceof Foo, "message");
+$this->assertInstanceOf("Foo", $foo, "message");
2019-02-04 00:32:53 +00:00
```
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertFalse($foo instanceof Foo, "message");
+$this->assertNotInstanceOf("Foo", $foo, "message");
2018-07-31 12:50:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-07-31 19:43:39 +00:00
2019-05-29 13:40:20 +00:00
### `AssertIssetToSpecificMethodRector`
2018-07-31 12:50:39 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertIssetToSpecificMethodRector`
2018-07-31 12:50:39 +00:00
2019-05-29 13:40:20 +00:00
Turns isset comparisons to their method name alternatives in PHPUnit TestCase
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-05-29 13:40:20 +00:00
-$this->assertTrue(isset($anything->foo));
+$this->assertFalse(isset($anything["foo"]), "message");
2018-10-12 23:15:00 +00:00
```
```diff
2019-05-29 13:40:20 +00:00
-$this->assertObjectHasAttribute("foo", $anything);
+$this->assertArrayNotHasKey("foo", $anything, "message");
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
### `AssertNotOperatorRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertNotOperatorRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Turns not-operator comparisons to their method name alternatives in PHPUnit TestCase
2018-10-12 23:15:00 +00:00
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertTrue(!$foo, "message");
+$this->assertFalse($foo, "message");
2018-10-21 22:26:45 +00:00
```
2018-10-12 23:15:00 +00:00
2018-11-11 12:22:35 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertFalse(!$foo, "message");
+$this->assertTrue($foo, "message");
2018-11-11 12:22:35 +00:00
```
2018-10-12 23:15:00 +00:00
2018-11-11 12:22:35 +00:00
< br >
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
### `AssertPropertyExistsRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertPropertyExistsRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Turns `property_exists` comparisons to their method name alternatives in PHPUnit TestCase
2018-10-12 23:15:00 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertTrue(property_exists(new Class, "property"), "message");
+$this->assertClassHasAttribute("property", "Class", "message");
2018-10-12 23:15:00 +00:00
```
```diff
2019-06-02 07:45:37 +00:00
-$this->assertFalse(property_exists(new Class, "property"), "message");
+$this->assertClassNotHasAttribute("property", "Class", "message");
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-05-29 13:40:20 +00:00
### `AssertRegExpRector`
2018-10-12 23:15:00 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertRegExpRector`
2018-10-12 23:15:00 +00:00
2019-05-29 13:40:20 +00:00
Turns `preg_match` comparisons to their method name alternatives in PHPUnit TestCase
2018-10-12 23:15:00 +00:00
```diff
2019-05-29 13:40:20 +00:00
-$this->assertSame(1, preg_match("/^Message for ".*"\.$/", $string), $message);
+$this->assertRegExp("/^Message for ".*"\.$/", $string, $message);
2018-10-12 23:15:00 +00:00
```
```diff
2019-05-29 13:40:20 +00:00
-$this->assertEquals(false, preg_match("/^Message for ".*"\.$/", $string), $message);
+$this->assertNotRegExp("/^Message for ".*"\.$/", $string, $message);
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `AssertSameBoolNullToSpecificMethodRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertSameBoolNullToSpecificMethodRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Turns same bool and null comparisons to their method name alternatives in PHPUnit TestCase
2018-10-12 23:15:00 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertSame(null, $anything);
+$this->assertNull($anything);
2018-10-12 23:15:00 +00:00
```
```diff
2019-06-02 07:45:37 +00:00
-$this->assertNotSame(false, $anything);
+$this->assertNotFalse($anything);
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
### `AssertTrueFalseInternalTypeToSpecificMethodRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertTrueFalseInternalTypeToSpecificMethodRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Turns true/false with internal type comparisons to their method name alternatives in PHPUnit TestCase
2018-10-12 23:15:00 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->assertTrue(is_{internal_type}($anything), "message");
+$this->assertInternalType({internal_type}, $anything, "message");
2018-10-12 23:15:00 +00:00
```
```diff
2019-06-02 07:45:37 +00:00
-$this->assertFalse(is_{internal_type}($anything), "message");
+$this->assertNotInternalType({internal_type}, $anything, "message");
2018-11-11 12:22:35 +00:00
```
< br >
### `AssertTrueFalseToSpecificMethodRector`
- class: `Rector\PHPUnit\Rector\SpecificMethod\AssertTrueFalseToSpecificMethodRector`
Turns true/false comparisons to their method name alternatives in PHPUnit TestCase when possible
```diff
-$this->assertTrue(is_readable($readmeFile), "message");
+$this->assertIsReadable($readmeFile, "message");
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `DelegateExceptionArgumentsRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\DelegateExceptionArgumentsRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$this->setExpectedException(Exception::class, "Message", "CODE");
+$this->setExpectedException(Exception::class);
+$this->expectExceptionMessage("Message");
+$this->expectExceptionCode("CODE");
2018-11-11 12:22:35 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ExceptionAnnotationRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\ExceptionAnnotationRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-/**
- * @expectedException Exception
- * @expectedExceptionMessage Message
- */
public function test()
{
+ $this->expectException('Exception');
+ $this->expectExceptionMessage('Message');
// tested code
}
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-02-04 00:32:53 +00:00
### `GetMockRector`
2018-10-12 23:15:00 +00:00
2019-02-04 00:32:53 +00:00
- class: `Rector\PHPUnit\Rector\GetMockRector`
2018-10-12 23:15:00 +00:00
2019-02-04 00:32:53 +00:00
Turns getMock*() methods to createMock()
2018-10-12 23:15:00 +00:00
```diff
2019-02-04 00:32:53 +00:00
-$this->getMock("Class");
+$this->createMock("Class");
2018-10-12 23:15:00 +00:00
```
```diff
2019-02-04 00:32:53 +00:00
-$this->getMockWithoutInvokingTheOriginalConstructor("Class");
+$this->createMock("Class");
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
### `RemoveExpectAnyFromMockRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\MethodCall\RemoveExpectAnyFromMockRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Remove `expect($this->any())` from mocks as it has no added value
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
use PHPUnit\Framework\TestCase;
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
class SomeClass extends TestCase
2019-03-31 12:25:39 +00:00
{
2019-06-02 07:45:37 +00:00
public function test()
2019-03-31 12:25:39 +00:00
{
2019-06-02 07:45:37 +00:00
$translator = $this->getMock('SomeClass');
- $translator->expects($this->any())
- ->method('trans')
+ $translator->method('trans')
->willReturn('translated max {{ max }}!');
2019-03-31 12:25:39 +00:00
}
2019-05-29 13:40:20 +00:00
}
```
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ReplaceAssertArraySubsetRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\MethodCall\ReplaceAssertArraySubsetRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Replace deprecated "assertArraySubset()" method with alternative methods
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeTest extends \PHPUnit\Framework\TestCase
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function test()
2019-03-31 12:25:39 +00:00
{
2019-06-02 07:45:37 +00:00
$checkedArray = [];
- $this->assertArraySubset([
- 'cache_directory' => 'new_value',
- ], $checkedArray);
+ $this->assertArrayHasKey('cache_directory', $checkedArray);
+ $this->assertSame('new_value', $checkedArray['cache_directory']);
2019-03-31 12:25:39 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `SimplifyForeachInstanceOfRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\Foreach_\SimplifyForeachInstanceOfRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Simplify unnecessary foreach check of instances
2018-10-12 23:15:00 +00:00
```diff
2019-06-02 07:45:37 +00:00
-foreach ($foos as $foo) {
- $this->assertInstanceOf(\SplFileInfo::class, $foo);
-}
+$this->assertContainsOnlyInstancesOf(\SplFileInfo::class, $foos);
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `SpecificAssertContainsRector`
2019-01-22 20:34:38 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsRector`
2019-01-22 20:34:38 +00:00
2019-06-02 07:45:37 +00:00
Change assertContains()/assertNotContains() method to new string and iterable alternatives
2019-01-22 20:34:38 +00:00
```diff
2019-06-02 07:45:37 +00:00
< ?php
final class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
- $this->assertContains('foo', 'foo bar');
- $this->assertNotContains('foo', 'foo bar');
+ $this->assertStringContainsString('foo', 'foo bar');
+ $this->assertStringNotContainsString('foo', 'foo bar');
}
}
2019-01-22 20:34:38 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `SpecificAssertInternalTypeRector`
2018-12-14 19:35:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector`
2018-12-14 19:35:35 +00:00
2019-06-02 07:45:37 +00:00
Change assertInternalType()/assertNotInternalType() method to new specific alternatives
2018-12-14 19:35:35 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$value = 'value';
- $this->assertInternalType('string', $value);
- $this->assertNotInternalType('array', $value);
+ $this->assertIsString($value);
+ $this->assertIsNotArray($value);
}
}
2018-12-14 19:35:35 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `TestListenerToHooksRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\Class_\TestListenerToHooksRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Refactor "*TestListener.php" to particular "*Hook.php" files
2018-10-12 23:15:00 +00:00
```diff
2019-06-02 07:45:37 +00:00
namespace App\Tests;
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
-use PHPUnit\Framework\TestListener;
-
-final class BeforeListHook implements TestListener
+final class BeforeListHook implements \PHPUnit\Runner\BeforeTestHook, \PHPUnit\Runner\AfterTestHook
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- public function addError(Test $test, \Throwable $t, float $time): void
+ public function executeBeforeTest(Test $test): void
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- }
-
- public function addWarning(Test $test, Warning $e, float $time): void
- {
- }
-
- public function addFailure(Test $test, AssertionFailedError $e, float $time): void
- {
- }
-
- public function addIncompleteTest(Test $test, \Throwable $t, float $time): void
- {
- }
-
- public function addRiskyTest(Test $test, \Throwable $t, float $time): void
- {
- }
-
- public function addSkippedTest(Test $test, \Throwable $t, float $time): void
- {
- }
-
- public function startTestSuite(TestSuite $suite): void
- {
- }
-
- public function endTestSuite(TestSuite $suite): void
- {
- }
-
- public function startTest(Test $test): void
- {
echo 'start test!';
}
- public function endTest(Test $test, float $time): void
+ public function executeAfterTest(Test $test, float $time): void
{
echo $time;
2019-05-29 13:40:20 +00:00
}
2019-02-04 00:32:53 +00:00
}
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `TryCatchToExpectExceptionRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\TryCatchToExpectExceptionRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Turns try/catch to expectException() call
2018-10-12 23:15:00 +00:00
```diff
2019-06-02 07:45:37 +00:00
-try {
- $someService->run();
-} catch (Throwable $exception) {
- $this->assertInstanceOf(RuntimeException::class, $e);
- $this->assertContains('There was an error executing the following script', $e->getMessage());
-}
+$this->expectException(RuntimeException::class);
+$this->expectExceptionMessage('There was an error executing the following script');
+$someService->run();
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `UseSpecificWillMethodRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PHPUnit\Rector\MethodCall\UseSpecificWillMethodRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
Changes ->will($this->xxx()) to one specific method
2018-08-01 13:34:55 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass extends PHPUnit\Framework\TestCase
2019-02-04 00:32:53 +00:00
{
2019-06-02 07:45:37 +00:00
public function test()
2019-02-04 00:32:53 +00:00
{
2019-06-02 07:45:37 +00:00
$translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
$translator->expects($this->any())
->method('trans')
- ->with($this->equalTo('old max {{ max }}!'))
- ->will($this->returnValue('translated max {{ max }}!'));
+ ->with('old max {{ max }}!')
+ ->willReturnValue('translated max {{ max }}!');
2019-02-04 00:32:53 +00:00
}
}
2018-07-31 12:50:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-08-05 21:10:47 +00:00
## PHPUnitSymfony
### `AddMessageToEqualsResponseCodeRector`
- class: `Rector\PHPUnitSymfony\Rector\StaticCall\AddMessageToEqualsResponseCodeRector`
Add response content to response code assert, so it is easier to debug
```diff
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
final class SomeClassTest extends TestCase
{
public function test(Response $response)
{
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$response->getStatusCode()
+ $response->getContent()
);
}
}
```
< br >
2019-08-18 10:56:56 +00:00
## PSR4
### `NormalizeNamespaceByPSR4ComposerAutoloadRector`
- class: `Rector\PSR4\Rector\Namespace_\NormalizeNamespaceByPSR4ComposerAutoloadRector`
Changes namespace and class names to match PSR-4 in composer.json autoload section
< br >
2019-06-02 07:45:37 +00:00
## Php
2018-11-11 12:22:35 +00:00
2019-05-29 13:40:20 +00:00
### `AddDefaultValueForUndefinedVariableRector`
2019-01-22 20:34:38 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Php\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector`
2019-01-22 20:34:38 +00:00
2019-05-29 13:40:20 +00:00
Adds default value for undefined variable
2019-01-22 20:34:38 +00:00
```diff
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function run()
2019-01-22 20:34:38 +00:00
{
2019-05-29 13:40:20 +00:00
+ $a = null;
if (rand(0, 1)) {
$a = 5;
}
echo $a;
2019-01-22 20:34:38 +00:00
}
}
```
< br >
2019-08-24 11:08:59 +00:00
### `AddLiteralSeparatorToNumberRector`
- class: `Rector\Php\Rector\LNumber\AddLiteralSeparatorToNumberRector`
Add "_" as thousands separator in numbers
```diff
class SomeClass
{
public function run()
{
- $int = 1000;
- $float = 1000500.001;
+ $int = 1_000;
+ $float = 1_000_500.001;
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `ArrayKeyExistsOnPropertyRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\ArrayKeyExistsOnPropertyRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
Change array_key_exists() on property to property_exists()
2019-02-04 00:32:53 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass {
public $value;
2018-10-12 23:15:00 +00:00
}
2019-06-02 07:45:37 +00:00
$someClass = new SomeClass;
2019-05-24 20:30:15 +00:00
2019-06-02 07:45:37 +00:00
-array_key_exists('value', $someClass);
+property_exists($someClass, 'value');
2019-05-19 08:27:38 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ArrayKeyFirstLastRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\ArrayKeyFirstLastRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
Make use of array_key_first() and array_key_last()
2018-12-31 11:50:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
-reset($items);
-$firstKey = key($items);
+$firstKey = array_key_first($items);
2019-02-04 00:32:53 +00:00
```
```diff
2019-06-02 07:45:37 +00:00
-end($items);
-$lastKey = key($items);
+$lastKey = array_key_last($items);
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ArraySpreadInsteadOfArrayMergeRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
Change array_merge() to spread operator, except values with possible string key values
2019-02-21 14:36:16 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-02-21 14:36:16 +00:00
{
2019-06-02 07:45:37 +00:00
public function run($iter1, $iter2)
2019-02-21 14:36:16 +00:00
{
2019-06-02 07:45:37 +00:00
- $values = array_merge(iterator_to_array($iter1), iterator_to_array($iter2));
+ $values = [...$iter1, ...$iter2];
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
// 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];
2019-05-01 23:56:58 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `AssignArrayToStringRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Assign\AssignArrayToStringRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
String cannot be turned into array by assignment anymore
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$string = '';
+$string = [];
$string[] = 1;
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `BarewordStringRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\ConstFetch\BarewordStringRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Changes unquoted non-existing constants to strings
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
-var_dump(VAR);
+var_dump("VAR");
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `BinaryOpBetweenNumberAndStringRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
Change binary operation between some number + string to PHP 7.1 compatible version
2019-02-21 14:36:16 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-02-21 14:36:16 +00:00
{
2019-06-02 07:45:37 +00:00
public function run()
{
- $value = 5 + '';
- $value = 5.0 + 'hi';
+ $value = 5 + 0;
+ $value = 5.0 + 0
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
$name = 'Tom';
- $value = 5 * $name;
+ $value = 5 * 0;
}
2019-02-04 00:32:53 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `BreakNotInLoopOrSwitchToReturnRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Break_\BreakNotInLoopOrSwitchToReturnRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Convert break outside for/foreach/switch context to return
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-02-04 00:32:53 +00:00
{
public function run()
{
2019-06-02 07:45:37 +00:00
$zhrs = abs($gmt)/3600;
$hrs = floor($zhrs);
if ($isphp5)
return sprintf('%s%02d%02d',($gmt< =0)?'+':'-',floor($zhrs),($zhrs-$hrs)*60);
else
return sprintf('%s%02d%02d',($gmt< 0 ) ? ' + ' : ' - ' , floor ( $ zhrs ) , ( $ zhrs- $ hrs ) * 60 ) ;
- break;
+ return;
2019-02-04 00:32:53 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `CallUserMethodRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\CallUserMethodRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Changes call_user_method()/call_user_method_array() to call_user_func()/call_user_func_array()
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
-call_user_method($method, $obj, "arg1", "arg2");
+call_user_func(array(& $obj, "method"), "arg1", "arg2");
2019-02-04 00:32:53 +00:00
```
< br >
2019-05-29 13:40:20 +00:00
### `ClassConstantToSelfClassRector`
2019-03-09 13:24:30 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Php\Rector\MagicConstClass\ClassConstantToSelfClassRector`
2019-03-09 13:24:30 +00:00
2019-05-29 13:40:20 +00:00
Change __CLASS__ to self::class
2019-03-09 13:24:30 +00:00
```diff
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function callOnMe()
{
- var_dump(__CLASS__);
+ var_dump(self::class);
}
2019-03-09 13:24:30 +00:00
}
```
< br >
2019-05-29 13:40:20 +00:00
### `ClosureToArrowFunctionRector`
2018-12-22 11:22:28 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Php\Rector\Closure\ClosureToArrowFunctionRector`
2018-12-22 11:22:28 +00:00
2019-05-29 13:40:20 +00:00
Change closure to arrow function
2018-12-22 11:22:28 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeClass
2018-12-22 11:22:28 +00:00
{
2019-05-29 13:40:20 +00:00
public function run($meetups)
2018-12-22 11:22:28 +00:00
{
2019-05-29 13:40:20 +00:00
- return array_filter($meetups, function (Meetup $meetup) {
- return is_object($meetup);
- });
+ return array_filter($meetups, fn(Meetup $meetup) => is_object($meetup));
2018-12-22 11:22:28 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `CompleteVarDocTypePropertyRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Property\CompleteVarDocTypePropertyRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
Complete property `@var` annotations for missing one, yet known.
2018-12-31 11:50:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeClass
{
+ /**
+ * @var EventDispatcher
+ */
private $eventDispatcher;
public function __construct(EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}
2018-12-31 11:50:32 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ContinueToBreakInSwitchRector`
2019-05-24 20:30:15 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Switch_\ContinueToBreakInSwitchRector`
2019-05-24 20:30:15 +00:00
2019-06-02 07:45:37 +00:00
Use break instead of continue in switch statements
2019-05-24 20:30:15 +00:00
```diff
2019-06-02 07:45:37 +00:00
function some_run($value)
{
switch ($value) {
case 1:
echo 'Hi';
- continue;
+ break;
case 2:
echo 'Hello';
break;
}
}
```
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
< br >
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
### `CountOnNullRector`
- class: `Rector\Php\Rector\FuncCall\CountOnNullRector`
Changes count() on null to safe ternary check
2018-08-01 13:34:55 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
$values = null;
-$count = count($values);
+$count = is_array($values) || $values instanceof Countable ? count($values) : 0;
2018-07-31 12:50:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
### `CreateFunctionToAnonymousFunctionRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
Use anonymous functions instead of deprecated create_function()
2018-11-11 12:22:35 +00:00
```diff
2019-06-02 07:45:37 +00:00
class ClassWithCreateFunction
{
public function run()
{
- $callable = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
+ $callable = function($matches) use ($delimiter) {
+ return $delimiter . strtolower($matches[1]);
+ };
}
}
2018-10-21 22:26:45 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
### `EmptyListRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\List_\EmptyListRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
list() cannot be empty
2018-10-21 22:26:45 +00:00
2018-10-23 18:58:57 +00:00
```diff
2019-06-02 07:45:37 +00:00
-list() = $values;
+list($generated) = $values;
2018-10-23 18:58:57 +00:00
```
2018-10-21 22:26:45 +00:00
2018-11-11 12:22:35 +00:00
< br >
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
### `EregToPregMatchRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\EregToPregMatchRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
Changes ereg*() to preg*() calls
2018-12-22 11:22:28 +00:00
```diff
2019-06-02 07:45:37 +00:00
-ereg("hi")
+preg_match("#hi#");
2018-12-22 11:22:28 +00:00
```
2019-06-02 07:45:37 +00:00
< br >
### `ExceptionHandlerTypehintRector`
- class: `Rector\Php\Rector\FunctionLike\ExceptionHandlerTypehintRector`
Changes property `@var` annotations from annotation to type.
2018-12-22 11:22:28 +00:00
```diff
2019-06-02 07:45:37 +00:00
-function handler(Exception $exception) { ... }
+function handler(Throwable $exception) { ... }
set_exception_handler('handler');
2018-12-22 11:22:28 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ExportToReflectionFunctionRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\StaticCall\ExportToReflectionFunctionRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
Change export() to ReflectionFunction alternatives
2019-02-21 14:36:16 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$reflectionFunction = ReflectionFunction::export('foo');
-$reflectionFunctionAsString = ReflectionFunction::export('foo', true);
+$reflectionFunction = new ReflectionFunction('foo');
+$reflectionFunctionAsString = (string) new ReflectionFunction('foo');
```
< br >
### `FilterVarToAddSlashesRector`
- class: `Rector\Php\Rector\FuncCall\FilterVarToAddSlashesRector`
Change filter_var() with slash escaping to addslashes()
```diff
$var= "Satya's here!";
-filter_var($var, FILTER_SANITIZE_MAGIC_QUOTES);
+addslashes($var);
2019-02-21 14:36:16 +00:00
```
< br >
2018-12-31 11:50:32 +00:00
### `GetCalledClassToStaticClassRector`
- class: `Rector\Php\Rector\FuncCall\GetCalledClassToStaticClassRector`
Change __CLASS__ to self::class
```diff
class SomeClass
-{
- public function callOnMe()
- {
- var_dump( get_called_class());
- }
-}
+ {
+ public function callOnMe()
+ {
+ var_dump( static::class);
+ }
+ }
```
< br >
2019-06-02 07:45:37 +00:00
### `GetClassOnNullRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\GetClassOnNullRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Null is no more allowed in get_class()
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function getItem()
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
$value = null;
- return get_class($value);
+ return $value !== null ? get_class($value) : self::class;
2019-05-29 13:40:20 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `IfToSpaceshipRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\If_\IfToSpaceshipRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Changes if/else to spaceship < => where useful
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function run()
{
usort($languages, function ($a, $b) {
- if ($a[0] === $b[0]) {
- return 0;
- }
-
- return ($a[0] < $b[0]) ? 1 : -1;
+ return $b[0] < => $a[0];
});
}
2019-05-29 13:40:20 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `IsCountableRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\BinaryOp\IsCountableRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
Changes is_array + Countable check to is_countable
2019-05-19 08:27:38 +00:00
```diff
2019-06-02 07:45:37 +00:00
-is_array($foo) || $foo instanceof Countable;
+is_countable($foo);
2019-05-19 08:27:38 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `IsIterableRector`
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\BinaryOp\IsIterableRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Changes is_array + Traversable check to is_iterable
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
-is_array($foo) || $foo instanceof Traversable;
+is_iterable($foo);
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `IsObjectOnIncompleteClassRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\IsObjectOnIncompleteClassRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
Incomplete class returns inverted bool on is_object()
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
$incompleteObject = new __PHP_Incomplete_Class;
-$isObject = is_object($incompleteObject);
+$isObject = ! is_object($incompleteObject);
2018-07-31 12:50:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-05-29 13:40:20 +00:00
### `JsonThrowOnErrorRector`
2018-12-22 11:22:28 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Php\Rector\FuncCall\JsonThrowOnErrorRector`
2018-12-22 11:22:28 +00:00
2019-05-29 13:40:20 +00:00
Adds JSON_THROW_ON_ERROR to json_encode() and json_decode() to throw JsonException on error
2018-12-22 11:22:28 +00:00
```diff
2019-05-29 13:40:20 +00:00
-json_encode($content);
-json_decode($json);
+json_encode($content, JSON_THROW_ON_ERROR);
+json_decode($json, null, null, JSON_THROW_ON_ERROR);
2018-12-22 11:22:28 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ListEachRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Each\ListEachRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
each() function is deprecated, use key() and current() instead
2018-07-31 12:50:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
-list($key, $callback) = each($callbacks);
+$key = key($opt->option);
+$val = current($opt->option);
2018-08-01 20:09:34 +00:00
```
2018-08-01 13:34:55 +00:00
2018-11-11 12:22:35 +00:00
< br >
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
### `ListSplitStringRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\List_\ListSplitStringRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
list() cannot split string directly anymore, use str_split()
2018-10-12 23:15:00 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
-list($foo) = "string";
+list($foo) = str_split("string");
2018-07-31 12:50:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
### `ListSwapArrayOrderRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\List_\ListSwapArrayOrderRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
list() assigns variables in reverse order - relevant in array assign
2019-03-09 13:24:30 +00:00
```diff
2019-06-02 07:45:37 +00:00
-list($a[], $a[]) = [1, 2];
+list($a[], $a[]) = array_reverse([1, 2]);
2019-03-09 13:24:30 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `MbStrrposEncodingArgumentPositionRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
Change mb_strrpos() encoding argument position
2019-02-21 14:36:16 +00:00
```diff
2019-06-02 07:45:37 +00:00
-mb_strrpos($text, "abc", "UTF-8");
+mb_strrpos($text, "abc", 0, "UTF-8");
2019-02-21 14:36:16 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `MultiDirnameRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\MultiDirnameRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Changes multiple dirname() calls to one with nesting level
2018-07-31 12:50:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
-dirname(dirname($path));
+dirname($path, 2);
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2018-11-11 12:22:35 +00:00
< br >
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
### `MultiExceptionCatchRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\TryCatch\MultiExceptionCatchRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
Changes multi catch of same exception to single one | separated.
2018-12-31 11:50:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
try {
// Some code...
-} catch (ExceptionType1 $exception) {
- $sameCode;
-} catch (ExceptionType2 $exception) {
+} catch (ExceptionType1 | ExceptionType2 $exception) {
$sameCode;
}
2018-12-31 11:50:32 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `MysqlAssignToMysqliRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Assign\MysqlAssignToMysqliRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Converts more complex mysql functions to mysqli
2018-10-12 23:15:00 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$data = mysql_db_name($result, $row);
+mysqli_data_seek($result, $row);
+$fetch = mysql_fetch_row($result);
+$data = $fetch[0];
2018-05-05 12:48:33 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `NullCoalescingOperatorRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Assign\NullCoalescingOperatorRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
Use null coalescing operator ??=
2019-05-01 23:56:58 +00:00
```diff
2019-06-02 07:45:37 +00:00
$array = [];
-$array['user_id'] = $array['user_id'] ?? 'value';
+$array['user_id'] ??= 'value';
2019-05-01 23:56:58 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ParseStrWithResultArgumentRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\ParseStrWithResultArgumentRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Use $result argument in parse_str() function
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
-parse_str($this->query);
-$data = get_defined_vars();
+parse_str($this->query, $result);
+$data = $result;
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `Php4ConstructorRector`
2019-05-24 20:30:15 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FunctionLike\Php4ConstructorRector`
2019-05-24 20:30:15 +00:00
2019-06-02 07:45:37 +00:00
Changes PHP 4 style constructor to __construct.
2019-05-24 20:30:15 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
- public function SomeClass()
+ public function __construct()
{
}
}
2019-05-24 20:30:15 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `PowToExpRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\PowToExpRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Changes pow(val, val2) to ** (exp) parameter
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
-pow(1, 2);
+1**2;
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `PreferThisOrSelfMethodCallRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\MethodCall\PreferThisOrSelfMethodCallRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Changes $this->... to self:: or vise versa for specific types
```yaml
services:
Rector\Php\Rector\MethodCall\PreferThisOrSelfMethodCallRector:
PHPUnit\TestCase: self
```
↓
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass extends PHPUnit\TestCase
{
public function run()
{
- $this->assertThis();
+ self::assertThis();
}
}
2019-02-04 00:32:53 +00:00
```
< br >
2019-05-29 13:40:20 +00:00
### `PregReplaceEModifierRector`
2019-02-04 00:32:53 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Php\Rector\FuncCall\PregReplaceEModifierRector`
2019-02-04 00:32:53 +00:00
2019-08-06 06:32:12 +00:00
The /e modifier is no longer supported, use preg_replace_callback instead
2019-02-04 00:32:53 +00:00
```diff
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function run()
{
- $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]));
+ }, , $comment);
}
2019-02-04 00:32:53 +00:00
}
```
< br >
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
### `PublicConstantVisibilityRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\ClassConst\PublicConstantVisibilityRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Add explicit public constant visibility.
2018-07-31 19:43:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
- const HEY = 'you';
+ public const HEY = 'you';
}
2018-07-31 19:43:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `RandomFunctionRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\RandomFunctionRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
Changes rand, srand and getrandmax by new md_* alternatives.
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
-rand();
+mt_rand();
2018-08-01 20:09:34 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `RealToFloatTypeCastRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Double\RealToFloatTypeCastRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
Change deprecated (real) to (float)
2019-05-01 23:56:58 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
public function run()
{
- $number = (real) 5;
+ $number = (float) 5;
$number = (float) 5;
$number = (double) 5;
}
}
2019-05-01 23:56:58 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ReduceMultipleDefaultSwitchRector`
2018-12-31 19:29:12 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Switch_\ReduceMultipleDefaultSwitchRector`
2018-12-31 19:29:12 +00:00
2019-06-02 07:45:37 +00:00
Remove first default switch, that is ignored
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
switch ($expr) {
default:
- echo "Hello World";
-
- default:
echo "Goodbye Moon!";
break;
2018-12-31 19:29:12 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RegexDashEscapeRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\RegexDashEscapeRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Escape - in some cases
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
-preg_match("#[\w-()]#", 'some text');
+preg_match("#[\w\-()]#", 'some text');
2018-08-01 20:09:34 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `RemoveExtraParametersRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\RemoveExtraParametersRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Remove extra parameters
2018-10-12 23:15:00 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
-strlen("asdf", 1);
+strlen("asdf");
2018-08-01 20:09:34 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `RemoveMissingCompactVariableRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\RemoveMissingCompactVariableRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
Remove non-existing vars from compact()
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function run()
{
$value = 'yes';
- compact('value', 'non_existing');
+ compact('value');
2019-05-29 13:40:20 +00:00
}
}
2018-08-01 20:09:34 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `RemoveReferenceFromCallRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\RemoveReferenceFromCallRector`
2018-12-31 11:50:32 +00:00
2019-06-02 07:45:37 +00:00
Remove & from function and method calls
2018-12-31 11:50:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeClass
{
public function run($one)
{
- return strlen(&$one);
+ return strlen($one);
}
2018-12-31 11:50:32 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RenameConstantRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\ConstFetch\RenameConstantRector`
2019-02-21 14:36:16 +00:00
2019-06-02 07:45:37 +00:00
Replace constant by new ones
2019-02-21 14:36:16 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeClass
{
public function run()
{
- return MYSQL_ASSOC;
+ return MYSQLI_ASSOC;
}
}
2019-05-29 13:40:20 +00:00
```
< br >
2019-07-06 19:51:36 +00:00
### `RenameMktimeWithoutArgsToTimeRector`
- class: `Rector\Php\Rector\FuncCall\RenameMktimeWithoutArgsToTimeRector`
```diff
class SomeClass
{
public function run()
{
$time = mktime(1, 2, 3);
- $nextTime = mktime();
+ $nextTime = time();
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `ReservedFnFunctionRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Function_\ReservedFnFunctionRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Change fn() function name, since it will be reserved keyword
2019-05-29 13:40:20 +00:00
```diff
class SomeClass
2019-02-21 14:36:16 +00:00
{
2019-05-29 13:40:20 +00:00
public function run()
{
2019-06-02 07:45:37 +00:00
- function fn($value)
+ function f($value)
{
return $value;
}
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- fn(5);
+ f(5);
2019-02-21 14:36:16 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `ReservedObjectRector`
2018-12-31 19:29:12 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Name\ReservedObjectRector`
2018-12-31 19:29:12 +00:00
2019-06-02 07:45:37 +00:00
Changes reserved "Object" name to "< Smart > Object" where < Smart > can be configured
2018-12-31 19:29:12 +00:00
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-class Object
+class SmartObject
{
}
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `SensitiveConstantNameRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\ConstFetch\SensitiveConstantNameRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Changes case insensitive constants to sensitive ones.
2018-12-31 19:29:12 +00:00
```diff
2019-06-02 07:45:37 +00:00
define('FOO', 42, true);
var_dump(FOO);
-var_dump(foo);
+var_dump(FOO);
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `SensitiveDefineRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\SensitiveDefineRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Changes case insensitive constants to sensitive ones.
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-define('FOO', 42, true);
+define('FOO', 42);
2018-12-31 19:29:12 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `SensitiveHereNowDocRector`
2018-12-31 19:29:12 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\String_\SensitiveHereNowDocRector`
2018-12-31 19:29:12 +00:00
2019-06-02 07:45:37 +00:00
Changes heredoc/nowdoc that contains closing word to safe wrapper name
2018-12-31 19:29:12 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$value = < < < A
+$value = < < < A_WRAP
A
-A
+A_WRAP
2018-12-31 19:29:12 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `StaticCallOnNonStaticToInstanceCallRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Changes static call to instance call, where not useful
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
class Something
{
public function doWork()
{
}
}
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
class Another
{
public function run()
{
- return Something::doWork();
+ return (new Something)->doWork();
}
}
2018-12-22 14:15:11 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `StringClassNameToClassConstantRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\String_\StringClassNameToClassConstantRector`
2018-10-12 23:15:00 +00:00
2019-06-02 07:45:37 +00:00
Replace string class names by < class > ::class constant
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
class AnotherClass
{
}
class SomeClass
{
public function run()
{
- return 'AnotherClass';
+ return \AnotherClass::class;
}
}
2018-08-01 20:09:34 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `StringifyDefineRector`
2018-12-31 19:29:12 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\StringifyDefineRector`
2018-12-31 19:29:12 +00:00
2019-06-02 07:45:37 +00:00
Make first argument of define() string
2018-12-31 19:29:12 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function run(int $a)
{
- define(CONSTANT_2, 'value');
+ define('CONSTANT_2', 'value');
define('CONSTANT', 'value');
}
2019-05-29 13:40:20 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `StringifyStrNeedlesRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\StringifyStrNeedlesRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Makes needles explicit strings
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
$needle = 5;
-$fivePosition = strpos('725', $needle);
+$fivePosition = strpos('725', (string) $needle);
2019-02-04 00:32:53 +00:00
```
2018-12-31 19:29:12 +00:00
2019-05-29 13:40:20 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `StringsAssertNakedRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\StringsAssertNakedRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
String asserts must be passed directly to assert()
2019-05-29 13:40:20 +00:00
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
function nakedAssert()
{
- assert('true === true');
- assert("true === true");
+ assert(true === true);
+ assert(true === true);
}
2018-12-31 19:29:12 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `SwapFuncCallArgumentsRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\FuncCall\SwapFuncCallArgumentsRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
Swap arguments in function calls
2019-03-31 12:25:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeClass
2019-03-31 12:25:39 +00:00
{
2019-06-02 07:45:37 +00:00
public function run($one, $two)
2019-03-31 12:25:39 +00:00
{
2019-06-02 07:45:37 +00:00
- return some_function($one, $two);
+ return some_function($two, $one);
2019-03-31 12:25:39 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `TernaryToNullCoalescingRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Php\Rector\Ternary\TernaryToNullCoalescingRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
Changes unneeded null check to ?? operator
2019-03-31 12:25:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$value === null ? 10 : $value;
+$value ?? 10;
```
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
```diff
-isset($value) ? $value : 10;
+$value ?? 10;
```
< br >
### `TernaryToSpaceshipRector`
- class: `Rector\Php\Rector\Ternary\TernaryToSpaceshipRector`
Use < => spaceship instead of ternary with same effect
```diff
function order_func($a, $b) {
- return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
+ return $a < => $b;
}
```
< br >
### `ThisCallOnStaticMethodToStaticCallRector`
- class: `Rector\Php\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector`
Changes $this->call() to static method to static call
```diff
class SomeClass
{
public static function run()
{
- $this->eat();
+ self::eat();
}
public static function eat()
{
}
}
```
< br >
### `TypedPropertyRector`
- class: `Rector\Php\Rector\Property\TypedPropertyRector`
Changes property `@var` annotations from annotation to type.
```diff
final class SomeClass
{
- /**
- * @var int
- */
- private count;
+ private int count;
}
```
< br >
### `UnsetCastRector`
- class: `Rector\Php\Rector\Unset_\UnsetCastRector`
Removes (unset) cast
```diff
-$value = (unset) $value;
+$value = null;
```
< br >
### `VarToPublicPropertyRector`
- class: `Rector\Php\Rector\Property\VarToPublicPropertyRector`
Remove unused private method
```diff
final class SomeController
{
- var $name = 'Tom';
+ public $name = 'Tom';
}
```
< br >
### `WhileEachToForeachRector`
- class: `Rector\Php\Rector\Each\WhileEachToForeachRector`
each() function is deprecated, use foreach() instead.
```diff
-while (list($key, $callback) = each($callbacks)) {
+foreach ($callbacks as $key => $callback) {
// ...
}
```
```diff
-while (list($key) = each($callbacks)) {
+foreach (array_keys($callbacks) as $key) {
// ...
}
```
< br >
## PhpParser
### `CatchAndClosureUseNameRector`
- class: `Rector\PhpParser\Rector\CatchAndClosureUseNameRector`
Turns `$catchNode->var` to its new `name` property in php-parser
```diff
-$catchNode->var;
+$catchNode->var->name
```
< br >
### `IdentifierRector`
- class: `Rector\PhpParser\Rector\IdentifierRector`
Turns node string names to Identifier object in php-parser
```diff
$constNode = new PhpParser\Node\Const_;
-$name = $constNode->name;
+$name = $constNode->name->toString();'
```
< br >
### `ParamAndStaticVarNameRector`
- class: `Rector\PhpParser\Rector\ParamAndStaticVarNameRector`
Turns old string `var` to `var->name` sub-variable in Node of PHP-Parser
```diff
-$paramNode->name;
+$paramNode->var->name;
```
```diff
-$staticVarNode->name;
+$staticVarNode->var->name;
```
< br >
### `RemoveNodeRector`
- class: `Rector\PhpParser\Rector\RemoveNodeRector`
Turns integer return to remove node to constant in NodeVisitor of PHP-Parser
```diff
public function leaveNode()
{
- return false;
+ return NodeTraverser::REMOVE_NODE;
}
```
< br >
### `SetLineRector`
- class: `Rector\PhpParser\Rector\SetLineRector`
Turns standalone line method to attribute in Node of PHP-Parser
```diff
-$node->setLine(5);
+$node->setAttribute("line", 5);
```
< br >
### `UseWithAliasRector`
- class: `Rector\PhpParser\Rector\UseWithAliasRector`
Turns use property to method and `$node->alias` to last name in UseAlias Node of PHP-Parser
```diff
-$node->alias;
+$node->getAlias();
```
```diff
-$node->name->getLast();
+$node->alias
```
< br >
## PhpSpecToPHPUnit
### `AddMockPropertiesRector`
- class: `Rector\PhpSpecToPHPUnit\Rector\Class_\AddMockPropertiesRector`
Migrate PhpSpec behavior to PHPUnit test
```diff
namespace spec\SomeNamespaceForThisTest;
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod)
+ /**
+ * @var \SomeNamespaceForThisTest\Order
+ */
+ private $order;
+ protected function setUp()
2019-03-31 12:25:39 +00:00
{
- $factory->createShippingMethodFor(Argument::any())->shouldBeCalled()->willReturn($shippingMethod);
+ /** @var OrderFactory|\PHPUnit\Framework\MockObject\MockObject $factory */
+ $factory = $this->createMock(OrderFactory::class);
+
+ /** @var ShippingMethod|\PHPUnit\Framework\MockObject\MockObject $shippingMethod */
+ $shippingMethod = $this->createMock(ShippingMethod::class);
+
+ $factory->expects($this->once())->method('createShippingMethodFor')->willReturn($shippingMethod);
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `MockVariableToPropertyFetchRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PhpSpecToPHPUnit\Rector\ClassMethod\MockVariableToPropertyFetchRector`
2019-03-31 12:25:39 +00:00
Migrate PhpSpec behavior to PHPUnit test
```diff
namespace spec\SomeNamespaceForThisTest;
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod)
+ /**
+ * @var \SomeNamespaceForThisTest\Order
+ */
+ private $order;
+ protected function setUp()
{
- $factory->createShippingMethodFor(Argument::any())->shouldBeCalled()->willReturn($shippingMethod);
+ /** @var OrderFactory|\PHPUnit\Framework\MockObject\MockObject $factory */
+ $factory = $this->createMock(OrderFactory::class);
+
+ /** @var ShippingMethod|\PHPUnit\Framework\MockObject\MockObject $shippingMethod */
+ $shippingMethod = $this->createMock(ShippingMethod::class);
+
+ $factory->expects($this->once())->method('createShippingMethodFor')->willReturn($shippingMethod);
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `PhpSpecClassToPHPUnitClassRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PhpSpecToPHPUnit\Rector\Class_\PhpSpecClassToPHPUnitClassRector`
2019-03-31 12:25:39 +00:00
Migrate PhpSpec behavior to PHPUnit test
```diff
namespace spec\SomeNamespaceForThisTest;
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod)
+ /**
+ * @var \SomeNamespaceForThisTest\Order
+ */
+ private $order;
+ protected function setUp()
{
- $factory->createShippingMethodFor(Argument::any())->shouldBeCalled()->willReturn($shippingMethod);
+ /** @var OrderFactory|\PHPUnit\Framework\MockObject\MockObject $factory */
+ $factory = $this->createMock(OrderFactory::class);
+
+ /** @var ShippingMethod|\PHPUnit\Framework\MockObject\MockObject $shippingMethod */
+ $shippingMethod = $this->createMock(ShippingMethod::class);
+
+ $factory->expects($this->once())->method('createShippingMethodFor')->willReturn($shippingMethod);
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `PhpSpecMethodToPHPUnitMethodRector`
2019-03-16 20:31:46 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PhpSpecToPHPUnit\Rector\ClassMethod\PhpSpecMethodToPHPUnitMethodRector`
2019-03-16 20:31:46 +00:00
2019-03-31 12:25:39 +00:00
Migrate PhpSpec behavior to PHPUnit test
2019-03-16 20:31:46 +00:00
```diff
2019-03-31 12:25:39 +00:00
namespace spec\SomeNamespaceForThisTest;
2019-03-16 20:31:46 +00:00
2019-03-31 12:25:39 +00:00
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
2019-03-16 20:31:46 +00:00
{
2019-03-31 12:25:39 +00:00
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod)
+ /**
+ * @var \SomeNamespaceForThisTest\Order
+ */
+ private $order;
2019-03-16 20:31:46 +00:00
+ protected function setUp()
{
2019-03-31 12:25:39 +00:00
- $factory->createShippingMethodFor(Argument::any())->shouldBeCalled()->willReturn($shippingMethod);
+ /** @var OrderFactory|\PHPUnit\Framework\MockObject\MockObject $factory */
+ $factory = $this->createMock(OrderFactory::class);
+
+ /** @var ShippingMethod|\PHPUnit\Framework\MockObject\MockObject $shippingMethod */
+ $shippingMethod = $this->createMock(ShippingMethod::class);
+
+ $factory->expects($this->once())->method('createShippingMethodFor')->willReturn($shippingMethod);
2019-03-16 20:31:46 +00:00
}
2019-03-31 12:25:39 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `PhpSpecMocksToPHPUnitMocksRector`
2019-03-31 12:25:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\PhpSpecToPHPUnit\Rector\MethodCall\PhpSpecMocksToPHPUnitMocksRector`
2019-03-16 20:31:46 +00:00
2019-03-31 12:25:39 +00:00
Migrate PhpSpec behavior to PHPUnit test
```diff
namespace spec\SomeNamespaceForThisTest;
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod)
+ /**
+ * @var \SomeNamespaceForThisTest\Order
+ */
+ private $order;
+ protected function setUp()
2019-03-16 20:31:46 +00:00
{
2019-03-31 12:25:39 +00:00
- $factory->createShippingMethodFor(Argument::any())->shouldBeCalled()->willReturn($shippingMethod);
+ /** @var OrderFactory|\PHPUnit\Framework\MockObject\MockObject $factory */
+ $factory = $this->createMock(OrderFactory::class);
+
+ /** @var ShippingMethod|\PHPUnit\Framework\MockObject\MockObject $shippingMethod */
+ $shippingMethod = $this->createMock(ShippingMethod::class);
+
+ $factory->expects($this->once())->method('createShippingMethodFor')->willReturn($shippingMethod);
2019-03-16 20:31:46 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `PhpSpecPromisesToPHPUnitAssertRector`
- class: `Rector\PhpSpecToPHPUnit\Rector\MethodCall\PhpSpecPromisesToPHPUnitAssertRector`
Migrate PhpSpec behavior to PHPUnit test
```diff
namespace spec\SomeNamespaceForThisTest;
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod)
+ /**
+ * @var \SomeNamespaceForThisTest\Order
+ */
+ private $order;
+ protected function setUp()
{
- $factory->createShippingMethodFor(Argument::any())->shouldBeCalled()->willReturn($shippingMethod);
+ /** @var OrderFactory|\PHPUnit\Framework\MockObject\MockObject $factory */
+ $factory = $this->createMock(OrderFactory::class);
+
+ /** @var ShippingMethod|\PHPUnit\Framework\MockObject\MockObject $shippingMethod */
+ $shippingMethod = $this->createMock(ShippingMethod::class);
+
+ $factory->expects($this->once())->method('createShippingMethodFor')->willReturn($shippingMethod);
}
}
```
< br >
### `RenameSpecFileToTestFileRector`
- class: `Rector\PhpSpecToPHPUnit\Rector\RenameSpecFileToTestFileRector`
Rename "*Spec.php" file to "*Test.php" file
< br >
## RemovingStatic
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
### `NewUniqueObjectToEntityFactoryRector`
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\RemovingStatic\Rector\Class_\NewUniqueObjectToEntityFactoryRector`
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
Convert new X to new factories
2019-05-01 23:56:58 +00:00
```diff
-< ?php
-
class SomeClass
{
+ public function __construct(AnotherClassFactory $anotherClassFactory)
+ {
+ $this->anotherClassFactory = $anotherClassFactory;
+ }
+
public function run()
{
- return new AnotherClass;
+ return $this->anotherClassFactory->create();
}
}
class AnotherClass
{
public function someFun()
{
return StaticClass::staticMethod();
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `PHPUnitStaticToKernelTestCaseGetRector`
- class: `Rector\RemovingStatic\Rector\Class_\PHPUnitStaticToKernelTestCaseGetRector`
Convert static calls in PHPUnit test cases, to get() from the container of KernelTestCase
```yaml
services:
Rector\RemovingStatic\Rector\Class_\PHPUnitStaticToKernelTestCaseGetRector:
staticClassTypes:
- EntityFactory
```
↓
```diff
-< ?php
+use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
-use PHPUnit\Framework\TestCase;
+final class SomeTestCase extends KernelTestCase
+{
+ /**
+ * @var EntityFactory
+ */
+ private $entityFactory;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->entityFactory = self::$container->get(EntityFactory::class);
+ }
-final class SomeTestCase extends TestCase
-{
public function test()
{
- $product = EntityFactory::create('product');
+ $product = $this->entityFactory->create('product');
}
}
```
< br >
2019-05-01 23:56:58 +00:00
### `PassFactoryToUniqueObjectRector`
- class: `Rector\RemovingStatic\Rector\Class_\PassFactoryToUniqueObjectRector`
Convert new X/Static::call() to factories in entities, pass them via constructor to each other
```yaml
services:
Rector\RemovingStatic\Rector\Class_\PassFactoryToUniqueObjectRector:
typesToServices:
- StaticClass
```
↓
```diff
-< ?php
-
class SomeClass
{
+ public function __construct(AnotherClassFactory $anotherClassFactory)
+ {
+ $this->anotherClassFactory = $anotherClassFactory;
+ }
+
public function run()
{
- return new AnotherClass;
+ return $this->anotherClassFactory->create();
}
}
class AnotherClass
{
+ public function __construct(StaticClass $staticClass)
+ {
+ $this->staticClass = $staticClass;
+ }
+
public function someFun()
{
- return StaticClass::staticMethod();
+ return $this->staticClass->staticMethod();
+ }
+}
+
+final class AnotherClassFactory
+{
+ /**
+ * @var StaticClass
+ */
+ private $staticClass;
+
+ public function __construct(StaticClass $staticClass)
+ {
+ $this->staticClass = $staticClass;
+ }
+
+ public function create(): AnotherClass
+ {
+ return new AnotherClass($this->staticClass);
}
}
```
< br >
2019-05-29 13:40:20 +00:00
### `StaticTypeToSetterInjectionRector`
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\RemovingStatic\Rector\Class_\StaticTypeToSetterInjectionRector`
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
Changes types to setter injection
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
```yaml
services:
Rector\RemovingStatic\Rector\Class_\StaticTypeToSetterInjectionRector:
$staticTypes:
- SomeStaticClass
```
↓
2019-05-01 23:56:58 +00:00
```diff
2019-05-29 13:40:20 +00:00
< ?php
final class CheckoutEntityFactory
2019-05-01 23:56:58 +00:00
{
2019-05-29 13:40:20 +00:00
+ /**
+ * @var SomeStaticClass
+ */
+ private $someStaticClass;
+
+ public function setSomeStaticClass(SomeStaticClass $someStaticClass)
+ {
+ $this->someStaticClass = $someStaticClass;
+ }
+
public function run()
{
- return SomeStaticClass::go();
+ return $this->someStaticClass->go();
}
-}
+}
```
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
< br >
2019-08-05 21:10:47 +00:00
## Restoration
### `CompleteImportForPartialAnnotationRector`
- class: `Rector\Restoration\Rector\Namespace_\CompleteImportForPartialAnnotationRector`
In case you have accidentally removed use imports but code still contains partial use statements, this will save you
```yaml
services:
Rector\Restoration\Rector\Namespace_\CompleteImportForPartialAnnotationRector:
$useImportToRestore:
-
- Doctrine\ORM\Mapping
- ORM
```
↓
```diff
+use Doctrine\ORM\Mapping as ORM;
+
class SomeClass
{
/**
* @ORM \Id
*/
public $id;
}
```
< br >
2019-06-02 07:45:37 +00:00
## SOLID
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
### `FinalizeClassesWithoutChildrenRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\SOLID\Rector\Class_\FinalizeClassesWithoutChildrenRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Finalize every class that has no children
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-class FirstClass
+final class FirstClass
{
}
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
class SecondClass
{
}
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
-class ThirdClass extends SecondClass
+final class ThirdClass extends SecondClass
{
2019-05-01 23:56:58 +00:00
}
```
< br >
2019-05-26 16:38:09 +00:00
### `MakeUnusedClassesWithChildrenAbstractRector`
2019-05-26 11:47:23 +00:00
2019-05-26 16:38:09 +00:00
- class: `Rector\SOLID\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector`
2019-05-26 11:47:23 +00:00
Classes that have no children nor are used, should have abstract
```diff
class SomeClass extends PossibleAbstractClass
{
}
-class PossibleAbstractClass
+abstract class PossibleAbstractClass
{
}
```
< br >
2019-05-29 13:40:20 +00:00
### `PrivatizeLocalClassConstantRector`
- class: `Rector\SOLID\Rector\ClassConst\PrivatizeLocalClassConstantRector`
Finalize every class constant that is used only locally
```diff
class ClassWithConstantUsedOnlyHere
{
- const LOCAL_ONLY = true;
+ private const LOCAL_ONLY = true;
public function isLocalOnly()
{
return self::LOCAL_ONLY;
}
}
```
< br >
2019-02-04 00:32:53 +00:00
## Sensio
2018-12-31 11:50:32 +00:00
2019-02-04 00:32:53 +00:00
### `TemplateAnnotationRector`
2018-12-31 11:50:32 +00:00
2019-02-04 00:32:53 +00:00
- class: `Rector\Sensio\Rector\FrameworkExtraBundle\TemplateAnnotationRector`
Turns `@Template` annotation to explicit method call in Controller of FrameworkExtraBundle in Symfony
2018-12-31 11:50:32 +00:00
```diff
2019-02-04 00:32:53 +00:00
-/**
- * @Template ()
- */
public function indexAction()
{
+ return $this->render("index.html.twig");
}
2018-12-31 11:50:32 +00:00
```
< br >
2019-03-16 20:31:46 +00:00
## Shopware
### `ReplaceEnlightResponseWithSymfonyResponseRector`
- class: `Rector\Shopware\Rector\MethodCall\ReplaceEnlightResponseWithSymfonyResponseRector`
Replace Enlight Response methods with Symfony Response methods
```diff
class FrontendController extends \Enlight_Controller_Action
{
public function run()
{
- $this->Response()->setHeader('Foo', 'Yea');
+ $this->Response()->headers->set('Foo', 'Yea');
}
}
```
< br >
2019-07-06 19:51:36 +00:00
### `ShopRegistrationServiceRector`
- class: `Rector\Shopware\Rector\MethodCall\ShopRegistrationServiceRector`
Replace $shop->registerResources() with ShopRegistrationService
```diff
class SomeClass
{
public function run()
{
$shop = new \Shopware\Models\Shop\Shop();
- $shop->registerResources();
+ Shopware()->Container()->get('shopware.components.shop_registration_service')->registerShop($shop);
}
}
```
< br >
### `ShopwareVersionConstsRector`
- class: `Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector`
Use version from di parameter
```diff
class SomeClass
{
public function run()
{
- echo \Shopware::VERSION;
+ echo Shopware()->Container()->getParameter('shopware.release.version');
}
}
```
< br >
2019-02-04 00:32:53 +00:00
## Silverstripe
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
### `ConstantToStaticCallRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Silverstripe\Rector\ConstantToStaticCallRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
Turns defined constant to static method call.
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
-SS_DATABASE_NAME;
2019-02-04 00:32:53 +00:00
+Environment::getEnv("SS_DATABASE_NAME");
2018-10-21 22:26:45 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `DefineConstantToStaticCallRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Silverstripe\Rector\DefineConstantToStaticCallRector`
2018-12-22 11:22:28 +00:00
2019-06-02 07:45:37 +00:00
Turns defined function call to static method call.
2018-12-22 11:22:28 +00:00
```diff
2019-06-02 07:45:37 +00:00
-defined("SS_DATABASE_NAME");
2019-02-04 00:32:53 +00:00
+Environment::getEnv("SS_DATABASE_NAME");
2018-12-22 11:22:28 +00:00
```
< br >
2019-02-04 00:32:53 +00:00
## Sylius
2018-08-01 20:09:34 +00:00
2019-02-04 00:32:53 +00:00
### `ReplaceCreateMethodWithoutReviewerRector`
2018-12-22 11:22:28 +00:00
2019-02-04 00:32:53 +00:00
- class: `Rector\Sylius\Rector\Review\ReplaceCreateMethodWithoutReviewerRector`
2018-12-22 11:22:28 +00:00
2019-02-04 00:32:53 +00:00
Turns `createForSubjectWithReviewer()` with null review to standalone method in Sylius
2018-12-22 11:22:28 +00:00
```diff
2019-02-04 00:32:53 +00:00
-$this->createForSubjectWithReviewer($subject, null)
+$this->createForSubject($subject)
2018-12-22 11:22:28 +00:00
```
< br >
2019-02-04 00:32:53 +00:00
## Symfony
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
### `ActionSuffixRemoverRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\Controller\ActionSuffixRemoverRector`
2018-10-21 22:26:45 +00:00
2019-06-02 07:45:37 +00:00
Removes Action suffixes from methods in Symfony Controllers
2018-10-21 22:26:45 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeController
{
- public function indexAction()
+ public function index()
{
}
}
2018-10-21 22:26:45 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-05-29 13:40:20 +00:00
### `AddFlashRector`
2018-08-01 20:09:34 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Symfony\Rector\Controller\AddFlashRector`
2018-08-01 20:09:34 +00:00
2019-05-29 13:40:20 +00:00
Turns long flash adding to short helper method in Controller in Symfony
2018-08-01 20:09:34 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeController extends Controller
2018-08-01 20:09:34 +00:00
{
2019-05-29 13:40:20 +00:00
public function some(Request $request)
2019-02-04 00:32:53 +00:00
{
2019-05-29 13:40:20 +00:00
- $request->getSession()->getFlashBag()->add("success", "something");
+ $this->addFlash("success", "something");
2019-02-04 00:32:53 +00:00
}
2018-08-01 20:09:34 +00:00
}
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `CascadeValidationFormBuilderRector`
2018-09-28 16:33:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\MethodCall\CascadeValidationFormBuilderRector`
2018-09-28 16:33:35 +00:00
2019-06-02 07:45:37 +00:00
Change "cascade_validation" option to specific node attribute
2018-09-28 16:33:35 +00:00
```diff
2019-02-04 00:32:53 +00:00
class SomeController
{
2019-06-02 07:45:37 +00:00
public function someMethod()
2019-02-04 00:32:53 +00:00
{
2019-06-02 07:45:37 +00:00
- $form = $this->createFormBuilder($article, ['cascade_validation' => true])
- ->add('author', new AuthorType())
+ $form = $this->createFormBuilder($article)
+ ->add('author', new AuthorType(), [
+ 'constraints' => new \Symfony\Component\Validator\Constraints\Valid(),
+ ])
->getForm();
}
2018-09-28 16:33:35 +00:00
2019-06-02 07:45:37 +00:00
protected function createFormBuilder()
{
return new FormBuilder();
}
2019-02-04 00:32:53 +00:00
}
2018-09-28 16:33:35 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ConsoleExceptionToErrorEventConstantRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\Console\ConsoleExceptionToErrorEventConstantRector`
2019-05-01 23:56:58 +00:00
2019-06-02 07:45:37 +00:00
Turns old event name with EXCEPTION to ERROR constant in Console in Symfony
2019-05-01 23:56:58 +00:00
```diff
2019-06-02 07:45:37 +00:00
-"console.exception"
+Symfony\Component\Console\ConsoleEvents::ERROR
2019-05-29 13:40:20 +00:00
```
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-Symfony\Component\Console\ConsoleEvents::EXCEPTION
+Symfony\Component\Console\ConsoleEvents::ERROR
2019-05-01 23:56:58 +00:00
```
< br >
2019-02-04 00:32:53 +00:00
### `ConstraintUrlOptionRector`
- class: `Rector\Symfony\Rector\Validator\ConstraintUrlOptionRector`
Turns true value to `Url::CHECK_DNS_TYPE_ANY` in Validator in Symfony.
```diff
-$constraint = new Url(["checkDNS" => true]);
+$constraint = new Url(["checkDNS" => Url::CHECK_DNS_TYPE_ANY]);
```
< br >
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
### `ContainerBuilderCompileEnvArgumentRector`
2018-08-01 13:34:55 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\DependencyInjection\ContainerBuilderCompileEnvArgumentRector`
2018-08-01 13:34:55 +00:00
2019-06-02 07:45:37 +00:00
Turns old default value to parameter in ContinerBuilder->build() method in DI in Symfony
2018-07-31 12:50:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$containerBuilder = new Symfony\Component\DependencyInjection\ContainerBuilder(); $containerBuilder->compile();
+$containerBuilder = new Symfony\Component\DependencyInjection\ContainerBuilder(); $containerBuilder->compile(true);
2018-07-31 12:50:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-05-29 13:40:20 +00:00
### `ContainerGetToConstructorInjectionRector`
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Symfony\Rector\FrameworkBundle\ContainerGetToConstructorInjectionRector`
2019-03-31 12:25:39 +00:00
2019-05-29 13:40:20 +00:00
Turns fetching of dependencies via `$container->get()` in ContainerAware to constructor injection in Command and Controller in Symfony
2019-03-31 12:25:39 +00:00
```diff
2019-05-29 13:40:20 +00:00
-final class SomeCommand extends ContainerAwareCommand
+final class SomeCommand extends Command
2019-03-31 12:25:39 +00:00
{
2019-05-29 13:40:20 +00:00
+ public function __construct(SomeService $someService)
+ {
+ $this->someService = $someService;
+ }
+
public function someMethod()
2019-03-31 12:25:39 +00:00
{
2019-05-29 13:40:20 +00:00
// ...
- $this->getContainer()->get('some_service');
- $this->container->get('some_service');
+ $this->someService;
+ $this->someService;
2019-03-31 12:25:39 +00:00
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `FormIsValidRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\Form\FormIsValidRector`
2018-08-01 13:34:55 +00:00
2019-06-02 07:45:37 +00:00
Adds `$form->isSubmitted()` validatoin to all `$form->isValid()` calls in Form in Symfony
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
-if ($form->isValid()) {
+if ($form->isSubmitted() & & $form->isValid()) {
2018-05-04 22:30:32 +00:00
}
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `FormTypeGetParentRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\Form\FormTypeGetParentRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Turns string Form Type references to their CONSTANT alternatives in `getParent()` and `getExtendedType()` methods in Form in Symfony
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
-function getParent() { return "collection"; }
+function getParent() { return CollectionType::class; }
2018-05-04 22:30:32 +00:00
```
```diff
2019-06-02 07:45:37 +00:00
-function getExtendedType() { return "collection"; }
+function getExtendedType() { return CollectionType::class; }
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-05-29 13:40:20 +00:00
### `FormTypeInstanceToClassConstRector`
2018-07-31 19:43:39 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Symfony\Rector\MethodCall\FormTypeInstanceToClassConstRector`
2018-05-04 22:30:32 +00:00
2019-05-29 13:40:20 +00:00
Changes createForm(new FormType), add(new FormType) to ones with "FormType::class"
2018-05-04 22:30:32 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeController
{
public function action()
{
- $form = $this->createForm(new TeamType, $entity, [
+ $form = $this->createForm(TeamType::class, $entity, [
'action' => $this->generateUrl('teams_update', ['id' => $entity->getId()]),
'method' => 'PUT',
- ]);
+ ));
}
2018-08-01 20:09:34 +00:00
}
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `GetParameterToConstructorInjectionRector`
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\FrameworkBundle\GetParameterToConstructorInjectionRector`
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
Turns fetching of parameters via `getParameter()` in ContainerAware to constructor injection in Command and Controller in Symfony
2018-10-23 18:58:57 +00:00
```diff
2019-06-02 07:45:37 +00:00
-class MyCommand extends ContainerAwareCommand
+class MyCommand extends Command
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
+ private $someParameter;
+
+ public function __construct($someParameter)
+ {
+ $this->someParameter = $someParameter;
+ }
+
public function someMethod()
{
- $this->getParameter('someParameter');
+ $this->someParameter;
}
2019-05-29 13:40:20 +00:00
}
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
### `GetRequestRector`
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\HttpKernel\GetRequestRector`
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
Turns fetching of dependencies via `$this->get()` to constructor injection in Command and Controller in Symfony
2018-10-23 18:58:57 +00:00
```diff
2019-06-02 07:45:37 +00:00
+use Symfony\Component\HttpFoundation\Request;
+
class SomeController
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- public function someAction()
+ public function someAction(Request $request)
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- $this->getRequest()->...();
+ $request->...();
2019-05-29 13:40:20 +00:00
}
2019-06-02 07:45:37 +00:00
}
```
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
< br >
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
### `GetToConstructorInjectionRector`
- class: `Rector\Symfony\Rector\FrameworkBundle\GetToConstructorInjectionRector`
Turns fetching of dependencies via `$this->get()` to constructor injection in Command and Controller in Symfony
```diff
-class MyCommand extends ContainerAwareCommand
+class MyCommand extends Command
{
+ public function __construct(SomeService $someService)
+ {
+ $this->someService = $someService;
+ }
+
public function someMethod()
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- // ...
- $this->get('some_service');
+ $this->someService;
2019-05-29 13:40:20 +00:00
}
}
2018-11-11 12:22:35 +00:00
```
< br >
2019-06-06 13:01:53 +00:00
### `MakeCommandLazyRector`
- class: `Rector\Symfony\Rector\Class_\MakeCommandLazyRector`
Make Symfony commands lazy
```diff
use Symfony\Component\Console\Command\Command
class SunshineCommand extends Command
{
+ protected static $defaultName = 'sunshine';
public function configure()
{
- $this->setName('sunshine');
}
}
```
< br >
2019-05-29 13:40:20 +00:00
### `MakeDispatchFirstArgumentEventRector`
2018-11-11 12:22:35 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Symfony\Rector\MethodCall\MakeDispatchFirstArgumentEventRector`
2018-11-11 12:22:35 +00:00
2019-05-29 13:40:20 +00:00
Make event object a first argument of dispatch() method, event name as second
2018-11-11 12:22:35 +00:00
```diff
2019-05-29 13:40:20 +00:00
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SomeClass
{
2019-06-06 13:01:53 +00:00
public function run(EventDispatcherInterface $eventDispatcher)
2019-05-29 13:40:20 +00:00
{
2019-06-06 13:01:53 +00:00
- $eventDispatcher->dispatch('event_name', new Event());
+ $eventDispatcher->dispatch(new Event(), 'event_name');
2019-05-29 13:40:20 +00:00
}
}
2018-11-11 12:22:35 +00:00
```
2019-05-29 13:40:20 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `OptionNameRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\Form\OptionNameRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Turns old option names to new ones in FormTypes in Form in Symfony
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
$builder = new FormBuilder;
-$builder->add("...", ["precision" => "...", "virtual" => "..."];
+$builder->add("...", ["scale" => "...", "inherit_data" => "..."];
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ParseFileRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\Yaml\ParseFileRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
session > use_strict_mode is true by default and can be removed
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-session > use_strict_mode: true
+session:
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ProcessBuilderGetProcessRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\Process\ProcessBuilderGetProcessRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Removes `$processBuilder->getProcess()` calls to $processBuilder in Process in Symfony, because ProcessBuilder was removed. This is part of multi-step Rector and has very narrow focus.
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
$processBuilder = new Symfony\Component\Process\ProcessBuilder;
-$process = $processBuilder->getProcess();
-$commamdLine = $processBuilder->getProcess()->getCommandLine();
+$process = $processBuilder;
+$commamdLine = $processBuilder->getCommandLine();
```
2018-10-23 18:58:57 +00:00
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ProcessBuilderInstanceRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\Process\ProcessBuilderInstanceRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Turns `ProcessBuilder::instance()` to new ProcessBuilder in Process in Symfony. Part of multi-step Rector.
2018-07-31 12:50:39 +00:00
2018-08-01 13:34:55 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$processBuilder = Symfony\Component\Process\ProcessBuilder::instance($args);
+$processBuilder = new Symfony\Component\Process\ProcessBuilder($args);
```
< br >
### `ReadOnlyOptionToAttributeRector`
- class: `Rector\Symfony\Rector\MethodCall\ReadOnlyOptionToAttributeRector`
Change "read_only" option in form to attribute
```diff
use Symfony\Component\Form\FormBuilderInterface;
function buildForm(FormBuilderInterface $builder, array $options)
{
- $builder->add('cuid', TextType::class, ['read_only' => true]);
+ $builder->add('cuid', TextType::class, ['attr' => [read_only' => true]]);
}
```
< br >
### `RedirectToRouteRector`
- class: `Rector\Symfony\Rector\Controller\RedirectToRouteRector`
Turns redirect to route to short helper method in Controller in Symfony
```diff
-$this->redirect($this->generateUrl("homepage"));
+$this->redirectToRoute("homepage");
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-05-29 13:40:20 +00:00
### `ResponseStatusCodeRector`
2018-11-11 12:22:35 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Symfony\Rector\BinaryOp\ResponseStatusCodeRector`
2018-11-11 12:22:35 +00:00
2019-05-29 13:40:20 +00:00
Turns status code numbers to constants
2018-11-11 12:22:35 +00:00
```diff
2019-05-29 13:40:20 +00:00
class SomeController
{
public function index()
{
$response = new \Symfony\Component\HttpFoundation\Response();
- $response->setStatusCode(200);
+ $response->setStatusCode(\Symfony\Component\HttpFoundation\Response::HTTP_OK);
- if ($response->getStatusCode() === 200) {}
+ if ($response->getStatusCode() === \Symfony\Component\HttpFoundation\Response::HTTP_OK) {}
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RootNodeTreeBuilderRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Symfony\Rector\New_\RootNodeTreeBuilderRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Changes Process string argument to an array
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
-$treeBuilder = new TreeBuilder();
-$rootNode = $treeBuilder->root('acme_root');
+$treeBuilder = new TreeBuilder('acme_root');
+$rootNode = $treeBuilder->getRootNode();
$rootNode->someCall();
```
< br >
### `SimplifyWebTestCaseAssertionsRector`
- class: `Rector\Symfony\Rector\MethodCall\SimplifyWebTestCaseAssertionsRector`
Simplify use of assertions in WebTestCase
```diff
use PHPUnit\Framework\TestCase;
class SomeClass extends TestCase
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function test()
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- $this->assertSame(200, $client->getResponse()->getStatusCode());
+ $this->assertResponseIsSuccessful();
}
public function testUrl()
{
- $this->assertSame(301, $client->getResponse()->getStatusCode());
- $this->assertSame('https://example.com', $client->getResponse()->headers->get('Location'));
+ $this->assertResponseRedirects('https://example.com', 301);
}
public function testContains()
{
- $this->assertContains('Hello World', $crawler->filter('h1')->text());
+ $this->assertSelectorTextContains('h1', 'Hello World');
2019-05-29 13:40:20 +00:00
}
}
2018-11-11 12:22:35 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `StringFormTypeToClassRector`
- class: `Rector\Symfony\Rector\Form\StringFormTypeToClassRector`
Turns string Form Type references to their CONSTANT alternatives in FormTypes in Form in Symfony
```diff
$formBuilder = new Symfony\Component\Form\FormBuilder;
-$formBuilder->add('name', 'form.type.text');
+$form->add('name', \Symfony\Component\Form\Extension\Core\Type\TextType::class);
```
< br >
### `StringToArrayArgumentProcessRector`
- class: `Rector\Symfony\Rector\New_\StringToArrayArgumentProcessRector`
Changes Process string argument to an array
```diff
use Symfony\Component\Process\Process;
-$process = new Process('ls -l');
+$process = new Process(['ls', '-l']);
```
< br >
### `VarDumperTestTraitMethodArgsRector`
- class: `Rector\Symfony\Rector\VarDumper\VarDumperTestTraitMethodArgsRector`
Adds new `$format` argument in `VarDumperTestTrait->assertDumpEquals()` in Validator in Symfony.
```diff
-$varDumperTestTrait->assertDumpEquals($dump, $data, $mesage = "");
+$varDumperTestTrait->assertDumpEquals($dump, $data, $context = null, $mesage = "");
```
```diff
-$varDumperTestTrait->assertDumpMatchesFormat($dump, $format, $mesage = "");
+$varDumperTestTrait->assertDumpMatchesFormat($dump, $format, $context = null, $mesage = "");
```
< br >
2019-08-05 21:10:47 +00:00
## SymfonyCodeQuality
### `EventListenerToEventSubscriberRector`
- class: `Rector\SymfonyCodeQuality\Rector\Class_\EventListenerToEventSubscriberRector`
Change Symfony Event listener class to Event Subscriber based on configuration in service.yaml file
```diff
< ?php
-class SomeListener
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class SomeEventSubscriber implements EventSubscriberInterface
{
+ /**
+ * @return string[]
+ */
+ public static function getSubscribedEvents(): array
+ {
+ return ['some_event' => 'methodToBeCalled'];
+ }
+
public function methodToBeCalled()
{
}
-}
-
-// in config.yaml
-services:
- SomeListener:
- tags:
- - { name: kernel.event_listener, event: 'some_event', method: 'methodToBeCalled' }
+}
```
< br >
2019-07-06 19:51:36 +00:00
## SymfonyPHPUnit
### `MultipleServiceGetToSetUpMethodRector`
- class: `Rector\SymfonyPHPUnit\Rector\Class_\MultipleServiceGetToSetUpMethodRector`
```diff
use ItemRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class SomeTest extends KernelTestCase
{
+ /**
+ * @var \ItemRepository
+ */
+ private $itemRepository;
+
+ protected function setUp()
+ {
+ parent::setUp();
+ $this->itemRepository = self::$container->get(ItemRepository::class);
+ }
+
public function testOne()
{
- $itemRepository = self::$container->get(ItemRepository::class);
- $itemRepository->doStuff();
+ $this->itemRepository->doStuff();
}
public function testTwo()
{
- $itemRepository = self::$container->get(ItemRepository::class);
- $itemRepository->doAnotherStuff();
+ $this->itemRepository->doAnotherStuff();
}
}
```
< br >
2018-09-28 16:33:35 +00:00
## Twig
### `SimpleFunctionAndFilterRector`
- class: `Rector\Twig\Rector\SimpleFunctionAndFilterRector`
Changes Twig_Function_Method to Twig_SimpleFunction calls in TwigExtension.
```diff
class SomeExtension extends Twig_Extension
{
public function getFunctions()
{
return [
- 'is_mobile' => new Twig_Function_Method($this, 'isMobile'),
+ new Twig_SimpleFunction('is_mobile', [$this, 'isMobile']),
];
}
2019-03-31 12:25:39 +00:00
public function getFilters()
2018-09-28 16:33:35 +00:00
{
return [
- 'is_mobile' => new Twig_Filter_Method($this, 'isMobile'),
+ new Twig_SimpleFilter('is_mobile', [$this, 'isMobile']),
];
}
}
```
2018-11-11 12:22:35 +00:00
< br >
2019-05-19 08:27:38 +00:00
## TypeDeclaration
2019-08-17 13:06:02 +00:00
### `AddArrayParamDocTypeRector`
- class: `Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector`
Adds @param annotation to array parameters inferred from the rest of the code
```diff
class SomeClass
{
/**
* @var int[]
*/
private $values;
+ /**
+ * @param int[] $values
+ */
public function __construct(array $values)
{
$this->values = $values;
}
}
```
< br >
### `AddArrayReturnDocTypeRector`
- class: `Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector`
Adds @return annotation to array parameters inferred from the rest of the code
```diff
class SomeClass
{
/**
* @var int[]
*/
private $values;
+ /**
+ * @return int[]
+ */
public function getValues(): array
{
return $this->values;
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `AddClosureReturnTypeRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
Add known return type to functions
2019-05-19 08:27:38 +00:00
```diff
class SomeClass
{
2019-06-02 07:45:37 +00:00
public function run($meetups)
2019-05-19 08:27:38 +00:00
{
2019-06-02 07:45:37 +00:00
- return array_filter($meetups, function (Meetup $meetup) {
+ return array_filter($meetups, function (Meetup $meetup): bool {
return is_object($meetup);
});
2019-05-19 08:27:38 +00:00
}
}
```
< br >
### `ParamTypeDeclarationRector`
- class: `Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector`
Change @param types to type declarations if not a BC-break
```diff
< ?php
class ParentClass
{
/**
* @param int $number
*/
public function keep($number)
{
}
}
final class ChildClass extends ParentClass
{
/**
* @param int $number
*/
public function keep($number)
{
}
/**
* @param int $number
*/
- public function change($number)
+ public function change(int $number)
{
}
}
```
< br >
2019-08-05 21:10:47 +00:00
### `PropertyTypeDeclarationRector`
- class: `Rector\TypeDeclaration\Rector\Property\PropertyTypeDeclarationRector`
Add missing @var to properties that are missing it
< br >
2019-06-02 07:45:37 +00:00
### `ReturnTypeDeclarationRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
Change @return types and type from static analysis to type declarations if not a BC-break
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
< ?php
2019-05-29 13:40:20 +00:00
class SomeClass
{
2019-06-02 07:45:37 +00:00
/**
* @return int
*/
- public function getCount()
+ public function getCount(): int
2019-05-29 13:40:20 +00:00
{
}
}
2019-02-04 00:32:53 +00:00
```
< br >
2019-05-29 13:40:20 +00:00
---
## General
- [Core ](#core )
## Core
2019-06-02 07:45:37 +00:00
### `ActionInjectionToConstructorInjectionRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Architecture\DependencyInjection\ActionInjectionToConstructorInjectionRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Turns action injection in Controllers to constructor injection
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeController
2018-08-01 20:09:34 +00:00
{
2019-06-02 07:45:37 +00:00
- public function default(ProductRepository $productRepository)
+ /**
+ * @var ProductRepository
+ */
+ private $productRepository;
+ public function __construct(ProductRepository $productRepository)
{
- $products = $productRepository->fetchAll();
+ $this->productRepository = $productRepository;
+ }
2019-02-04 00:32:53 +00:00
+
2019-06-02 07:45:37 +00:00
+ public function default()
+ {
+ $products = $this->productRepository->fetchAll();
}
2018-08-01 20:09:34 +00:00
}
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-06 13:01:53 +00:00
### `AddMethodParentCallRector`
- class: `Rector\Rector\ClassMethod\AddMethodParentCallRector`
Add method parent call, in case new parent method is added
```diff
class SunshineCommand extends ParentClassWithNewConstructor
{
public function __construct()
{
$value = 5;
+
+ parent::__construct();
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `AddReturnTypeDeclarationRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\ClassMethod\AddReturnTypeDeclarationRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Changes defined return typehint of method and class.
2018-08-01 13:34:55 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\ClassMethod\AddReturnTypeDeclarationRector:
SomeClass:
getData: array
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
↓
2018-07-31 19:43:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- public getData();
+ public getData(): array;
2019-05-29 13:40:20 +00:00
}
2018-08-01 20:09:34 +00:00
```
2018-07-31 19:43:39 +00:00
2019-02-04 00:32:53 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `AnnotatedPropertyInjectToConstructorInjectionRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Turns non-private properties with `@annotation` to private properties and constructor injection
2019-02-04 00:32:53 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Architecture\DependencyInjection\AnnotatedPropertyInjectToConstructorInjectionRector:
$annotation: inject
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
/**
* @var SomeService
- * @inject
*/
-public $someService;
+private $someService;
+
+public function __construct(SomeService $someService)
+{
+ $this->someService = $someService;
+}
2019-02-03 23:55:22 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ArgumentAdderRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Argument\ArgumentAdderRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
This Rector adds new default arguments in calls of defined methods and class types.
2018-07-31 19:43:39 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Argument\ArgumentAdderRector:
SomeExampleClass:
someMethod:
-
name: someArgument
default_value: 'true'
type: SomeType
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
$someObject = new SomeExampleClass;
-$someObject->someMethod();
+$someObject->someMethod(true);
2018-05-04 22:30:32 +00:00
```
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Argument\ArgumentAdderRector:
SomeExampleClass:
someMethod:
-
name: someArgument
default_value: 'true'
type: SomeType
2018-08-01 20:09:34 +00:00
```
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
class MyCustomClass extends SomeExampleClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
- public function someMethod()
+ public function someMethod($value = true)
2019-05-29 13:40:20 +00:00
{
}
}
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ArgumentDefaultValueReplacerRector`
2019-03-16 20:31:46 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Argument\ArgumentDefaultValueReplacerRector`
2019-03-16 20:31:46 +00:00
2019-06-02 07:45:37 +00:00
Replaces defined map of arguments in defined methods and their calls.
2019-03-16 20:31:46 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Argument\ArgumentDefaultValueReplacerRector:
SomeExampleClass:
someMethod:
-
-
before: 'SomeClass::OLD_CONSTANT'
after: 'false'
2019-03-16 20:31:46 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
$someObject = new SomeClass;
-$someObject->someMethod(SomeClass::OLD_CONSTANT);
+$someObject->someMethod(false);'
2019-03-16 20:31:46 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ArgumentRemoverRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Argument\ArgumentRemoverRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Removes defined arguments in defined methods and their calls.
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Argument\ArgumentRemoverRector:
ExampleClass:
someMethod:
-
value: 'true'
2018-08-01 20:09:34 +00:00
```
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
$someObject = new SomeClass;
-$someObject->someMethod(true);
+$someObject->someMethod();'
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ChangeConstantVisibilityRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Visibility\ChangeConstantVisibilityRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Change visibility of constant from parent class.
2018-11-11 12:22:35 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Visibility\ChangeConstantVisibilityRector:
ParentObject:
SOME_CONSTANT: protected
2018-11-11 12:22:35 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
class FrameworkClass
2018-10-23 18:58:57 +00:00
{
2019-06-02 07:45:37 +00:00
protected const SOME_CONSTANT = 1;
}
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
class MyClass extends FrameworkClass
{
- public const SOME_CONSTANT = 1;
+ protected const SOME_CONSTANT = 1;
2018-10-21 22:26:45 +00:00
}
2018-10-12 23:15:00 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ChangeMethodVisibilityRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Visibility\ChangeMethodVisibilityRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
Change visibility of method from parent class.
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Visibility\ChangeMethodVisibilityRector:
FrameworkClass:
someMethod: protected
2018-08-01 20:09:34 +00:00
```
↓
2018-07-31 12:50:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
class FrameworkClass
{
protected someMethod()
{
}
}
class MyClass extends FrameworkClass
{
- public someMethod()
+ protected someMethod()
{
}
}
2018-07-31 12:50:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ChangePropertyVisibilityRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Visibility\ChangePropertyVisibilityRector`
2018-07-31 12:50:39 +00:00
2019-06-02 07:45:37 +00:00
Change visibility of property from parent class.
2018-08-01 13:34:55 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Visibility\ChangePropertyVisibilityRector:
FrameworkClass:
someProperty: protected
2018-08-01 20:09:34 +00:00
```
↓
2018-07-31 12:50:39 +00:00
```diff
2019-06-02 07:45:37 +00:00
class FrameworkClass
{
protected $someProperty;
}
class MyClass extends FrameworkClass
{
- public $someProperty;
+ protected $someProperty;
}
2018-08-01 20:09:34 +00:00
```
2018-08-01 13:34:55 +00:00
2019-05-29 13:40:20 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `FluentReplaceRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\MethodBody\FluentReplaceRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Turns fluent interface calls to classic ones.
2019-05-29 13:40:20 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\MethodBody\FluentReplaceRector:
2019-08-17 13:06:02 +00:00
$classesToDefluent:
2019-06-02 07:45:37 +00:00
- SomeExampleClass
2018-07-31 12:50:39 +00:00
```
2018-08-01 20:09:34 +00:00
↓
2018-07-31 19:43:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
$someClass = new SomeClass();
-$someClass->someFunction()
- ->otherFunction();
+$someClass->someFunction();
+$someClass->otherFunction();
2018-08-01 20:09:34 +00:00
```
2018-08-01 13:34:55 +00:00
2019-06-02 07:45:37 +00:00
< br >
### `FunctionToMethodCallRector`
- class: `Rector\Rector\Function_\FunctionToMethodCallRector`
Turns defined function calls to local method calls.
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Function_\FunctionToMethodCallRector:
view:
- this
- render
2018-08-01 20:09:34 +00:00
```
2018-08-01 13:34:55 +00:00
2018-08-01 20:09:34 +00:00
↓
2018-08-01 13:34:55 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
-view("...", []);
+$this->render("...", []);
2018-08-01 20:09:34 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `FunctionToNewRector`
2018-09-28 16:33:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\FuncCall\FunctionToNewRector`
2018-09-28 16:33:35 +00:00
2019-06-02 07:45:37 +00:00
Change configured function calls to new Instance
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-02-04 00:32:53 +00:00
{
2019-06-02 07:45:37 +00:00
public function run()
{
- $array = collection([]);
+ $array = new \Collection([]);
}
2018-08-01 20:09:34 +00:00
}
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
### `FunctionToStaticCallRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Function_\FunctionToStaticCallRector`
2019-02-18 15:51:24 +00:00
2019-06-02 07:45:37 +00:00
Turns defined function call to static method call.
2019-02-18 15:51:24 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Function_\FunctionToStaticCallRector:
view:
- SomeStaticClass
- render
2019-02-18 15:51:24 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
-view("...", []);
+SomeClass::render("...", []);
2019-02-18 15:51:24 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `GetAndSetToMethodCallRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Turns defined `__get` /`__set` to specific method calls.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector:
SomeContainer:
set: addService
2018-08-01 20:09:34 +00:00
```
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
$container = new SomeContainer;
-$container->someService = $someService;
+$container->setService("someService", $someService);
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector:
$typeToMethodCalls:
SomeContainer:
get: getService
2018-11-11 12:22:35 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
$container = new SomeContainer;
-$someService = $container->someService;
+$someService = $container->getService("someService");
2018-11-11 12:22:35 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `InjectAnnotationClassRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Property\InjectAnnotationClassRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
Changes properties with specified annotations class to constructor injection
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Property\InjectAnnotationClassRector:
2019-07-06 19:51:36 +00:00
$annotationClasses:
- JMS\DiExtraBundle\Annotation\Inject
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
↓
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
use JMS\DiExtraBundle\Annotation as DI;
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
class SomeController
{
/**
- * @DI \Inject("entity.manager")
+ * @var EntityManager
*/
private $entityManager;
+
+ public function __construct(EntityManager $entityManager)
+ {
+ $this->entityManager = entityManager;
+ }
}
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `MergeInterfacesRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Interface_\MergeInterfacesRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
Merges old interface to a new one, that already has its methods
2018-05-05 00:04:41 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Interface_\MergeInterfacesRector:
SomeOldInterface: SomeInterface
2018-08-01 20:09:34 +00:00
```
2018-05-05 00:04:41 +00:00
2018-08-01 20:09:34 +00:00
↓
2018-08-01 13:34:55 +00:00
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
-class SomeClass implements SomeInterface, SomeOldInterface
+class SomeClass implements SomeInterface
{
}
2018-05-05 00:04:41 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-05-29 13:40:20 +00:00
### `MethodCallToAnotherMethodCallWithArgumentsRector`
2018-07-31 19:43:39 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector`
2018-07-31 19:43:39 +00:00
2019-08-24 11:08:59 +00:00
Turns old method call with specific types to new one with arguments
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-05-29 13:40:20 +00:00
Rector\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector:
Nette\DI\ServiceDefinition:
setInject:
-
- addTag
-
- inject
2018-12-06 00:13:34 +00:00
```
↓
```diff
2019-05-29 13:40:20 +00:00
$serviceDefinition = new Nette\DI\ServiceDefinition;
-$serviceDefinition->setInject();
+$serviceDefinition->addTag('inject');
2018-12-06 00:13:34 +00:00
```
2019-05-29 13:40:20 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `MoveRepositoryFromParentToConstructorRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Architecture\RepositoryAsService\MoveRepositoryFromParentToConstructorRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Turns parent EntityRepository class to constructor dependency
2019-05-29 13:40:20 +00:00
2018-12-06 00:13:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Architecture\RepositoryAsService\MoveRepositoryFromParentToConstructorRector:
$entityRepositoryClass: Doctrine\ORM\EntityRepository
$entityManagerClass: Doctrine\ORM\EntityManager
2018-08-01 20:09:34 +00:00
```
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
namespace App\Repository;
+use App\Entity\Post;
use Doctrine\ORM\EntityRepository;
-final class PostRepository extends EntityRepository
+final class PostRepository
{
+ /**
+ * @var \Doctrine\ORM\EntityRepository
+ */
+ private $repository;
+ public function __construct(\Doctrine\ORM\EntityManager $entityManager)
+ {
+ $this->repository = $entityManager->getRepository(\App\Entity\Post::class);
+ }
}
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `MultipleClassFileToPsr4ClassesRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Psr4\MultipleClassFileToPsr4ClassesRector`
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
Turns namespaced classes in one file to standalone PSR-4 classes.
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
```diff
+// new file: "app/Exceptions/FirstException.php"
namespace App\Exceptions;
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
use Exception;
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
final class FirstException extends Exception
{
2019-03-09 13:24:30 +00:00
2019-06-02 07:45:37 +00:00
}
+
+// new file: "app/Exceptions/SecondException.php"
+namespace App\Exceptions;
+
+use Exception;
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
final class SecondException extends Exception
{
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
}
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `NewObjectToFactoryCreateRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Architecture\Factory\NewObjectToFactoryCreateRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Replaces creating object instances with "new" keyword with factory method.
2019-02-04 00:32:53 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Architecture\Factory\NewObjectToFactoryCreateRector:
MyClass:
class: MyClassFactory
method: create
2019-02-04 00:32:53 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
+ /**
+ * @var \MyClassFactory
+ */
+ private $myClassFactory;
+
public function example() {
- new MyClass($argument);
+ $this->myClassFactory->create($argument);
}
2019-05-29 13:40:20 +00:00
}
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `NewToStaticCallRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\New_\NewToStaticCallRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Change new Object to static call
2019-02-04 00:32:53 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\New_\NewToStaticCallRector:
Cookie:
-
- Cookie
- create
2019-02-04 00:32:53 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
public function run()
{
- new Cookie($name);
+ Cookie::create($name);
}
2019-05-29 13:40:20 +00:00
}
2019-02-04 00:32:53 +00:00
```
< br >
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
### `NormalToFluentRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\MethodBody\NormalToFluentRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Turns fluent interface calls to classic ones.
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\MethodBody\NormalToFluentRector:
SomeClass:
- someFunction
- otherFunction
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
↓
```diff
2019-06-02 07:45:37 +00:00
$someObject = new SomeClass();
-$someObject->someFunction();
-$someObject->otherFunction();
+$someObject->someFunction()
+ ->otherFunction();
2018-08-01 13:34:55 +00:00
```
2018-07-31 19:43:39 +00:00
2019-05-29 13:40:20 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ParentClassToTraitsRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Class_\ParentClassToTraitsRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Replaces parent class to specific traits
2019-05-29 13:40:20 +00:00
2018-09-28 16:33:35 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Class_\ParentClassToTraitsRector:
Nette\Object:
- Nette\SmartObject
2018-09-28 16:33:35 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
-class SomeClass extends Nette\Object
+class SomeClass
2019-05-29 13:40:20 +00:00
{
2019-06-02 07:45:37 +00:00
+ use Nette\SmartObject;
2019-05-29 13:40:20 +00:00
}
2018-09-28 16:33:35 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ParentTypehintedArgumentRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Typehint\ParentTypehintedArgumentRector`
2019-05-19 08:27:38 +00:00
2019-06-02 07:45:37 +00:00
Changes defined parent class typehints.
```yaml
services:
Rector\Rector\Typehint\ParentTypehintedArgumentRector:
SomeInterface:
read:
$content: string
```
↓
2019-05-19 08:27:38 +00:00
```diff
2019-06-02 07:45:37 +00:00
interface SomeInterface
2019-05-19 08:27:38 +00:00
{
2019-06-02 07:45:37 +00:00
public read(string $content);
}
class SomeClass implements SomeInterface
{
- public read($content);
+ public read(string $content);
2019-05-19 08:27:38 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `PropertyAssignToMethodCallRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Assign\PropertyAssignToMethodCallRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Turns property assign of specific type and property name to method call
2019-02-04 00:32:53 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Assign\PropertyAssignToMethodCallRector:
$oldPropertiesToNewMethodCallsByType:
SomeClass:
oldPropertyName: oldProperty
newMethodName: newMethodCall
2019-02-04 00:32:53 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
-$someObject = new SomeClass;
-$someObject->oldProperty = false;
+$someObject = new SomeClass;
+$someObject->newMethodCall(false);
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `PropertyToMethodRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Property\PropertyToMethodRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Replaces properties assign calls be defined methods.
```yaml
services:
Rector\Rector\Property\PropertyToMethodRector:
$perClassPropertyToMethods:
SomeObject:
property:
get: getProperty
set: setProperty
```
↓
2019-02-04 00:32:53 +00:00
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$result = $object->property;
-$object->property = $value;
+$result = $object->getProperty();
+$object->setProperty($value);
```
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
```yaml
services:
Rector\Rector\Property\PropertyToMethodRector:
$perClassPropertyToMethods:
SomeObject:
property:
get:
method: getConfig
arguments:
- someArg
```
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
↓
```diff
-$result = $object->property;
+$result = $object->getProperty('someArg');
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `PseudoNamespaceToNamespaceRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Namespace_\PseudoNamespaceToNamespaceRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Replaces defined Pseudo_Namespaces by Namespace\Ones.
```yaml
services:
Rector\Rector\Namespace_\PseudoNamespaceToNamespaceRector:
-
Some_: { }
```
↓
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$someService = new Some_Object;
+$someService = new Some\Object;
```
```yaml
services:
Rector\Rector\Namespace_\PseudoNamespaceToNamespaceRector:
-
Some_:
- Some_Class_To_Keep
```
↓
```diff
-/** @var Some_Object $someService */
-$someService = new Some_Object;
+/** @var Some\Object $someService */
+$someService = new Some\Object;
$someClassToKeep = new Some_Class_To_Keep;
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `RemoveInterfacesRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Interface_\RemoveInterfacesRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Removes interfaces usage from class.
```yaml
services:
Rector\Rector\Interface_\RemoveInterfacesRector:
- SomeInterface
```
↓
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
-class SomeClass implements SomeInterface
+class SomeClass
2019-02-04 00:32:53 +00:00
{
2019-06-02 07:45:37 +00:00
}
```
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
< br >
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
### `RemoveTraitRector`
- class: `Rector\Rector\ClassLike\RemoveTraitRector`
Remove specific traits from code
```diff
class SomeClass
{
- use SomeTrait;
2019-02-04 00:32:53 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `RenameAnnotationRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Annotation\RenameAnnotationRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Turns defined annotations above properties and methods to their new values.
2019-05-29 13:40:20 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Annotation\RenameAnnotationRector:
PHPUnit\Framework\TestCase:
test: scenario
2019-05-29 13:40:20 +00:00
```
↓
2019-02-04 00:32:53 +00:00
```diff
2019-06-02 07:45:37 +00:00
class SomeTest extends PHPUnit\Framework\TestCase
{
- /**
- * @test
+ /**
+ * @scenario
*/
public function someMethod()
{
}
}
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `RenameClassConstantRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Constant\RenameClassConstantRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Replaces defined class constants in their calls.
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Constant\RenameClassConstantRector:
SomeClass:
OLD_CONSTANT: NEW_CONSTANT
OTHER_OLD_CONSTANT: 'DifferentClass::NEW_CONSTANT'
2018-08-01 20:09:34 +00:00
```
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$value = SomeClass::OLD_CONSTANT;
-$value = SomeClass::OTHER_OLD_CONSTANT;
+$value = SomeClass::NEW_CONSTANT;
+$value = DifferentClass::NEW_CONSTANT;
2018-09-28 16:33:35 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `RenameClassConstantsUseToStringsRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Constant\RenameClassConstantsUseToStringsRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Replaces constant by value
2019-05-29 13:40:20 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Constant\RenameClassConstantsUseToStringsRector:
Nette\Configurator:
DEVELOPMENT: development
PRODUCTION: production
2019-05-29 13:40:20 +00:00
```
↓
2018-11-11 12:22:35 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$value === Nette\Configurator::DEVELOPMENT
+$value === "development"
2019-05-29 13:40:20 +00:00
```
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
< br >
### `RenameClassRector`
- class: `Rector\Rector\Class_\RenameClassRector`
Replaces defined classes by new ones.
2019-05-29 13:40:20 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Class_\RenameClassRector:
App\SomeOldClass: App\SomeNewClass
2019-05-29 13:40:20 +00:00
```
2018-11-11 12:22:35 +00:00
2019-05-29 13:40:20 +00:00
↓
```diff
2019-06-02 07:45:37 +00:00
namespace App;
-use SomeOldClass;
+use SomeNewClass;
-function someFunction(SomeOldClass $someOldClass): SomeOldClass
+function someFunction(SomeNewClass $someOldClass): SomeNewClass
{
- if ($someOldClass instanceof SomeOldClass) {
- return new SomeOldClass;
+ if ($someOldClass instanceof SomeNewClass) {
+ return new SomeNewClass;
}
}
2018-11-11 12:22:35 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `RenameFunctionRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Function_\RenameFunctionRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Turns defined function call new one.
2019-05-29 13:40:20 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Function_\RenameFunctionRector:
view: Laravel\Templating\render
2019-05-29 13:40:20 +00:00
```
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
-view("...", []);
+Laravel\Templating\render("...", []);
2019-05-29 13:40:20 +00:00
```
2019-06-02 07:45:37 +00:00
< br >
### `RenameMethodCallRector`
- class: `Rector\Rector\MethodCall\RenameMethodCallRector`
Turns method call names to new ones.
2019-05-29 13:40:20 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\MethodCall\RenameMethodCallRector:
2019-05-29 13:40:20 +00:00
SomeExampleClass:
2019-06-02 07:45:37 +00:00
oldMethod: newMethod
2019-05-29 13:40:20 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
$someObject = new SomeExampleClass;
-$someObject->oldMethod();
+$someObject->newMethod();
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-10-23 18:58:57 +00:00
2019-06-02 07:45:37 +00:00
### `RenameMethodRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\MethodCall\RenameMethodRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Turns method names to new ones.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\MethodCall\RenameMethodRector:
2019-05-29 13:40:20 +00:00
SomeExampleClass:
2019-06-02 07:45:37 +00:00
oldMethod: newMethod
2018-08-01 20:09:34 +00:00
```
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
$someObject = new SomeExampleClass;
-$someObject->oldMethod();
+$someObject->newMethod();
2019-05-29 13:40:20 +00:00
```
2018-05-04 22:30:32 +00:00
2019-05-29 13:40:20 +00:00
< br >
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
### `RenameNamespaceRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Namespace_\RenameNamespaceRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Replaces old namespace by new one.
2019-05-29 13:40:20 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\Namespace_\RenameNamespaceRector:
$oldToNewNamespaces:
SomeOldNamespace: SomeNewNamespace
2019-05-29 13:40:20 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
-$someObject = new SomeOldNamespace\SomeClass;
+$someObject = new SomeNewNamespace\SomeClass;
2019-05-29 13:40:20 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `RenamePropertyRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Property\RenamePropertyRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Replaces defined old properties by new ones.
```yaml
services:
Rector\Rector\Property\RenamePropertyRector:
$oldToNewPropertyByTypes:
SomeClass:
someOldProperty: someNewProperty
```
↓
2019-05-29 13:40:20 +00:00
```diff
2019-06-02 07:45:37 +00:00
-$someObject->someOldProperty;
+$someObject->someNewProperty;
2018-07-31 12:50:39 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `RenameStaticMethodRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\MethodCall\RenameStaticMethodRector`
2019-05-29 13:40:20 +00:00
2019-06-02 07:45:37 +00:00
Turns method names to new ones.
2019-03-16 20:31:46 +00:00
2019-05-29 13:40:20 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\MethodCall\RenameStaticMethodRector:
SomeClass:
oldMethod:
- AnotherExampleClass
- newStaticMethod
2019-05-29 13:40:20 +00:00
```
2019-03-16 20:31:46 +00:00
2019-05-29 13:40:20 +00:00
↓
2019-03-16 20:31:46 +00:00
```diff
2019-06-02 07:45:37 +00:00
-SomeClass::oldStaticMethod();
+AnotherExampleClass::newStaticMethod();
```
```yaml
services:
Rector\Rector\MethodCall\RenameStaticMethodRector:
$oldToNewMethodByClasses:
SomeClass:
oldMethod: newStaticMethod
```
↓
```diff
-SomeClass::oldStaticMethod();
+SomeClass::newStaticMethod();
2019-03-16 20:31:46 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `ReplaceParentRepositoryCallsByRepositoryPropertyRector`
2019-03-16 20:31:46 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Architecture\RepositoryAsService\ReplaceParentRepositoryCallsByRepositoryPropertyRector`
2019-03-16 20:31:46 +00:00
2019-06-02 07:45:37 +00:00
Handles method calls in child of Doctrine EntityRepository and moves them to "$this->repository" property.
2019-03-16 20:31:46 +00:00
```diff
2019-06-02 07:45:37 +00:00
< ?php
use Doctrine\ORM\EntityRepository;
class SomeRepository extends EntityRepository
2019-03-16 20:31:46 +00:00
{
2019-06-02 07:45:37 +00:00
public function someMethod()
{
- return $this->findAll();
+ return $this->repository->findAll();
}
2019-03-16 20:31:46 +00:00
}
```
< br >
2019-06-02 07:45:37 +00:00
### `ReplaceVariableByPropertyFetchRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Architecture\DependencyInjection\ReplaceVariableByPropertyFetchRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Turns variable in controller action to property fetch, as follow up to action injection variable to property change.
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
```diff
final class SomeController
{
/**
* @var ProductRepository
*/
private $productRepository;
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
public function __construct(ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
}
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
public function default()
{
- $products = $productRepository->fetchAll();
+ $products = $this->productRepository->fetchAll();
}
}
2018-08-01 20:09:34 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ReturnThisRemoveRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\MethodBody\ReturnThisRemoveRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
Removes "return $this;" from *fluent interfaces* for specified classes.
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\MethodBody\ReturnThisRemoveRector:
-
- SomeExampleClass
2018-08-01 20:09:34 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
class SomeClass
{
public function someFunction()
{
- return $this;
}
public function otherFunction()
{
- return $this;
}
}
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-08-05 21:10:47 +00:00
### `ServiceGetterToConstructorInjectionRector`
- class: `Rector\Rector\MethodCall\ServiceGetterToConstructorInjectionRector`
Get service call to constructor injection
```yaml
services:
Rector\Rector\MethodCall\ServiceGetterToConstructorInjectionRector:
$methodNamesByTypesToServiceTypes:
FirstService:
getAnotherService: AnotherService
```
↓
```diff
final class SomeClass
{
/**
* @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)
{
+ $this->firstService = $firstService;
$this->anotherService = $anotherService;
}
- public function getAnotherService(): AnotherService
+ public function run()
{
- return $this->anotherService;
+ $anotherService = $this->anotherService;
+ $anotherService->run();
}
}
```
< br >
2019-06-02 07:45:37 +00:00
### `ServiceLocatorToDIRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\Architecture\RepositoryAsService\ServiceLocatorToDIRector`
2018-08-01 20:09:34 +00:00
2019-06-02 07:45:37 +00:00
Turns "$this->getRepository()" in Symfony Controller to constructor injection and private property access.
2018-08-01 20:09:34 +00:00
```diff
2019-06-02 07:45:37 +00:00
class ProductController extends Controller
{
+ /**
+ * @var ProductRepository
+ */
+ private $productRepository;
+
+ public function __construct(ProductRepository $productRepository)
+ {
+ $this->productRepository = $productRepository;
+ }
+
public function someAction()
{
$entityManager = $this->getDoctrine()->getManager();
- $entityManager->getRepository('SomethingBundle:Product')->findSomething(...);
+ $this->productRepository->findSomething(...);
}
}
2018-08-01 20:09:34 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
### `StaticCallToFunctionRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\StaticCall\StaticCallToFunctionRector`
2018-11-11 12:22:35 +00:00
2019-06-02 07:45:37 +00:00
Turns static call to function call.
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\StaticCall\StaticCallToFunctionRector:
$staticCallToFunction:
OldClass:
oldMethod: new_function
2018-08-01 20:09:34 +00:00
```
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
-OldClass::oldMethod("args");
+new_function("args");
2018-07-31 06:38:48 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `StringToClassConstantRector`
2018-07-31 19:43:39 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\String_\StringToClassConstantRector`
2018-05-04 22:30:32 +00:00
2019-06-02 07:45:37 +00:00
Changes strings to specific constants
2018-05-04 22:30:32 +00:00
2018-08-01 20:09:34 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\String_\StringToClassConstantRector:
compiler.post_dump:
- Yet\AnotherClass
- CONSTANT
2018-08-01 20:09:34 +00:00
```
↓
2018-05-04 22:30:32 +00:00
```diff
2019-06-02 07:45:37 +00:00
final class SomeSubscriber
2018-08-01 13:34:55 +00:00
{
2019-06-02 07:45:37 +00:00
public static function getSubscribedEvents()
{
- return ['compiler.post_dump' => 'compile'];
+ return [\Yet\AnotherClass::CONSTANT => 'compile'];
}
2018-08-01 13:34:55 +00:00
}
2018-05-04 22:30:32 +00:00
```
2018-11-11 12:22:35 +00:00
< br >
2019-06-02 07:45:37 +00:00
### `ToStringToMethodCallRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\MagicDisclosure\ToStringToMethodCallRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Turns defined code uses of "__toString()" method to specific method calls.
2019-02-04 00:32:53 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\MagicDisclosure\ToStringToMethodCallRector:
SomeObject: getPath
2019-02-04 00:32:53 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
$someValue = new SomeObject;
-$result = (string) $someValue;
-$result = $someValue->__toString();
+$result = $someValue->getPath();
+$result = $someValue->getPath();
2019-02-04 00:32:53 +00:00
```
< br >
2019-05-29 13:40:20 +00:00
### `UnsetAndIssetToMethodCallRector`
2019-02-04 00:32:53 +00:00
2019-05-29 13:40:20 +00:00
- class: `Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector`
2019-02-04 00:32:53 +00:00
2019-05-29 13:40:20 +00:00
Turns defined `__isset` /`__unset` calls to specific method calls.
2019-02-04 00:32:53 +00:00
```yaml
services:
2019-05-29 13:40:20 +00:00
Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector:
SomeContainer:
isset: hasService
2019-02-04 00:32:53 +00:00
```
↓
```diff
2019-05-29 13:40:20 +00:00
$container = new SomeContainer;
-isset($container["someKey"]);
+$container->hasService("someKey");
2019-02-04 00:32:53 +00:00
```
```yaml
services:
2019-05-29 13:40:20 +00:00
Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector:
SomeContainer:
unset: removeService
2019-02-04 00:32:53 +00:00
```
↓
```diff
2019-05-29 13:40:20 +00:00
$container = new SomeContainer;
-unset($container["someKey"]);
+$container->removeService("someKey");
2019-02-04 00:32:53 +00:00
```
< br >
2019-06-02 07:45:37 +00:00
### `WrapReturnRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
- class: `Rector\Rector\ClassMethod\WrapReturnRector`
2019-02-04 00:32:53 +00:00
2019-06-02 07:45:37 +00:00
Wrap return value of specific method
2019-02-04 00:32:53 +00:00
```yaml
services:
2019-06-02 07:45:37 +00:00
Rector\Rector\ClassMethod\WrapReturnRector:
SomeClass:
getItem: array
2019-02-04 00:32:53 +00:00
```
↓
```diff
2019-06-02 07:45:37 +00:00
final class SomeClass
{
public function getItem()
{
- return 1;
+ return [1];
}
}
2019-02-04 00:32:53 +00:00
```
< br >