rector/docs/rector_rules_overview.md

16404 lines
438 KiB
Markdown
Raw Normal View History

# All 596 Rectors Overview
2018-04-29 09:03:47 +00:00
2018-08-01 20:09:34 +00:00
- [Projects](#projects)
---
2018-08-01 20:09:34 +00:00
## Projects
- [Architecture](#architecture) (2)
- [Autodiscovery](#autodiscovery) (4)
- [CakePHP](#cakephp) (6)
- [CodeQuality](#codequality) (60)
- [CodingStyle](#codingstyle) (33)
- [DeadCode](#deadcode) (41)
- [Decouple](#decouple) (1)
- [Defluent](#defluent) (8)
- [Doctrine](#doctrine) (17)
- [DoctrineCodeQuality](#doctrinecodequality) (9)
- [DoctrineGedmoToKnplabs](#doctrinegedmotoknplabs) (7)
- [DowngradePhp71](#downgradephp71) (3)
- [DowngradePhp72](#downgradephp72) (2)
- [DowngradePhp73](#downgradephp73) (1)
- [DowngradePhp74](#downgradephp74) (7)
- [DowngradePhp80](#downgradephp80) (6)
- [DynamicTypeAnalysis](#dynamictypeanalysis) (3)
- [FileSystemRector](#filesystemrector) (1)
- [Generic](#generic) (36)
- [JMS](#jms) (2)
- [Laravel](#laravel) (3)
2020-07-14 21:23:06 +00:00
- [Legacy](#legacy) (4)
- [MagicDisclosure](#magicdisclosure) (3)
- [MockeryToProphecy](#mockerytoprophecy) (2)
2020-05-14 10:26:57 +00:00
- [MockistaToMockery](#mockistatomockery) (2)
- [MysqlToMysqli](#mysqltomysqli) (4)
- [Naming](#naming) (11)
2020-10-09 20:01:37 +00:00
- [Nette](#nette) (17)
- [NetteCodeQuality](#nettecodequality) (6)
2020-05-29 10:41:25 +00:00
- [NetteKdyby](#nettekdyby) (4)
- [NetteTesterToPHPUnit](#nettetestertophpunit) (3)
- [NetteToSymfony](#nettetosymfony) (9)
- [NetteUtilsCodeQuality](#netteutilscodequality) (1)
- [Order](#order) (9)
- [PHPOffice](#phpoffice) (14)
- [PHPStan](#phpstan) (3)
- [PHPUnit](#phpunit) (38)
- [PHPUnitSymfony](#phpunitsymfony) (1)
- [PSR4](#psr4) (2)
- [Performance](#performance) (1)
- [Phalcon](#phalcon) (4)
- [Php52](#php52) (2)
2020-05-31 15:45:51 +00:00
- [Php53](#php53) (4)
- [Php54](#php54) (2)
- [Php55](#php55) (2)
- [Php56](#php56) (2)
- [Php70](#php70) (19)
- [Php71](#php71) (9)
- [Php72](#php72) (11)
- [Php73](#php73) (10)
- [Php74](#php74) (15)
- [Php80](#php80) (12)
- [PhpDeglobalize](#phpdeglobalize) (1)
- [PhpSpecToPHPUnit](#phpspectophpunit) (7)
- [Polyfill](#polyfill) (2)
2020-05-29 10:41:25 +00:00
- [Privatization](#privatization) (7)
- [RectorGenerator](#rectorgenerator) (1)
- [RemovingStatic](#removingstatic) (6)
- [Renaming](#renaming) (10)
- [Restoration](#restoration) (8)
- [SOLID](#solid) (13)
- [Sensio](#sensio) (3)
- [StrictCodeQuality](#strictcodequality) (1)
- [Symfony](#symfony) (34)
- [SymfonyCodeQuality](#symfonycodequality) (1)
- [SymfonyPHPUnit](#symfonyphpunit) (1)
- [SymfonyPhpConfig](#symfonyphpconfig) (2)
- [Transform](#transform) (11)
- [Twig](#twig) (1)
- [TypeDeclaration](#typedeclaration) (9)
2018-09-28 16:33:35 +00:00
2019-08-05 21:10:47 +00:00
## Architecture
2020-02-13 13:42:40 +00:00
### `ReplaceParentRepositoryCallsByRepositoryPropertyRector`
- class: [`Rector\Architecture\Rector\MethodCall\ReplaceParentRepositoryCallsByRepositoryPropertyRector`](/rules/architecture/src/Rector/MethodCall/ReplaceParentRepositoryCallsByRepositoryPropertyRector.php)
2020-02-13 13:42:40 +00:00
2020-07-07 10:15:52 +00:00
Handles method calls in child of Doctrine EntityRepository and moves them to `$this->repository` property.
2020-02-13 13:42:40 +00:00
```diff
<?php
use Doctrine\ORM\EntityRepository;
class SomeRepository extends EntityRepository
{
public function someMethod()
{
- return $this->findAll();
+ return $this->repository->findAll();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-02-13 13:42:40 +00:00
### `ServiceLocatorToDIRector`
- class: [`Rector\Architecture\Rector\MethodCall\ServiceLocatorToDIRector`](/rules/architecture/src/Rector/MethodCall/ServiceLocatorToDIRector.php)
2020-02-13 13:42:40 +00:00
2020-07-07 10:15:52 +00:00
Turns `$this->getRepository()` in Symfony Controller to constructor injection and private property access.
2020-02-13 13:42:40 +00:00
```diff
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(...);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-02-13 13:42:40 +00:00
2019-09-15 18:28:10 +00:00
## Autodiscovery
### `MoveEntitiesToEntityDirectoryRector`
- class: [`Rector\Autodiscovery\Rector\FileSystem\MoveEntitiesToEntityDirectoryRector`](/rules/autodiscovery/src/Rector/FileSystem/MoveEntitiesToEntityDirectoryRector.php)
2019-09-15 18:28:10 +00:00
Move entities to Entity namespace
```diff
-// file: app/Controller/Product.php
+// file: app/Entity/Product.php
-namespace App\Controller;
+namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Product
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-15 18:28:10 +00:00
### `MoveInterfacesToContractNamespaceDirectoryRector`
- class: [`Rector\Autodiscovery\Rector\FileSystem\MoveInterfacesToContractNamespaceDirectoryRector`](/rules/autodiscovery/src/Rector/FileSystem/MoveInterfacesToContractNamespaceDirectoryRector.php)
2019-09-15 18:28:10 +00:00
Move interface to "Contract" namespace
```diff
-// file: app/Exception/Rule.php
+// file: app/Contract/Rule.php
-namespace App\Exception;
+namespace App\Contract;
interface Rule
{
}
2019-09-15 18:28:10 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-15 18:28:10 +00:00
### `MoveServicesBySuffixToDirectoryRector`
- class: [`Rector\Autodiscovery\Rector\FileSystem\MoveServicesBySuffixToDirectoryRector`](/rules/autodiscovery/src/Rector/FileSystem/MoveServicesBySuffixToDirectoryRector.php)
2019-09-15 18:28:10 +00:00
Move classes by their suffix to their own group/directory
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
2020-07-24 11:46:57 +00:00
use Rector\Autodiscovery\Rector\FileSystem\MoveServicesBySuffixToDirectoryRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
2020-07-24 11:46:57 +00:00
$services->set(MoveServicesBySuffixToDirectoryRector::class)
->call('configure', [[
MoveServicesBySuffixToDirectoryRector::GROUP_NAMES_BY_SUFFIX => ['Repository'],
]]);
2020-07-24 11:46:57 +00:00
};
2019-09-15 18:28:10 +00:00
```
```diff
-// file: app/Entity/ProductRepository.php
+// file: app/Repository/ProductRepository.php
-namespace App/Entity;
+namespace App/Repository;
class ProductRepository
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-15 18:28:10 +00:00
2019-12-26 10:21:09 +00:00
### `MoveValueObjectsToValueObjectDirectoryRector`
- class: [`Rector\Autodiscovery\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector`](/rules/autodiscovery/src/Rector/FileSystem/MoveValueObjectsToValueObjectDirectoryRector.php)
2019-12-26 10:21:09 +00:00
Move value object to ValueObject namespace/directory
```php
<?php
declare(strict_types=1);
use Rector\Autodiscovery\Rector\FileSystem\MoveValueObjectsToValueObjectDirectoryRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(MoveValueObjectsToValueObjectDirectoryRector::class)
->call('configure', [[
2020-10-07 14:20:53 +00:00
MoveValueObjectsToValueObjectDirectoryRector::TYPES => ['ValueObjectInterfaceClassName'],
MoveValueObjectsToValueObjectDirectoryRector::SUFFIXES => ['Search'],
MoveValueObjectsToValueObjectDirectoryRector::ENABLE_VALUE_OBJECT_GUESSING => true,
]]);
};
```
```diff
-// app/Exception/Name.php
+// app/ValueObject/Name.php
class Name
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-26 10:21:09 +00:00
## CakePHP
2018-09-28 16:33:35 +00:00
### `AppUsesStaticCallToUseStatementRector`
- class: [`Rector\CakePHP\Rector\Namespace_\AppUsesStaticCallToUseStatementRector`](/rules/cakephp/src/Rector/Namespace_/AppUsesStaticCallToUseStatementRector.php)
- [test fixtures](/rules/cakephp/tests/Rector/Namespace_/AppUsesStaticCallToUseStatementRector/Fixture)
2020-06-21 14:49:47 +00:00
Change `App::uses()` to use imports
```diff
-App::uses('NotificationListener', 'Event');
+use Event\NotificationListener;
CakeEventManager::instance()->attach(new NotificationListener());
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ArrayToFluentCallRector`
- class: [`Rector\CakePHP\Rector\MethodCall\ArrayToFluentCallRector`](/rules/cakephp/src/Rector/MethodCall/ArrayToFluentCallRector.php)
- [test fixtures](/rules/cakephp/tests/Rector/MethodCall/ArrayToFluentCallRector/Fixture)
Moves array options to fluent setter method calls.
```php
<?php
declare(strict_types=1);
use Rector\CakePHP\Rector\MethodCall\ArrayToFluentCallRector;
use Rector\CakePHP\ValueObject\ArrayToFluentCall;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ArrayToFluentCallRector::class)
->call('configure', [[
2020-10-07 14:20:53 +00:00
ArrayToFluentCallRector::ARRAYS_TO_FLUENT_CALLS => inline_value_objects(
[new ArrayToFluentCall('ArticlesTable', [
'foreignKey' => 'setForeignKey',
'propertyName' => 'setProperty',
])]
),
]]);
};
```
```diff
use Cake\ORM\Table;
final class ArticlesTable extends Table
{
public function initialize(array $config)
{
- $this->belongsTo('Authors', [
- 'foreignKey' => 'author_id',
- 'propertyName' => 'person'
- ]);
+ $this->belongsTo('Authors')
+ ->setForeignKey('author_id')
+ ->setProperty('person');
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ChangeSnakedFixtureNameToPascalRector`
- class: [`Rector\CakePHP\Rector\Property\ChangeSnakedFixtureNameToPascalRector`](/rules/cakephp/src/Rector/Property/ChangeSnakedFixtureNameToPascalRector.php)
Changes `$fixtues` style from snake_case to PascalCase.
```diff
class SomeTest
{
protected $fixtures = [
- 'app.posts',
- 'app.users',
- 'some_plugin.posts/special_posts',
+ 'app.Posts',
+ 'app.Users',
+ 'some_plugin.Posts/SpecialPosts',
];
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ImplicitShortClassNameUseStatementRector`
- class: [`Rector\CakePHP\Rector\FileWithoutNamespace\ImplicitShortClassNameUseStatementRector`](/rules/cakephp/src/Rector/FileWithoutNamespace/ImplicitShortClassNameUseStatementRector.php)
- [test fixtures](/rules/cakephp/tests/Rector/FileWithoutNamespace/ImplicitShortClassNameUseStatementRector/Fixture)
Collect implicit class names and add imports
```diff
use App\Foo\Plugin;
+use Cake\TestSuite\Fixture\TestFixture;
class LocationsFixture extends TestFixture implements Plugin
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-09-28 16:33:35 +00:00
### `ModalToGetSetRector`
- class: [`Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector`](/rules/cakephp/src/Rector/MethodCall/ModalToGetSetRector.php)
- [test fixtures](/rules/cakephp/tests/Rector/MethodCall/ModalToGetSetRector/Fixture)
2018-09-28 16:33:35 +00:00
Changes combined set/get `value()` to specific `getValue()` or `setValue(x)`.
```php
<?php
declare(strict_types=1);
use Rector\CakePHP\Rector\MethodCall\ModalToGetSetRector;
use Rector\CakePHP\ValueObject\ModalToGetSet;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ModalToGetSetRector::class)
->call('configure', [[
ModalToGetSetRector::UNPREFIXED_METHODS_TO_GET_SET => inline_value_objects(
[new ModalToGetSet('InstanceConfigTrait', 'config', 'getConfig', 'setConfig', 1, null)]
),
]]);
};
```
2018-09-28 16:33:35 +00:00
```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
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-02 08:04:14 +00:00
### `RenameMethodCallBasedOnParameterRector`
- class: [`Rector\CakePHP\Rector\MethodCall\RenameMethodCallBasedOnParameterRector`](/rules/cakephp/src/Rector/MethodCall/RenameMethodCallBasedOnParameterRector.php)
- [test fixtures](/rules/cakephp/tests/Rector/MethodCall/RenameMethodCallBasedOnParameterRector/Fixture)
2019-10-02 08:04:14 +00:00
Changes method calls based on matching the first parameter value.
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
2020-07-24 11:46:57 +00:00
use Rector\CakePHP\Rector\MethodCall\RenameMethodCallBasedOnParameterRector;
use Rector\CakePHP\ValueObject\RenameMethodCallBasedOnParameter;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
2020-07-24 11:46:57 +00:00
$services->set(RenameMethodCallBasedOnParameterRector::class)
->call('configure', [[
RenameMethodCallBasedOnParameterRector::CALLS_WITH_PARAM_RENAMES => inline_value_objects(
[new RenameMethodCallBasedOnParameter(
'getParam',
'paging',
'getAttribute',
'ServerRequest'
2020-10-07 14:20:53 +00:00
), new RenameMethodCallBasedOnParameter('withParam', 'paging', 'withAttribute', 'ServerRequest')]
),
]]);
2020-07-24 11:46:57 +00:00
};
2019-10-02 08:04:14 +00:00
```
```diff
$object = new ServerRequest();
-$config = $object->getParam('paging');
-$object = $object->withParam('paging', ['a value']);
+$config = $object->getAttribute('paging');
+$object = $object->withAttribute('paging', ['a value']);
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-02 08:04:14 +00:00
2019-05-29 13:40:20 +00:00
## CodeQuality
2019-03-09 13:24:30 +00:00
2020-01-03 18:20:13 +00:00
### `AbsolutizeRequireAndIncludePathRector`
- class: [`Rector\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector`](/rules/code-quality/src/Rector/Include_/AbsolutizeRequireAndIncludePathRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Include_/AbsolutizeRequireAndIncludePathRector/Fixture)
2020-01-03 18:20:13 +00:00
include/require to absolute path. This Rector might introduce backwards incompatible code, when the include/require beeing changed depends on the current working directory.
```diff
class SomeClass
{
public function run()
{
- require 'autoload.php';
+ require __DIR__ . '/autoload.php';
require $variable;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-03 18:20:13 +00:00
2019-11-06 23:52:19 +00:00
### `AddPregQuoteDelimiterRector`
- class: [`Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector`](/rules/code-quality/src/Rector/FuncCall/AddPregQuoteDelimiterRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/AddPregQuoteDelimiterRector/Fixture)
2019-11-06 23:52:19 +00:00
2020-06-16 14:39:45 +00:00
Add `preg_quote` delimiter when missing
2019-11-06 23:52:19 +00:00
```diff
-'#' . preg_quote('name') . '#';
+'#' . preg_quote('name', '#') . '#';
2019-11-06 23:52:19 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-11-06 23:52:19 +00:00
### `AndAssignsToSeparateLinesRector`
2019-03-09 13:24:30 +00:00
- class: [`Rector\CodeQuality\Rector\LogicalAnd\AndAssignsToSeparateLinesRector`](/rules/code-quality/src/Rector/LogicalAnd/AndAssignsToSeparateLinesRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/LogicalAnd/AndAssignsToSeparateLinesRector/Fixture)
2019-05-29 13:40:20 +00:00
Split 2 assigns ands to separate line
2019-03-09 13:24:30 +00:00
```diff
class SomeClass
{
public function run()
2019-03-09 13:24:30 +00:00
{
$tokens = [];
- $token = 4 and $tokens[] = $token;
+ $token = 4;
+ $tokens[] = $token;
2019-03-09 13:24:30 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-09 13:24:30 +00:00
2020-01-03 18:20:13 +00:00
### `ArrayKeyExistsTernaryThenValueToCoalescingRector`
- class: [`Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector`](/rules/code-quality/src/Rector/Ternary/ArrayKeyExistsTernaryThenValueToCoalescingRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Ternary/ArrayKeyExistsTernaryThenValueToCoalescingRector/Fixture)
2020-01-03 18:20:13 +00:00
2020-06-16 14:39:45 +00:00
Change `array_key_exists()` ternary to coalesing
2020-01-03 18:20:13 +00:00
```diff
class SomeClass
{
public function run($values, $keyToMatch)
{
- $result = array_key_exists($keyToMatch, $values) ? $values[$keyToMatch] : null;
+ $result = $values[$keyToMatch] ?? null;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-03 18:20:13 +00:00
2020-03-28 13:29:19 +00:00
### `ArrayKeysAndInArrayToArrayKeyExistsRector`
- class: [`Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector`](/rules/code-quality/src/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector/Fixture)
2020-06-16 14:39:45 +00:00
Replace `array_keys()` and `in_array()` to `array_key_exists()`
```diff
class SomeClass
{
public function run($packageName, $values)
{
- $keys = array_keys($values);
- return in_array($packageName, $keys, true);
+ return array_key_exists($packageName, $values);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-11-06 23:52:19 +00:00
### `ArrayMergeOfNonArraysToSimpleArrayRector`
- class: [`Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector`](/rules/code-quality/src/Rector/FuncCall/ArrayMergeOfNonArraysToSimpleArrayRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/ArrayMergeOfNonArraysToSimpleArrayRector/Fixture)
2019-11-06 23:52:19 +00:00
2020-06-16 14:39:45 +00:00
Change `array_merge` of non arrays to array directly
2019-11-06 23:52:19 +00:00
```diff
class SomeClass
{
public function go()
{
$value = 5;
$value2 = 10;
- return array_merge([$value], [$value2]);
+ return [$value, $value2];
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-11-06 23:52:19 +00:00
### `ArrayThisCallToThisMethodCallRector`
- class: [`Rector\CodeQuality\Rector\Array_\ArrayThisCallToThisMethodCallRector`](/rules/code-quality/src/Rector/Array_/ArrayThisCallToThisMethodCallRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Array_/ArrayThisCallToThisMethodCallRector/Fixture)
2020-06-21 14:49:47 +00:00
Change `[$this, someMethod]` without any args to `$this->someMethod()`
```diff
class SomeClass
{
public function run()
{
- $values = [$this, 'giveMeMore'];
+ $values = $this->giveMeMore();
}
public function giveMeMore()
{
return 'more';
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `BooleanNotIdenticalToNotIdenticalRector`
- class: [`Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector`](/rules/code-quality/src/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector/Fixture)
Negated identical boolean compare to not identical compare (does not apply to non-bool values)
```diff
2019-05-29 13:40:20 +00:00
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function run()
{
$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
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `CallableThisArrayToAnonymousFunctionRector`
2018-12-31 11:50:32 +00:00
- class: [`Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector`](/rules/code-quality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture)
2018-12-31 11:50:32 +00:00
Convert [$this, "method"] to proper anonymous function
2018-12-31 11:50:32 +00:00
```diff
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
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 11:50:32 +00:00
### `ChangeArrayPushToArrayAssignRector`
- class: [`Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector`](/rules/code-quality/src/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/ChangeArrayPushToArrayAssignRector/Fixture)
2020-06-16 14:39:45 +00:00
Change `array_push()` to direct variable assign
```diff
class SomeClass
{
public function run()
{
$items = [];
- array_push($items, $item);
+ $items[] = $item;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-15 20:40:44 +00:00
### `CombineIfRector`
- class: [`Rector\CodeQuality\Rector\If_\CombineIfRector`](/rules/code-quality/src/Rector/If_/CombineIfRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/If_/CombineIfRector/Fixture)
2020-01-15 20:40:44 +00:00
Merges nested if statements
```diff
class SomeClass
{
2020-01-15 20:40:44 +00:00
public function run()
{
- if ($cond1) {
- if ($cond2) {
- return 'foo';
- }
+ if ($cond1 && $cond2) {
+ return 'foo';
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-15 20:40:44 +00:00
### `CombinedAssignRector`
2018-10-21 22:26:45 +00:00
- class: [`Rector\CodeQuality\Rector\Assign\CombinedAssignRector`](/rules/code-quality/src/Rector/Assign/CombinedAssignRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Assign/CombinedAssignRector/Fixture)
2018-10-21 22:26:45 +00:00
2020-07-07 10:15:52 +00:00
Simplify `$value` = `$value` + 5; assignments to shorter ones
2018-10-21 22:26:45 +00:00
```diff
-$value = $value + 5;
+$value += 5;
2018-10-21 22:26:45 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-07-27 08:16:16 +00:00
### `CommonNotEqualRector`
- class: [`Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector`](/rules/code-quality/src/Rector/NotEqual/CommonNotEqualRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/NotEqual/CommonNotEqualRector/Fixture)
2020-07-27 08:16:16 +00:00
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><br>
### `CompactToVariablesRector`
2019-05-01 23:56:58 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector`](/rules/code-quality/src/Rector/FuncCall/CompactToVariablesRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/CompactToVariablesRector/Fixture)
2019-05-01 23:56:58 +00:00
2020-06-16 14:39:45 +00:00
Change `compact()` call to own array
2019-05-29 13:40:20 +00:00
```diff
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
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `CompleteDynamicPropertiesRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector`](/rules/code-quality/src/Rector/Class_/CompleteDynamicPropertiesRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Class_/CompleteDynamicPropertiesRector/Fixture)
2019-05-29 13:40:20 +00:00
Add missing dynamic properties
```diff
class SomeClass
{
+ /**
+ * @var int
+ */
+ public $value;
public function set()
{
$this->value = 5;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ConsecutiveNullCompareReturnsToNullCoalesceQueueRector`
- class: [`Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector`](/rules/code-quality/src/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector/Fixture)
Change multiple null compares to ?? queue
2019-05-01 23:56:58 +00:00
```diff
class SomeClass
{
public function run()
{
- if (null !== $this->orderItem) {
- return $this->orderItem;
2019-05-29 13:40:20 +00:00
- }
-
- if (null !== $this->orderItemUnit) {
- return $this->orderItemUnit;
- }
-
- return null;
+ return $this->orderItem ?? $this->orderItemUnit;
2019-05-01 23:56:58 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
### `ExplicitBoolCompareRector`
2019-05-19 08:27:38 +00:00
- class: [`Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector`](/rules/code-quality/src/Rector/If_/ExplicitBoolCompareRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/If_/ExplicitBoolCompareRector/Fixture)
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
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-19 08:27:38 +00:00
2020-07-27 13:40:30 +00:00
### `FixClassCaseSensitivityNameRector`
- class: [`Rector\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector`](/rules/code-quality/src/Rector/Name/FixClassCaseSensitivityNameRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Name/FixClassCaseSensitivityNameRector/Fixture)
2020-07-27 13:40:30 +00:00
Change miss-typed case sensitivity name to correct one
```diff
final class SomeClass
{
public function run()
{
- $anotherClass = new anotherclass;
+ $anotherClass = new AnotherClass;
}
}
final class AnotherClass
{
}
```
<br><br>
2020-01-04 17:49:26 +00:00
### `ForRepeatedCountToOwnVariableRector`
- class: [`Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector`](/rules/code-quality/src/Rector/For_/ForRepeatedCountToOwnVariableRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture)
2020-01-04 17:49:26 +00:00
2020-06-16 14:39:45 +00:00
Change `count()` in for function to own variable
2020-01-04 17:49:26 +00:00
```diff
class SomeClass
{
public function run($items)
{
- for ($i = 5; $i <= count($items); $i++) {
+ $itemsCount = count($items);
+ for ($i = 5; $i <= $itemsCount; $i++) {
echo $items[$i];
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-04 17:49:26 +00:00
### `ForToForeachRector`
- class: [`Rector\CodeQuality\Rector\For_\ForToForeachRector`](/rules/code-quality/src/Rector/For_/ForToForeachRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/For_/ForToForeachRector/Fixture)
2020-06-21 14:49:47 +00:00
Change `for()` to `foreach()` where useful
```diff
class SomeClass
2019-05-29 13:40:20 +00:00
{
public function run($tokens)
2019-05-29 13:40:20 +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
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-21 22:26:45 +00:00
2020-01-04 17:49:26 +00:00
### `ForeachItemsAssignToEmptyArrayToAssignRector`
- class: [`Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector`](/rules/code-quality/src/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector/Fixture)
2020-01-04 17:49:26 +00:00
2020-06-21 14:49:47 +00:00
Change `foreach()` items assign to empty array to direct assign
2020-01-04 17:49:26 +00:00
```diff
class SomeClass
{
public function run($items)
{
$collectedItems = [];
2020-01-04 17:49:26 +00:00
- foreach ($items as $item) {
- $collectedItems[] = $item;
2020-01-04 17:49:26 +00:00
- }
+ $collectedItems = $items;
2020-01-04 17:49:26 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-04 17:49:26 +00:00
### `ForeachToInArrayRector`
2018-10-21 22:26:45 +00:00
- class: [`Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector`](/rules/code-quality/src/Rector/Foreach_/ForeachToInArrayRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Foreach_/ForeachToInArrayRector/Fixture)
2018-10-21 22:26:45 +00:00
Simplify `foreach` loops into `in_array` when possible
2018-10-21 22:26:45 +00:00
```diff
-foreach ($items as $item) {
- if ($item === 'something') {
- return true;
- }
2019-05-29 13:40:20 +00:00
-}
-
-return false;
+in_array("something", $items, true);
2018-10-21 22:26:45 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `GetClassToInstanceOfRector`
- class: [`Rector\CodeQuality\Rector\Identical\GetClassToInstanceOfRector`](/rules/code-quality/src/Rector/Identical/GetClassToInstanceOfRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Identical/GetClassToInstanceOfRector/Fixture)
2020-06-16 14:39:45 +00:00
Changes comparison with `get_class` to instanceof
```diff
-if (EventsListener::class === get_class($event->job)) { }
+if ($event->job instanceof EventsListener) { }
```
2020-06-16 16:14:51 +00:00
<br><br>
### `InArrayAndArrayKeysToArrayKeyExistsRector`
2019-05-19 08:27:38 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector`](/rules/code-quality/src/Rector/FuncCall/InArrayAndArrayKeysToArrayKeyExistsRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/InArrayAndArrayKeysToArrayKeyExistsRector/Fixture)
2019-05-19 08:27:38 +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
-in_array("key", array_keys($array), true);
+array_key_exists("key", $array);
```
2020-06-16 16:14:51 +00:00
<br><br>
### `InlineIfToExplicitIfRector`
- class: [`Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector`](/rules/code-quality/src/Rector/Expression/InlineIfToExplicitIfRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Expression/InlineIfToExplicitIfRector/Fixture)
Change inline if to explicit if
```diff
class SomeClass
{
public function run()
{
$userId = null;
- is_null($userId) && $userId = 5;
+ if (is_null($userId)) {
+ $userId = 5;
+ }
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-27 17:50:00 +00:00
### `IntvalToTypeCastRector`
- class: [`Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector`](/rules/code-quality/src/Rector/FuncCall/IntvalToTypeCastRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/IntvalToTypeCastRector/Fixture)
2019-12-27 17:50:00 +00:00
2020-07-07 10:15:52 +00:00
Change `intval()` to faster and readable (int) `$value`
2019-12-27 17:50:00 +00:00
```diff
class SomeClass
{
public function run($value)
{
- return intval($value);
+ return (int) $value;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-27 17:50:00 +00:00
2019-08-05 21:10:47 +00:00
### `IsAWithStringWithThirdArgumentRector`
- class: [`Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector`](/rules/code-quality/src/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/IsAWithStringWithThirdArgumentRector/Fixture)
2019-08-05 21:10:47 +00:00
2020-06-16 14:39:45 +00:00
Complete missing 3rd argument in case `is_a()` function in case of strings
2019-08-05 21:10:47 +00:00
```diff
class SomeClass
{
public function __construct(string $value)
{
- return is_a($value, 'stdClass');
+ return is_a($value, 'stdClass', true);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `IssetOnPropertyObjectToPropertyExistsRector`
- class: [`Rector\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector`](/rules/code-quality/src/Rector/Isset_/IssetOnPropertyObjectToPropertyExistsRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Isset_/IssetOnPropertyObjectToPropertyExistsRector/Fixture)
Change isset on property object to `property_exists()`
```diff
class SomeClass
{
private $x;
public function run(): void
{
- isset($this->x);
+ property_exists($this, 'x') && $this->x !== null;
}
}
```
<br><br>
### `JoinStringConcatRector`
- class: [`Rector\CodeQuality\Rector\Concat\JoinStringConcatRector`](/rules/code-quality/src/Rector/Concat/JoinStringConcatRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Concat/JoinStringConcatRector/Fixture)
Joins concat of 2 strings, unless the lenght is too long
```diff
class SomeClass
{
2019-05-19 08:27:38 +00:00
public function run()
{
- $name = 'Hi' . ' Tom';
+ $name = 'Hi Tom';
2019-05-19 08:27:38 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-19 08:27:38 +00:00
2020-07-27 08:16:16 +00:00
### `LogicalToBooleanRector`
- class: [`Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector`](/rules/code-quality/src/Rector/LogicalAnd/LogicalToBooleanRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/LogicalAnd/LogicalToBooleanRector/Fixture)
2020-07-27 08:16:16 +00:00
Change OR, AND to ||, && with more common understanding
```diff
-if ($f = false or true) {
+if (($f = false) || true) {
return $f;
}
```
<br><br>
### `NewStaticToNewSelfRector`
- class: [`Rector\CodeQuality\Rector\New_\NewStaticToNewSelfRector`](/rules/code-quality/src/Rector/New_/NewStaticToNewSelfRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/New_/NewStaticToNewSelfRector/Fixture)
Change unsafe new `static()` to new `self()`
```diff
class SomeClass
{
public function build()
{
- return new static();
+ return new self();
}
}
```
<br><br>
2019-08-05 21:10:47 +00:00
### `RemoveAlwaysTrueConditionSetInConstructorRector`
- class: [`Rector\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector`](/rules/code-quality/src/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector/Fixture)
2019-08-05 21:10:47 +00:00
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';
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `RemoveSoleValueSprintfRector`
- class: [`Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector`](/rules/code-quality/src/Rector/FuncCall/RemoveSoleValueSprintfRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/RemoveSoleValueSprintfRector/Fixture)
2020-06-16 14:39:45 +00:00
Remove `sprintf()` wrapper if not needed
```diff
class SomeClass
{
public function run()
{
- $value = sprintf('%s', 'hi');
+ $value = 'hi';
$welcome = 'hello';
- $value = sprintf('%s', $welcome);
+ $value = $welcome;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-07-27 08:16:16 +00:00
### `SetTypeToCastRector`
- class: [`Rector\CodeQuality\Rector\FuncCall\SetTypeToCastRector`](/rules/code-quality/src/Rector/FuncCall/SetTypeToCastRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/SetTypeToCastRector/Fixture)
2020-07-27 08:16:16 +00:00
Changes `settype()` to (type) where possible
```diff
class SomeClass
{
- public function run($foo)
+ public function run(array $items)
{
- settype($foo, 'string');
+ $foo = (string) $foo;
- return settype($foo, 'integer');
+ return (int) $foo;
}
}
```
<br><br>
### `ShortenElseIfRector`
- class: [`Rector\CodeQuality\Rector\If_\ShortenElseIfRector`](/rules/code-quality/src/Rector/If_/ShortenElseIfRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/If_/ShortenElseIfRector/Fixture)
Shortens else/if to elseif
```diff
class SomeClass
{
public function run()
{
if ($cond1) {
return $action1;
- } else {
- if ($cond2) {
- return $action2;
- }
+ } elseif ($cond2) {
+ return $action2;
2019-11-06 23:52:19 +00:00
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SimplifyArraySearchRector`
- class: [`Rector\CodeQuality\Rector\Identical\SimplifyArraySearchRector`](/rules/code-quality/src/Rector/Identical/SimplifyArraySearchRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Identical/SimplifyArraySearchRector/Fixture)
2020-06-16 14:39:45 +00:00
Simplify `array_search` to `in_array`
```diff
-array_search("searching", $array) !== false;
+in_array("searching", $array);
```
```diff
-array_search("searching", $array, true) !== false;
+in_array("searching", $array, true);
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SimplifyBoolIdenticalTrueRector`
2018-12-31 11:50:32 +00:00
- class: [`Rector\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector`](/rules/code-quality/src/Rector/Identical/SimplifyBoolIdenticalTrueRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Identical/SimplifyBoolIdenticalTrueRector/Fixture)
2018-12-31 11:50:32 +00:00
Symplify bool value compare to true or false
2018-12-31 11:50:32 +00:00
```diff
class SomeClass
{
public function run(bool $value, string $items)
2019-05-29 13:40:20 +00:00
{
- $match = in_array($value, $items, TRUE) === TRUE;
- $match = in_array($value, $items, TRUE) !== FALSE;
+ $match = in_array($value, $items, TRUE);
+ $match = in_array($value, $items, TRUE);
2018-12-31 11:50:32 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 11:50:32 +00:00
### `SimplifyConditionsRector`
2018-10-21 22:26:45 +00:00
- class: [`Rector\CodeQuality\Rector\Identical\SimplifyConditionsRector`](/rules/code-quality/src/Rector/Identical/SimplifyConditionsRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Identical/SimplifyConditionsRector/Fixture)
2018-10-21 22:26:45 +00:00
Simplify conditions
2018-10-21 22:26:45 +00:00
```diff
-if (! ($foo !== 'bar')) {...
+if ($foo === 'bar') {...
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SimplifyDeMorganBinaryRector`
- class: [`Rector\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector`](/rules/code-quality/src/Rector/BooleanNot/SimplifyDeMorganBinaryRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/BooleanNot/SimplifyDeMorganBinaryRector/Fixture)
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
```
2020-06-16 16:14:51 +00:00
<br><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
- class: [`Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector`](/rules/code-quality/src/Rector/Ternary/SimplifyDuplicatedTernaryRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Ternary/SimplifyDuplicatedTernaryRector/Fixture)
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
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `SimplifyEmptyArrayCheckRector`
- class: [`Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector`](/rules/code-quality/src/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector/Fixture)
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
-is_array($values) && empty($values)
+$values === []
2018-10-21 22:26:45 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-21 22:26:45 +00:00
### `SimplifyForeachToArrayFilterRector`
2019-02-18 15:51:24 +00:00
- class: [`Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToArrayFilterRector`](/rules/code-quality/src/Rector/Foreach_/SimplifyForeachToArrayFilterRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Foreach_/SimplifyForeachToArrayFilterRector/Fixture)
2019-02-18 15:51:24 +00:00
Simplify foreach with function filtering to array filter
2019-02-18 15:51:24 +00:00
```diff
-$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
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `SimplifyForeachToCoalescingRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector`](/rules/code-quality/src/Rector/Foreach_/SimplifyForeachToCoalescingRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Foreach_/SimplifyForeachToCoalescingRector/Fixture)
2019-05-29 13:40:20 +00:00
Changes foreach that returns set value to ??
2019-05-29 13:40:20 +00:00
```diff
-foreach ($this->oldToNewFunctions as $oldFunction => $newFunction) {
- if ($currentFunction === $oldFunction) {
- return $newFunction;
- }
-}
-
-return null;
+return $this->oldToNewFunctions[$currentFunction] ?? null;
2019-02-18 15:51:24 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-18 15:51:24 +00:00
### `SimplifyFuncGetArgsCountRector`
2018-12-31 11:50:32 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SimplifyFuncGetArgsCountRector`](/rules/code-quality/src/Rector/FuncCall/SimplifyFuncGetArgsCountRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/SimplifyFuncGetArgsCountRector/Fixture)
2018-12-31 11:50:32 +00:00
2020-06-16 14:39:45 +00:00
Simplify `count` of `func_get_args()` to `func_num_args()`
```diff
-count(func_get_args());
+func_num_args();
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SimplifyIfElseToTernaryRector`
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector`](/rules/code-quality/src/Rector/If_/SimplifyIfElseToTernaryRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/If_/SimplifyIfElseToTernaryRector/Fixture)
Changes if/else for same value as assign to ternary
2018-12-31 11:50:32 +00:00
```diff
class SomeClass
{
public function run()
{
- if (empty($value)) {
- $this->arrayBuilt[][$key] = true;
- } else {
- $this->arrayBuilt[][$key] = $value;
- }
+ $this->arrayBuilt[][$key] = empty($value) ? true : $value;
2018-12-31 11:50:32 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 11:50:32 +00:00
### `SimplifyIfIssetToNullCoalescingRector`
2018-10-21 22:26:45 +00:00
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector`](/rules/code-quality/src/Rector/If_/SimplifyIfIssetToNullCoalescingRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/If_/SimplifyIfIssetToNullCoalescingRector/Fixture)
Simplify binary if to null coalesce
2018-10-21 22:26:45 +00:00
```diff
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
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-23 18:58:57 +00:00
### `SimplifyIfNotNullReturnRector`
2019-02-18 15:51:24 +00:00
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfNotNullReturnRector`](/rules/code-quality/src/Rector/If_/SimplifyIfNotNullReturnRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/If_/SimplifyIfNotNullReturnRector/Fixture)
2019-02-18 15:51:24 +00:00
Changes redundant null check to instant return
2019-02-18 15:51:24 +00:00
```diff
$newNode = 'something ;
-if ($newNode !== null) {
- return $newNode;
-}
-
-return null;
+return $newNode;
2019-02-18 15:51:24 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-18 15:51:24 +00:00
### `SimplifyIfReturnBoolRector`
2019-01-22 20:34:38 +00:00
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector`](/rules/code-quality/src/Rector/If_/SimplifyIfReturnBoolRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/If_/SimplifyIfReturnBoolRector/Fixture)
2019-01-22 20:34:38 +00:00
Shortens if return false/true to direct return
2019-01-22 20:34:38 +00:00
```diff
-if (strpos($docToken->getContent(), "\n") === false) {
- return true;
-}
-
-return false;
+return strpos($docToken->getContent(), "\n") === false;
2019-01-22 20:34:38 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-01-22 20:34:38 +00:00
### `SimplifyInArrayValuesRector`
2019-05-19 08:27:38 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector`](/rules/code-quality/src/Rector/FuncCall/SimplifyInArrayValuesRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/SimplifyInArrayValuesRector/Fixture)
2019-05-29 13:40:20 +00:00
2020-06-16 14:39:45 +00:00
Removes unneeded `array_values()` in `in_array()` call
2019-05-29 13:40:20 +00:00
```diff
-in_array("key", array_values($array), true);
+in_array("key", $array, true);
2019-05-29 13:40:20 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `SimplifyRegexPatternRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector`](/rules/code-quality/src/Rector/FuncCall/SimplifyRegexPatternRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/SimplifyRegexPatternRector/Fixture)
2019-05-29 13:40:20 +00:00
Simplify regex pattern to known ranges
2019-05-19 08:27:38 +00:00
```diff
class SomeClass
{
public function run($value)
2019-05-19 08:27:38 +00:00
{
- preg_match('#[a-zA-Z0-9+]#', $value);
+ preg_match('#[\w\d+]#', $value);
2019-05-19 08:27:38 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-19 08:27:38 +00:00
### `SimplifyStrposLowerRector`
- class: [`Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector`](/rules/code-quality/src/Rector/FuncCall/SimplifyStrposLowerRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/SimplifyStrposLowerRector/Fixture)
Simplify strpos(strtolower(), "...") calls
```diff
-strpos(strtolower($var), "...")"
+stripos($var, "...")"
2019-05-29 13:40:20 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SimplifyTautologyTernaryRector`
- class: [`Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector`](/rules/code-quality/src/Rector/Ternary/SimplifyTautologyTernaryRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Ternary/SimplifyTautologyTernaryRector/Fixture)
Simplify tautology ternary to value
```diff
-$value = ($fullyQualifiedTypeHint !== $typeHint) ? $fullyQualifiedTypeHint : $typeHint;
+$value = $fullyQualifiedTypeHint;
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SimplifyUselessVariableRector`
- class: [`Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector`](/rules/code-quality/src/Rector/Return_/SimplifyUselessVariableRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Return_/SimplifyUselessVariableRector/Fixture)
Removes useless variable assigns
2018-10-23 18:58:57 +00:00
```diff
function () {
- $a = true;
- return $a;
+ return true;
};
2018-10-23 18:58:57 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-21 22:26:45 +00:00
### `SingleInArrayToCompareRector`
2019-05-01 23:56:58 +00:00
- class: [`Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector`](/rules/code-quality/src/Rector/FuncCall/SingleInArrayToCompareRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/FuncCall/SingleInArrayToCompareRector/Fixture)
2019-05-01 23:56:58 +00:00
2020-06-16 14:39:45 +00:00
Changes `in_array()` with single element to ===
2019-05-01 23:56:58 +00:00
```diff
class SomeClass
{
public function run()
2019-05-01 23:56:58 +00:00
{
- if (in_array(strtolower($type), ['$this'], true)) {
+ if (strtolower($type) === '$this') {
return strtolower($type);
}
2019-05-01 23:56:58 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
### `SplitListAssignToSeparateLineRector`
- class: [`Rector\CodeQuality\Rector\Assign\SplitListAssignToSeparateLineRector`](/rules/code-quality/src/Rector/Assign/SplitListAssignToSeparateLineRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Assign/SplitListAssignToSeparateLineRector/Fixture)
2020-07-07 10:15:52 +00:00
Splits `[$a, $b] = [5, 10]` scalar assign to standalone lines
```diff
final class SomeClass
{
public function run(): void
{
- [$a, $b] = [1, 2];
+ $a = 1;
+ $b = 2;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `StrlenZeroToIdenticalEmptyStringRector`
- class: [`Rector\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector`](/rules/code-quality/src/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture)
2019-08-05 21:10:47 +00:00
2020-06-16 14:39:45 +00:00
Changes `strlen` comparison to 0 to direct empty string compare
2019-08-05 21:10:47 +00:00
```diff
class SomeClass
{
public function run($value)
{
- $empty = strlen($value) === 0;
+ $empty = $value === '';
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `ThrowWithPreviousExceptionRector`
- class: [`Rector\CodeQuality\Rector\Catch_\ThrowWithPreviousExceptionRector`](/rules/code-quality/src/Rector/Catch_/ThrowWithPreviousExceptionRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture)
2019-08-05 21:10:47 +00:00
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);
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `UnnecessaryTernaryExpressionRector`
2018-12-25 19:55:16 +00:00
- class: [`Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector`](/rules/code-quality/src/Rector/Ternary/UnnecessaryTernaryExpressionRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Ternary/UnnecessaryTernaryExpressionRector/Fixture)
2018-12-25 19:55:16 +00:00
Remove unnecessary ternary expressions.
2018-12-25 19:55:16 +00:00
```diff
-$foo === $bar ? true : false;
+$foo === $bar;
```
2020-06-16 16:14:51 +00:00
<br><br>
### `UnusedForeachValueToArrayKeysRector`
- class: [`Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector`](/rules/code-quality/src/Rector/Foreach_/UnusedForeachValueToArrayKeysRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Foreach_/UnusedForeachValueToArrayKeysRector/Fixture)
2020-07-07 10:15:52 +00:00
Change foreach with unused `$value` but only `$key,` to `array_keys()`
```diff
class SomeClass
{
public function run()
{
$items = [];
- foreach ($values as $key => $value) {
+ foreach (array_keys($values) as $key) {
$items[$key] = null;
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `UseIdenticalOverEqualWithSameTypeRector`
- class: [`Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector`](/rules/code-quality/src/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php)
- [test fixtures](/rules/code-quality/tests/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector/Fixture)
2019-05-29 13:40:20 +00:00
Use ===/!== over ==/!=, it values have the same type
```diff
class SomeClass
{
2019-05-29 13:40:20 +00:00
public function run(int $firstValue, int $secondValue)
{
2019-05-29 13:40:20 +00:00
- $isSame = $firstValue == $secondValue;
- $isDiffernt = $firstValue != $secondValue;
+ $isSame = $firstValue === $secondValue;
+ $isDiffernt = $firstValue !== $secondValue;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## 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
- class: [`Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector`](/rules/coding-style/src/Rector/Class_/AddArrayDefaultToArrayPropertyRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Class_/AddArrayDefaultToArrayPropertyRector/Fixture)
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
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-08-17 13:06:02 +00:00
- return $this->values === null;
+ return $this->values === [];
}
}
2019-05-26 19:26:33 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-26 19:26:33 +00:00
### `AnnotateThrowablesRector`
- class: [`Rector\CodingStyle\Rector\Throw_\AnnotateThrowablesRector`](/rules/coding-style/src/Rector/Throw_/AnnotateThrowablesRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Throw_/AnnotateThrowablesRector/Fixture)
Adds @throws DocBlock comments to methods that thrwo \Throwables.
```diff
class RootExceptionInMethodWithDocblock
{
/**
* This is a comment.
*
* @param int $code
+ * @throws \RuntimeException
*/
public function throwException(int $code)
{
throw new \RuntimeException('', $code);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `BinarySwitchToIfElseRector`
- class: [`Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector`](/rules/coding-style/src/Rector/Switch_/BinarySwitchToIfElseRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Switch_/BinarySwitchToIfElseRector/Fixture)
Changes switch with 2 options to if-else
```diff
-switch ($foo) {
- case 'my string':
- $result = 'ok';
- break;
-
- default:
- $result = 'not ok';
+if ($foo == 'my string') {
+ $result = 'ok;
+} else {
+ $result = 'not ok';
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `CallUserFuncCallToVariadicRector`
- class: [`Rector\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector`](/rules/coding-style/src/Rector/FuncCall/CallUserFuncCallToVariadicRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/FuncCall/CallUserFuncCallToVariadicRector/Fixture)
Replace call_user_func_call with variadic
```diff
class SomeClass
{
public function run()
{
- call_user_func_array('some_function', $items);
+ some_function(...$items);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `CamelCaseFunctionNamingToUnderscoreRector`
- class: [`Rector\CodingStyle\Rector\Function_\CamelCaseFunctionNamingToUnderscoreRector`](/rules/coding-style/src/Rector/Function_/CamelCaseFunctionNamingToUnderscoreRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Function_/CamelCaseFunctionNamingToUnderscoreRector/Fixture)
Change CamelCase naming of functions to under_score naming
```diff
-function someCamelCaseFunction()
+function some_camel_case_function()
{
}
-someCamelCaseFunction();
+some_camel_case_function();
```
2020-06-16 16:14:51 +00:00
<br><br>
### `CatchExceptionNameMatchingTypeRector`
- class: [`Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector`](/rules/coding-style/src/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture)
Type and name of catch exception should match
```diff
2019-05-29 13:40:20 +00:00
class SomeClass
{
public function run()
{
try {
// ...
- } catch (SomeException $typoException) {
- $typoException->getMessage();
+ } catch (SomeException $someException) {
+ $someException->getMessage();
2019-05-29 13:40:20 +00:00
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ConsistentImplodeRector`
- class: [`Rector\CodingStyle\Rector\FuncCall\ConsistentImplodeRector`](/rules/coding-style/src/Rector/FuncCall/ConsistentImplodeRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/FuncCall/ConsistentImplodeRector/Fixture)
2020-06-16 14:39:45 +00:00
Changes various `implode` forms to consistent one
```diff
class SomeClass
{
public function run(array $items)
{
- $itemsAsStrings = implode($items);
- $itemsAsStrings = implode($items, '|');
+ $itemsAsStrings = implode('', $items);
+ $itemsAsStrings = implode('|', $items);
$itemsAsStrings = implode('|', $items);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ConsistentPregDelimiterRector`
- class: [`Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector`](/rules/coding-style/src/Rector/FuncCall/ConsistentPregDelimiterRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/FuncCall/ConsistentPregDelimiterRector/Fixture)
Replace PREG delimiter with configured one
```php
<?php
declare(strict_types=1);
use Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ConsistentPregDelimiterRector::class)
->call('configure', [[
ConsistentPregDelimiterRector::DELIMITER => '#',
]]);
};
```
```diff
2019-03-09 13:24:30 +00:00
class SomeClass
{
2019-03-09 13:24:30 +00:00
public function run()
{
- 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
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `EncapsedStringsToSprintfRector`
- class: [`Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector`](/rules/coding-style/src/Rector/Encapsed/EncapsedStringsToSprintfRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture)
2019-08-05 21:10:47 +00:00
2020-06-16 14:39:45 +00:00
Convert enscaped {$string} to more readable `sprintf`
2019-08-05 21:10:47 +00:00
```diff
final class SomeClass
{
public function run(string $format)
{
- return "Unsupported format {$format}";
+ return sprintf('Unsupported format %s', $format);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `FollowRequireByDirRector`
2019-05-01 23:56:58 +00:00
- class: [`Rector\CodingStyle\Rector\Include_\FollowRequireByDirRector`](/rules/coding-style/src/Rector/Include_/FollowRequireByDirRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Include_/FollowRequireByDirRector/Fixture)
2019-05-01 23:56:58 +00:00
include/require should be followed by absolute path
2019-05-01 23:56:58 +00:00
```diff
class SomeClass
{
public function run()
2019-05-01 23:56:58 +00:00
{
- require 'autoload.php';
+ require __DIR__ . '/autoload.php';
2019-05-01 23:56:58 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
2019-10-15 14:46:31 +00:00
### `FunctionCallToConstantRector`
- class: [`Rector\CodingStyle\Rector\FuncCall\FunctionCallToConstantRector`](/rules/coding-style/src/Rector/FuncCall/FunctionCallToConstantRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture)
2019-10-15 14:46:31 +00:00
Changes use of function calls to use constants
```php
<?php
declare(strict_types=1);
use Rector\CodingStyle\Rector\FuncCall\FunctionCallToConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(FunctionCallToConstantRector::class)
->call('configure', [[
FunctionCallToConstantRector::FUNCTIONS_TO_CONSTANTS => [
'php_sapi_name' => 'PHP_SAPI',
],
]]);
};
```
2019-10-15 14:46:31 +00:00
```diff
class SomeClass
{
public function run()
{
- $value = php_sapi_name();
+ $value = PHP_SAPI;
}
}
```
```php
<?php
declare(strict_types=1);
use Rector\CodingStyle\Rector\FuncCall\FunctionCallToConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(FunctionCallToConstantRector::class)
->call('configure', [[
FunctionCallToConstantRector::FUNCTIONS_TO_CONSTANTS => [
'pi' => 'M_PI',
],
]]);
};
```
2019-10-15 14:46:31 +00:00
```diff
class SomeClass
{
public function run()
{
- $value = pi();
+ $value = M_PI;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-15 14:46:31 +00:00
### `MakeInheritedMethodVisibilitySameAsParentRector`
- class: [`Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector`](/rules/coding-style/src/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector/Fixture)
Make method visibility same as parent one
```diff
class ChildClass extends ParentClass
{
- public function run()
+ protected function run()
{
}
}
class ParentClass
{
protected function run()
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `ManualJsonStringToJsonEncodeArrayRector`
- class: [`Rector\CodingStyle\Rector\Assign\ManualJsonStringToJsonEncodeArrayRector`](/rules/coding-style/src/Rector/Assign/ManualJsonStringToJsonEncodeArrayRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Assign/ManualJsonStringToJsonEncodeArrayRector/Fixture)
2019-08-05 21:10:47 +00:00
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
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `NewlineBeforeNewAssignSetRector`
- class: [`Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector`](/rules/coding-style/src/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/ClassMethod/NewlineBeforeNewAssignSetRector/Fixture)
2019-08-05 21:10:47 +00:00
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);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `NullableCompareToNullRector`
- class: [`Rector\CodingStyle\Rector\If_\NullableCompareToNullRector`](/rules/coding-style/src/Rector/If_/NullableCompareToNullRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/If_/NullableCompareToNullRector/Fixture)
Changes negate of empty comparison of nullable value to explicit === or !== compare
```diff
/** @var stdClass|null $value */
-if ($value) {
+if ($value !== null) {
}
-if (!$value) {
+if ($value === null) {
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `PreferThisOrSelfMethodCallRector`
- class: [`Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector`](/rules/coding-style/src/Rector/MethodCall/PreferThisOrSelfMethodCallRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/MethodCall/PreferThisOrSelfMethodCallRector/Fixture)
2019-09-25 08:49:53 +00:00
2020-07-07 10:15:52 +00:00
Changes `$this->...` to self:: or vise versa for specific types
2019-09-25 08:49:53 +00:00
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
2020-07-24 11:46:57 +00:00
use Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
2020-07-24 11:46:57 +00:00
$services->set(PreferThisOrSelfMethodCallRector::class)
->call('configure', [[
PreferThisOrSelfMethodCallRector::TYPE_TO_PREFERENCE => [
'PHPUnit\TestCase' => 'self',
],
]]);
2020-07-24 11:46:57 +00:00
};
2019-09-25 08:49:53 +00:00
```
```diff
class SomeClass extends PHPUnit\TestCase
{
public function run()
{
- $this->assertThis();
+ self::assertThis();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `RemoveDoubleUnderscoreInMethodNameRector`
- class: [`Rector\CodingStyle\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector`](/rules/coding-style/src/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/ClassMethod/RemoveDoubleUnderscoreInMethodNameRector/Fixture)
Non-magic PHP object methods cannot start with "__"
```diff
class SomeClass
{
- public function __getName($anotherObject)
+ public function getName($anotherObject)
{
- $anotherObject->__getSurname();
+ $anotherObject->getSurname();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveUnusedAliasRector`
- class: [`Rector\CodingStyle\Rector\Use_\RemoveUnusedAliasRector`](/rules/coding-style/src/Rector/Use_/RemoveUnusedAliasRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Use_/RemoveUnusedAliasRector/Fixture)
Removes unused use aliases. Keep annotation aliases like "Doctrine\ORM\Mapping as ORM" to keep convention format
```diff
-use Symfony\Kernel as BaseKernel;
+use Symfony\Kernel;
-class SomeClass extends BaseKernel
+class SomeClass extends Kernel
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ReturnArrayClassMethodToYieldRector`
- class: [`Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector`](/rules/coding-style/src/Rector/ClassMethod/ReturnArrayClassMethodToYieldRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/ClassMethod/ReturnArrayClassMethodToYieldRector/Fixture)
2019-09-21 22:14:49 +00:00
Turns array return to yield return in specific type and method
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
2020-07-24 11:46:57 +00:00
use Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector;
use Rector\CodingStyle\ValueObject\ReturnArrayClassMethodToYield;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
2020-07-24 11:46:57 +00:00
$services->set(ReturnArrayClassMethodToYieldRector::class)
->call('configure', [[
ReturnArrayClassMethodToYieldRector::METHODS_TO_YIELDS => inline_value_objects(
[new ReturnArrayClassMethodToYield('EventSubscriberInterface', 'getSubscribedEvents')]
),
]]);
2020-07-24 11:46:57 +00:00
};
```
```diff
class SomeEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
2019-09-21 22:14:49 +00:00
- return ['event' => 'callback'];
+ yield 'event' => 'callback';
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SplitDoubleAssignRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector`](/rules/coding-style/src/Rector/Assign/SplitDoubleAssignRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Assign/SplitDoubleAssignRector/Fixture)
2019-05-29 13:40:20 +00:00
2020-06-16 14:39:45 +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()
{
- $one = $two = 1;
+ $one = 1;
+ $two = 1;
2019-05-29 13:40:20 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
2019-05-01 23:56:58 +00:00
### `SplitGroupedConstantsAndPropertiesRector`
- class: [`Rector\CodingStyle\Rector\ClassConst\SplitGroupedConstantsAndPropertiesRector`](/rules/coding-style/src/Rector/ClassConst/SplitGroupedConstantsAndPropertiesRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/ClassConst/SplitGroupedConstantsAndPropertiesRector/Fixture)
2019-05-01 23:56:58 +00:00
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;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
### `SplitGroupedUseImportsRector`
- class: [`Rector\CodingStyle\Rector\Use_\SplitGroupedUseImportsRector`](/rules/coding-style/src/Rector/Use_/SplitGroupedUseImportsRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Use_/SplitGroupedUseImportsRector/Fixture)
Split grouped use imports and trait statements to standalone lines
```diff
-use A, B;
+use A;
+use B;
class SomeClass
{
- use SomeTrait, AnotherTrait;
+ use SomeTrait;
+ use AnotherTrait;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SplitStringClassConstantToClassConstFetchRector`
- class: [`Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector`](/rules/coding-style/src/Rector/String_/SplitStringClassConstantToClassConstFetchRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/String_/SplitStringClassConstantToClassConstFetchRector/Fixture)
Separate class constant in a string to class constant fetch and string
```diff
class SomeClass
{
const HI = true;
}
class AnotherClass
{
public function get()
{
- return 'SomeClass::HI';
+ return SomeClass::class . '::HI';
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `StrictArraySearchRector`
- class: [`Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector`](/rules/coding-style/src/Rector/FuncCall/StrictArraySearchRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/FuncCall/StrictArraySearchRector/Fixture)
2020-06-16 14:39:45 +00:00
Makes `array_search` search for identical elements
```diff
-array_search($value, $items);
+array_search($value, $items, true);
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SymplifyQuoteEscapeRector`
- class: [`Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector`](/rules/coding-style/src/Rector/String_/SymplifyQuoteEscapeRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/String_/SymplifyQuoteEscapeRector/Fixture)
2018-10-21 22:26:45 +00:00
2020-01-03 18:20:13 +00:00
Prefer quote that are not inside the string
2018-10-21 22:26:45 +00:00
```diff
class SomeClass
{
public function run()
{
- $name = "\" Tom";
- $name = '\' Sara';
+ $name = '" Tom';
+ $name = "' Sara";
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `TernaryConditionVariableAssignmentRector`
- class: [`Rector\CodingStyle\Rector\Ternary\TernaryConditionVariableAssignmentRector`](/rules/coding-style/src/Rector/Ternary/TernaryConditionVariableAssignmentRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Ternary/TernaryConditionVariableAssignmentRector/Fixture)
Assign outcome of ternary condition to variable, where applicable
```diff
function ternary($value)
{
- $value ? $a = 1 : $a = 0;
+ $a = $value ? 1 : 0;
}
```
<br><br>
### `UseClassKeywordForClassNameResolutionRector`
- class: [`Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector`](/rules/coding-style/src/Rector/String_/UseClassKeywordForClassNameResolutionRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/String_/UseClassKeywordForClassNameResolutionRector/Fixture)
Use `class` keyword for class name resolution in string instead of hardcoded string reference
```diff
-$value = 'App\SomeClass::someMethod()';
+$value = \App\SomeClass . '::someMethod()';
```
<br><br>
### `UseIncrementAssignRector`
- class: [`Rector\CodingStyle\Rector\Plus\UseIncrementAssignRector`](/rules/coding-style/src/Rector/Plus/UseIncrementAssignRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Plus/UseIncrementAssignRector/Fixture)
Use ++ increment instead of `$var += 1`
```diff
class SomeClass
{
public function run()
{
- $style += 1;
+ ++$style
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `UseMessageVariableForSprintfInSymfonyStyleRector`
- class: [`Rector\CodingStyle\Rector\MethodCall\UseMessageVariableForSprintfInSymfonyStyleRector`](/rules/coding-style/src/Rector/MethodCall/UseMessageVariableForSprintfInSymfonyStyleRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/MethodCall/UseMessageVariableForSprintfInSymfonyStyleRector/Fixture)
Decouple `$message` property from `sprintf()` calls in `$this->smyfonyStyle->method()`
```diff
use Symfony\Component\Console\Style\SymfonyStyle;
final class SomeClass
{
public function run(SymfonyStyle $symfonyStyle)
{
- $symfonyStyle->info(sprintf('Hi %s', 'Tom'));
+ $message = sprintf('Hi %s', 'Tom');
+ $symfonyStyle->info($message);
}
}
```
<br><br>
### `VarConstantCommentRector`
- class: [`Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector`](/rules/coding-style/src/Rector/ClassConst/VarConstantCommentRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/ClassConst/VarConstantCommentRector/Fixture)
2020-06-16 14:39:45 +00:00
`Constant` should have a @var comment with type
```diff
class SomeClass
{
+ /**
+ * @var string
+ */
const HI = 'hi';
}
2018-10-21 22:26:45 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-21 22:26:45 +00:00
2019-10-15 14:46:31 +00:00
### `VersionCompareFuncCallToConstantRector`
- class: [`Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector`](/rules/coding-style/src/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/FuncCall/VersionCompareFuncCallToConstantRector/Fixture)
2019-10-15 14:46:31 +00:00
Changes use of call to version compare function to use of PHP version constant
```diff
class SomeClass
{
public function run()
{
- version_compare(PHP_VERSION, '5.3.0', '<');
+ PHP_VERSION_ID < 50300;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-15 14:46:31 +00:00
### `WrapEncapsedVariableInCurlyBracesRector`
- class: [`Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector`](/rules/coding-style/src/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector/Fixture)
Wrap encapsed variables in curly braces
```diff
function run($world)
{
- echo "Hello $world!"
+ echo "Hello {$world}!"
}
```
<br><br>
2019-05-29 13:40:20 +00:00
### `YieldClassMethodToArrayClassMethodRector`
- class: [`Rector\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector`](/rules/coding-style/src/Rector/ClassMethod/YieldClassMethodToArrayClassMethodRector.php)
- [test fixtures](/rules/coding-style/tests/Rector/ClassMethod/YieldClassMethodToArrayClassMethodRector/Fixture)
2019-05-29 13:40:20 +00:00
Turns yield return to array return in specific type and method
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
2020-07-24 11:46:57 +00:00
use Rector\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
2020-07-24 11:46:57 +00:00
$services->set(YieldClassMethodToArrayClassMethodRector::class)
->call('configure', [[
YieldClassMethodToArrayClassMethodRector::METHODS_BY_TYPE => [
'EventSubscriberInterface' => ['getSubscribedEvents'],
],
]]);
2020-07-24 11:46:57 +00:00
};
2019-05-29 13:40:20 +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'];
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## DeadCode
### `RemoveAlwaysTrueIfConditionRector`
- class: [`Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector`](/rules/dead-code/src/Rector/If_/RemoveAlwaysTrueIfConditionRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/If_/RemoveAlwaysTrueIfConditionRector/Fixture)
Remove if condition that is always true
```diff
final class SomeClass
{
public function go()
{
- if (1 === 1) {
- return 'yes';
- }
+ return 'yes';
return 'no';
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveAndTrueRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector`](/rules/dead-code/src/Rector/BooleanAnd/RemoveAndTrueRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/BooleanAnd/RemoveAndTrueRector/Fixture)
2019-05-29 13:40:20 +00:00
Remove and true that has no added value
```diff
class SomeClass
2019-05-29 13:40:20 +00:00
{
public function run()
2019-05-29 13:40:20 +00:00
{
- return true && 5 === 1;
+ return 5 === 1;
2019-05-29 13:40:20 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveAssignOfVoidReturnFunctionRector`
- class: [`Rector\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector`](/rules/dead-code/src/Rector/Assign/RemoveAssignOfVoidReturnFunctionRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Assign/RemoveAssignOfVoidReturnFunctionRector/Fixture)
Remove assign of void function/method to variable
```diff
class SomeClass
{
public function run()
{
- $value = $this->getOne();
+ $this->getOne();
}
private function getOne(): void
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveCodeAfterReturnRector`
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector`](/rules/dead-code/src/Rector/FunctionLike/RemoveCodeAfterReturnRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/FunctionLike/RemoveCodeAfterReturnRector/Fixture)
Remove dead code after return statement
```diff
class SomeClass
{
public function run(int $a)
{
return $a;
- $a++;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-06-06 13:01:53 +00:00
### `RemoveConcatAutocastRector`
- class: [`Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector`](/rules/dead-code/src/Rector/Concat/RemoveConcatAutocastRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Concat/RemoveConcatAutocastRector/Fixture)
2019-06-06 13:01:53 +00:00
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;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-06-06 13:01:53 +00:00
### `RemoveDeadConstructorRector`
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveDeadConstructorRector`](/rules/dead-code/src/Rector/ClassMethod/RemoveDeadConstructorRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/ClassMethod/RemoveDeadConstructorRector/Fixture)
Remove empty constructor
```diff
class SomeClass
{
- public function __construct()
- {
- }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-25 19:55:16 +00:00
### `RemoveDeadIfForeachForRector`
2018-12-25 19:55:16 +00:00
- class: [`Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector`](/rules/dead-code/src/Rector/For_/RemoveDeadIfForeachForRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/For_/RemoveDeadIfForeachForRector/Fixture)
2018-12-25 19:55:16 +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
{
public function run($someObject)
{
$value = 5;
- if ($value) {
- }
-
if ($someObject->run()) {
- }
-
- foreach ($values as $value) {
}
return $value;
}
}
2018-12-25 19:55:16 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-25 19:55:16 +00:00
### `RemoveDeadRecursiveClassMethodRector`
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveDeadRecursiveClassMethodRector`](/rules/dead-code/src/Rector/ClassMethod/RemoveDeadRecursiveClassMethodRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/ClassMethod/RemoveDeadRecursiveClassMethodRector/Fixture)
Remove unused public method that only calls itself recursively
```diff
class SomeClass
{
- public function run()
- {
- return $this->run();
- }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `RemoveDeadReturnRector`
2018-12-25 19:55:16 +00:00
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector`](/rules/dead-code/src/Rector/FunctionLike/RemoveDeadReturnRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/FunctionLike/RemoveDeadReturnRector/Fixture)
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
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-25 19:55:16 +00:00
### `RemoveDeadStmtRector`
2018-12-31 11:50:32 +00:00
- class: [`Rector\DeadCode\Rector\Expression\RemoveDeadStmtRector`](/rules/dead-code/src/Rector/Expression/RemoveDeadStmtRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Expression/RemoveDeadStmtRector/Fixture)
2018-12-31 11:50:32 +00:00
Removes dead code statements
2018-12-31 11:50:32 +00:00
```diff
-$value = 5;
-$value;
+$value = 5;
2018-12-31 11:50:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 11:50:32 +00:00
### `RemoveDeadTryCatchRector`
- class: [`Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector`](/rules/dead-code/src/Rector/TryCatch/RemoveDeadTryCatchRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture)
Remove dead try/catch
```diff
class SomeClass
{
public function run()
{
- try {
- // some code
- }
- catch (Throwable $throwable) {
- throw $throwable;
- }
+ // some code
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveDeadZeroAndOneOperationRector`
- class: [`Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector`](/rules/dead-code/src/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Plus/RemoveDeadZeroAndOneOperationRector/Fixture)
Remove operation with 1 and 0, that have no effect on the value
```diff
class SomeClass
{
public function run()
{
- $value = 5 * 1;
- $value = 5 + 0;
+ $value = 5;
+ $value = 5;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveDefaultArgumentValueRector`
2019-03-09 13:24:30 +00:00
- class: [`Rector\DeadCode\Rector\MethodCall\RemoveDefaultArgumentValueRector`](/rules/dead-code/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/MethodCall/RemoveDefaultArgumentValueRector/Fixture)
2019-03-09 13:24:30 +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()
{
- $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
public function runWithDefault($items = [])
{
return $items;
}
2019-02-21 14:36:16 +00:00
public function runStaticWithDefault($cards = [])
2019-02-21 14:36:16 +00:00
{
return $cards;
2019-02-21 14:36:16 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-21 14:36:16 +00:00
### `RemoveDelegatingParentCallRector`
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector`](/rules/dead-code/src/Rector/ClassMethod/RemoveDelegatingParentCallRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/ClassMethod/RemoveDelegatingParentCallRector/Fixture)
Removed dead parent call, that does not change anything
```diff
class SomeClass
{
- public function prettyPrint(array $stmts): string
- {
- return parent::prettyPrint($stmts);
- }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveDoubleAssignRector`
2019-02-18 15:51:24 +00:00
- class: [`Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector`](/rules/dead-code/src/Rector/Assign/RemoveDoubleAssignRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Assign/RemoveDoubleAssignRector/Fixture)
2019-02-18 15:51:24 +00:00
Simplify useless double assigns
2019-02-18 15:51:24 +00:00
```diff
-$value = 1;
$value = 1;
2019-02-18 15:51:24 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-18 15:51:24 +00:00
### `RemoveDuplicatedArrayKeyRector`
2019-05-19 08:27:38 +00:00
- class: [`Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector`](/rules/dead-code/src/Rector/Array_/RemoveDuplicatedArrayKeyRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Array_/RemoveDuplicatedArrayKeyRector/Fixture)
2019-05-19 08:27:38 +00:00
2020-06-16 16:13:37 +00:00
Remove duplicated `key` in defined arrays.
2019-05-19 08:27:38 +00:00
```diff
$item = [
- 1 => 'A',
1 => 'B'
];
2019-05-19 08:27:38 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-19 08:27:38 +00:00
2019-08-05 21:10:47 +00:00
### `RemoveDuplicatedCaseInSwitchRector`
- class: [`Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector`](/rules/dead-code/src/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Switch_/RemoveDuplicatedCaseInSwitchRector/Fixture)
2019-08-05 21:10:47 +00:00
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;
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `RemoveDuplicatedIfReturnRector`
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector`](/rules/dead-code/src/Rector/FunctionLike/RemoveDuplicatedIfReturnRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/FunctionLike/RemoveDuplicatedIfReturnRector/Fixture)
Remove duplicated if stmt with return in function/method body
```diff
class SomeClass
{
public function run($value)
{
if ($value) {
return true;
}
$value2 = 100;
-
- if ($value) {
- return true;
- }
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveDuplicatedInstanceOfRector`
- class: [`Rector\DeadCode\Rector\BinaryOp\RemoveDuplicatedInstanceOfRector`](/rules/dead-code/src/Rector/BinaryOp/RemoveDuplicatedInstanceOfRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/BinaryOp/RemoveDuplicatedInstanceOfRector/Fixture)
Remove duplicated instanceof in one call
```diff
class SomeClass
{
- public function run($value)
+ public function run($value): void
{
- $isIt = $value instanceof A || $value instanceof A;
- $isIt = $value instanceof A && $value instanceof A;
+ $isIt = $value instanceof A;
+ $isIt = $value instanceof A;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveEmptyClassMethodRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector`](/rules/dead-code/src/Rector/ClassMethod/RemoveEmptyClassMethodRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/ClassMethod/RemoveEmptyClassMethodRector/Fixture)
2019-03-31 12:25:39 +00:00
Remove empty method calls not required by parents
2019-03-31 12:25:39 +00:00
```diff
class OrphanClass
{
- public function __construct()
- {
- }
}
2019-03-31 12:25:39 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `RemoveEmptyMethodCallRector`
- class: [`Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector`](/rules/dead-code/src/Rector/MethodCall/RemoveEmptyMethodCallRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/MethodCall/RemoveEmptyMethodCallRector/Fixture)
Remove empty method call
```diff
class SomeClass
{
public function callThis()
{
}
}
-$some = new SomeClass();
-$some->callThis();
+$some = new SomeClass();
```
<br><br>
2019-08-24 11:08:59 +00:00
### `RemoveNullPropertyInitializationRector`
- class: [`Rector\DeadCode\Rector\PropertyProperty\RemoveNullPropertyInitializationRector`](/rules/dead-code/src/Rector/PropertyProperty/RemoveNullPropertyInitializationRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/PropertyProperty/RemoveNullPropertyInitializationRector/Fixture)
2019-08-24 11:08:59 +00:00
Remove initialization with null value from property declarations
```diff
class SunshineCommand extends ParentClassWithNewConstructor
{
- private $myVar = null;
+ private $myVar;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-24 11:08:59 +00:00
### `RemoveOverriddenValuesRector`
2018-12-25 19:55:16 +00:00
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveOverriddenValuesRector`](/rules/dead-code/src/Rector/FunctionLike/RemoveOverriddenValuesRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/FunctionLike/RemoveOverriddenValuesRector/Fixture)
2018-12-25 19:55:16 +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()
{
- $directories = [];
$possibleDirectories = [];
$directories = array_filter($possibleDirectories, 'file_exists');
2019-05-29 13:40:20 +00:00
}
2018-12-25 19:55:16 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-25 19:55:16 +00:00
### `RemoveParentCallWithoutParentRector`
2018-12-25 19:55:16 +00:00
- class: [`Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector`](/rules/dead-code/src/Rector/StaticCall/RemoveParentCallWithoutParentRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/StaticCall/RemoveParentCallWithoutParentRector/Fixture)
2018-12-25 19:55:16 +00:00
Remove unused parent call with no parent class
2018-12-25 19:55:16 +00:00
```diff
class OrphanClass
2019-05-29 13:40:20 +00:00
{
public function __construct()
{
- parent::__construct();
}
2019-05-29 13:40:20 +00:00
}
2018-12-25 19:55:16 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-25 19:55:16 +00:00
2019-08-17 13:06:02 +00:00
### `RemoveSetterOnlyPropertyAndMethodCallRector`
- class: [`Rector\DeadCode\Rector\Property\RemoveSetterOnlyPropertyAndMethodCallRector`](/rules/dead-code/src/Rector/Property/RemoveSetterOnlyPropertyAndMethodCallRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Property/RemoveSetterOnlyPropertyAndMethodCallRector/Fixture)
2019-08-17 13:06:02 +00:00
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');
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-17 13:06:02 +00:00
### `RemoveUnreachableStatementRector`
- class: [`Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector`](/rules/dead-code/src/Rector/Stmt/RemoveUnreachableStatementRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Stmt/RemoveUnreachableStatementRector/Fixture)
Remove unreachable statements
```diff
class SomeClass
{
public function run()
{
return 5;
-
- $removeMe = 10;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveUnusedAssignVariableRector`
- class: [`Rector\DeadCode\Rector\Assign\RemoveUnusedAssignVariableRector`](/rules/dead-code/src/Rector/Assign/RemoveUnusedAssignVariableRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Assign/RemoveUnusedAssignVariableRector/Fixture)
Remove assigned unused variable
```diff
class SomeClass
{
public function run()
{
- $value = $this->process();
+ $this->process();
}
public function process()
{
// something going on
return 5;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveUnusedClassConstantRector`
- class: [`Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector`](/rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture)
Remove unused class constants
```diff
class SomeClass
{
- private const SOME_CONST = 'dead';
-
public function run()
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-04 23:18:57 +00:00
### `RemoveUnusedClassesRector`
- class: [`Rector\DeadCode\Rector\Class_\RemoveUnusedClassesRector`](/rules/dead-code/src/Rector/Class_/RemoveUnusedClassesRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Class_/RemoveUnusedClassesRector/Fixture)
2020-01-04 23:18:57 +00:00
Remove unused classes without interface
```diff
interface SomeInterface
{
}
class SomeClass implements SomeInterface
{
public function run($items)
{
return null;
}
-}
-
-class NowhereUsedClass
-{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-04 23:18:57 +00:00
2019-08-05 21:10:47 +00:00
### `RemoveUnusedDoctrineEntityMethodAndPropertyRector`
- class: [`Rector\DeadCode\Rector\Class_\RemoveUnusedDoctrineEntityMethodAndPropertyRector`](/rules/dead-code/src/Rector/Class_/RemoveUnusedDoctrineEntityMethodAndPropertyRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Class_/RemoveUnusedDoctrineEntityMethodAndPropertyRector/Fixture)
2019-08-05 21:10:47 +00:00
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;
- }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `RemoveUnusedForeachKeyRector`
2019-05-19 08:27:38 +00:00
- class: [`Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector`](/rules/dead-code/src/Rector/Foreach_/RemoveUnusedForeachKeyRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Foreach_/RemoveUnusedForeachKeyRector/Fixture)
2019-05-19 08:27:38 +00:00
2020-06-16 14:39:45 +00:00
Remove unused `key` in foreach
2019-05-19 08:27:38 +00:00
```diff
$items = [];
-foreach ($items as $key => $value) {
+foreach ($items as $value) {
$result = $value;
2019-05-19 08:27:38 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-19 08:27:38 +00:00
### `RemoveUnusedFunctionRector`
- class: [`Rector\DeadCode\Rector\Function_\RemoveUnusedFunctionRector`](/rules/dead-code/src/Rector/Function_/RemoveUnusedFunctionRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Function_/RemoveUnusedFunctionRector/Fixture)
Remove unused function
```diff
-function removeMe()
-{
-}
-
function useMe()
{
}
useMe();
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveUnusedNonEmptyArrayBeforeForeachRector`
- class: [`Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector`](/rules/dead-code/src/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector/Fixture)
Remove unused if check to non-empty array before foreach of the array
```diff
class SomeClass
{
public function run()
{
$values = [];
- if ($values !== []) {
- foreach ($values as $value) {
- echo $value;
- }
+ foreach ($values as $value) {
+ echo $value;
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `RemoveUnusedParameterRector`
2019-02-21 14:36:16 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParameterRector`](/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedParameterRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/ClassMethod/RemoveUnusedParameterRector/Fixture)
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
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-21 14:36:16 +00:00
### `RemoveUnusedPrivateConstantRector`
2019-02-21 14:36:16 +00:00
- class: [`Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector`](/rules/dead-code/src/Rector/ClassConst/RemoveUnusedPrivateConstantRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedPrivateConstantRector/Fixture)
2019-02-21 14:36:16 +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
{
- private const SOME_CONSTANT = 5;
2019-05-29 13:40:20 +00:00
public function run()
2019-02-21 14:36:16 +00:00
{
return 5;
2019-02-21 14:36:16 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-21 14:36:16 +00:00
### `RemoveUnusedPrivateMethodRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector`](/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/ClassMethod/RemoveUnusedPrivateMethodRector/Fixture)
2019-03-31 12:25:39 +00:00
Remove unused private method
2019-03-31 12:25:39 +00:00
```diff
final class SomeController
2019-03-31 12:25:39 +00:00
{
public function run()
{
return 5;
}
-
- private function skip()
2019-05-29 13:40:20 +00:00
- {
- return 10;
2019-05-29 13:40:20 +00:00
- }
2019-03-31 12:25:39 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `RemoveUnusedPrivatePropertyRector`
2018-12-25 19:55:16 +00:00
- class: [`Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector`](/rules/dead-code/src/Rector/Property/RemoveUnusedPrivatePropertyRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Property/RemoveUnusedPrivatePropertyRector/Fixture)
2018-12-25 19:55:16 +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
{
- private $property;
2019-05-29 13:40:20 +00:00
}
2018-12-25 19:55:16 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-25 19:55:16 +00:00
### `RemoveUnusedVariableAssignRector`
- class: [`Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector`](/rules/dead-code/src/Rector/Assign/RemoveUnusedVariableAssignRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Assign/RemoveUnusedVariableAssignRector/Fixture)
Remove unused assigns to variables
```diff
class SomeClass
{
public function run()
{
- $value = 5;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-15 14:46:31 +00:00
### `SimplifyIfElseWithSameContentRector`
- class: [`Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector`](/rules/dead-code/src/Rector/If_/SimplifyIfElseWithSameContentRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/If_/SimplifyIfElseWithSameContentRector/Fixture)
2019-10-15 14:46:31 +00:00
Remove if/else if they have same content
```diff
class SomeClass
{
public function run()
{
- if (true) {
- return 1;
- } else {
- return 1;
- }
+ return 1;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-15 14:46:31 +00:00
### `SimplifyMirrorAssignRector`
- class: [`Rector\DeadCode\Rector\Expression\SimplifyMirrorAssignRector`](/rules/dead-code/src/Rector/Expression/SimplifyMirrorAssignRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Expression/SimplifyMirrorAssignRector/Fixture)
Removes unneeded $a = $a assigns
```diff
-$a = $a;
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-27 17:50:00 +00:00
### `TernaryToBooleanOrFalseToBooleanAndRector`
- class: [`Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector`](/rules/dead-code/src/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector.php)
- [test fixtures](/rules/dead-code/tests/Rector/Ternary/TernaryToBooleanOrFalseToBooleanAndRector/Fixture)
2019-12-27 17:50:00 +00:00
Change ternary of bool : false to && bool
```diff
class SomeClass
{
public function go()
{
- return $value ? $this->getBool() : false;
+ return $value && $this->getBool();
}
private function getBool(): bool
{
return (bool) 5;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-27 17:50:00 +00:00
## Decouple
### `DecoupleClassMethodToOwnClassRector`
- class: [`Rector\Decouple\Rector\ClassMethod\DecoupleClassMethodToOwnClassRector`](/rules/decouple/src/Rector/ClassMethod/DecoupleClassMethodToOwnClassRector.php)
- [test fixtures](/rules/decouple/tests/Rector/ClassMethod/DecoupleClassMethodToOwnClassRector/Fixture)
Move class method with its all dependencies to own class by method name
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Decouple\Rector\ClassMethod\DecoupleClassMethodToOwnClassRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
2020-07-24 11:46:57 +00:00
$services->set(DecoupleClassMethodToOwnClassRector::class)
->call('configure', [[
DecoupleClassMethodToOwnClassRector::METHOD_NAMES_BY_CLASS => [
'SomeClass' => [
'someMethod' => [
'class' => 'NewDecoupledClass',
'method' => 'someRenamedMethod',
'parent_class' => 'AddedParentClass',
],
],
],
]]);
2020-07-24 11:46:57 +00:00
};
```
```diff
class SomeClass
{
- public function someMethod()
- {
- $this->alsoCallThis();
- }
-
- private function alsoCallThis()
- {
- }
}
```
**New file**
```php
<?php declare(strict_types=1);
class NewDecoupledClass extends AddedParentClass
{
public function someRenamedMethod(): void
{
$this->alsoCallThis();
}
private function alsoCallThis(): void
{
}
}
```
<br><br>
## Defluent
### `DefluentReturnMethodCallRector`
- class: [`Rector\Defluent\Rector\Return_\DefluentReturnMethodCallRector`](/rules/defluent/src/Rector/Return_/DefluentReturnMethodCallRector.php)
- [test fixtures](/rules/defluent/tests/Rector/Return_/DefluentReturnMethodCallRector/Fixture)
Turns return of fluent, to standalone call line and return of value
```diff
$someClass = new SomeClass();
-return $someClass->someFunction();
+$someClass->someFunction();
+return $someClass;
```
<br><br>
### `FluentChainMethodCallToNormalMethodCallRector`
- class: [`Rector\Defluent\Rector\MethodCall\FluentChainMethodCallToNormalMethodCallRector`](/rules/defluent/src/Rector/MethodCall/FluentChainMethodCallToNormalMethodCallRector.php)
- [test fixtures](/rules/defluent/tests/Rector/MethodCall/FluentChainMethodCallToNormalMethodCallRector/Fixture)
Turns fluent interface calls to classic ones.
```diff
$someClass = new SomeClass();
-$someClass->someFunction()
- ->otherFunction();
+$someClass->someFunction();
+$someClass->otherFunction();
```
<br><br>
### `InArgFluentChainMethodCallToStandaloneMethodCallRector`
- class: [`Rector\Defluent\Rector\MethodCall\InArgFluentChainMethodCallToStandaloneMethodCallRector`](/rules/defluent/src/Rector/MethodCall/InArgFluentChainMethodCallToStandaloneMethodCallRector.php)
Turns fluent interface calls to classic ones.
```diff
class UsedAsParameter
{
public function someFunction(FluentClass $someClass)
{
- $this->processFluentClass($someClass->someFunction()->otherFunction());
+ $someClass->someFunction();
+ $someClass->otherFunction();
+ $this->processFluentClass($someClass);
}
public function processFluentClass(FluentClass $someClass)
{
}
-}
+}
```
<br><br>
### `MethodCallOnSetterMethodCallToStandaloneAssignRector`
- class: [`Rector\Defluent\Rector\MethodCall\MethodCallOnSetterMethodCallToStandaloneAssignRector`](/rules/defluent/src/Rector/MethodCall/MethodCallOnSetterMethodCallToStandaloneAssignRector.php)
- [test fixtures](/rules/defluent/tests/Rector/MethodCall/MethodCallOnSetterMethodCallToStandaloneAssignRector/Fixture)
Change method call on setter to standalone assign before the setter
```diff
class SomeClass
{
public function some()
{
- $this->anotherMethod(new AnotherClass())
- ->someFunction();
+ $anotherClass = new AnotherClass();
+ $anotherClass->someFunction();
+ $this->anotherMethod($anotherClass);
}
public function anotherMethod(AnotherClass $anotherClass)
{
}
}
```
<br><br>
### `NewFluentChainMethodCallToNonFluentRector`
- class: [`Rector\Defluent\Rector\MethodCall\NewFluentChainMethodCallToNonFluentRector`](/rules/defluent/src/Rector/MethodCall/NewFluentChainMethodCallToNonFluentRector.php)
- [test fixtures](/rules/defluent/tests/Rector/MethodCall/NewFluentChainMethodCallToNonFluentRector/Fixture)
Turns fluent interface calls to classic ones.
```diff
-(new SomeClass())->someFunction()
- ->otherFunction();
+$someClass = new SomeClass();
+$someClass->someFunction();
+$someClass->otherFunction();
```
<br><br>
### `ReturnFluentChainMethodCallToNormalMethodCallRector`
- class: [`Rector\Defluent\Rector\Return_\ReturnFluentChainMethodCallToNormalMethodCallRector`](/rules/defluent/src/Rector/Return_/ReturnFluentChainMethodCallToNormalMethodCallRector.php)
- [test fixtures](/rules/defluent/tests/Rector/Return_/ReturnFluentChainMethodCallToNormalMethodCallRector/Fixture)
Turns fluent interface calls to classic ones.
```diff
$someClass = new SomeClass();
-return $someClass->someFunction()
- ->otherFunction();
+$someClass->someFunction();
+$someClass->otherFunction();
+return $someClass;
```
<br><br>
### `ReturnNewFluentChainMethodCallToNonFluentRector`
- class: [`Rector\Defluent\Rector\Return_\ReturnNewFluentChainMethodCallToNonFluentRector`](/rules/defluent/src/Rector/Return_/ReturnNewFluentChainMethodCallToNonFluentRector.php)
- [test fixtures](/rules/defluent/tests/Rector/Return_/ReturnNewFluentChainMethodCallToNonFluentRector/Fixture)
Turns fluent interface calls to classic ones.
```diff
-return (new SomeClass())->someFunction()
- ->otherFunction();
+$someClass = new SomeClass();
+$someClass->someFunction();
+$someClass->otherFunction();
+return $someClass;
```
<br><br>
### `ReturnThisRemoveRector`
- class: [`Rector\Defluent\Rector\ClassMethod\ReturnThisRemoveRector`](/rules/defluent/src/Rector/ClassMethod/ReturnThisRemoveRector.php)
- [test fixtures](/rules/defluent/tests/Rector/ClassMethod/ReturnThisRemoveRector/Fixture)
Removes "return `$this;"` from *fluent interfaces* for specified classes.
```diff
class SomeExampleClass
{
public function someFunction()
{
- return $this;
}
public function otherFunction()
{
- return $this;
}
}
```
<br><br>
2018-08-01 20:09:34 +00:00
## Doctrine
2019-12-22 18:38:09 +00:00
### `AddEntityIdByConditionRector`
- class: [`Rector\Doctrine\Rector\Class_\AddEntityIdByConditionRector`](/rules/doctrine/src/Rector/Class_/AddEntityIdByConditionRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Class_/AddEntityIdByConditionRector/Fixture)
2019-12-22 18:38:09 +00:00
Add entity id with annotations when meets condition
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
2020-07-24 11:46:57 +00:00
use Rector\Doctrine\Rector\Class_\AddEntityIdByConditionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
2020-07-24 11:46:57 +00:00
$services->set(AddEntityIdByConditionRector::class);
};
2019-12-22 18:38:09 +00:00
```
```diff
class SomeClass
{
use SomeTrait;
+
+ /**
+ * @ORM\Id
+ * @ORM\Column(type="integer")
+ * @ORM\GeneratedValue(strategy="AUTO")
+ */
+ private $id;
+
+ public function getId(): int
+ {
+ return $this->id;
+ }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-22 18:38:09 +00:00
2019-09-21 22:14:49 +00:00
### `AddUuidAnnotationsToIdPropertyRector`
- class: [`Rector\Doctrine\Rector\Property\AddUuidAnnotationsToIdPropertyRector`](/rules/doctrine/src/Rector/Property/AddUuidAnnotationsToIdPropertyRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Property/AddUuidAnnotationsToIdPropertyRector/Fixture)
2019-09-21 22:14:49 +00:00
2020-07-07 10:15:52 +00:00
Add uuid annotations to `$id` property
2019-09-21 22:14:49 +00:00
```diff
use Doctrine\ORM\Attributes as ORM;
/**
* @ORM\Entity
*/
class SomeClass
{
/**
- * @var int
+ * @var \Ramsey\Uuid\UuidInterface
* @ORM\Id
- * @ORM\Column(type="integer")
- * @ORM\GeneratedValue(strategy="AUTO")
- * @Serializer\Type("int")
+ * @ORM\Column(type="uuid_binary")
+ * @Serializer\Type("string")
*/
public $id;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-21 22:14:49 +00:00
### `AddUuidMirrorForRelationPropertyRector`
- class: [`Rector\Doctrine\Rector\Class_\AddUuidMirrorForRelationPropertyRector`](/rules/doctrine/src/Rector/Class_/AddUuidMirrorForRelationPropertyRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Class_/AddUuidMirrorForRelationPropertyRector/Fixture)
2020-07-07 10:15:52 +00:00
Adds `$uuid` property to entities, that already have `$id` with integer type.Require for step-by-step migration from int to uuid.
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SomeEntity
{
/**
* @ORM\ManyToOne(targetEntity="AnotherEntity", cascade={"persist", "merge"})
* @ORM\JoinColumn(nullable=false)
*/
private $amenity;
+
+ /**
+ * @ORM\ManyToOne(targetEntity="AnotherEntity", cascade={"persist", "merge"})
+ * @ORM\JoinColumn(nullable=true, referencedColumnName="uuid")
+ */
+ private $amenityUuid;
}
/**
* @ORM\Entity
*/
class AnotherEntity
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private $uuid;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AddUuidToEntityWhereMissingRector`
- class: [`Rector\Doctrine\Rector\Class_\AddUuidToEntityWhereMissingRector`](/rules/doctrine/src/Rector/Class_/AddUuidToEntityWhereMissingRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Class_/AddUuidToEntityWhereMissingRector/Fixture)
2018-08-01 20:09:34 +00:00
2020-07-07 10:15:52 +00:00
Adds `$uuid` property to entities, that already have `$id` with integer type.Require for step-by-step migration from int to uuid. In following step it should be renamed to `$id` and replace it
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SomeEntityWithIntegerId
{
/**
+ * @var \Ramsey\Uuid\UuidInterface
+ * @ORM\Column(type="uuid_binary", unique=true, nullable=true)
+ */
+ private $uuid;
+ /**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-19 09:27:29 +00:00
### `AlwaysInitializeUuidInEntityRector`
- class: [`Rector\Doctrine\Rector\Class_\AlwaysInitializeUuidInEntityRector`](/rules/doctrine/src/Rector/Class_/AlwaysInitializeUuidInEntityRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Class_/AlwaysInitializeUuidInEntityRector/Fixture)
2019-09-19 09:27:29 +00:00
Add uuid initializion to all entities that misses it
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class AddUuidInit
{
/**
* @ORM\Id
* @var UuidInterface
*/
private $superUuid;
+ public function __construct()
+ {
+ $this->superUuid = \Ramsey\Uuid\Uuid::uuid4();
+ }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-19 09:27:29 +00:00
2019-09-21 22:14:49 +00:00
### `ChangeGetIdTypeToUuidRector`
- class: [`Rector\Doctrine\Rector\ClassMethod\ChangeGetIdTypeToUuidRector`](/rules/doctrine/src/Rector/ClassMethod/ChangeGetIdTypeToUuidRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/ClassMethod/ChangeGetIdTypeToUuidRector/Fixture)
2019-09-21 22:14:49 +00:00
2020-06-21 14:49:47 +00:00
Change return type of `getId()` to uuid interface
2019-09-21 22:14:49 +00:00
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class GetId
{
- public function getId(): int
+ public function getId(): \Ramsey\Uuid\UuidInterface
{
return $this->id;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-21 22:14:49 +00:00
### `ChangeGetUuidMethodCallToGetIdRector`
- class: [`Rector\Doctrine\Rector\MethodCall\ChangeGetUuidMethodCallToGetIdRector`](/rules/doctrine/src/Rector/MethodCall/ChangeGetUuidMethodCallToGetIdRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/MethodCall/ChangeGetUuidMethodCallToGetIdRector/Fixture)
2019-09-21 22:14:49 +00:00
2020-06-21 14:49:47 +00:00
Change `getUuid()` method call to `getId()`
2019-09-21 22:14:49 +00:00
```diff
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
class SomeClass
{
public function run()
{
$buildingFirst = new Building();
- return $buildingFirst->getUuid()->toString();
+ return $buildingFirst->getId()->toString();
}
}
/**
* @ORM\Entity
*/
class UuidEntity
{
private $uuid;
public function getUuid(): UuidInterface
{
return $this->uuid;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-21 22:14:49 +00:00
### `ChangeIdenticalUuidToEqualsMethodCallRector`
- class: [`Rector\Doctrine\Rector\Identical\ChangeIdenticalUuidToEqualsMethodCallRector`](/rules/doctrine/src/Rector/Identical/ChangeIdenticalUuidToEqualsMethodCallRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Identical/ChangeIdenticalUuidToEqualsMethodCallRector/Fixture)
2019-09-21 22:14:49 +00:00
2020-07-07 10:15:52 +00:00
Change `$uuid` === 1 to `$uuid->equals(\Ramsey\Uuid\Uuid::fromString(1))`
2019-09-21 22:14:49 +00:00
```diff
class SomeClass
{
public function match($checkedId): int
{
$building = new Building();
- return $building->getId() === $checkedId;
+ return $building->getId()->equals(\Ramsey\Uuid\Uuid::fromString($checkedId));
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-21 22:14:49 +00:00
### `ChangeReturnTypeOfClassMethodWithGetIdRector`
- class: [`Rector\Doctrine\Rector\ClassMethod\ChangeReturnTypeOfClassMethodWithGetIdRector`](/rules/doctrine/src/Rector/ClassMethod/ChangeReturnTypeOfClassMethodWithGetIdRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/ClassMethod/ChangeReturnTypeOfClassMethodWithGetIdRector/Fixture)
2019-09-21 22:14:49 +00:00
2020-06-21 14:49:47 +00:00
Change `getUuid()` method call to `getId()`
2019-09-21 22:14:49 +00:00
```diff
class SomeClass
{
- public function getBuildingId(): int
+ public function getBuildingId(): \Ramsey\Uuid\UuidInterface
{
$building = new Building();
return $building->getId();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-21 22:14:49 +00:00
### `ChangeSetIdToUuidValueRector`
- class: [`Rector\Doctrine\Rector\MethodCall\ChangeSetIdToUuidValueRector`](/rules/doctrine/src/Rector/MethodCall/ChangeSetIdToUuidValueRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/MethodCall/ChangeSetIdToUuidValueRector/Fixture)
2019-09-21 22:14:49 +00:00
Change set id to uuid values
```diff
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
class SomeClass
{
public function run()
{
$buildingFirst = new Building();
- $buildingFirst->setId(1);
- $buildingFirst->setUuid(Uuid::fromString('a3bfab84-e207-4ddd-b96d-488151de9e96'));
+ $buildingFirst->setId(Uuid::fromString('a3bfab84-e207-4ddd-b96d-488151de9e96'));
}
}
/**
* @ORM\Entity
*/
class Building
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-21 22:14:49 +00:00
### `ChangeSetIdTypeToUuidRector`
- class: [`Rector\Doctrine\Rector\ClassMethod\ChangeSetIdTypeToUuidRector`](/rules/doctrine/src/Rector/ClassMethod/ChangeSetIdTypeToUuidRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/ClassMethod/ChangeSetIdTypeToUuidRector/Fixture)
2019-09-21 22:14:49 +00:00
2020-06-21 14:49:47 +00:00
Change param type of `setId()` to uuid interface
2019-09-21 22:14:49 +00:00
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SetId
{
private $id;
- public function setId(int $uuid): int
+ public function setId(\Ramsey\Uuid\UuidInterface $uuid): int
{
return $this->id = $uuid;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-21 22:14:49 +00:00
### `EntityAliasToClassConstantReferenceRector`
- class: [`Rector\Doctrine\Rector\MethodCall\EntityAliasToClassConstantReferenceRector`](/rules/doctrine/src/Rector/MethodCall/EntityAliasToClassConstantReferenceRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/MethodCall/EntityAliasToClassConstantReferenceRector/Fixture)
2018-08-01 20:09:34 +00:00
Replaces doctrine alias with class.
```php
<?php
declare(strict_types=1);
use Rector\Doctrine\Rector\MethodCall\EntityAliasToClassConstantReferenceRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(EntityAliasToClassConstantReferenceRector::class)
->call('configure', [[
EntityAliasToClassConstantReferenceRector::ALIASES_TO_NAMESPACES => [
App::class => 'App\Entity',
],
]]);
};
```
2018-08-01 20:09:34 +00:00
```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
2020-06-16 16:14:51 +00:00
<br><br>
### `ManagerRegistryGetManagerToEntityManagerRector`
- class: [`Rector\Doctrine\Rector\Class_\ManagerRegistryGetManagerToEntityManagerRector`](/rules/doctrine/src/Rector/Class_/ManagerRegistryGetManagerToEntityManagerRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Class_/ManagerRegistryGetManagerToEntityManagerRector/Fixture)
Changes ManagerRegistry intermediate calls directly to EntityManager calls
```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');
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-02 08:04:14 +00:00
### `RemoveRepositoryFromEntityAnnotationRector`
- class: [`Rector\Doctrine\Rector\Class_\RemoveRepositoryFromEntityAnnotationRector`](/rules/doctrine/src/Rector/Class_/RemoveRepositoryFromEntityAnnotationRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Class_/RemoveRepositoryFromEntityAnnotationRector/Fixture)
2019-10-02 08:04:14 +00:00
Removes repository class from @Entity annotation
```diff
use Doctrine\ORM\Mapping as ORM;
/**
- * @ORM\Entity(repositoryClass="ProductRepository")
+ * @ORM\Entity
*/
class Product
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-02 08:04:14 +00:00
2019-09-21 22:14:49 +00:00
### `RemoveTemporaryUuidColumnPropertyRector`
- class: [`Rector\Doctrine\Rector\Property\RemoveTemporaryUuidColumnPropertyRector`](/rules/doctrine/src/Rector/Property/RemoveTemporaryUuidColumnPropertyRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Property/RemoveTemporaryUuidColumnPropertyRector/Fixture)
2019-09-21 22:14:49 +00:00
2020-07-07 10:15:52 +00:00
Remove temporary `$uuid` property
2019-09-21 22:14:49 +00:00
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Column
{
/**
* @ORM\Column
*/
public $id;
-
- /**
- * @ORM\Column
- */
- public $uuid;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-21 22:14:49 +00:00
### `RemoveTemporaryUuidRelationPropertyRector`
- class: [`Rector\Doctrine\Rector\Property\RemoveTemporaryUuidRelationPropertyRector`](/rules/doctrine/src/Rector/Property/RemoveTemporaryUuidRelationPropertyRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/Property/RemoveTemporaryUuidRelationPropertyRector/Fixture)
2019-09-21 22:14:49 +00:00
Remove temporary *Uuid relation properties
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Column
{
/**
* @ORM\ManyToMany(targetEntity="Phonenumber")
*/
private $apple;
-
- /**
- * @ORM\ManyToMany(targetEntity="Phonenumber")
- */
- private $appleUuid;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-21 22:14:49 +00:00
### `ServiceEntityRepositoryConstructorToDependencyInjectionWithRepositoryPropertyRector`
- class: [`Rector\Doctrine\Rector\ClassMethod\ServiceEntityRepositoryConstructorToDependencyInjectionWithRepositoryPropertyRector`](/rules/doctrine/src/Rector/ClassMethod/ServiceEntityRepositoryConstructorToDependencyInjectionWithRepositoryPropertyRector.php)
- [test fixtures](/rules/doctrine/tests/Rector/ClassMethod/ServiceEntityRepositoryConstructorToDependencyInjectionWithRepositoryPropertyRector/Fixture)
Change ServiceEntityRepository to dependency injection, with repository property
```diff
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
final class ProjectRepository extends ServiceEntityRepository
{
- public function __construct(ManagerRegistry $registry)
+ /**
+ * @var \Doctrine\ORM\EntityManagerInterface
+ */
+ private $entityManager;
+
+ /**
+ * @var \Doctrine\ORM\EntityRepository<Project>
+ */
+ private $repository;
+
+ public function __construct(\Doctrine\ORM\EntityManagerInterface $entityManager)
{
- parent::__construct($registry, Project::class);
+ $this->repository = $entityManager->getRepository(Project::class);
+ $this->entityManager = $entityManager;
}
}
```
<br><br>
2019-10-02 08:04:14 +00:00
## DoctrineCodeQuality
### `ChangeBigIntEntityPropertyToIntTypeRector`
- class: [`Rector\DoctrineCodeQuality\Rector\Property\ChangeBigIntEntityPropertyToIntTypeRector`](/rules/doctrine-code-quality/src/Rector/Property/ChangeBigIntEntityPropertyToIntTypeRector.php)
- [test fixtures](/rules/doctrine-code-quality/tests/Rector/Property/ChangeBigIntEntityPropertyToIntTypeRector/Fixture)
Change database type "bigint" for @var/type declaration to string
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class SomeEntity
{
/**
- * @var int|null
+ * @var string|null
* @ORM\Column(type="bigint", nullable=true)
*/
private $bigNumber;
}
```
<br><br>
### `ChangeQuerySetParametersMethodParameterFromArrayToArrayCollectionRector`
- class: [`Rector\DoctrineCodeQuality\Rector\MethodCall\ChangeQuerySetParametersMethodParameterFromArrayToArrayCollectionRector`](/rules/doctrine-code-quality/src/Rector/MethodCall/ChangeQuerySetParametersMethodParameterFromArrayToArrayCollectionRector.php)
- [test fixtures](/rules/doctrine-code-quality/tests/Rector/MethodCall/ChangeQuerySetParametersMethodParameterFromArrayToArrayCollection/Fixture)
Change array to ArrayCollection in setParameters method of query builder
```diff
2020-04-01 00:05:51 +00:00
+use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityRepository;
+use Doctrine\ORM\Query\Parameter;
class SomeRepository extends EntityRepository
{
public function getSomething()
{
return $this
->createQueryBuilder('sm')
->select('sm')
->where('sm.foo = :bar')
- ->setParameters([
- 'bar' => 'baz'
- ])
+ ->setParameters(new ArrayCollection([
+ new Parameter('bar', 'baz'),
+ ]))
->getQuery()
->getResult()
;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `CorrectDefaultTypesOnEntityPropertyRector`
- class: [`Rector\DoctrineCodeQuality\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector`](/rules/doctrine-code-quality/src/Rector/Property/CorrectDefaultTypesOnEntityPropertyRector.php)
- [test fixtures](/rules/doctrine-code-quality/tests/Rector/Property/CorrectDefaultTypesOnEntityPropertyRector/Fixture)
Change default value types to match Doctrine annotation type
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class User
{
/**
* @ORM\Column(name="is_old", type="boolean")
*/
- private $isOld = '0';
+ private $isOld = false;
}
```
<br><br>
### `ImproveDoctrineCollectionDocTypeInEntityRector`
- class: [`Rector\DoctrineCodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector`](/rules/doctrine-code-quality/src/Rector/Property/ImproveDoctrineCollectionDocTypeInEntityRector.php)
- [test fixtures](/rules/doctrine-code-quality/tests/Rector/Property/ImproveDoctrineCollectionDocTypeInEntityRector/Fixture)
Improve @var, @param and @return types for Doctrine collections to make them useful both for PHPStan and PHPStorm
```diff
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SomeClass
{
/**
* @ORM\OneToMany(targetEntity=Training::class, mappedBy="trainer")
- * @var Collection|Trainer[]
+ * @var Collection<int, Training>|Trainer[]
*/
private $trainings = [];
}
```
<br><br>
2019-10-02 08:04:14 +00:00
### `InitializeDefaultEntityCollectionRector`
- class: [`Rector\DoctrineCodeQuality\Rector\Class_\InitializeDefaultEntityCollectionRector`](/rules/doctrine-code-quality/src/Rector/Class_/InitializeDefaultEntityCollectionRector.php)
- [test fixtures](/rules/doctrine-code-quality/tests/Rector/Class_/InitializeDefaultEntityCollectionRector/Fixture)
2019-10-02 08:04:14 +00:00
Initialize collection property in Entity constructor
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SomeClass
{
/**
* @ORM\OneToMany(targetEntity="MarketingEvent")
*/
private $marketingEvents = [];
+
+ public function __construct()
+ {
+ $this->marketingEvents = new ArrayCollection();
+ }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-02 08:04:14 +00:00
### `MakeEntityDateTimePropertyDateTimeInterfaceRector`
- class: [`Rector\DoctrineCodeQuality\Rector\ClassMethod\MakeEntityDateTimePropertyDateTimeInterfaceRector`](/rules/doctrine-code-quality/src/Rector/ClassMethod/MakeEntityDateTimePropertyDateTimeInterfaceRector.php)
- [test fixtures](/rules/doctrine-code-quality/tests/Rector/ClassMethod/MakeEntityDateTimePropertyDateTimeInterfaceRector/Fixture)
Make maker bundle generate DateTime property accept DateTimeInterface too
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class User
{
/**
- * @var DateTime|null
+ * @var DateTimeInterface|null
*/
private $bornAt;
public function setBornAt(DateTimeInterface $bornAt)
{
$this->bornAt = $bornAt;
}
}
```
<br><br>
### `MakeEntitySetterNullabilityInSyncWithPropertyRector`
- class: [`Rector\DoctrineCodeQuality\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector`](/rules/doctrine-code-quality/src/Rector/ClassMethod/MakeEntitySetterNullabilityInSyncWithPropertyRector.php)
- [test fixtures](/rules/doctrine-code-quality/tests/Rector/ClassMethod/MakeEntitySetterNullabilityInSyncWithPropertyRector/Fixture)
Make nullability in setter class method with respect to property
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Product
{
/**
* @ORM\ManyToOne(targetEntity="AnotherEntity")
*/
private $anotherEntity;
- public function setAnotherEntity(?AnotherEntity $anotherEntity)
+ public function setAnotherEntity(AnotherEntity $anotherEntity)
{
$this->anotherEntity = $anotherEntity;
}
}
```
<br><br>
### `MoveCurrentDateTimeDefaultInEntityToConstructorRector`
- class: [`Rector\DoctrineCodeQuality\Rector\Class_\MoveCurrentDateTimeDefaultInEntityToConstructorRector`](/rules/doctrine-code-quality/src/Rector/Class_/MoveCurrentDateTimeDefaultInEntityToConstructorRector.php)
- [test fixtures](/rules/doctrine-code-quality/tests/Rector/Property/MoveCurrentDateTimeDefaultInEntityToConstructorRector/Fixture)
Move default value for entity property to constructor, the safest place
```diff
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class User
{
/**
* @var DateTimeInterface
*
- * @ORM\Column(type="datetime", nullable=false, options={"default"="now()"})
+ * @ORM\Column(type="datetime", nullable=false)
*/
- private $when = 'now()';
+ private $when;
+
+ public function __construct()
+ {
+ $this->when = new \DateTime();
+ }
}
```
<br><br>
### `MoveRepositoryFromParentToConstructorRector`
- class: [`Rector\DoctrineCodeQuality\Rector\Class_\MoveRepositoryFromParentToConstructorRector`](/rules/doctrine-code-quality/src/Rector/Class_/MoveRepositoryFromParentToConstructorRector.php)
Turns parent EntityRepository class to constructor dependency
```diff
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);
+ }
}
```
<br><br>
2019-12-26 10:21:09 +00:00
## DoctrineGedmoToKnplabs
2020-01-08 00:05:45 +00:00
### `BlameableBehaviorRector`
- class: [`Rector\DoctrineGedmoToKnplabs\Rector\Class_\BlameableBehaviorRector`](/rules/doctrine-gedmo-to-knplabs/src/Rector/Class_/BlameableBehaviorRector.php)
- [test fixtures](/rules/doctrine-gedmo-to-knplabs/tests/Rector/Class_/BlameableBehaviorRector/Fixture)
2020-01-08 00:05:45 +00:00
Change Blameable from gedmo/doctrine-extensions to knplabs/doctrine-behaviors
```diff
-use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
+use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
+use Knp\DoctrineBehaviors\Model\Blameable\BlameableTrait;
/**
* @ORM\Entity
*/
-class SomeClass
+class SomeClass implements BlameableInterface
{
- /**
- * @Gedmo\Blameable(on="create")
- */
- private $createdBy;
-
- /**
- * @Gedmo\Blameable(on="update")
- */
- private $updatedBy;
-
- /**
- * @Gedmo\Blameable(on="change", field={"title", "body"})
- */
- private $contentChangedBy;
-
- public function getCreatedBy()
- {
- return $this->createdBy;
- }
-
- public function getUpdatedBy()
- {
- return $this->updatedBy;
- }
-
- public function getContentChangedBy()
- {
- return $this->contentChangedBy;
- }
+ use BlameableTrait;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-08 00:05:45 +00:00
### `LoggableBehaviorRector`
- class: [`Rector\DoctrineGedmoToKnplabs\Rector\Class_\LoggableBehaviorRector`](/rules/doctrine-gedmo-to-knplabs/src/Rector/Class_/LoggableBehaviorRector.php)
- [test fixtures](/rules/doctrine-gedmo-to-knplabs/tests/Rector/Class_/LoggableBehaviorRector/Fixture)
2020-01-08 00:05:45 +00:00
Change Loggable from gedmo/doctrine-extensions to knplabs/doctrine-behaviors
```diff
-use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
+use Knp\DoctrineBehaviors\Model\Loggable\LoggableTrait;
+use Knp\DoctrineBehaviors\Contract\Entity\LoggableInterface;
/**
* @ORM\Entity
- * @Gedmo\Loggable
*/
-class SomeClass
+class SomeClass implements LoggableInterface
{
+ use LoggableTrait;
+
/**
- * @Gedmo\Versioned
* @ORM\Column(name="title", type="string", length=8)
*/
private $title;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-08 00:05:45 +00:00
2019-12-26 10:21:09 +00:00
### `SluggableBehaviorRector`
- class: [`Rector\DoctrineGedmoToKnplabs\Rector\Class_\SluggableBehaviorRector`](/rules/doctrine-gedmo-to-knplabs/src/Rector/Class_/SluggableBehaviorRector.php)
- [test fixtures](/rules/doctrine-gedmo-to-knplabs/tests/Rector/Class_/SluggableBehaviorRector/Fixture)
2019-12-26 10:21:09 +00:00
Change Sluggable from gedmo/doctrine-extensions to knplabs/doctrine-behaviors
```diff
use Gedmo\Mapping\Annotation as Gedmo;
+use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait;
+use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
-class SomeClass
+class SomeClass implements SluggableInterface
{
+ use SluggableTrait;
+
/**
- * @Gedmo\Slug(fields={"name"})
+ * @return string[]
*/
- private $slug;
-
- public function getSlug(): ?string
+ public function getSluggableFields(): array
{
- return $this->slug;
- }
-
- public function setSlug(?string $slug): void
- {
- $this->slug = $slug;
+ return ['name'];
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-26 10:21:09 +00:00
2020-01-08 00:05:45 +00:00
### `SoftDeletableBehaviorRector`
- class: [`Rector\DoctrineGedmoToKnplabs\Rector\Class_\SoftDeletableBehaviorRector`](/rules/doctrine-gedmo-to-knplabs/src/Rector/Class_/SoftDeletableBehaviorRector.php)
- [test fixtures](/rules/doctrine-gedmo-to-knplabs/tests/Rector/Class_/SoftDeletableBehaviorRector/Fixture)
2020-01-08 00:05:45 +00:00
Change SoftDeletable from gedmo/doctrine-extensions to knplabs/doctrine-behaviors
```diff
-use Gedmo\Mapping\Annotation as Gedmo;
+use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
+use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
-/**
- * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=true)
- */
-class SomeClass
+class SomeClass implements SoftDeletableInterface
{
- /**
- * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
- */
- private $deletedAt;
-
- public function getDeletedAt()
- {
- return $this->deletedAt;
- }
-
- public function setDeletedAt($deletedAt)
- {
- $this->deletedAt = $deletedAt;
- }
+ use SoftDeletableTrait;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-08 00:05:45 +00:00
2019-12-26 10:21:09 +00:00
### `TimestampableBehaviorRector`
- class: [`Rector\DoctrineGedmoToKnplabs\Rector\Class_\TimestampableBehaviorRector`](/rules/doctrine-gedmo-to-knplabs/src/Rector/Class_/TimestampableBehaviorRector.php)
- [test fixtures](/rules/doctrine-gedmo-to-knplabs/tests/Rector/Class_/TimestampableBehaviorRector/Fixture)
2019-12-26 10:21:09 +00:00
Change Timestampable from gedmo/doctrine-extensions to knplabs/doctrine-behaviors
```diff
-use Gedmo\Timestampable\Traits\TimestampableEntity;
+use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
+use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
-class SomeClass
+class SomeClass implements TimestampableInterface
{
- use TimestampableEntity;
+ use TimestampableTrait;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-26 10:21:09 +00:00
2019-12-27 17:50:00 +00:00
### `TranslationBehaviorRector`
- class: [`Rector\DoctrineGedmoToKnplabs\Rector\Class_\TranslationBehaviorRector`](/rules/doctrine-gedmo-to-knplabs/src/Rector/Class_/TranslationBehaviorRector.php)
- [test fixtures](/rules/doctrine-gedmo-to-knplabs/tests/Rector/Class_/TranslationBehaviorRector/Fixture)
2019-12-27 17:50:00 +00:00
Change Translation from gedmo/doctrine-extensions to knplabs/doctrine-behaviors
```diff
-use Gedmo\Mapping\Annotation as Gedmo;
-use Doctrine\ORM\Mapping as ORM;
-use Gedmo\Translatable\Translatable;
+use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
+use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
-/**
- * @ORM\Table
- */
-class Article implements Translatable
+class SomeClass implements TranslatableInterface
{
+ use TranslatableTrait;
+}
+
+
+use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
+use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;
+
+class SomeClassTranslation implements TranslationInterface
+{
+ use TranslationTrait;
+
/**
- * @Gedmo\Translatable
* @ORM\Column(length=128)
*/
private $title;
/**
- * @Gedmo\Translatable
* @ORM\Column(type="text")
*/
private $content;
-
- /**
- * @Gedmo\Locale
- * Used locale to override Translation listener`s locale
- * this is not a mapped field of entity metadata, just a simple property
- * and it is not necessary because globally locale can be set in listener
- */
- private $locale;
-
- public function setTitle($title)
- {
- $this->title = $title;
- }
-
- public function getTitle()
- {
- return $this->title;
- }
-
- public function setContent($content)
- {
- $this->content = $content;
- }
-
- public function getContent()
- {
- return $this->content;
- }
-
- public function setTranslatableLocale($locale)
- {
- $this->locale = $locale;
- }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-27 17:50:00 +00:00
2019-12-26 10:21:09 +00:00
### `TreeBehaviorRector`
- class: [`Rector\DoctrineGedmoToKnplabs\Rector\Class_\TreeBehaviorRector`](/rules/doctrine-gedmo-to-knplabs/src/Rector/Class_/TreeBehaviorRector.php)
- [test fixtures](/rules/doctrine-gedmo-to-knplabs/tests/Rector/Class_/TreeBehaviorRector/Fixture)
2019-12-26 10:21:09 +00:00
Change Tree from gedmo/doctrine-extensions to knplabs/doctrine-behaviors
```diff
-use Doctrine\Common\Collections\Collection;
-use Gedmo\Mapping\Annotation as Gedmo;
+use Knp\DoctrineBehaviors\Contract\Entity\TreeNodeInterface;
+use Knp\DoctrineBehaviors\Model\Tree\TreeNodeTrait;
-/**
- * @Gedmo\Tree(type="nested")
- */
-class SomeClass
+class SomeClass implements TreeNodeInterface
{
- /**
- * @Gedmo\TreeLeft
- * @ORM\Column(name="lft", type="integer")
- * @var int
- */
- private $lft;
-
- /**
- * @Gedmo\TreeRight
- * @ORM\Column(name="rgt", type="integer")
- * @var int
- */
- private $rgt;
-
- /**
- * @Gedmo\TreeLevel
- * @ORM\Column(name="lvl", type="integer")
- * @var int
- */
- private $lvl;
-
- /**
- * @Gedmo\TreeRoot
- * @ORM\ManyToOne(targetEntity="Category")
- * @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
- * @var Category
- */
- private $root;
-
- /**
- * @Gedmo\TreeParent
- * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
- * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
- * @var Category
- */
- private $parent;
-
- /**
- * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
- * @var Category[]|Collection
- */
- private $children;
-
- public function getRoot(): self
- {
- return $this->root;
- }
-
- public function setParent(self $category): void
- {
- $this->parent = $category;
- }
-
- public function getParent(): self
- {
- return $this->parent;
- }
+ use TreeNodeTrait;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-26 10:21:09 +00:00
## DowngradePhp71
### `DowngradeNullableTypeParamDeclarationRector`
- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeParamDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector.php)
- [test fixtures](/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeParamDeclarationRector/Fixture)
Remove the nullable type params, add @param tags instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeParamDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeNullableTypeParamDeclarationRector::class)
->call('configure', [[
DowngradeNullableTypeParamDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function run(?string $input)
+ /**
+ * @param string|null $input
+ */
+ public function run($input)
{
// do something
}
}
```
<br><br>
### `DowngradeNullableTypeReturnDeclarationRector`
- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeReturnDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector.php)
- [test fixtures](/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeNullableTypeReturnDeclarationRector/Fixture)
Remove returning nullable types, add a @return tag instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeNullableTypeReturnDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeNullableTypeReturnDeclarationRector::class)
->call('configure', [[
DowngradeNullableTypeReturnDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function getResponseOrNothing(bool $flag): ?string
+ /**
+ * @return string|null
+ */
+ public function getResponseOrNothing(bool $flag)
{
if ($flag) {
return 'Hello world';
}
return null;
}
}
```
<br><br>
### `DowngradeVoidTypeReturnDeclarationRector`
- class: [`Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeReturnDeclarationRector`](/rules/downgrade-php71/src/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector.php)
- [test fixtures](/rules/downgrade-php71/tests/Rector/FunctionLike/DowngradeVoidTypeReturnDeclarationRector/Fixture)
Remove the 'void' function type, add a @return tag instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp71\Rector\FunctionLike\DowngradeVoidTypeReturnDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeVoidTypeReturnDeclarationRector::class)
->call('configure', [[
DowngradeVoidTypeReturnDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function run(): void
+ /**
+ * @return void
+ */
+ public function run()
{
// do something
}
}
```
<br><br>
## DowngradePhp72
### `DowngradeParamObjectTypeDeclarationRector`
- class: [`Rector\DowngradePhp72\Rector\FunctionLike\DowngradeParamObjectTypeDeclarationRector`](/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeParamObjectTypeDeclarationRector.php)
- [test fixtures](/rules/downgrade-php72/tests/Rector/FunctionLike/DowngradeParamObjectTypeDeclarationRector/Fixture)
Remove the 'object' param type, add a @param tag instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeParamObjectTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeParamObjectTypeDeclarationRector::class)
->call('configure', [[
DowngradeParamObjectTypeDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function someFunction(object $someObject)
+ /**
+ * @param object $someObject
+ */
+ public function someFunction($someObject)
{
}
}
```
<br><br>
### `DowngradeReturnObjectTypeDeclarationRector`
- class: [`Rector\DowngradePhp72\Rector\FunctionLike\DowngradeReturnObjectTypeDeclarationRector`](/rules/downgrade-php72/src/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector.php)
- [test fixtures](/rules/downgrade-php72/tests/Rector/FunctionLike/DowngradeReturnObjectTypeDeclarationRector/Fixture)
Remove the 'object' function type, add a @return tag instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeReturnObjectTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeReturnObjectTypeDeclarationRector::class)
->call('configure', [[
DowngradeReturnObjectTypeDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function getSomeObject(): object
+ /**
+ * @return object
+ */
+ public function getSomeObject()
{
return new SomeObject();
}
}
```
<br><br>
## DowngradePhp73
### `DowngradeListReferenceAssignmentRector`
- class: [`Rector\DowngradePhp73\Rector\List_\DowngradeListReferenceAssignmentRector`](/rules/downgrade-php73/src/Rector/List_/DowngradeListReferenceAssignmentRector.php)
- [test fixtures](/rules/downgrade-php73/tests/Rector/List_/DowngradeListReferenceAssignmentRector/Fixture)
Convert the list reference assignment to its equivalent PHP 7.2 code
```diff
class SomeClass
{
public function run($string)
{
- $array = [1, 2, 3];
- list($a, &$b) = $array;
+ $array = [1, 2];
+ list($a) = $array;
+ $b =& $array[1];
- [&$c, $d, &$e] = $array;
+ [$c, $d, $e] = $array;
+ $c =& $array[0];
+ $e =& $array[2];
- list(&$a, &$b) = $array;
+ $a =& $array[0];
+ $b =& $array[1];
}
}
```
<br><br>
## DowngradePhp74
### `ArrowFunctionToAnonymousFunctionRector`
- class: [`Rector\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector`](/rules/downgrade-php74/src/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php)
- [test fixtures](/rules/downgrade-php74/tests/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector/Fixture)
Replace arrow functions with anonymous functions
```diff
class SomeClass
{
public function run()
{
$delimiter = ",";
- $callable = fn($matches) => $delimiter . strtolower($matches[1]);
+ $callable = function ($matches) use ($delimiter) {
+ return $delimiter . strtolower($matches[1]);
+ };
}
}
```
<br><br>
### `DowngradeArrayMergeCallWithoutArgumentsRector`
- class: [`Rector\DowngradePhp74\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector`](/rules/downgrade-php74/src/Rector/FuncCall/DowngradeArrayMergeCallWithoutArgumentsRector.php)
- [test fixtures](/rules/downgrade-php74/tests/Rector/FuncCall/DowngradeArrayMergeCallWithoutArgumentsRector/Fixture)
Add missing param to `array_merge` and `array_merge_recursive`
```diff
class SomeClass
{
public function run()
{
- array_merge();
- array_merge_recursive();
+ array_merge([]);
+ array_merge_recursive([]);
}
}
```
<br><br>
2020-10-09 20:01:37 +00:00
### `DowngradeArraySpreadRector`
- class: [`Rector\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector`](/rules/downgrade-php74/src/Rector/Array_/DowngradeArraySpreadRector.php)
- [test fixtures](/rules/downgrade-php74/tests/Rector/Array_/DowngradeArraySpreadRector/Fixture)
Replace array spread with `array_merge` function
```diff
class SomeClass
{
public function run()
{
$parts = ['apple', 'pear'];
- $fruits = ['banana', 'orange', ...$parts, 'watermelon'];
+ $fruits = array_merge(['banana', 'orange'], $parts, ['watermelon']);
}
public function runWithIterable()
{
- $fruits = ['banana', 'orange', ...new ArrayIterator(['durian', 'kiwi']), 'watermelon'];
+ $item0Unpacked = new ArrayIterator(['durian', 'kiwi']);
+ $fruits = array_merge(['banana', 'orange'], is_array($item0Unpacked) ? $item0Unpacked : iterator_to_array($item0Unpacked), ['watermelon']);
}
}
```
<br><br>
### `DowngradeNullCoalescingOperatorRector`
- class: [`Rector\DowngradePhp74\Rector\Coalesce\DowngradeNullCoalescingOperatorRector`](/rules/downgrade-php74/src/Rector/Coalesce/DowngradeNullCoalescingOperatorRector.php)
- [test fixtures](/rules/downgrade-php74/tests/Rector/Coalesce/DowngradeNullCoalescingOperatorRector/Fixture)
Remove null coalescing operator ??=
```diff
$array = [];
-$array['user_id'] ??= 'value';
+$array['user_id'] = $array['user_id'] ?? 'value';
```
<br><br>
2020-10-07 13:41:19 +00:00
### `DowngradeNumericLiteralSeparatorRector`
- class: [`Rector\DowngradePhp74\Rector\LNumber\DowngradeNumericLiteralSeparatorRector`](/rules/downgrade-php74/src/Rector/LNumber/DowngradeNumericLiteralSeparatorRector.php)
- [test fixtures](/rules/downgrade-php74/tests/Rector/LNumber/DowngradeNumericLiteralSeparatorRector/Fixture)
Remove "_" as thousands separator in numbers
```diff
class SomeClass
{
public function run()
{
- $int = 1_000;
- $float = 1_000_500.001;
+ $int = 1000;
+ $float = 1000500.001;
}
}
```
<br><br>
### `DowngradeStripTagsCallWithArrayRector`
- class: [`Rector\DowngradePhp74\Rector\FuncCall\DowngradeStripTagsCallWithArrayRector`](/rules/downgrade-php74/src/Rector/FuncCall/DowngradeStripTagsCallWithArrayRector.php)
- [test fixtures](/rules/downgrade-php74/tests/Rector/FuncCall/DowngradeStripTagsCallWithArrayRector/Fixture)
Convert 2nd param to `strip_tags` from array to string
```diff
class SomeClass
{
public function run($string)
{
// Arrays: change to string
- strip_tags($string, ['a', 'p']);
+ strip_tags($string, '<' . implode('><', ['a', 'p']) . '>');
// Variables/consts/properties: if array, change to string
$tags = ['a', 'p'];
- strip_tags($string, $tags);
+ strip_tags($string, $tags !== null && is_array($tags) ? '<' . implode('><', $tags) . '>' : $tags);
// Default case (eg: function call): externalize to var, then if array, change to string
- strip_tags($string, getTags());
+ $expr = getTags();
+ strip_tags($string, is_array($expr) ? '<' . implode('><', $expr) . '>' : $expr);
}
}
```
<br><br>
### `DowngradeTypedPropertyRector`
- class: [`Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector`](/rules/downgrade-php74/src/Rector/Property/DowngradeTypedPropertyRector.php)
- [test fixtures](/rules/downgrade-php74/tests/Rector/Property/DowngradeTypedPropertyRector/Fixture)
Changes property type definition from type definitions to `@var` annotations.
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeTypedPropertyRector::class)
->call('configure', [[
DowngradeTypedPropertyRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
class SomeClass
{
- private string $property;
+ /**
+ * @var string
+ */
+ private $property;
}
```
<br><br>
## DowngradePhp80
### `DowngradeParamMixedTypeDeclarationRector`
- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeParamMixedTypeDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector.php)
- [test fixtures](/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeParamMixedTypeDeclarationRector/Fixture)
Remove the 'mixed' param type, add a @param tag instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeParamMixedTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeParamMixedTypeDeclarationRector::class)
->call('configure', [[
DowngradeParamMixedTypeDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function someFunction(mixed $anything)
+ /**
+ * @param mixed $anything
+ */
+ public function someFunction($anything)
{
}
}
```
<br><br>
### `DowngradeReturnMixedTypeDeclarationRector`
- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnMixedTypeDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeReturnMixedTypeDeclarationRector.php)
- [test fixtures](/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnMixedTypeDeclarationRector/Fixture)
Remove the 'mixed' function type, add a @return tag instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnMixedTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeReturnMixedTypeDeclarationRector::class)
->call('configure', [[
DowngradeReturnMixedTypeDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function getAnything(bool $flag): mixed
+ /**
+ * @return mixed
+ */
+ public function getAnything(bool $flag)
{
if ($flag) {
return 1;
}
return 'Hello world'
}
}
```
<br><br>
### `DowngradeReturnStaticTypeDeclarationRector`
- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnStaticTypeDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector.php)
- [test fixtures](/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeReturnStaticTypeDeclarationRector/Fixture)
Remove the 'static' function type, add a @return tag instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnStaticTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeReturnStaticTypeDeclarationRector::class)
->call('configure', [[
DowngradeReturnStaticTypeDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function getStatic(): static
+ /**
+ * @return static
+ */
+ public function getStatic()
{
return new static();
}
}
```
<br><br>
### `DowngradeUnionTypeParamDeclarationRector`
- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeParamDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeParamDeclarationRector.php)
- [test fixtures](/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeUnionTypeParamDeclarationRector/Fixture)
Remove the union type params, add @param tags instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeParamDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeUnionTypeParamDeclarationRector::class)
->call('configure', [[
DowngradeUnionTypeParamDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function echoInput(string|int $input)
+ /**
+ * @param string|int $input
+ */
+ public function echoInput($input)
{
echo $input;
}
}
```
<br><br>
### `DowngradeUnionTypeReturnDeclarationRector`
- class: [`Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeReturnDeclarationRector`](/rules/downgrade-php80/src/Rector/FunctionLike/DowngradeUnionTypeReturnDeclarationRector.php)
- [test fixtures](/rules/downgrade-php80/tests/Rector/FunctionLike/DowngradeUnionTypeReturnDeclarationRector/Fixture)
Remove returning union types, add a @return tag instead
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeReturnDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeUnionTypeReturnDeclarationRector::class)
->call('configure', [[
DowngradeUnionTypeReturnDeclarationRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
<?php
class SomeClass
{
- public function getSomeObject(bool $flag): string|int
+ /**
+ * @return string|int
+ */
+ public function getSomeObject(bool $flag)
{
if ($flag) {
return 1;
}
return 'Hello world';
}
}
```
<br><br>
### `DowngradeUnionTypeTypedPropertyRector`
- class: [`Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector`](/rules/downgrade-php80/src/Rector/Property/DowngradeUnionTypeTypedPropertyRector.php)
- [test fixtures](/rules/downgrade-php80/tests/Rector/Property/DowngradeUnionTypeTypedPropertyRector/Fixture)
Removes union type property type definition, adding `@var` annotations instead.
```php
<?php
declare(strict_types=1);
use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeUnionTypeTypedPropertyRector::class)
->call('configure', [[
DowngradeUnionTypeTypedPropertyRector::ADD_DOC_BLOCK => true,
]]);
};
```
```diff
class SomeClass
{
- private string|int $property;
+ /**
+ * @var string|int
+ */
+ private $property;
}
```
<br><br>
2019-11-12 07:01:15 +00:00
## DynamicTypeAnalysis
### `AddArgumentTypeWithProbeDataRector`
- class: [`Rector\DynamicTypeAnalysis\Rector\ClassMethod\AddArgumentTypeWithProbeDataRector`](/packages/dynamic-type-analysis/src/Rector/ClassMethod/AddArgumentTypeWithProbeDataRector.php)
- [test fixtures](/packages/dynamic-type-analysis/tests/Rector/ClassMethod/AddArgumentTypeWithProbeDataRector/Fixture)
2019-11-12 07:01:15 +00:00
Add argument type based on probed data
```diff
class SomeClass
{
- public function run($arg)
+ public function run(string $arg)
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-11-12 07:01:15 +00:00
### `DecorateMethodWithArgumentTypeProbeRector`
- class: [`Rector\DynamicTypeAnalysis\Rector\ClassMethod\DecorateMethodWithArgumentTypeProbeRector`](/packages/dynamic-type-analysis/src/Rector/ClassMethod/DecorateMethodWithArgumentTypeProbeRector.php)
- [test fixtures](/packages/dynamic-type-analysis/tests/Rector/ClassMethod/DecorateMethodWithArgumentTypeProbeRector/Fixture)
2019-11-12 07:01:15 +00:00
2020-06-16 14:39:45 +00:00
Add probe that records argument types to `each` method
2019-11-12 07:01:15 +00:00
```diff
class SomeClass
{
public function run($arg)
{
+ \Rector\DynamicTypeAnalysis\Probe\TypeStaticProbe::recordArgumentType($arg, __METHOD__, 0);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-11-12 07:01:15 +00:00
### `RemoveArgumentTypeProbeRector`
- class: [`Rector\DynamicTypeAnalysis\Rector\StaticCall\RemoveArgumentTypeProbeRector`](/packages/dynamic-type-analysis/src/Rector/StaticCall/RemoveArgumentTypeProbeRector.php)
- [test fixtures](/packages/dynamic-type-analysis/tests/Rector/StaticCall/RemoveArgumentTypeProbeRector/Fixture)
2019-11-12 07:01:15 +00:00
Clean up probe that records argument types
```diff
-use Rector\DynamicTypeAnalysis\Probe\TypeStaticProbe;
-
class SomeClass
{
public function run($arg)
{
- TypeStaticProbe::recordArgumentType($arg, __METHOD__, 0);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-11-12 07:01:15 +00:00
## FileSystemRector
### `RemoveProjectFileRector`
- class: [`Rector\FileSystemRector\Rector\Removing\RemoveProjectFileRector`](/packages/file-system-rector/src/Rector/Removing/RemoveProjectFileRector.php)
Remove file relative to project directory
```php
<?php
declare(strict_types=1);
use Rector\FileSystemRector\Rector\Removing\RemoveProjectFileRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveProjectFileRector::class)
->call('configure', [[
RemoveProjectFileRector::FILE_PATHS_TO_REMOVE => ['someFile/ToBeRemoved.txt'],
]]);
};
```
```diff
-// someFile/ToBeRemoved.txt
```
2020-06-16 16:14:51 +00:00
<br><br>
## Generic
### `ActionInjectionToConstructorInjectionRector`
- class: [`Rector\Generic\Rector\Class_\ActionInjectionToConstructorInjectionRector`](/rules/generic/src/Rector/Class_/ActionInjectionToConstructorInjectionRector.php)
- [test fixtures](/rules/generic/tests/Rector/Class_/ActionInjectionToConstructorInjectionRector/Fixture)
Turns action injection in Controllers to constructor injection
```diff
final class SomeController
{
- public function default(ProductRepository $productRepository)
+ /**
+ * @var ProductRepository
+ */
+ private $productRepository;
+ public function __construct(ProductRepository $productRepository)
{
- $products = $productRepository->fetchAll();
+ $this->productRepository = $productRepository;
+ }
+
+ public function default()
+ {
+ $products = $this->productRepository->fetchAll();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AddInterfaceByTraitRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Generic\Rector\Class_\AddInterfaceByTraitRector`](/rules/generic/src/Rector/Class_/AddInterfaceByTraitRector.php)
- [test fixtures](/rules/generic/tests/Rector/Class_/AddInterfaceByTraitRector/Fixture)
2020-07-24 11:46:57 +00:00
Add interface by used trait
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\Class_\AddInterfaceByTraitRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(AddInterfaceByTraitRector::class)
->call('configure', [[
AddInterfaceByTraitRector::INTERFACE_BY_TRAIT => [
'SomeTrait' => SomeInterface::class,
],
]]);
2020-07-24 11:46:57 +00:00
};
```
```diff
-class SomeClass
+class SomeClass implements SomeInterface
2020-07-24 11:46:57 +00:00
{
use SomeTrait;
2020-07-24 11:46:57 +00:00
}
```
<br><br>
### `AddMethodParentCallRector`
- class: [`Rector\Generic\Rector\ClassMethod\AddMethodParentCallRector`](/rules/generic/src/Rector/ClassMethod/AddMethodParentCallRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassMethod/AddMethodParentCallRector/Fixture)
Add method parent call, in case new parent method is added
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\AddMethodParentCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddMethodParentCallRector::class)
->call('configure', [[
AddMethodParentCallRector::METHODS_BY_PARENT_TYPES => [
'ParentClassWithNewConstructor' => '__construct',
],
]]);
};
```
```diff
class SunshineCommand extends ParentClassWithNewConstructor
{
public function __construct()
{
$value = 5;
+
+ parent::__construct();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AddPropertyByParentRector`
- class: [`Rector\Generic\Rector\Class_\AddPropertyByParentRector`](/rules/generic/src/Rector/Class_/AddPropertyByParentRector.php)
- [test fixtures](/rules/generic/tests/Rector/Class_/AddPropertyByParentRector/Fixture)
Add dependency via constructor by parent class type
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\Class_\AddPropertyByParentRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddPropertyByParentRector::class)
->call('configure', [[
AddPropertyByParentRector::PARENT_DEPENDENCIES => [
'SomeParentClass' => ['SomeDependency'],
],
]]);
};
```
```diff
final class SomeClass extends SomeParentClass
{
+ /**
+ * @var SomeDependency
+ */
+ private $someDependency;
+
+ public function __construct(SomeDependency $someDependency)
+ {
+ $this->someDependency = $someDependency;
+ }
}
```
<br><br>
### `AddReturnTypeDeclarationRector`
- class: [`Rector\Generic\Rector\ClassMethod\AddReturnTypeDeclarationRector`](/rules/generic/src/Rector/ClassMethod/AddReturnTypeDeclarationRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassMethod/AddReturnTypeDeclarationRector/Fixture)
Changes defined return typehint of method and class.
```php
<?php
2019-03-09 13:24:30 +00:00
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\AddReturnTypeDeclarationRector;
use Rector\Generic\ValueObject\AddReturnTypeDeclaration;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-03-09 13:24:30 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(AddReturnTypeDeclarationRector::class)
->call('configure', [[
AddReturnTypeDeclarationRector::METHOD_RETURN_TYPES => inline_value_objects(
[new AddReturnTypeDeclaration('SomeClass', 'getData', 'array')]
),
]]);
};
```
2019-03-09 13:24:30 +00:00
2019-03-09 13:24:30 +00:00
```diff
class SomeClass
2019-03-09 13:24:30 +00:00
{
- public getData()
+ public getData(): array
2019-03-09 13:24:30 +00:00
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-09 13:24:30 +00:00
### `AnnotatedPropertyInjectToConstructorInjectionRector`
2019-03-09 13:24:30 +00:00
- class: [`Rector\Generic\Rector\Property\AnnotatedPropertyInjectToConstructorInjectionRector`](/rules/generic/src/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector.php)
- [test fixtures](/rules/generic/tests/Rector/Property/AnnotatedPropertyInjectToConstructorInjectionRector/Fixture)
2019-03-09 13:24:30 +00:00
Turns non-private properties with `@annotation` to private properties and constructor injection
2019-03-09 13:24:30 +00:00
```diff
/**
* @var SomeService
- * @inject
*/
-public $someService;
+private $someService;
2019-03-09 13:24:30 +00:00
+
+public function __construct(SomeService $someService)
+{
+ $this->someService = $someService;
+}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ArgumentAdderRector`
2019-09-19 09:27:29 +00:00
- class: [`Rector\Generic\Rector\ClassMethod\ArgumentAdderRector`](/rules/generic/src/Rector/ClassMethod/ArgumentAdderRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassMethod/ArgumentAdderRector/Fixture)
2019-09-19 09:27:29 +00:00
This Rector adds new default arguments in calls of defined methods and class types.
2019-09-19 09:27:29 +00:00
```php
<?php
2019-09-19 09:27:29 +00:00
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector;
use Rector\Generic\ValueObject\ArgumentAdder;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ArgumentAdderRector::class)
->call('configure', [[
ArgumentAdderRector::ADDED_ARGUMENTS => inline_value_objects(
[new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', 'true', 'SomeType', null)]
),
]]);
};
```
```diff
$someObject = new SomeExampleClass;
-$someObject->someMethod();
+$someObject->someMethod(true);
2019-03-09 13:24:30 +00:00
```
```php
<?php
2019-03-09 13:24:30 +00:00
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector;
use Rector\Generic\ValueObject\ArgumentAdder;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-03-09 13:24:30 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ArgumentAdderRector::class)
->call('configure', [[
ArgumentAdderRector::ADDED_ARGUMENTS => inline_value_objects(
[new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', 'true', 'SomeType', null)]
),
]]);
};
```
2019-03-09 13:24:30 +00:00
2019-03-09 13:24:30 +00:00
```diff
class MyCustomClass extends SomeExampleClass
2019-03-09 13:24:30 +00:00
{
- public function someMethod()
+ public function someMethod($value = true)
2019-03-09 13:24:30 +00:00
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-09 13:24:30 +00:00
### `ArgumentDefaultValueReplacerRector`
2019-03-09 13:24:30 +00:00
- class: [`Rector\Generic\Rector\ClassMethod\ArgumentDefaultValueReplacerRector`](/rules/generic/src/Rector/ClassMethod/ArgumentDefaultValueReplacerRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassMethod/ArgumentDefaultValueReplacerRector/Fixture)
2019-03-09 13:24:30 +00:00
Replaces defined map of arguments in defined methods and their calls.
2019-03-09 13:24:30 +00:00
```php
<?php
2019-03-09 13:24:30 +00:00
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\ArgumentDefaultValueReplacerRector;
use Rector\Generic\ValueObject\ArgumentDefaultValueReplacer;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-14 21:23:06 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ArgumentDefaultValueReplacerRector::class)
->call('configure', [[
ArgumentDefaultValueReplacerRector::REPLACED_ARGUMENTS => inline_value_objects(
2020-10-07 14:20:53 +00:00
[
new ArgumentDefaultValueReplacer(
'SomeExampleClass',
'someMethod',
0,
'SomeClass::OLD_CONSTANT',
'false'
),
]
),
]]);
};
2020-07-14 21:23:06 +00:00
```
```diff
$someObject = new SomeClass;
-$someObject->someMethod(SomeClass::OLD_CONSTANT);
+$someObject->someMethod(false);'
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ArgumentRemoverRector`
- class: [`Rector\Generic\Rector\ClassMethod\ArgumentRemoverRector`](/rules/generic/src/Rector/ClassMethod/ArgumentRemoverRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassMethod/ArgumentRemoverRector/Fixture)
Removes defined arguments in defined methods and their calls.
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\ArgumentRemoverRector;
use Rector\Generic\ValueObject\ArgumentRemover;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ArgumentRemoverRector::class)
->call('configure', [[
ArgumentRemoverRector::REMOVED_ARGUMENTS => inline_value_objects(
[new ArgumentRemover('ExampleClass', 'someMethod', 0, 'true')]
),
]]);
};
```
```diff
$someObject = new SomeClass;
-$someObject->someMethod(true);
+$someObject->someMethod();'
```
<br><br>
### `ChangeConstantVisibilityRector`
- class: [`Rector\Generic\Rector\ClassConst\ChangeConstantVisibilityRector`](/rules/generic/src/Rector/ClassConst/ChangeConstantVisibilityRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassConst/ChangeConstantVisibilityRector/Fixture)
Change visibility of constant from parent class.
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\ClassConst\ChangeConstantVisibilityRector;
use Rector\Generic\ValueObject\ClassConstantVisibilityChange;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ChangeConstantVisibilityRector::class)
->call('configure', [[
ChangeConstantVisibilityRector::CLASS_CONSTANT_VISIBILITY_CHANGES => inline_value_objects(
[new ClassConstantVisibilityChange('ParentObject', 'SOME_CONSTANT', 'protected')]
),
]]);
2020-07-24 11:46:57 +00:00
};
```
```diff
class FrameworkClass
{
protected const SOME_CONSTANT = 1;
}
class MyClass extends FrameworkClass
{
- public const SOME_CONSTANT = 1;
+ protected const SOME_CONSTANT = 1;
}
```
<br><br>
### `ChangeContractMethodSingleToManyRector`
- class: [`Rector\Generic\Rector\ClassMethod\ChangeContractMethodSingleToManyRector`](/rules/generic/src/Rector/ClassMethod/ChangeContractMethodSingleToManyRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassMethod/ChangeContractMethodSingleToManyRector/Fixture)
Change method that returns single value to multiple values
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\ChangeContractMethodSingleToManyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ChangeContractMethodSingleToManyRector::class)
->call('configure', [[
ChangeContractMethodSingleToManyRector::OLD_TO_NEW_METHOD_BY_TYPE => [
'SomeClass' => [
'getNode' => 'getNodes',
],
],
]]);
2020-07-24 11:46:57 +00:00
};
```
```diff
class SomeClass
{
- public function getNode(): string
+ /**
+ * @return string[]
+ */
+ public function getNodes(): array
{
- return 'Echo_';
+ return ['Echo_'];
}
}
```
<br><br>
### `ChangeMethodVisibilityRector`
- class: [`Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector`](/rules/generic/src/Rector/ClassMethod/ChangeMethodVisibilityRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassMethod/ChangeMethodVisibilityRector/Fixture)
Change visibility of method from parent class.
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Generic\ValueObject\ChangeMethodVisibility;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ChangeMethodVisibilityRector::class)
->call('configure', [[
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => inline_value_objects(
[new ChangeMethodVisibility('FrameworkClass', 'someMethod', 'protected')]
),
]]);
2020-07-24 11:46:57 +00:00
};
```
```diff
class FrameworkClass
{
protected someMethod()
{
}
}
class MyClass extends FrameworkClass
{
- public someMethod()
+ protected someMethod()
{
}
}
```
<br><br>
### `ChangePropertyVisibilityRector`
- class: [`Rector\Generic\Rector\Property\ChangePropertyVisibilityRector`](/rules/generic/src/Rector/Property/ChangePropertyVisibilityRector.php)
- [test fixtures](/rules/generic/tests/Rector/Property/ChangePropertyVisibilityRector/Fixture)
Change visibility of property from parent class.
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\Property\ChangePropertyVisibilityRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ChangePropertyVisibilityRector::class)
->call('configure', [[
ChangePropertyVisibilityRector::PROPERTY_TO_VISIBILITY_BY_CLASS => [
'FrameworkClass' => [
'someProperty' => 'protected',
],
],
]]);
2020-07-24 11:46:57 +00:00
};
```
```diff
class FrameworkClass
{
protected $someProperty;
}
class MyClass extends FrameworkClass
{
- public $someProperty;
+ protected $someProperty;
}
```
<br><br>
### `FormerNullableArgumentToScalarTypedRector`
- class: [`Rector\Generic\Rector\MethodCall\FormerNullableArgumentToScalarTypedRector`](/rules/generic/src/Rector/MethodCall/FormerNullableArgumentToScalarTypedRector.php)
- [test fixtures](/rules/generic/tests/Rector/MethodCall/FormerNullableArgumentToScalarTypedRector/Fixture)
Change null in argument, that is now not nullable anymore
```diff
final class SomeClass
{
public function run()
{
- $this->setValue(null);
+ $this->setValue('');
}
public function setValue(string $value)
{
}
}
```
<br><br>
### `FuncCallToNewRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Generic\Rector\FuncCall\FuncCallToNewRector`](/rules/generic/src/Rector/FuncCall/FuncCallToNewRector.php)
- [test fixtures](/rules/generic/tests/Rector/FuncCall/FuncCallToNewRector/Fixture)
Change configured function calls to new Instance
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\FuncCall\FuncCallToNewRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(FuncCallToNewRector::class)
->call('configure', [[
FuncCallToNewRector::FUNCTION_TO_NEW => [
'collection' => ['Collection'],
],
]]);
};
```
```diff
class SomeClass
{
public function run()
{
- $array = collection([]);
+ $array = new \Collection([]);
}
}
```
<br><br>
### `FuncCallToStaticCallRector`
- class: [`Rector\Generic\Rector\FuncCall\FuncCallToStaticCallRector`](/rules/generic/src/Rector/FuncCall/FuncCallToStaticCallRector.php)
- [test fixtures](/rules/generic/tests/Rector/FuncCall/FuncCallToStaticCallRector/Fixture)
Turns defined function call to static method call.
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\FuncCall\FuncCallToStaticCallRector;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\ValueObject\FuncCallToStaticCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(FuncCallToStaticCallRector::class)
->call('configure', [[
FuncCallToStaticCallRector::FUNC_CALLS_TO_STATIC_CALLS => inline_value_objects(
[new FuncCallToStaticCall('view', 'SomeStaticClass', 'render')]
),
]]);
};
```
```diff
-view("...", []);
+SomeClass::render("...", []);
```
<br><br>
### `InjectAnnotationClassRector`
2020-05-14 10:26:57 +00:00
- class: [`Rector\Generic\Rector\Property\InjectAnnotationClassRector`](/rules/generic/src/Rector/Property/InjectAnnotationClassRector.php)
- [test fixtures](/rules/generic/tests/Rector/Property/InjectAnnotationClassRector/Fixture)
2020-05-14 10:26:57 +00:00
Changes properties with specified annotations class to constructor injection
2020-05-14 10:26:57 +00:00
```php
<?php
declare(strict_types=1);
use DI\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\Inject;
use Rector\Generic\Rector\Property\InjectAnnotationClassRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(InjectAnnotationClassRector::class)
->call('configure', [[
InjectAnnotationClassRector::ANNOTATION_CLASSES => [Inject::class, Inject::class],
]]);
};
```
2020-05-14 10:26:57 +00:00
```diff
use JMS\DiExtraBundle\Annotation as DI;
2020-05-14 10:26:57 +00:00
class SomeController
2020-05-14 10:26:57 +00:00
{
/**
- * @DI\Inject("entity.manager")
+ * @var EntityManager
*/
private $entityManager;
+
+ public function __construct(EntityManager $entityManager)
2020-05-14 10:26:57 +00:00
+ {
+ $this->entityManager = entityManager;
2020-05-14 10:26:57 +00:00
+ }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-05-14 10:26:57 +00:00
### `MergeInterfacesRector`
2020-05-14 10:26:57 +00:00
- class: [`Rector\Generic\Rector\Class_\MergeInterfacesRector`](/rules/generic/src/Rector/Class_/MergeInterfacesRector.php)
- [test fixtures](/rules/generic/tests/Rector/Class_/MergeInterfacesRector/Fixture)
2020-05-14 10:26:57 +00:00
Merges old interface to a new one, that already has its methods
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\Class_\MergeInterfacesRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(MergeInterfacesRector::class)
->call('configure', [[
MergeInterfacesRector::OLD_TO_NEW_INTERFACES => [
'SomeOldInterface' => SomeInterface::class,
],
]]);
};
```
2020-05-14 10:26:57 +00:00
```diff
-class SomeClass implements SomeInterface, SomeOldInterface
+class SomeClass implements SomeInterface
2020-05-14 10:26:57 +00:00
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-05-14 10:26:57 +00:00
### `MethodCallRemoverRector`
- class: [`Rector\Generic\Rector\MethodCall\MethodCallRemoverRector`](/rules/generic/src/Rector/MethodCall/MethodCallRemoverRector.php)
- [test fixtures](/rules/generic/tests/Rector/MethodCall/MethodCallRemoverRector/Fixture)
Turns "$this->something()->anything()" to "$this->anything()"
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\MethodCall\MethodCallRemoverRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(MethodCallRemoverRector::class)
->call('configure', [[
MethodCallRemoverRector::METHOD_CALL_REMOVER_ARGUMENT => [
'$methodCallRemoverArgument' => [
'Car' => 'something',
],
],
]]);
};
```
```diff
$someObject = new Car;
-$someObject->something()->anything();
+$someObject->anything();
```
<br><br>
### `MethodCallToReturnRector`
2019-02-21 14:36:16 +00:00
- class: [`Rector\Generic\Rector\Expression\MethodCallToReturnRector`](/rules/generic/src/Rector/Expression/MethodCallToReturnRector.php)
- [test fixtures](/rules/generic/tests/Rector/Expression/MethodCallToReturnRector/Fixture)
2019-02-21 14:36:16 +00:00
Wrap method call to return
2019-02-21 14:36:16 +00:00
```php
<?php
2019-02-21 14:36:16 +00:00
declare(strict_types=1);
use Rector\Generic\Rector\Expression\MethodCallToReturnRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(MethodCallToReturnRector::class)
->call('configure', [[
MethodCallToReturnRector::METHOD_CALL_WRAPS => [
'SomeClass' => ['deny'],
],
]]);
};
```
```diff
class SomeClass
{
public function run()
{
- $this->deny();
+ return $this->deny();
}
public function deny()
{
return 1;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `NewObjectToFactoryCreateRector`
- class: [`Rector\Generic\Rector\New_\NewObjectToFactoryCreateRector`](/rules/generic/src/Rector/New_/NewObjectToFactoryCreateRector.php)
- [test fixtures](/rules/generic/tests/Rector/New_/NewObjectToFactoryCreateRector/Fixture)
Replaces creating object instances with "new" keyword with factory method.
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\New_\NewObjectToFactoryCreateRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-06-30 08:46:05 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(NewObjectToFactoryCreateRector::class)
->call('configure', [[
NewObjectToFactoryCreateRector::OBJECT_TO_FACTORY_METHOD => [
'MyClass' => [
'class' => 'MyClassFactory',
'method' => 'create',
],
],
]]);
};
```
2020-06-30 08:46:05 +00:00
2020-06-30 08:46:05 +00:00
```diff
class SomeClass
2020-06-30 08:46:05 +00:00
{
+ /**
+ * @var \MyClassFactory
+ */
+ private $myClassFactory;
+
public function example() {
- new MyClass($argument);
+ $this->myClassFactory->create($argument);
}
2020-06-30 08:46:05 +00:00
}
```
<br><br>
### `NormalToFluentRector`
- class: [`Rector\Generic\Rector\ClassMethod\NormalToFluentRector`](/rules/generic/src/Rector/ClassMethod/NormalToFluentRector.php)
- [test fixtures](/rules/generic/tests/Rector/MethodCall/NormalToFluentRector/Fixture)
Turns fluent interface calls to classic ones.
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\NormalToFluentRector;
use Rector\Generic\ValueObject\NormalToFluent;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(NormalToFluentRector::class)
->call('configure', [[
NormalToFluentRector::CALLS_TO_FLUENT => inline_value_objects(
[new NormalToFluent('SomeClass', ['someFunction', 'otherFunction'])]
),
]]);
};
```
```diff
$someObject = new SomeClass();
-$someObject->someFunction();
-$someObject->otherFunction();
+$someObject->someFunction()
+ ->otherFunction();
```
2019-04-02 13:35:35 +00:00
<br><br>
2019-04-02 13:35:35 +00:00
### `ParentClassToTraitsRector`
2019-04-02 13:35:35 +00:00
- class: [`Rector\Generic\Rector\Class_\ParentClassToTraitsRector`](/rules/generic/src/Rector/Class_/ParentClassToTraitsRector.php)
- [test fixtures](/rules/generic/tests/Rector/Class_/ParentClassToTraitsRector/Fixture)
2019-05-29 13:40:20 +00:00
Replaces parent class to specific traits
2019-04-02 13:35:35 +00:00
```php
<?php
2019-04-02 13:35:35 +00:00
declare(strict_types=1);
use Rector\Generic\Rector\Class_\ParentClassToTraitsRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-11-12 07:01:15 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ParentClassToTraitsRector::class)
->call('configure', [[
ParentClassToTraitsRector::PARENT_CLASS_TO_TRAITS => [
'Nette\Object' => ['Nette\SmartObject'],
],
]]);
};
```
2019-11-12 07:01:15 +00:00
2019-11-12 07:01:15 +00:00
```diff
-class SomeClass extends Nette\Object
+class SomeClass
2019-11-12 07:01:15 +00:00
{
+ use Nette\SmartObject;
2019-11-12 07:01:15 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-11-12 07:01:15 +00:00
### `RemoveAnnotationRector`
- class: [`Rector\Generic\Rector\ClassLike\RemoveAnnotationRector`](/rules/generic/src/Rector/ClassLike/RemoveAnnotationRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassLike/RemoveAnnotationRector/Fixture)
Remove annotation by names
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\ClassLike\RemoveAnnotationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveAnnotationRector::class)
->call('configure', [[
RemoveAnnotationRector::ANNOTATIONS_TO_REMOVE => ['method'],
]]);
};
```
```diff
-/**
- * @method getName()
- */
final class SomeClass
{
}
```
<br><br>
### `RemoveFuncCallArgRector`
2019-04-02 13:35:35 +00:00
- class: [`Rector\Generic\Rector\FuncCall\RemoveFuncCallArgRector`](/rules/generic/src/Rector/FuncCall/RemoveFuncCallArgRector.php)
- [test fixtures](/rules/generic/tests/Rector/FuncCall/RemoveFuncCallArgRector/Fixture)
2019-04-02 13:35:35 +00:00
Remove argument by position by function name
2019-04-02 13:35:35 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\FuncCall\RemoveFuncCallArgRector;
use Rector\Generic\ValueObject\RemoveFuncCallArg;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RemoveFuncCallArgRector::class)
->call('configure', [[
RemoveFuncCallArgRector::REMOVED_FUNCTION_ARGUMENTS => inline_value_objects(
[new RemoveFuncCallArg('remove_last_arg', 1)]
),
]]);
};
```
```diff
-remove_last_arg(1, 2);
+remove_last_arg(1);
2019-04-02 13:35:35 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-04-02 13:35:35 +00:00
### `RemoveIniGetSetFuncCallRector`
2019-04-02 13:35:35 +00:00
- class: [`Rector\Generic\Rector\FuncCall\RemoveIniGetSetFuncCallRector`](/rules/generic/src/Rector/FuncCall/RemoveIniGetSetFuncCallRector.php)
- [test fixtures](/rules/generic/tests/Rector/FuncCall/RemoveIniGetSetFuncCallRector/Fixture)
2019-04-02 13:35:35 +00:00
Remove `ini_get` by configuration
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\FuncCall\RemoveIniGetSetFuncCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RemoveIniGetSetFuncCallRector::class)
->call('configure', [[
RemoveIniGetSetFuncCallRector::KEYS_TO_REMOVE => ['y2k_compliance'],
]]);
};
```
2019-04-02 13:35:35 +00:00
```diff
-ini_get('y2k_compliance');
-ini_set('y2k_compliance', 1);
2019-04-02 13:35:35 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-04-02 13:35:35 +00:00
### `RemoveInterfacesRector`
2019-04-02 13:35:35 +00:00
- class: [`Rector\Generic\Rector\Class_\RemoveInterfacesRector`](/rules/generic/src/Rector/Class_/RemoveInterfacesRector.php)
- [test fixtures](/rules/generic/tests/Rector/Class_/RemoveInterfacesRector/Fixture)
2019-04-02 13:35:35 +00:00
Removes interfaces usage from class.
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\Class_\RemoveInterfacesRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RemoveInterfacesRector::class)
->call('configure', [[
RemoveInterfacesRector::INTERFACES_TO_REMOVE => [SomeInterface::class],
]]);
};
```
2019-04-02 13:35:35 +00:00
```diff
-class SomeClass implements SomeInterface
+class SomeClass
2019-04-02 13:35:35 +00:00
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-04-02 13:35:35 +00:00
### `RemoveParentRector`
- class: [`Rector\Generic\Rector\Class_\RemoveParentRector`](/rules/generic/src/Rector/Class_/RemoveParentRector.php)
- [test fixtures](/rules/generic/tests/Rector/Class_/RemoveParentRector/Fixture)
Removes extends class by name
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\Class_\RemoveParentRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveParentRector::class)
->call('configure', [[
RemoveParentRector::PARENT_TYPES_TO_REMOVE => ['SomeParentClass'],
]]);
};
```
```diff
-final class SomeClass extends SomeParentClass
+final class SomeClass
{
}
```
<br><br>
### `RemoveTraitRector`
2019-12-26 18:35:02 +00:00
- class: [`Rector\Generic\Rector\Class_\RemoveTraitRector`](/rules/generic/src/Rector/Class_/RemoveTraitRector.php)
- [test fixtures](/rules/generic/tests/Rector/Class_/RemoveTraitRector/Fixture)
2019-12-26 18:35:02 +00:00
Remove specific traits from code
2019-12-26 18:35:02 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\Class_\RemoveTraitRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveTraitRector::class)
->call('configure', [[
RemoveTraitRector::TRAITS_TO_REMOVE => ['TraitNameToRemove'],
]]);
};
```
2019-12-26 18:35:02 +00:00
```diff
class SomeClass
2019-12-26 18:35:02 +00:00
{
- use SomeTrait;
2019-12-26 18:35:02 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-26 18:35:02 +00:00
### `RenameClassConstantsUseToStringsRector`
- class: [`Rector\Generic\Rector\ClassConstFetch\RenameClassConstantsUseToStringsRector`](/rules/generic/src/Rector/ClassConstFetch/RenameClassConstantsUseToStringsRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassConstFetch/RenameClassConstantsUseToStringsRector/Fixture)
Replaces constant by value
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\ClassConstFetch\RenameClassConstantsUseToStringsRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RenameClassConstantsUseToStringsRector::class)
->call('configure', [[
RenameClassConstantsUseToStringsRector::OLD_CONSTANTS_TO_NEW_VALUES_BY_TYPE => [
'Nette\Configurator' => [
'DEVELOPMENT' => 'development',
'PRODUCTION' => 'production',
],
],
]]);
};
```
```diff
-$value === Nette\Configurator::DEVELOPMENT
+$value === "development"
```
<br><br>
### `ReplaceParentCallByPropertyCallRector`
- class: [`Rector\Generic\Rector\MethodCall\ReplaceParentCallByPropertyCallRector`](/rules/generic/src/Rector/MethodCall/ReplaceParentCallByPropertyCallRector.php)
- [test fixtures](/rules/transform/tests/Rector/MethodCall/ReplaceParentCallByPropertyCallRector/Fixture)
Changes method calls in child of specific types to defined property method call
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\MethodCall\ReplaceParentCallByPropertyCallRector;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\ValueObject\ReplaceParentCallByPropertyCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ReplaceParentCallByPropertyCallRector::class)
->call('configure', [[
ReplaceParentCallByPropertyCallRector::PARENT_CALLS_TO_PROPERTIES => inline_value_objects(
[new ReplaceParentCallByPropertyCall('SomeTypeToReplace', 'someMethodCall', 'someProperty')]
),
]]);
};
```
```diff
final class SomeClass
{
public function run(SomeTypeToReplace $someTypeToReplace)
{
- $someTypeToReplace->someMethodCall();
+ $this->someProperty->someMethodCall();
}
}
```
<br><br>
### `ReplaceVariableByPropertyFetchRector`
- class: [`Rector\Generic\Rector\Variable\ReplaceVariableByPropertyFetchRector`](/rules/generic/src/Rector/Variable/ReplaceVariableByPropertyFetchRector.php)
Turns variable in controller action to property fetch, as follow up to action injection variable to property change.
```diff
final class SomeController
{
/**
* @var ProductRepository
*/
private $productRepository;
public function __construct(ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
}
public function default()
{
- $products = $productRepository->fetchAll();
+ $products = $this->productRepository->fetchAll();
}
}
```
<br><br>
### `StringToClassConstantRector`
2020-05-29 10:41:25 +00:00
- class: [`Rector\Generic\Rector\String_\StringToClassConstantRector`](/rules/generic/src/Rector/String_/StringToClassConstantRector.php)
- [test fixtures](/rules/generic/tests/Rector/String_/StringToClassConstantRector/Fixture)
2020-05-29 10:41:25 +00:00
Changes strings to specific constants
2020-05-29 10:41:25 +00:00
```php
<?php
2020-05-29 10:41:25 +00:00
declare(strict_types=1);
use Rector\Generic\Rector\String_\StringToClassConstantRector;
use Rector\Generic\ValueObject\StringToClassConstant;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-05-29 10:41:25 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(StringToClassConstantRector::class)
->call('configure', [[
StringToClassConstantRector::STRINGS_TO_CLASS_CONSTANTS => inline_value_objects(
[new StringToClassConstant('compiler.post_dump', 'Yet\AnotherClass', 'CONSTANT')]
),
]]);
};
```
2020-05-29 10:41:25 +00:00
```diff
final class SomeSubscriber
{
public static function getSubscribedEvents()
2020-05-29 10:41:25 +00:00
{
- return ['compiler.post_dump' => 'compile'];
+ return [\Yet\AnotherClass::CONSTANT => 'compile'];
2020-05-29 10:41:25 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SwapClassMethodArgumentsRector`
2020-05-29 10:41:25 +00:00
- class: [`Rector\Generic\Rector\StaticCall\SwapClassMethodArgumentsRector`](/rules/generic/src/Rector/StaticCall/SwapClassMethodArgumentsRector.php)
- [test fixtures](/rules/generic/tests/Rector/StaticCall/SwapClassMethodArgumentsRector/Fixture)
2020-05-29 10:41:25 +00:00
Reorder class method arguments, including their calls
2020-05-29 10:41:25 +00:00
```php
<?php
2020-05-29 10:41:25 +00:00
declare(strict_types=1);
use Rector\Generic\Rector\StaticCall\SwapClassMethodArgumentsRector;
use Rector\Generic\ValueObject\SwapClassMethodArguments;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-05-29 10:41:25 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(SwapClassMethodArgumentsRector::class)
->call('configure', [[
SwapClassMethodArgumentsRector::ARGUMENT_SWAPS => inline_value_objects(
[new SwapClassMethodArguments('SomeClass', 'run', [1, 0])]
),
]]);
};
```
```diff
class SomeClass
{
- public static function run($first, $second)
+ public static function run($second, $first)
{
- self::run($first, $second);
+ self::run($second, $first);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SwapFuncCallArgumentsRector`
- class: [`Rector\Generic\Rector\FuncCall\SwapFuncCallArgumentsRector`](/rules/generic/src/Rector/FuncCall/SwapFuncCallArgumentsRector.php)
- [test fixtures](/rules/generic/tests/Rector/FuncCall/SwapFuncCallArgumentsRector/Fixture)
Swap arguments in function calls
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\FuncCall\SwapFuncCallArgumentsRector;
use Rector\Generic\ValueObject\SwapFuncCallArguments;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(SwapFuncCallArgumentsRector::class)
->call('configure', [[
SwapFuncCallArgumentsRector::FUNCTION_ARGUMENT_SWAPS => inline_value_objects(
[new SwapFuncCallArguments('some_function', [1, 0])]
),
]]);
};
```
```diff
final class SomeClass
{
public function run($one, $two)
{
- return some_function($one, $two);
+ return some_function($two, $one);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `WrapReturnRector`
2019-03-16 20:31:46 +00:00
- class: [`Rector\Generic\Rector\ClassMethod\WrapReturnRector`](/rules/generic/src/Rector/ClassMethod/WrapReturnRector.php)
- [test fixtures](/rules/generic/tests/Rector/ClassMethod/WrapReturnRector/Fixture)
Wrap return value of specific method
```php
<?php
declare(strict_types=1);
use Rector\Generic\Rector\ClassMethod\WrapReturnRector;
use Rector\Generic\ValueObject\WrapReturn;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(WrapReturnRector::class)
->call('configure', [[
WrapReturnRector::TYPE_METHOD_WRAPS => inline_value_objects([new WrapReturn('SomeClass', 'getItem', true)]),
]]);
};
```
```diff
final class SomeClass
{
public function getItem()
{
- return 1;
+ return [1];
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## JMS
2019-02-02 16:22:15 +00:00
### `RemoveJmsInjectParamsAnnotationRector`
2019-02-02 16:22:15 +00:00
- class: [`Rector\JMS\Rector\ClassMethod\RemoveJmsInjectParamsAnnotationRector`](/rules/jms/src/Rector/ClassMethod/RemoveJmsInjectParamsAnnotationRector.php)
- [test fixtures](/rules/jms/tests/Rector/ClassMethod/RemoveJmsInjectParamsAnnotationRector/Fixture)
Removes `JMS\DiExtraBundle\Annotation\InjectParams` annotation
2019-02-02 16:22:15 +00:00
```diff
use JMS\DiExtraBundle\Annotation as DI;
class SomeClass
{
- /**
- * @DI\InjectParams({
- * "subscribeService" = @DI\Inject("app.email.service.subscribe"),
- * "ipService" = @DI\Inject("app.util.service.ip")
- * })
- */
public function __construct()
{
}
-}
+}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-02 16:22:15 +00:00
### `RemoveJmsInjectServiceAnnotationRector`
- class: [`Rector\JMS\Rector\Class_\RemoveJmsInjectServiceAnnotationRector`](/rules/jms/src/Rector/Class_/RemoveJmsInjectServiceAnnotationRector.php)
- [test fixtures](/rules/jms/tests/Rector/Class_/RemoveJmsInjectServiceAnnotationRector/Fixture)
Removes JMS\DiExtraBundle\Annotation\Services annotation
```diff
use JMS\DiExtraBundle\Annotation as DI;
-/**
- * @DI\Service("email.web.services.subscribe_token", public=true)
- */
class SomeClass
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## Laravel
2019-12-22 18:38:09 +00:00
### `MinutesToSecondsInCacheRector`
2019-02-18 15:51:24 +00:00
- class: [`Rector\Laravel\Rector\StaticCall\MinutesToSecondsInCacheRector`](/rules/laravel/src/Rector/StaticCall/MinutesToSecondsInCacheRector.php)
- [test fixtures](/rules/laravel/tests/Rector/StaticCall/MinutesToSecondsInCacheRector/Fixture)
2019-02-18 15:51:24 +00:00
Change minutes argument to seconds in `Illuminate\Contracts\Cache\Store` and Illuminate\Support\Facades\Cache
2019-02-18 15:51:24 +00:00
```diff
class SomeClass
2019-02-18 15:51:24 +00:00
{
public function run()
{
- Illuminate\Support\Facades\Cache::put('key', 'value', 60);
+ Illuminate\Support\Facades\Cache::put('key', 'value', 60 * 60);
}
2019-02-18 15:51:24 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-18 15:51:24 +00:00
### `Redirect301ToPermanentRedirectRector`
2019-02-18 15:51:24 +00:00
- class: [`Rector\Laravel\Rector\StaticCall\Redirect301ToPermanentRedirectRector`](/rules/laravel/src/Rector/StaticCall/Redirect301ToPermanentRedirectRector.php)
- [test fixtures](/rules/laravel/tests/Rector/StaticCall/Redirect301ToPermanentRedirectRector/Fixture)
2019-02-18 15:51:24 +00:00
Change "redirect" call with 301 to "permanentRedirect"
2019-02-18 15:51:24 +00:00
```diff
class SomeClass
2019-02-18 15:51:24 +00:00
{
public function run()
2019-02-18 15:51:24 +00:00
{
- Illuminate\Routing\Route::redirect('/foo', '/bar', 301);
+ Illuminate\Routing\Route::permanentRedirect('/foo', '/bar');
2019-02-18 15:51:24 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-18 15:51:24 +00:00
### `RequestStaticValidateToInjectRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\Laravel\Rector\StaticCall\RequestStaticValidateToInjectRector`](/rules/laravel/src/Rector/StaticCall/RequestStaticValidateToInjectRector.php)
- [test fixtures](/rules/laravel/tests/Rector/StaticCall/RequestStaticValidateToInjectRector/Fixture)
2019-03-31 12:25:39 +00:00
Change static `validate()` method to `$request->validate()`
2019-03-31 12:25:39 +00:00
```diff
use Illuminate\Http\Request;
2019-03-31 12:25:39 +00:00
class SomeClass
2019-03-31 12:25:39 +00:00
{
- public function store()
+ public function store(\Illuminate\Http\Request $request)
2019-05-29 13:40:20 +00:00
{
- $validatedData = Request::validate(['some_attribute' => 'required']);
+ $validatedData = $request->validate(['some_attribute' => 'required']);
2019-05-29 13:40:20 +00:00
}
2019-03-31 12:25:39 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
## Legacy
2019-03-31 12:25:39 +00:00
### `AddTopIncludeRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\Legacy\Rector\FileWithoutNamespace\AddTopIncludeRector`](/rules/legacy/src/Rector/FileWithoutNamespace/AddTopIncludeRector.php)
- [test fixtures](/rules/legacy/tests/Rector/FileWithoutNamespace/AddTopIncludeRector/Fixture)
2019-03-31 12:25:39 +00:00
Adds an include file at the top of matching files, except class definitions
2019-05-29 13:40:20 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Legacy\Rector\FileWithoutNamespace\AddTopIncludeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddTopIncludeRector::class)
->call('configure', [[
AddTopIncludeRector::AUTOLOAD_FILE_PATH => '/../autoloader.php',
2020-10-07 14:20:53 +00:00
AddTopIncludeRector::PATTERNS => ['pat*/*/?ame.php', 'somepath/?ame.php'],
]]);
};
```
```diff
+require_once __DIR__ . '/../autoloader.php';
2019-09-19 09:27:29 +00:00
+
if (isset($_POST['csrf'])) {
processPost($_POST);
2019-03-31 12:25:39 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `ChangeSingletonToServiceRector`
- class: [`Rector\Legacy\Rector\Class_\ChangeSingletonToServiceRector`](/rules/legacy/src/Rector/Class_/ChangeSingletonToServiceRector.php)
- [test fixtures](/rules/legacy/tests/Rector/Class_/ChangeSingletonToServiceRector/Fixture)
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;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `FunctionToStaticMethodRector`
- class: [`Rector\Legacy\Rector\FileWithoutNamespace\FunctionToStaticMethodRector`](/rules/legacy/src/Rector/FileWithoutNamespace/FunctionToStaticMethodRector.php)
- [test fixtures](/rules/legacy/tests/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/Fixture)
Change functions to static calls, so composer can autoload them
```diff
-function some_function()
+class SomeUtilsClass
{
+ public static function someFunction()
+ {
+ }
}
-some_function('lol');
+SomeUtilsClass::someFunction('lol');
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveIncludeRector`
- class: [`Rector\Legacy\Rector\Include_\RemoveIncludeRector`](/rules/legacy/src/Rector/Include_/RemoveIncludeRector.php)
- [test fixtures](/rules/legacy/tests/Rector/Include_/RemoveIncludeRector/Fixture)
Remove includes (include, include_once, require, require_once) from source
```diff
// Comment before require
-include 'somefile.php';
+
// Comment after require
```
2020-06-16 16:14:51 +00:00
<br><br>
## MagicDisclosure
### `GetAndSetToMethodCallRector`
- class: [`Rector\MagicDisclosure\Rector\Assign\GetAndSetToMethodCallRector`](/rules/magic-disclosure/src/Rector/Assign/GetAndSetToMethodCallRector.php)
- [test fixtures](/rules/magic-disclosure/tests/Rector/Assign/GetAndSetToMethodCallRector/Fixture)
Turns defined `__get`/`__set` to specific method calls.
```php
<?php
declare(strict_types=1);
use Rector\MagicDisclosure\Rector\Assign\GetAndSetToMethodCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(GetAndSetToMethodCallRector::class)
->call('configure', [[
GetAndSetToMethodCallRector::TYPE_TO_METHOD_CALLS => [
'SomeContainer' => [
'set' => 'addService',
],
],
]]);
};
```
```diff
$container = new SomeContainer;
-$container->someService = $someService;
+$container->setService("someService", $someService);
```
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use Rector\MagicDisclosure\Rector\Assign\GetAndSetToMethodCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(GetAndSetToMethodCallRector::class)
->call('configure', [[
GetAndSetToMethodCallRector::TYPE_TO_METHOD_CALLS => [
'SomeContainer' => [
'get' => 'getService',
],
],
]]);
};
```
```diff
$container = new SomeContainer;
-$someService = $container->someService;
+$someService = $container->getService("someService");
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ToStringToMethodCallRector`
- class: [`Rector\MagicDisclosure\Rector\String_\ToStringToMethodCallRector`](/rules/magic-disclosure/src/Rector/String_/ToStringToMethodCallRector.php)
- [test fixtures](/rules/magic-disclosure/tests/Rector/String_/ToStringToMethodCallRector/Fixture)
Turns defined code uses of "__toString()" method to specific method calls.
```php
<?php
declare(strict_types=1);
use Rector\MagicDisclosure\Rector\String_\ToStringToMethodCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ToStringToMethodCallRector::class)
->call('configure', [[
ToStringToMethodCallRector::METHOD_NAMES_BY_TYPE => [
'SomeObject' => 'getPath',
],
]]);
};
```
```diff
$someValue = new SomeObject;
-$result = (string) $someValue;
-$result = $someValue->__toString();
+$result = $someValue->getPath();
+$result = $someValue->getPath();
```
2020-06-16 16:14:51 +00:00
<br><br>
### `UnsetAndIssetToMethodCallRector`
- class: [`Rector\MagicDisclosure\Rector\Isset_\UnsetAndIssetToMethodCallRector`](/rules/magic-disclosure/src/Rector/Isset_/UnsetAndIssetToMethodCallRector.php)
- [test fixtures](/rules/magic-disclosure/tests/Rector/Isset_/UnsetAndIssetToMethodCallRector/Fixture)
Turns defined `__isset`/`__unset` calls to specific method calls.
```php
<?php
declare(strict_types=1);
use Rector\MagicDisclosure\Rector\Isset_\UnsetAndIssetToMethodCallRector;
use Rector\MagicDisclosure\ValueObject\IssetUnsetToMethodCall;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(UnsetAndIssetToMethodCallRector::class)
->call('configure', [[
UnsetAndIssetToMethodCallRector::ISSET_UNSET_TO_METHOD_CALL => inline_value_objects(
[new IssetUnsetToMethodCall('SomeContainer', 'hasService', 'removeService')]
),
]]);
};
```
```diff
$container = new SomeContainer;
-isset($container["someKey"]);
+$container->hasService("someKey");
```
```php
<?php
declare(strict_types=1);
use Rector\MagicDisclosure\Rector\Isset_\UnsetAndIssetToMethodCallRector;
use Rector\MagicDisclosure\ValueObject\IssetUnsetToMethodCall;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(UnsetAndIssetToMethodCallRector::class)
->call('configure', [[
UnsetAndIssetToMethodCallRector::ISSET_UNSET_TO_METHOD_CALL => inline_value_objects(
[new IssetUnsetToMethodCall('SomeContainer', 'hasService', 'removeService')]
),
]]);
};
```
```diff
$container = new SomeContainer;
-unset($container["someKey"]);
+$container->removeService("someKey");
```
2020-06-16 16:14:51 +00:00
<br><br>
## MockeryToProphecy
### `MockeryCloseRemoveRector`
- class: [`Rector\MockeryToProphecy\Rector\StaticCall\MockeryCloseRemoveRector`](/rules/mockery-to-prophecy/src/Rector/StaticCall/MockeryCloseRemoveRector.php)
Removes mockery close from test classes
```diff
public function tearDown() : void
{
- \Mockery::close();
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MockeryCreateMockToProphizeRector`
- class: [`Rector\MockeryToProphecy\Rector\ClassMethod\MockeryCreateMockToProphizeRector`](/rules/mockery-to-prophecy/src/Rector/ClassMethod/MockeryCreateMockToProphizeRector.php)
Changes mockery mock creation to Prophesize
```diff
-$mock = \Mockery::mock(\'MyClass\');
+ $mock = $this->prophesize(\'MyClass\');
+
$service = new Service();
-$service->injectDependency($mock);
+$service->injectDependency($mock->reveal());
```
2020-06-16 16:14:51 +00:00
<br><br>
## MockistaToMockery
### `MockeryTearDownRector`
- class: [`Rector\MockistaToMockery\Rector\Class_\MockeryTearDownRector`](/rules/mockista-to-mockery/src/Rector/Class_/MockeryTearDownRector.php)
- [test fixtures](/rules/mockista-to-mockery/tests/Rector/Class_/MockeryTearDownRector/Fixture)
Add `Mockery::close()` in `tearDown()` method if not yet
```diff
use PHPUnit\Framework\TestCase;
class SomeTest extends TestCase
{
+ protected function tearDown(): void
+ {
+ Mockery::close();
+ }
public function test()
{
$mockUser = mock(User::class);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MockistaMockToMockeryMockRector`
- class: [`Rector\MockistaToMockery\Rector\ClassMethod\MockistaMockToMockeryMockRector`](/rules/mockista-to-mockery/src/Rector/ClassMethod/MockistaMockToMockeryMockRector.php)
- [test fixtures](/rules/mockista-to-mockery/tests/Rector/ClassMethod/MockistaMockToMockeryMockRector/Fixture)
Change functions to static calls, so composer can autoload them
```diff
class SomeTest
{
public function run()
{
- $mockUser = mock(User::class);
- $mockUser->getId()->once->andReturn(1);
- $mockUser->freeze();
+ $mockUser = Mockery::mock(User::class);
+ $mockUser->expects()->getId()->once()->andReturn(1);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## MysqlToMysqli
### `MysqlAssignToMysqliRector`
- class: [`Rector\MysqlToMysqli\Rector\Assign\MysqlAssignToMysqliRector`](/rules/mysql-to-mysqli/src/Rector/Assign/MysqlAssignToMysqliRector.php)
- [test fixtures](/rules/mysql-to-mysqli/tests/Rector/Assign/MysqlAssignToMysqliRector/Fixture)
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><br>
### `MysqlFuncCallToMysqliRector`
- class: [`Rector\MysqlToMysqli\Rector\FuncCall\MysqlFuncCallToMysqliRector`](/rules/mysql-to-mysqli/src/Rector/FuncCall/MysqlFuncCallToMysqliRector.php)
- [test fixtures](/rules/mysql-to-mysqli/tests/Rector/FuncCall/MysqlFuncCallToMysqliRector/Fixture)
Converts more complex mysql functions to mysqli
```diff
-mysql_drop_db($database);
+mysqli_query('DROP DATABASE ' . $database);
```
<br><br>
### `MysqlPConnectToMysqliConnectRector`
- class: [`Rector\MysqlToMysqli\Rector\FuncCall\MysqlPConnectToMysqliConnectRector`](/rules/mysql-to-mysqli/src/Rector/FuncCall/MysqlPConnectToMysqliConnectRector.php)
- [test fixtures](/rules/mysql-to-mysqli/tests/Rector/FuncCall/MysqlPConnectToMysqliConnectRector/Fixture)
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);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MysqlQueryMysqlErrorWithLinkRector`
- class: [`Rector\MysqlToMysqli\Rector\FuncCall\MysqlQueryMysqlErrorWithLinkRector`](/rules/mysql-to-mysqli/src/Rector/FuncCall/MysqlQueryMysqlErrorWithLinkRector.php)
- [test fixtures](/rules/mysql-to-mysqli/tests/Rector/FuncCall/MysqlQueryMysqlErrorWithLinkRector/Fixture)
Add mysql_query and mysql_error with connection
```diff
class SomeClass
{
public function run()
{
$conn = mysqli_connect('host', 'user', 'pass');
- mysql_error();
+ mysqli_error($conn);
$sql = 'SELECT';
- return mysql_query($sql);
+ return mysqli_query($conn, $sql);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## Naming
### `MakeBoolPropertyRespectIsHasWasMethodNamingRector`
- class: [`Rector\Naming\Rector\Property\MakeBoolPropertyRespectIsHasWasMethodNamingRector`](/rules/naming/src/Rector/Property/MakeBoolPropertyRespectIsHasWasMethodNamingRector.php)
- [test fixtures](/rules/naming/tests/Rector/Property/MakeBoolPropertyRespectIsHasWasMethodNamingRector/Fixture)
Renames property to respect is/has/was method naming
```diff
class SomeClass
{
- private $full = false;
+ private $isFull = false;
public function isFull()
{
- return $this->full;
+ return $this->isFull;
}
+
}
```
<br><br>
### `MakeGetterClassMethodNameStartWithGetRector`
- class: [`Rector\Naming\Rector\ClassMethod\MakeGetterClassMethodNameStartWithGetRector`](/rules/naming/src/Rector/ClassMethod/MakeGetterClassMethodNameStartWithGetRector.php)
- [test fixtures](/rules/naming/tests/Rector/ClassMethod/MakeGetterClassMethodNameStartWithGetRector/Fixture)
Change getter method names to start with get/provide
```diff
class SomeClass
{
/**
* @var string
*/
private $name;
- public function name(): string
+ public function getName(): string
{
return $this->name;
}
}
```
<br><br>
### `MakeIsserClassMethodNameStartWithIsRector`
- class: [`Rector\Naming\Rector\ClassMethod\MakeIsserClassMethodNameStartWithIsRector`](/rules/naming/src/Rector/ClassMethod/MakeIsserClassMethodNameStartWithIsRector.php)
- [test fixtures](/rules/naming/tests/Rector/ClassMethod/MakeIsserClassMethodNameStartWithIsRector/Fixture)
Change is method names to start with is/has/was
```diff
class SomeClass
{
/**
* @var bool
*/
private $isActive = false;
- public function getIsActive()
+ public function isActive()
{
return $this->isActive;
}
}
```
<br><br>
### `RenameForeachValueVariableToMatchMethodCallReturnTypeRector`
- class: [`Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector`](/rules/naming/src/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector.php)
- [test fixtures](/rules/naming/tests/Rector/Foreach_/RenameForeachValueVariableToMatchMethodCallReturnTypeRector/Fixture)
Renames value variable name in foreach loop to match method type
```diff
class SomeClass
{
public function run()
{
$array = [];
- foreach ($object->getMethods() as $property) {
- $array[] = $property;
+ foreach ($object->getMethods() as $method) {
+ $array[] = $method;
}
}
}
```
<br><br>
### `RenameParamToMatchTypeRector`
- class: [`Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector`](/rules/naming/src/Rector/ClassMethod/RenameParamToMatchTypeRector.php)
- [test fixtures](/rules/naming/tests/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture)
Rename variable to match new ClassType
```diff
final class SomeClass
{
- public function run(Apple $pie)
+ public function run(Apple $apple)
{
- $food = $pie;
+ $food = $apple;
}
}
```
<br><br>
### `RenamePropertyToMatchTypeRector`
- class: [`Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector`](/rules/naming/src/Rector/Class_/RenamePropertyToMatchTypeRector.php)
- [test fixtures](/rules/naming/tests/Rector/Class_/RenamePropertyToMatchTypeRector/Fixture)
Rename property and method param to match its type
```diff
class SomeClass
{
/**
* @var EntityManager
*/
- private $eventManager;
+ private $entityManager;
- public function __construct(EntityManager $eventManager)
+ public function __construct(EntityManager $entityManager)
{
- $this->eventManager = $eventManager;
+ $this->entityManager = $entityManager;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameVariableToMatchMethodCallReturnTypeRector`
- class: [`Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector`](/rules/naming/src/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php)
- [test fixtures](/rules/naming/tests/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector/Fixture)
Rename variable to match method return type
```diff
class SomeClass
{
public function run()
{
- $a = $this->getRunner();
+ $runner = $this->getRunner();
}
public function getRunner(): Runner
{
return new Runner();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameVariableToMatchNewTypeRector`
- class: [`Rector\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector`](/rules/naming/src/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php)
- [test fixtures](/rules/naming/tests/Rector/ClassMethod/RenameVariableToMatchNewTypeRector/Fixture)
Rename variable to match new ClassType
```diff
final class SomeClass
{
public function run()
{
- $search = new DreamSearch();
- $search->advance();
+ $dreamSearch = new DreamSearch();
+ $dreamSearch->advance();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `UnderscoreToCamelCaseLocalVariableNameRector`
- class: [`Rector\Naming\Rector\Variable\UnderscoreToCamelCaseLocalVariableNameRector`](/rules/naming/src/Rector/Variable/UnderscoreToCamelCaseLocalVariableNameRector.php)
- [test fixtures](/rules/naming/tests/Rector/Variable/UnderscoreToCamelCaseLocalVariableNameRector/Fixture)
Change under_score local variable names to camelCase
```diff
final class SomeClass
{
public function run($a_b)
{
- $some_value = $a_b;
+ $someValue = $a_b;
}
}
```
<br><br>
### `UnderscoreToCamelCasePropertyNameRector`
- class: [`Rector\Naming\Rector\Property\UnderscoreToCamelCasePropertyNameRector`](/rules/naming/src/Rector/Property/UnderscoreToCamelCasePropertyNameRector.php)
- [test fixtures](/rules/naming/tests/Rector/Property/UnderscoreToCamelCasePropertyNameRector/Fixture)
Change under_score names to camelCase
```diff
final class SomeClass
{
- public $property_name;
+ public $propertyName;
public function run($a)
{
- $this->property_name = 5;
+ $this->propertyName = 5;
}
}
```
<br><br>
### `UnderscoreToCamelCaseVariableNameRector`
- class: [`Rector\Naming\Rector\Variable\UnderscoreToCamelCaseVariableNameRector`](/rules/naming/src/Rector/Variable/UnderscoreToCamelCaseVariableNameRector.php)
- [test fixtures](/rules/naming/tests/Rector/Variable/UnderscoreToCamelCaseVariableNameRector/Fixture)
Change under_score names to camelCase
```diff
final class SomeClass
{
- public function run($a_b)
+ public function run($aB)
{
- $some_value = $a_b;
+ $someValue = $aB;
}
}
```
<br><br>
## Nette
### `AddDatePickerToDateControlRector`
- class: [`Rector\Nette\Rector\MethodCall\AddDatePickerToDateControlRector`](/rules/nette/src/Rector/MethodCall/AddDatePickerToDateControlRector.php)
- [test fixtures](/rules/nette/tests/Rector/MethodCall/AddDatePickerToDateControlRector/Fixture)
Nextras/Form upgrade of addDatePicker method call to DateControl assign
```diff
use Nette\Application\UI\Form;
class SomeClass
{
public function run()
{
$form = new Form();
- $form->addDatePicker('key', 'Label');
+ $form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label');
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `BuilderExpandToHelperExpandRector`
- class: [`Rector\Nette\Rector\MethodCall\BuilderExpandToHelperExpandRector`](/rules/nette/src/Rector/MethodCall/BuilderExpandToHelperExpandRector.php)
- [test fixtures](/rules/nette/tests/Rector/MethodCall/BuilderExpandToHelperExpandRector/Fixture)
Change `containerBuilder->expand()` to static call with parameters
```diff
use Nette\DI\CompilerExtension;
final class SomeClass extends CompilerExtension
{
public function loadConfiguration()
{
- $value = $this->getContainerBuilder()->expand('%value');
+ $value = \Nette\DI\Helpers::expand('%value', $this->getContainerBuilder()->parameters);
}
}
```
<br><br>
### `ContextGetByTypeToConstructorInjectionRector`
- class: [`Rector\Nette\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector`](/rules/nette/src/Rector/MethodCall/ContextGetByTypeToConstructorInjectionRector.php)
- [test fixtures](/rules/nette/tests/Rector/MethodCall/ContextGetByTypeToConstructorInjectionRector/Fixture)
Move dependency get via `$context->getByType()` to constructor injection
```diff
class SomeClass
{
/**
* @var \Nette\DI\Container
*/
private $context;
+ /**
+ * @var SomeTypeToInject
+ */
+ private $someTypeToInject;
+
+ public function __construct(SomeTypeToInject $someTypeToInject)
+ {
+ $this->someTypeToInject = $someTypeToInject;
+ }
+
public function run()
{
- $someTypeToInject = $this->context->getByType(SomeTypeToInject::class);
+ $someTypeToInject = $this->someTypeToInject;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `EndsWithFunctionToNetteUtilsStringsRector`
2019-08-05 21:10:47 +00:00
- class: [`Rector\Nette\Rector\Identical\EndsWithFunctionToNetteUtilsStringsRector`](/rules/nette/src/Rector/Identical/EndsWithFunctionToNetteUtilsStringsRector.php)
- [test fixtures](/rules/nette/tests/Rector/Identical/EndsWithFunctionToNetteUtilsStringsRector/Fixture)
2019-08-05 21:10:47 +00:00
Use `Nette\Utils\Strings::endWith()` over bare string-functions
2019-08-05 21:10:47 +00:00
```diff
class SomeClass
{
public function end($needle)
2019-08-05 21:10:47 +00:00
{
$content = 'Hi, my name is Tom';
- $yes = substr($content, -strlen($needle)) === $needle;
+ $yes = \Nette\Utils\Strings::endsWith($content, $needle);
2019-08-05 21:10:47 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `FilePutContentsToFileSystemWriteRector`
2019-10-15 14:46:31 +00:00
- class: [`Rector\Nette\Rector\FuncCall\FilePutContentsToFileSystemWriteRector`](/rules/nette/src/Rector/FuncCall/FilePutContentsToFileSystemWriteRector.php)
- [test fixtures](/rules/nette/tests/Rector/FuncCall/FilePutContentsToFileSystemWriteRector/Fixture)
2019-10-15 14:46:31 +00:00
Change `file_put_contents()` to `FileSystem::write()`
2019-10-15 14:46:31 +00:00
```diff
class SomeClass
2019-10-15 14:46:31 +00:00
{
public function run()
2019-10-15 14:46:31 +00:00
{
- file_put_contents('file.txt', 'content');
+ \Nette\Utils\FileSystem::write('file.txt', 'content');
file_put_contents('file.txt', 'content_to_append', FILE_APPEND);
2019-10-15 14:46:31 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-15 14:46:31 +00:00
### `GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector`
2020-04-01 00:05:51 +00:00
- class: [`Rector\Nette\Rector\MethodCall\GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector`](/rules/nette/src/Rector/MethodCall/GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector.php)
- [test fixtures](/rules/nette/tests/Rector/MethodCall/GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector/Fixture)
2020-04-01 00:05:51 +00:00
Change `$this->getConfig($defaults)` to `array_merge`
2020-04-01 00:05:51 +00:00
```diff
use Nette\DI\CompilerExtension;
2020-04-01 00:05:51 +00:00
final class SomeExtension extends CompilerExtension
2020-04-01 00:05:51 +00:00
{
private $defaults = [
'key' => 'value'
];
public function loadConfiguration()
2020-04-01 00:05:51 +00:00
{
- $config = $this->getConfig($this->defaults);
+ $config = array_merge($this->defaults, $this->getConfig());
2020-04-01 00:05:51 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-04-01 00:05:51 +00:00
### `JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector`
- class: [`Rector\Nette\Rector\FuncCall\JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector`](/rules/nette/src/Rector/FuncCall/JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector.php)
- [test fixtures](/rules/nette/tests/Rector/FuncCall/JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector/Fixture)
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);
}
2020-02-11 13:49:32 +00:00
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);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-08-03 22:21:59 +00:00
### `MagicHtmlCallToAppendAttributeRector`
- class: [`Rector\Nette\Rector\MethodCall\MagicHtmlCallToAppendAttributeRector`](/rules/nette/src/Rector/MethodCall/MagicHtmlCallToAppendAttributeRector.php)
- [test fixtures](/rules/nette/tests/Rector/MethodCall/MagicHtmlCallToAppendAttributeRector/Fixture)
2020-08-03 22:21:59 +00:00
Change magic `addClass()` etc. calls on Html to explicit methods
```diff
use Nette\Utils\Html;
final class SomeClass
{
public function run()
{
$html = Html::el();
- $html->setClass('first');
+ $html->appendAttribute('class', 'first');
}
}
```
<br><br>
### `PregFunctionToNetteUtilsStringsRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Nette\Rector\FuncCall\PregFunctionToNetteUtilsStringsRector`](/rules/nette/src/Rector/FuncCall/PregFunctionToNetteUtilsStringsRector.php)
- [test fixtures](/rules/nette/tests/Rector/FuncCall/PregFunctionToNetteUtilsStringsRector/Fixture)
Use `Nette\Utils\Strings` over bare `preg_split()` and `preg_replace()` functions
```diff
+use Nette\Utils\Strings;
+
class SomeClass
{
public function run()
{
$content = 'Hi my name is Tom';
- $splitted = preg_split('#Hi#', $content);
+ $splitted = \Nette\Utils\Strings::split($content, '#Hi#');
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `PregMatchFunctionToNetteUtilsStringsRector`
2018-10-21 22:26:45 +00:00
- class: [`Rector\Nette\Rector\FuncCall\PregMatchFunctionToNetteUtilsStringsRector`](/rules/nette/src/Rector/FuncCall/PregMatchFunctionToNetteUtilsStringsRector.php)
- [test fixtures](/rules/nette/tests/Rector/FuncCall/PregMatchFunctionToNetteUtilsStringsRector/Fixture)
2018-10-21 22:26:45 +00:00
Use `Nette\Utils\Strings` over bare `preg_match()` and `preg_match_all()` functions
```diff
+use Nette\Utils\Strings;
+
class SomeClass
{
public function run()
{
$content = 'Hi my name is Tom';
- preg_match('#Hi#', $content, $matches);
+ $matches = Strings::match($content, '#Hi#');
}
}
2018-12-14 19:35:35 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-14 19:35:35 +00:00
2020-10-09 20:01:37 +00:00
### `RequestGetCookieDefaultArgumentToCoalesceRector`
- class: [`Rector\Nette\Rector\MethodCall\RequestGetCookieDefaultArgumentToCoalesceRector`](/rules/nette/src/Rector/MethodCall/RequestGetCookieDefaultArgumentToCoalesceRector.php)
- [test fixtures](/rules/nette/tests/Rector/MethodCall/RequestGetCookieDefaultArgumentToCoalesceRector/Fixture)
Add removed `Nette\Http\Request::getCookies()` default value as coalesce
```diff
use Nette\Http\Request;
class SomeClass
{
public function run(Request $request)
{
- return $request->getCookie('name', 'default');
+ return $request->getCookie('name') ?? 'default';
}
}
```
<br><br>
### `SetClassWithArgumentToSetFactoryRector`
2018-12-14 19:35:35 +00:00
- class: [`Rector\Nette\Rector\MethodCall\SetClassWithArgumentToSetFactoryRector`](/rules/nette/src/Rector/MethodCall/SetClassWithArgumentToSetFactoryRector.php)
- [test fixtures](/rules/nette/tests/Rector/MethodCall/SetClassWithArgumentToSetFactoryRector/Fixture)
2019-05-29 13:40:20 +00:00
Change setClass with class and arguments to separated methods
2019-05-29 13:40:20 +00:00
```diff
use Nette\DI\ContainerBuilder;
2019-05-29 13:40:20 +00:00
class SomeClass
{
public function run(ContainerBuilder $containerBuilder)
{
$containerBuilder->addDefinition('...')
- ->setClass('SomeClass', [1, 2]);
+ ->setFactory('SomeClass', [1, 2]);
}
}
2019-05-29 13:40:20 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `StartsWithFunctionToNetteUtilsStringsRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Nette\Rector\Identical\StartsWithFunctionToNetteUtilsStringsRector`](/rules/nette/src/Rector/Identical/StartsWithFunctionToNetteUtilsStringsRector.php)
- [test fixtures](/rules/nette/tests/Rector/Identical/StartsWithFunctionToNetteUtilsStringsRector/Fixture)
2018-12-14 19:35:35 +00:00
Use `Nette\Utils\Strings::startsWith()` over bare string-functions
2018-12-14 19:35:35 +00:00
```diff
class SomeClass
2018-12-14 19:35:35 +00:00
{
public function start($needle)
2018-12-14 19:35:35 +00:00
{
$content = 'Hi, my name is Tom';
- $yes = substr($content, 0, strlen($needle)) === $needle;
+ $yes = \Nette\Utils\Strings::startsWith($content, $needle);
2019-03-16 20:31:46 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-16 20:31:46 +00:00
### `StrposToStringsContainsRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Nette\Rector\NotIdentical\StrposToStringsContainsRector`](/rules/nette/src/Rector/NotIdentical/StrposToStringsContainsRector.php)
- [test fixtures](/rules/nette/tests/Rector/NotIdentical/StrposToStringsContainsRector/Fixture)
2019-05-29 13:40:20 +00:00
Use `Nette\Utils\Strings` over bare string-functions
2019-05-29 13:40:20 +00:00
```diff
class SomeClass
{
public function run()
{
$name = 'Hi, my name is Tom';
- return strpos($name, 'Hi') !== false;
+ return \Nette\Utils\Strings::contains($name, 'Hi');
}
}
2019-05-29 13:40:20 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `SubstrStrlenFunctionToNetteUtilsStringsRector`
2019-03-16 20:31:46 +00:00
- class: [`Rector\Nette\Rector\FuncCall\SubstrStrlenFunctionToNetteUtilsStringsRector`](/rules/nette/src/Rector/FuncCall/SubstrStrlenFunctionToNetteUtilsStringsRector.php)
- [test fixtures](/rules/nette/tests/Rector/FuncCall/SubstrStrlenFunctionToNetteUtilsStringsRector/Fixture)
2019-03-16 20:31:46 +00:00
Use `Nette\Utils\Strings` over bare string-functions
2018-08-01 20:09:34 +00:00
```diff
class SomeClass
{
public function run()
{
- return substr($value, 0, 3);
+ return \Nette\Utils\Strings::substring($value, 0, 3);
}
}
2018-07-31 12:50:39 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `TemplateMagicAssignToExplicitVariableArrayRector`
2018-07-31 12:50:39 +00:00
- class: [`Rector\Nette\Rector\ClassMethod\TemplateMagicAssignToExplicitVariableArrayRector`](/rules/nette/src/Rector/ClassMethod/TemplateMagicAssignToExplicitVariableArrayRector.php)
- [test fixtures](/rules/nette/tests/Rector/ClassMethod/TemplateMagicAssignToExplicitVariableArrayRector/Fixture)
2018-07-31 12:50:39 +00:00
Change `$this->templates->{magic}` to `$this->template->render(..., $values)`
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
use Nette\Application\UI\Control;
2018-10-12 23:15:00 +00:00
class SomeControl extends Control
{
public function render()
{
- $this->template->param = 'some value';
- $this->template->render(__DIR__ . '/poll.latte');
+ $this->template->render(__DIR__ . '/poll.latte', ['param' => 'some value']);
}
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-07-31 12:50:39 +00:00
2020-07-29 08:33:10 +00:00
### `TranslateClassMethodToVariadicsRector`
- class: [`Rector\Nette\Rector\ClassMethod\TranslateClassMethodToVariadicsRector`](/rules/nette/src/Rector/ClassMethod/TranslateClassMethodToVariadicsRector.php)
- [test fixtures](/rules/nette/tests/Rector/ClassMethod/TranslateClassMethodToVariadicsRector/Fixture)
2020-07-29 08:33:10 +00:00
Change `translate()` method call 2nd arg to variadic
```diff
use Nette\Localization\ITranslator;
final class SomeClass implements ITranslator
{
- public function translate($message, $count = null)
+ public function translate($message, ... $parameters)
{
+ $count = $parameters[0] ?? null;
return [$message, $count];
}
}
```
<br><br>
## NetteCodeQuality
2018-10-12 23:15:00 +00:00
### `ArrayAccessGetControlToGetComponentMethodCallRector`
- class: [`Rector\NetteCodeQuality\Rector\Assign\ArrayAccessGetControlToGetComponentMethodCallRector`](/rules/nette-code-quality/src/Rector/Assign/ArrayAccessGetControlToGetComponentMethodCallRector.php)
- [test fixtures](/rules/nette-code-quality/tests/Rector/Assign/ArrayAccessGetControlToGetComponentMethodCallRector/Fixture)
Change magic arrays access get, to explicit `$this->getComponent(...)` method
```diff
use Nette\Application\UI\Presenter;
class SomeClass extends Presenter
{
public function some()
{
- $someControl = $this['whatever'];
+ $someControl = $this->getComponent('whatever');
}
}
```
<br><br>
### `ArrayAccessSetControlToAddComponentMethodCallRector`
- class: [`Rector\NetteCodeQuality\Rector\Assign\ArrayAccessSetControlToAddComponentMethodCallRector`](/rules/nette-code-quality/src/Rector/Assign/ArrayAccessSetControlToAddComponentMethodCallRector.php)
- [test fixtures](/rules/nette-code-quality/tests/Rector/Assign/ArrayAccessSetControlToAddComponentMethodCallRector/Fixture)
Change magic arrays access set, to explicit `$this->setComponent(...)` method
```diff
use Nette\Application\UI\Control;
use Nette\Application\UI\Presenter;
class SomeClass extends Presenter
{
public function some()
{
$someControl = new Control();
- $this['whatever'] = $someControl;
+ $this->addComponent($someControl, 'whatever');
}
}
```
<br><br>
### `ChangeControlArrayAccessToAnnotatedControlVariableRector`
- class: [`Rector\NetteCodeQuality\Rector\ArrayDimFetch\ChangeControlArrayAccessToAnnotatedControlVariableRector`](/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php)
- [test fixtures](/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector/Fixture)
2018-10-12 23:15:00 +00:00
Change magic `$this["some_component"]` to variable assign with @var annotation
2018-10-12 23:15:00 +00:00
```diff
use Nette\Application\UI\Presenter;
use Nette\Application\UI\Form;
final class SomePresenter extends Presenter
{
public function run()
{
- if ($this['some_form']->isSubmitted()) {
+ /** @var \Nette\Application\UI\Form $someForm */
+ $someForm = $this['some_form'];
+ if ($someForm->isSubmitted()) {
}
}
protected function createComponentSomeForm()
{
return new Form();
}
}
```
2018-10-12 23:15:00 +00:00
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-21 22:26:45 +00:00
### `ChangeFormArrayAccessToAnnotatedControlVariableRector`
- class: [`Rector\NetteCodeQuality\Rector\ArrayDimFetch\ChangeFormArrayAccessToAnnotatedControlVariableRector`](/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector.php)
- [test fixtures](/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector/Fixture)
Change array access magic on `$form` to explicit standalone typed variable
2018-10-12 23:15:00 +00:00
```diff
use Nette\Application\UI\Form;
2018-10-12 23:15:00 +00:00
class SomePresenter
{
public function run()
{
$form = new Form();
$this->addText('email', 'Email');
- $form['email']->value = 'hey@hi.hello';
+ /** @var \Nette\Forms\Controls\TextInput $emailControl */
+ $emailControl = $form['email'];
+ $emailControl->value = 'hey@hi.hello';
}
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MakeGetComponentAssignAnnotatedRector`
2018-10-12 23:15:00 +00:00
- class: [`Rector\NetteCodeQuality\Rector\Assign\MakeGetComponentAssignAnnotatedRector`](/rules/nette-code-quality/src/Rector/Assign/MakeGetComponentAssignAnnotatedRector.php)
- [test fixtures](/rules/nette-code-quality/tests/Rector/Assign/MakeGetComponentAssignAnnotatedRector/Fixture)
2018-10-12 23:15:00 +00:00
Add doc type for magic `$control->getComponent(...)` assign
2018-10-12 23:15:00 +00:00
```diff
use Nette\Application\UI\Control;
2018-10-12 23:15:00 +00:00
final class SomeClass
{
public function run()
{
$externalControl = new ExternalControl();
+ /** @var AnotherControl $anotherControl */
$anotherControl = $externalControl->getComponent('another');
}
}
final class ExternalControl extends Control
{
public function createComponentAnother(): AnotherControl
{
return new AnotherControl();
}
}
final class AnotherControl extends Control
{
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MoveInjectToExistingConstructorRector`
2018-10-12 23:15:00 +00:00
- class: [`Rector\NetteCodeQuality\Rector\Class_\MoveInjectToExistingConstructorRector`](/rules/nette-code-quality/src/Rector/Class_/MoveInjectToExistingConstructorRector.php)
- [test fixtures](/rules/nette-code-quality/tests/Rector/Class_/MoveInjectToExistingConstructorRector/Fixture)
2018-10-12 23:15:00 +00:00
Move @inject properties to constructor, if there already is one
2018-10-12 23:15:00 +00:00
```diff
final class SomeClass
{
/**
* @var SomeDependency
- * @inject
*/
- public $someDependency;
+ private $someDependency;
2018-10-12 23:15:00 +00:00
/**
* @var OtherDependency
*/
private $otherDependency;
- public function __construct(OtherDependency $otherDependency)
+ public function __construct(OtherDependency $otherDependency, SomeDependency $someDependency)
{
$this->otherDependency = $otherDependency;
+ $this->someDependency = $someDependency;
}
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-12 23:15:00 +00:00
## NetteKdyby
2018-10-12 23:15:00 +00:00
### `ChangeNetteEventNamesInGetSubscribedEventsRector`
- class: [`Rector\NetteKdyby\Rector\ClassMethod\ChangeNetteEventNamesInGetSubscribedEventsRector`](/rules/nette-kdyby/src/Rector/ClassMethod/ChangeNetteEventNamesInGetSubscribedEventsRector.php)
- [test fixtures](/rules/nette-kdyby/tests/Rector/ClassMethod/ChangeNetteEventNamesInGetSubscribedEventsRector/Fixture)
2018-10-12 23:15:00 +00:00
Change EventSubscriber from Kdyby to Contributte
2018-10-12 23:15:00 +00:00
```diff
+use Contributte\Events\Extra\Event\Application\ShutdownEvent;
use Kdyby\Events\Subscriber;
use Nette\Application\Application;
-use Nette\Application\UI\Presenter;
class GetApplesSubscriber implements Subscriber
{
- public function getSubscribedEvents()
+ public static function getSubscribedEvents()
{
return [
- Application::class . '::onShutdown',
+ ShutdownEvent::class => 'onShutdown',
];
}
- public function onShutdown(Presenter $presenter)
+ public function onShutdown(ShutdownEvent $shutdownEvent)
{
+ $presenter = $shutdownEvent->getPresenter();
$presenterName = $presenter->getName();
// ...
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ReplaceEventManagerWithEventSubscriberRector`
- class: [`Rector\NetteKdyby\Rector\MethodCall\ReplaceEventManagerWithEventSubscriberRector`](/rules/nette-kdyby/src/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector.php)
- [test fixtures](/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector/Fixture)
Change Kdyby EventManager to EventDispatcher
```diff
use Kdyby\Events\EventManager;
final class SomeClass
{
/**
* @var EventManager
*/
private $eventManager;
public function __construct(EventManager $eventManager)
{
$this->eventManager = eventManager;
}
public function run()
{
$key = '2000';
- $this->eventManager->dispatchEvent(static::class . '::onCopy', new EventArgsList([$this, $key]));
+ $this->eventManager->dispatch(new SomeClassCopyEvent($this, $key));
}
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector`
2020-03-23 16:13:04 +00:00
- class: [`Rector\NetteKdyby\Rector\ClassMethod\ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector`](/rules/nette-kdyby/src/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector.php)
- [test fixtures](/rules/nette-kdyby/tests/Rector/ClassMethod/ReplaceMagicEventPropertySubscriberWithEventClassSubscriberRector/Fixture)
2020-03-23 16:13:04 +00:00
Change `getSubscribedEvents()` from on magic property, to Event class
2020-03-23 16:13:04 +00:00
```diff
use Kdyby\Events\Subscriber;
2020-03-23 16:13:04 +00:00
final class ActionLogEventSubscriber implements Subscriber
2020-03-23 16:13:04 +00:00
{
public function getSubscribedEvents(): array
2020-03-23 16:13:04 +00:00
{
return [
- AlbumService::class . '::onApprove' => 'onAlbumApprove',
+ AlbumServiceApproveEvent::class => 'onAlbumApprove',
];
}
2020-03-23 16:13:04 +00:00
- public function onAlbumApprove(Album $album, int $adminId): void
+ public function onAlbumApprove(AlbumServiceApproveEventAlbum $albumServiceApproveEventAlbum): void
{
+ $album = $albumServiceApproveEventAlbum->getAlbum();
$album->play();
2020-03-23 16:13:04 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-03-23 16:13:04 +00:00
### `ReplaceMagicPropertyEventWithEventClassRector`
2018-10-12 23:15:00 +00:00
- class: [`Rector\NetteKdyby\Rector\MethodCall\ReplaceMagicPropertyEventWithEventClassRector`](/rules/nette-kdyby/src/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector.php)
- [test fixtures](/rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Fixture)
2018-10-12 23:15:00 +00:00
Change `$onProperty` magic call with event disptacher and class dispatch
2019-05-29 13:40:20 +00:00
```diff
final class FileManager
{
- public $onUpload;
+ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
+ public function __construct(EventDispatcherInterface $eventDispatcher)
+ {
+ $this->eventDispatcher = $eventDispatcher;
+ }
+
public function run(User $user)
{
- $this->onUpload($user);
+ $onFileManagerUploadEvent = new FileManagerUploadEvent($user);
+ $this->eventDispatcher->dispatch($onFileManagerUploadEvent);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## NetteTesterToPHPUnit
2019-10-15 14:46:31 +00:00
### `NetteAssertToPHPUnitAssertRector`
2019-10-15 14:46:31 +00:00
- class: [`Rector\NetteTesterToPHPUnit\Rector\StaticCall\NetteAssertToPHPUnitAssertRector`](/rules/nette-tester-to-phpunit/src/Rector/StaticCall/NetteAssertToPHPUnitAssertRector.php)
Migrate Nette/Assert calls to PHPUnit
2019-10-15 14:46:31 +00:00
```diff
use Tester\Assert;
function someStaticFunctions()
2019-10-15 14:46:31 +00:00
{
- Assert::true(10 == 5);
+ \PHPUnit\Framework\Assert::assertTrue(10 == 5);
2019-10-15 14:46:31 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-15 14:46:31 +00:00
### `NetteTesterClassToPHPUnitClassRector`
2018-10-12 23:15:00 +00:00
- class: [`Rector\NetteTesterToPHPUnit\Rector\Class_\NetteTesterClassToPHPUnitClassRector`](/rules/nette-tester-to-phpunit/src/Rector/Class_/NetteTesterClassToPHPUnitClassRector.php)
2018-10-12 23:15:00 +00:00
Migrate Nette Tester test case to PHPUnit
2019-05-29 13:40:20 +00:00
```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);
+ $this->assertInstanceOf(\Kdyby\Doctrine\EntityManager::cllass, $default);
+ $this->assertTrue(5);
+ $this->same($container->getService('kdyby.doctrine.default.entityManager'), $default);
}
-}
-
-(new \ExtensionTest())->run();
+}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameTesterTestToPHPUnitToTestFileRector`
- class: [`Rector\NetteTesterToPHPUnit\Rector\RenameTesterTestToPHPUnitToTestFileRector`](/rules/nette-tester-to-phpunit/src/Rector/RenameTesterTestToPHPUnitToTestFileRector.php)
Rename "*.phpt" file to "*Test.php" file
```diff
-// tests/SomeTestCase.phpt
+// tests/SomeTestCase.php
```
2020-06-16 16:14:51 +00:00
<br><br>
## NetteToSymfony
2018-10-12 23:15:00 +00:00
### `DeleteFactoryInterfaceRector`
2018-10-12 23:15:00 +00:00
- class: [`Rector\NetteToSymfony\Rector\Interface_\DeleteFactoryInterfaceRector`](/rules/nette-to-symfony/src/Rector/Interface_/DeleteFactoryInterfaceRector.php)
2018-10-12 23:15:00 +00:00
Interface factories are not needed in Symfony. Clear constructor injection is used instead
2018-10-12 23:15:00 +00:00
```diff
-interface SomeControlFactoryInterface
-{
- public function create();
-}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-23 18:58:57 +00:00
### `FormControlToControllerAndFormTypeRector`
2019-10-15 14:46:31 +00:00
- class: [`Rector\NetteToSymfony\Rector\Class_\FormControlToControllerAndFormTypeRector`](/rules/nette-to-symfony/src/Rector/Class_/FormControlToControllerAndFormTypeRector.php)
- [test fixtures](/rules/nette-to-symfony/tests/Rector/Class_/FormControlToControllerAndFormTypeRector/Fixture)
2019-10-15 14:46:31 +00:00
Change Form that extends Control to Controller and decoupled FormType
2019-10-15 14:46:31 +00:00
```diff
-use Nette\Application\UI\Form;
-use Nette\Application\UI\Control;
-
-class SomeForm extends Control
+class SomeFormController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
2019-10-15 14:46:31 +00:00
{
- public function createComponentForm()
+ /**
+ * @Route(...)
+ */
+ public function actionSomeForm(\Symfony\Component\HttpFoundation\Request $request): \Symfony\Component\HttpFoundation\Response
2019-10-15 14:46:31 +00:00
{
- $form = new Form();
- $form->addText('name', 'Your name');
+ $form = $this->createForm(SomeFormType::class);
+ $form->handleRequest($request);
2019-10-15 14:46:31 +00:00
- $form->onSuccess[] = [$this, 'processForm'];
- }
-
- public function processForm(Form $form)
- {
- // process me
+ if ($form->isSuccess() && $form->isValid()) {
+ // process me
+ }
2019-10-15 14:46:31 +00:00
}
}
```
**New file**
```php
<?php declare(strict_types=1);
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class SomeFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $formBuilder, array $options): void
{
$formBuilder->add('name', TextType::class, [
'label' => 'Your name',
]);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `FromHttpRequestGetHeaderToHeadersGetRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\NetteToSymfony\Rector\MethodCall\FromHttpRequestGetHeaderToHeadersGetRector`](/rules/nette-to-symfony/src/Rector/MethodCall/FromHttpRequestGetHeaderToHeadersGetRector.php)
- [test fixtures](/rules/nette-to-symfony/tests/Rector/MethodCall/FromHttpRequestGetHeaderToHeadersGetRector/Fixture)
2019-05-29 13:40:20 +00:00
Changes `getHeader()` to `$request->headers->get()`
2019-05-29 13:40:20 +00:00
```diff
use Nette\Request;
2019-05-29 13:40:20 +00:00
final class SomeController
2019-03-31 12:25:39 +00:00
{
public static function someAction(Request $request)
2019-03-31 12:25:39 +00:00
{
- $header = $this->httpRequest->getHeader('x');
+ $header = $request->headers->get('x');
2019-03-31 12:25:39 +00:00
}
2019-05-29 13:40:20 +00:00
}
```
2019-03-31 12:25:39 +00:00
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `FromRequestGetParameterToAttributesGetRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\NetteToSymfony\Rector\MethodCall\FromRequestGetParameterToAttributesGetRector`](/rules/nette-to-symfony/src/Rector/MethodCall/FromRequestGetParameterToAttributesGetRector.php)
- [test fixtures](/rules/nette-to-symfony/tests/Rector/MethodCall/FromRequestGetParameterToAttributesGetRector/Fixture)
2019-05-29 13:40:20 +00:00
Changes "getParameter()" to "attributes->get()" from Nette to Symfony
2019-05-29 13:40:20 +00:00
```diff
use Nette\Request;
final class SomeController
2019-05-29 13:40:20 +00:00
{
public static function someAction(Request $request)
2019-03-31 12:25:39 +00:00
{
- $value = $request->getParameter('abz');
+ $value = $request->attribute->get('abz');
2019-03-31 12:25:39 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `NetteControlToSymfonyControllerRector`
- class: [`Rector\NetteToSymfony\Rector\Class_\NetteControlToSymfonyControllerRector`](/rules/nette-to-symfony/src/Rector/Class_/NetteControlToSymfonyControllerRector.php)
- [test fixtures](/rules/nette-to-symfony/tests/Rector/Class_/NetteControlToSymfonyControllerRector/Fixture)
Migrate Nette Component to Symfony Controller
```diff
-use Nette\Application\UI\Control;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Response;
-class SomeControl extends Control
+class SomeController extends AbstractController
{
- public function render()
- {
- $this->template->param = 'some value';
- $this->template->render(__DIR__ . '/poll.latte');
- }
+ public function some(): Response
+ {
+ return $this->render(__DIR__ . '/poll.latte', ['param' => 'some value']);
+ }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `NetteFormToSymfonyFormRector`
- class: [`Rector\NetteToSymfony\Rector\MethodCall\NetteFormToSymfonyFormRector`](/rules/nette-to-symfony/src/Rector/MethodCall/NetteFormToSymfonyFormRector.php)
- [test fixtures](/rules/nette-to-symfony/tests/Rector/MethodCall/NetteFormToSymfonyFormRector/Fixture)
Migrate Nette\Forms in Presenter to Symfony
```diff
use Nette\Application\UI;
class SomePresenter extends UI\Presenter
{
public function someAction()
{
- $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'
+ ]);
}
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameEventNamesInEventSubscriberRector`
2019-01-22 20:34:38 +00:00
- class: [`Rector\NetteToSymfony\Rector\ClassMethod\RenameEventNamesInEventSubscriberRector`](/rules/nette-to-symfony/src/Rector/ClassMethod/RenameEventNamesInEventSubscriberRector.php)
- [test fixtures](/rules/nette-to-symfony/tests/Rector/ClassMethod/RenameEventNamesInEventSubscriberRector/Fixture)
2019-01-22 20:34:38 +00:00
Changes event names from Nette ones to Symfony ones
2019-01-22 20:34:38 +00:00
```diff
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class SomeClass implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
- return ['nette.application' => 'someMethod'];
+ return [\SymfonyEvents::KERNEL => 'someMethod'];
}
}
2019-01-22 20:34:38 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-01-22 20:34:38 +00:00
### `RouterListToControllerAnnotationsRector`
- class: [`Rector\NetteToSymfony\Rector\ClassMethod\RouterListToControllerAnnotationsRector`](/rules/nette-to-symfony/src/Rector/ClassMethod/RouterListToControllerAnnotationsRector.php)
- [test fixtures](/rules/nette-to-symfony/tests/Rector/ClassMethod/RouterListToControllerAnnotationsRetor/Fixture)
Change new `Route()` from RouteFactory to @Route annotation above controller method
```diff
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);
return $routeList;
}
}
+use Symfony\Component\Routing\Annotation\Route;
+
final class SomePresenter
{
+ /**
+ * @Route(path="some-path")
+ */
public function run()
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `WrapTransParameterNameRector`
2018-12-14 19:35:35 +00:00
- class: [`Rector\NetteToSymfony\Rector\MethodCall\WrapTransParameterNameRector`](/rules/nette-to-symfony/src/Rector/MethodCall/WrapTransParameterNameRector.php)
- [test fixtures](/rules/nette-to-symfony/tests/Rector/MethodCall/WrapTransParameterNameRector/Fixture)
2018-12-14 19:35:35 +00:00
Adds %% to placeholder name of `trans()` method if missing
2018-12-14 19:35:35 +00:00
```diff
use Symfony\Component\Translation\Translator;
final class SomeController
{
public function run()
{
$translator = new Translator('');
$translated = $translator->trans(
'Hello %name%',
- ['name' => $name]
+ ['%name%' => $name]
);
}
}
2018-12-14 19:35:35 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-14 19:35:35 +00:00
## NetteUtilsCodeQuality
2018-10-12 23:15:00 +00:00
### `ReplaceTimeNumberWithDateTimeConstantRector`
2018-10-12 23:15:00 +00:00
- class: [`Rector\NetteUtilsCodeQuality\Rector\LNumber\ReplaceTimeNumberWithDateTimeConstantRector`](/rules/nette-utils-code-quality/src/Rector/LNumber/ReplaceTimeNumberWithDateTimeConstantRector.php)
- [test fixtures](/rules/nette-utils-code-quality/tests/Rector/LNumber/ReplaceTimeNumberWithDateTimeConstantRector/Fixture)
2018-10-12 23:15:00 +00:00
Replace `time` numbers with `Nette\Utils\DateTime` constants
2019-05-29 13:40:20 +00:00
```diff
final class SomeClass
2019-05-29 13:40:20 +00:00
{
public function run()
{
- return 86400;
+ return \Nette\Utils\DateTime::DAY;
2019-05-29 13:40:20 +00:00
}
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
## Order
2018-10-12 23:15:00 +00:00
### `OrderClassConstantsByIntegerValueRector`
2018-10-12 23:15:00 +00:00
- class: [`Rector\Order\Rector\Class_\OrderClassConstantsByIntegerValueRector`](/rules/order/src/Rector/Class_/OrderClassConstantsByIntegerValueRector.php)
- [test fixtures](/rules/order/tests/Rector/Class_/OrderClassConstantsByIntegerValueRector/Fixture)
Order class constant order by their integer value
2018-10-12 23:15:00 +00:00
```diff
class SomeClass
{
const MODE_ON = 0;
+ const MODE_MAYBE = 1;
+
const MODE_OFF = 2;
-
- const MODE_MAYBE = 1;
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `OrderConstantsByVisibilityRector`
- class: [`Rector\Order\Rector\Class_\OrderConstantsByVisibilityRector`](/rules/order/src/Rector/Class_/OrderConstantsByVisibilityRector.php)
- [test fixtures](/rules/order/tests/Rector/Class_/OrderConstantsByVisibilityRector/Fixture)
Orders constants by visibility
```diff
final class SomeClass
{
+ public const PUBLIC_CONST = 'public';
+ protected const PROTECTED_CONST = 'protected';
private const PRIVATE_CONST = 'private';
- protected const PROTECTED_CONST = 'protected';
- public const PUBLIC_CONST = 'public';
}
```
<br><br>
### `OrderConstructorDependenciesByTypeAlphabeticallyRector`
2018-07-31 12:50:39 +00:00
- class: [`Rector\Order\Rector\ClassMethod\OrderConstructorDependenciesByTypeAlphabeticallyRector`](/rules/order/src/Rector/ClassMethod/OrderConstructorDependenciesByTypeAlphabeticallyRector.php)
- [test fixtures](/rules/order/tests/Rector/ClassMethod/OrderConstructorDependenciesByTypeAlphabeticallyRector/Fixture)
2018-07-31 12:50:39 +00:00
Order __constructor dependencies by type A-Z
```php
<?php
declare(strict_types=1);
use Rector\Order\Rector\ClassMethod\OrderConstructorDependenciesByTypeAlphabeticallyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(OrderConstructorDependenciesByTypeAlphabeticallyRector::class)
->call('configure', [[
OrderConstructorDependenciesByTypeAlphabeticallyRector::SKIP_PATTERNS => ['Cla*ame', 'Ano?herClassName'],
]]);
};
```
2018-08-01 20:09:34 +00:00
```diff
class SomeClass
{
public function __construct(
+ LatteAndTwigFinder $latteAndTwigFinder,
LatteToTwigConverter $latteToTwigConverter,
- SymfonyStyle $symfonyStyle,
- LatteAndTwigFinder $latteAndTwigFinder,
- SmartFileSystem $smartFileSystem
+ SmartFileSystem $smartFileSystem,
+ SymfonyStyle $symfonyStyle
) {
}
}
2018-07-31 12:50:39 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `OrderFirstLevelClassStatementsRector`
- class: [`Rector\Order\Rector\Class_\OrderFirstLevelClassStatementsRector`](/rules/order/src/Rector/Class_/OrderFirstLevelClassStatementsRector.php)
- [test fixtures](/rules/order/tests/Rector/Class_/OrderFirstLevelClassStatementsRector/Fixture)
Orders first level Class statements
```diff
final class SomeClass
{
+ use TraitName;
+ private const CONST_NAME = 'constant_value';
+ protected $propertyName;
public function functionName();
- protected $propertyName;
- private const CONST_NAME = 'constant_value';
- use TraitName;
}
```
<br><br>
### `OrderMethodsByVisibilityRector`
- class: [`Rector\Order\Rector\Class_\OrderMethodsByVisibilityRector`](/rules/order/src/Rector/Class_/OrderMethodsByVisibilityRector.php)
- [test fixtures](/rules/order/tests/Rector/Class_/OrderMethodsByVisibilityRector/Fixture)
Orders method by visibility
```diff
class SomeClass
{
+ public function publicFunctionName();
protected function protectedFunctionName();
private function privateFunctionName();
- public function publicFunctionName();
}
```
<br><br>
### `OrderPrivateMethodsByUseRector`
2019-11-06 23:52:19 +00:00
- class: [`Rector\Order\Rector\Class_\OrderPrivateMethodsByUseRector`](/rules/order/src/Rector/Class_/OrderPrivateMethodsByUseRector.php)
- [test fixtures](/rules/order/tests/Rector/Class_/OrderPrivateMethodsByUseRector/Fixture)
2019-11-06 23:52:19 +00:00
Order private methods in order of their use
2019-11-06 23:52:19 +00:00
```diff
class SomeClass
{
public function run()
2019-11-06 23:52:19 +00:00
{
$this->call1();
$this->call2();
2019-11-06 23:52:19 +00:00
}
- private function call2()
+ private function call1()
2019-11-06 23:52:19 +00:00
{
}
2019-08-05 21:10:47 +00:00
- private function call1()
+ private function call2()
2019-08-05 21:10:47 +00:00
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `OrderPropertiesByVisibilityRector`
- class: [`Rector\Order\Rector\Class_\OrderPropertiesByVisibilityRector`](/rules/order/src/Rector/Class_/OrderPropertiesByVisibilityRector.php)
- [test fixtures](/rules/order/tests/Rector/Class_/OrderPropertiesByVisibilityRector/Fixture)
Orders properties by visibility
```diff
final class SomeClass
{
+ public $publicProperty;
protected $protectedProperty;
private $privateProperty;
- public $publicProperty;
}
```
<br><br>
### `OrderPropertyByComplexityRector`
- class: [`Rector\Order\Rector\Class_\OrderPropertyByComplexityRector`](/rules/order/src/Rector/Class_/OrderPropertyByComplexityRector.php)
- [test fixtures](/rules/order/tests/Rector/Class_/OrderPropertyByComplexityRector/Fixture)
Order properties by complexity, from the simplest like scalars to the most complex, like union or collections
```diff
-class SomeClass
+class SomeClass implements FoodRecipeInterface
{
/**
* @var string
*/
private $name;
/**
- * @var Type
+ * @var int
*/
- private $service;
+ private $price;
/**
- * @var int
+ * @var Type
*/
- private $price;
+ private $service;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `OrderPublicInterfaceMethodRector`
- class: [`Rector\Order\Rector\Class_\OrderPublicInterfaceMethodRector`](/rules/order/src/Rector/Class_/OrderPublicInterfaceMethodRector.php)
- [test fixtures](/rules/order/tests/Rector/Class_/OrderPublicInterfaceMethodRector/Fixture)
Order public methods required by interface in custom orderer
```php
<?php
declare(strict_types=1);
use Rector\Order\Rector\Class_\OrderPublicInterfaceMethodRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(OrderPublicInterfaceMethodRector::class)
->call('configure', [[
OrderPublicInterfaceMethodRector::METHOD_ORDER_BY_INTERFACES => [
'FoodRecipeInterface' => ['getDescription', 'process'],
],
]]);
};
```
2020-06-16 16:13:37 +00:00
```diff
class SomeClass implements FoodRecipeInterface
{
- public function process()
+ public function getDescription()
{
}
-
- public function getDescription()
+ public function process()
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## PHPOffice
### `AddRemovedDefaultValuesRector`
- class: [`Rector\PHPOffice\Rector\StaticCall\AddRemovedDefaultValuesRector`](/rules/php-office/src/Rector/StaticCall/AddRemovedDefaultValuesRector.php)
- [test fixtures](/rules/php-office/tests/Rector/StaticCall/AddRemovedDefaultValuesRector/Fixture)
Complete removed default values explicitly
```diff
final class SomeClass
{
public function run(): void
{
$logger = new \PHPExcel_CalcEngine_Logger;
- $logger->setWriteDebugLog();
+ $logger->setWriteDebugLog(false);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `CellStaticToCoordinateRector`
- class: [`Rector\PHPOffice\Rector\StaticCall\CellStaticToCoordinateRector`](/rules/php-office/src/Rector/StaticCall/CellStaticToCoordinateRector.php)
- [test fixtures](/rules/php-office/tests/Rector/StaticCall/CellStaticToCoordinateRector/Fixture)
Methods to manipulate coordinates that used to exists in PHPExcel_Cell to PhpOffice\PhpSpreadsheet\Cell\Coordinate
```diff
class SomeClass
{
public function run()
{
- \PHPExcel_Cell::stringFromColumnIndex();
+ \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ChangeChartRendererRector`
2019-12-18 09:53:46 +00:00
- class: [`Rector\PHPOffice\Rector\StaticCall\ChangeChartRendererRector`](/rules/php-office/src/Rector/StaticCall/ChangeChartRendererRector.php)
- [test fixtures](/rules/php-office/tests/Rector/StaticCall/ChangeChartRendererRector/Fixture)
2019-12-18 09:53:46 +00:00
Change chart renderer
2019-12-18 09:53:46 +00:00
```diff
final class SomeClass
{
public function run(): void
2019-12-18 09:53:46 +00:00
{
- \PHPExcel_Settings::setChartRenderer($rendererName, $rendererLibraryPath);
+ \PHPExcel_Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
2019-12-18 09:53:46 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-18 09:53:46 +00:00
### `ChangeConditionalGetConditionRector`
- class: [`Rector\PHPOffice\Rector\MethodCall\ChangeConditionalGetConditionRector`](/rules/php-office/src/Rector/MethodCall/ChangeConditionalGetConditionRector.php)
- [test fixtures](/rules/php-office/tests/Rector/MethodCall/ChangeConditionalGetConditionRector/Fixture)
Change argument `PHPExcel_Style_Conditional->getCondition()` to `getConditions()`
```diff
final class SomeClass
{
public function run(): void
{
$conditional = new \PHPExcel_Style_Conditional;
- $someCondition = $conditional->getCondition();
+ $someCondition = $conditional->getConditions()[0] ?? '';
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ChangeConditionalReturnedCellRector`
2019-12-18 09:53:46 +00:00
- class: [`Rector\PHPOffice\Rector\MethodCall\ChangeConditionalReturnedCellRector`](/rules/php-office/src/Rector/MethodCall/ChangeConditionalReturnedCellRector.php)
- [test fixtures](/rules/php-office/tests/Rector/MethodCall/ChangeConditionalReturnedCellRector/Fixture)
2019-12-18 09:53:46 +00:00
Change conditional call to `getCell()`
2019-12-18 09:53:46 +00:00
```diff
final class SomeClass
{
public function run(): void
2019-12-18 09:53:46 +00:00
{
$worksheet = new \PHPExcel_Worksheet();
- $cell = $worksheet->setCellValue('A1', 'value', true);
+ $cell = $worksheet->getCell('A1')->setValue('value');
2019-12-18 09:53:46 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-18 09:53:46 +00:00
### `ChangeConditionalSetConditionRector`
2019-12-18 09:53:46 +00:00
- class: [`Rector\PHPOffice\Rector\MethodCall\ChangeConditionalSetConditionRector`](/rules/php-office/src/Rector/MethodCall/ChangeConditionalSetConditionRector.php)
- [test fixtures](/rules/php-office/tests/Rector/MethodCall/ChangeConditionalSetConditionRector/Fixture)
2019-12-18 09:53:46 +00:00
Change argument `PHPExcel_Style_Conditional->setCondition()` to `setConditions()`
2019-12-18 09:53:46 +00:00
```diff
final class SomeClass
2019-12-18 09:53:46 +00:00
{
public function run(): void
2019-12-18 09:53:46 +00:00
{
$conditional = new \PHPExcel_Style_Conditional;
- $someCondition = $conditional->setCondition(1);
+ $someCondition = $conditional->setConditions((array) 1);
2019-12-18 09:53:46 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-18 09:53:46 +00:00
### `ChangeDataTypeForValueRector`
- class: [`Rector\PHPOffice\Rector\StaticCall\ChangeDataTypeForValueRector`](/rules/php-office/src/Rector/StaticCall/ChangeDataTypeForValueRector.php)
- [test fixtures](/rules/php-office/tests/Rector/StaticCall/ChangeDataTypeForValueRector/Fixture)
2019-01-22 20:34:38 +00:00
Change argument `DataType::dataTypeForValue()` to DefaultValueBinder
2019-01-22 20:34:38 +00:00
```diff
final class SomeClass
2019-01-22 20:34:38 +00:00
{
public function run(): void
{
- $type = \PHPExcel_Cell_DataType::dataTypeForValue('value');
+ $type = \PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder::dataTypeForValue('value');
2019-01-22 20:34:38 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-01-22 20:34:38 +00:00
### `ChangeDuplicateStyleArrayToApplyFromArrayRector`
2019-08-24 11:08:59 +00:00
- class: [`Rector\PHPOffice\Rector\MethodCall\ChangeDuplicateStyleArrayToApplyFromArrayRector`](/rules/php-office/src/Rector/MethodCall/ChangeDuplicateStyleArrayToApplyFromArrayRector.php)
- [test fixtures](/rules/php-office/tests/Rector/MethodCall/ChangeDuplicateStyleArrayToApplyFromArrayRector/Fixture)
2019-08-24 11:08:59 +00:00
Change method call `duplicateStyleArray()` to `getStyle()` + `applyFromArray()`
2019-08-24 11:08:59 +00:00
```diff
final class SomeClass
2019-08-24 11:08:59 +00:00
{
public function run(): void
{
$worksheet = new \PHPExcel_Worksheet();
- $worksheet->duplicateStyleArray($styles, $range, $advanced);
+ $worksheet->getStyle($range)->applyFromArray($styles, $advanced);
}
2019-08-24 11:08:59 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-24 11:08:59 +00:00
### `ChangeIOFactoryArgumentRector`
2020-05-31 15:26:08 +00:00
- class: [`Rector\PHPOffice\Rector\StaticCall\ChangeIOFactoryArgumentRector`](/rules/php-office/src/Rector/StaticCall/ChangeIOFactoryArgumentRector.php)
- [test fixtures](/rules/php-office/tests/Rector/StaticCall/ChangeIOFactoryArgumentRector/Fixture)
2020-05-31 15:26:08 +00:00
Change argument of PHPExcel_IOFactory::createReader(), `PHPExcel_IOFactory::createWriter()` and `PHPExcel_IOFactory::identify()`
2020-05-31 15:26:08 +00:00
```diff
final class SomeClass
{
public function run(): void
{
- $writer = \PHPExcel_IOFactory::createWriter('CSV');
+ $writer = \PHPExcel_IOFactory::createWriter('Csv');
}
}
2020-05-31 15:26:08 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-05-31 15:26:08 +00:00
### `ChangePdfWriterRector`
- class: [`Rector\PHPOffice\Rector\StaticCall\ChangePdfWriterRector`](/rules/php-office/src/Rector/StaticCall/ChangePdfWriterRector.php)
- [test fixtures](/rules/php-office/tests/Rector/StaticCall/ChangePdfWriterRector/Fixture)
Change init of PDF writer
```diff
final class SomeClass
{
public function run(): void
{
- \PHPExcel_Settings::setPdfRendererName(PHPExcel_Settings::PDF_RENDERER_MPDF);
- \PHPExcel_Settings::setPdfRenderer($somePath);
- $writer = \PHPExcel_IOFactory::createWriter($spreadsheet, 'PDF');
+ $writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ChangeSearchLocationToRegisterReaderRector`
2020-05-31 15:45:51 +00:00
- class: [`Rector\PHPOffice\Rector\StaticCall\ChangeSearchLocationToRegisterReaderRector`](/rules/php-office/src/Rector/StaticCall/ChangeSearchLocationToRegisterReaderRector.php)
- [test fixtures](/rules/php-office/tests/Rector/StaticCall/ChangeSearchLocationToRegisterReaderRector/Fixture)
2020-05-31 15:45:51 +00:00
Change argument `addSearchLocation()` to `registerReader()`
2020-05-31 15:45:51 +00:00
```diff
final class SomeClass
{
public function run(): void
{
- \PHPExcel_IOFactory::addSearchLocation($type, $location, $classname);
+ \PhpOffice\PhpSpreadsheet\IOFactory::registerReader($type, $classname);
}
}
2020-05-31 15:45:51 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-05-31 15:45:51 +00:00
### `GetDefaultStyleToGetParentRector`
2018-07-31 12:50:39 +00:00
- class: [`Rector\PHPOffice\Rector\MethodCall\GetDefaultStyleToGetParentRector`](/rules/php-office/src/Rector/MethodCall/GetDefaultStyleToGetParentRector.php)
- [test fixtures](/rules/php-office/tests/Rector/MethodCall/GetDefaultStyleToGetParentRector/Fixture)
2018-07-31 12:50:39 +00:00
Methods to (new `Worksheet())->getDefaultStyle()` to `getParent()->getDefaultStyle()`
2018-08-01 20:09:34 +00:00
```diff
class SomeClass
2019-09-25 08:49:53 +00:00
{
public function run()
{
$worksheet = new \PHPExcel_Worksheet();
- $worksheet->getDefaultStyle();
+ $worksheet->getParent()->getDefaultStyle();
}
2018-10-12 23:15:00 +00:00
}
2019-05-19 08:27:38 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-19 08:27:38 +00:00
### `IncreaseColumnIndexRector`
2018-12-31 11:50:32 +00:00
- class: [`Rector\PHPOffice\Rector\MethodCall\IncreaseColumnIndexRector`](/rules/php-office/src/Rector/MethodCall/IncreaseColumnIndexRector.php)
- [test fixtures](/rules/php-office/tests/Rector/MethodCall/IncreaseColumnIndexRector/Fixture)
2018-12-31 11:50:32 +00:00
Column index changed from 0 to 1 - run only ONCE! changes current value without memory
```diff
2019-09-25 08:49:53 +00:00
final class SomeClass
{
public function run(): void
2019-09-25 08:49:53 +00:00
{
$worksheet = new \PHPExcel_Worksheet();
- $worksheet->setCellValueByColumnAndRow(0, 3, '1150');
+ $worksheet->setCellValueByColumnAndRow(1, 3, '1150');
2019-09-25 08:49:53 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveSetTempDirOnExcelWriterRector`
- class: [`Rector\PHPOffice\Rector\MethodCall\RemoveSetTempDirOnExcelWriterRector`](/rules/php-office/src/Rector/MethodCall/RemoveSetTempDirOnExcelWriterRector.php)
- [test fixtures](/rules/php-office/tests/Rector/MethodCall/RemoveSetTempDirOnExcelWriterRector/Fixture)
Remove `setTempDir()` on PHPExcel_Writer_Excel5
```diff
final class SomeClass
{
public function run(): void
{
$writer = new \PHPExcel_Writer_Excel5;
- $writer->setTempDir();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## PHPStan
2019-02-21 14:36:16 +00:00
### `PHPStormVarAnnotationRector`
2019-02-21 14:36:16 +00:00
- class: [`Rector\PHPStan\Rector\Assign\PHPStormVarAnnotationRector`](/rules/phpstan/src/Rector/Assign/PHPStormVarAnnotationRector.php)
- [test fixtures](/rules/phpstan/tests/Rector/Assign/PHPStormVarAnnotationRector/Fixture)
2019-09-25 08:49:53 +00:00
Change various @var annotation formats to one PHPStorm understands
2019-02-21 14:36:16 +00:00
```diff
-$config = 5;
-/** @var \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterConfig $config */
+/** @var \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterConfig $config */
+$config = 5;
2019-05-01 23:56:58 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
### `RecastingRemovalRector`
- class: [`Rector\PHPStan\Rector\Cast\RecastingRemovalRector`](/rules/phpstan/src/Rector/Cast/RecastingRemovalRector.php)
- [test fixtures](/rules/phpstan/tests/Rector/Cast/RecastingRemovalRector/Fixture)
Removes recasting of the same type
```diff
$string = '';
-$string = (string) $string;
+$string = $string;
2019-09-25 08:49:53 +00:00
$array = [];
-$array = (array) $array;
+$array = $array;
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveNonExistingVarAnnotationRector`
- class: [`Rector\PHPStan\Rector\Node\RemoveNonExistingVarAnnotationRector`](/rules/phpstan/src/Rector/Node/RemoveNonExistingVarAnnotationRector.php)
- [test fixtures](/rules/phpstan/tests/Rector/Node/RemoveNonExistingVarAnnotationRector/Fixture)
2019-09-25 08:49:53 +00:00
Removes non-existing @var annotations above the code
```diff
2019-09-25 08:49:53 +00:00
class SomeClass
{
public function get()
2019-09-25 08:49:53 +00:00
{
- /** @var Training[] $trainings */
return $this->getData();
2019-09-25 08:49:53 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## PHPUnit
2019-02-21 14:36:16 +00:00
### `AddDoesNotPerformAssertionToNonAssertingTestRector`
2019-02-21 14:36:16 +00:00
- class: [`Rector\PHPUnit\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector`](/rules/phpunit/src/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture)
Tests without assertion will have @doesNotPerformAssertion
2019-02-21 14:36:16 +00:00
```diff
class SomeClass extends PHPUnit\Framework\TestCase
{
+ /**
+ * @doesNotPerformAssertions
+ */
public function test()
{
$nothing = 5;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AddProphecyTraitRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\PHPUnit\Rector\Class_\AddProphecyTraitRector`](/rules/phpunit/src/Rector/Class_/AddProphecyTraitRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/Class_/AddProphecyTraitRector/Fixture)
2019-05-29 13:40:20 +00:00
Add Prophecy trait for method using `$this->prophesize()`
```diff
use PHPUnit\Framework\TestCase;
+use Prophecy\PhpUnit\ProphecyTrait;
final class ExampleTest extends TestCase
{
+ use ProphecyTrait;
+
public function testOne(): void
{
$prophecy = $this->prophesize(\AnInterface::class);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AddSeeTestAnnotationRector`
- class: [`Rector\PHPUnit\Rector\Class_\AddSeeTestAnnotationRector`](/rules/phpunit/src/Rector/Class_/AddSeeTestAnnotationRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/Class_/AddSeeTestAnnotationRector/Fixture)
Add @see annotation test of the class for faster jump to test. Make it FQN, so it stays in the annotation, not in the PHP source code.
```diff
+/**
+ * @see \SomeServiceTest
+ */
class SomeService
{
}
use PHPUnit\Framework\TestCase;
class SomeServiceTest extends TestCase
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ArrayArgumentInTestToDataProviderRector`
2019-03-09 13:24:30 +00:00
- class: [`Rector\PHPUnit\Rector\Class_\ArrayArgumentInTestToDataProviderRector`](/rules/phpunit/src/Rector/Class_/ArrayArgumentInTestToDataProviderRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/Class_/ArrayArgumentInTestToDataProviderRector/Fixture)
2019-03-09 13:24:30 +00:00
Move array argument from tests into data provider [configurable]
```php
<?php
declare(strict_types=1);
use Rector\PHPUnit\Rector\Class_\ArrayArgumentInTestToDataProviderRector;
use Rector\PHPUnit\ValueObject\ArrayArgumentToDataProvider;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ArrayArgumentInTestToDataProviderRector::class)
->call('configure', [[
ArrayArgumentInTestToDataProviderRector::ARRAY_ARGUMENTS_TO_DATA_PROVIDERS => inline_value_objects(
2020-10-07 14:20:53 +00:00
[
new ArrayArgumentToDataProvider(
'PHPUnit\Framework\TestCase',
'doTestMultiple',
'doTestSingle',
'number'
),
]
),
]]);
};
```
2019-03-09 13:24:30 +00:00
```diff
use PHPUnit\Framework\TestCase;
class SomeServiceTest extends TestCase
{
- public function test()
+ /**
+ * @dataProvider provideData()
+ */
+ public function test(int $number)
{
- $this->doTestMultiple([1, 2, 3]);
+ $this->doTestSingle($number);
+ }
+
+ public function provideData(): \Iterator
+ {
+ yield [1];
+ yield [2];
+ yield [3];
}
}
2019-03-09 13:24:30 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-09 13:24:30 +00:00
### `AssertCompareToSpecificMethodRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertCompareToSpecificMethodRector`](/rules/phpunit/src/Rector/MethodCall/AssertCompareToSpecificMethodRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertCompareToSpecificMethodRector/Fixture)
Turns vague php-only method in PHPUnit TestCase to more specific
```diff
-$this->assertSame(10, count($anything), "message");
+$this->assertCount(10, $anything, "message");
```
```diff
-$this->assertNotEquals(get_class($value), stdClass::class);
+$this->assertNotInstanceOf(stdClass::class, $value);
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AssertComparisonToSpecificMethodRector`
2018-12-31 11:50:32 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertComparisonToSpecificMethodRector`](/rules/phpunit/src/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertComparisonToSpecificMethodRector/Fixture)
2018-12-31 11:50:32 +00:00
Turns comparison operations to their method name alternatives in PHPUnit TestCase
2018-12-31 11:50:32 +00:00
```diff
-$this->assertTrue($foo === $bar, "message");
+$this->assertSame($bar, $foo, "message");
```
```diff
-$this->assertFalse($foo >= $bar, "message");
+$this->assertLessThanOrEqual($bar, $foo, "message");
2019-09-25 08:49:53 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `AssertEqualsParameterToSpecificMethodsTypeRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertEqualsParameterToSpecificMethodsTypeRector`](/rules/phpunit/src/Rector/MethodCall/AssertEqualsParameterToSpecificMethodsTypeRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertEqualsParameterToSpecificMethodsTypeRector/Fixture)
2019-09-25 08:49:53 +00:00
Change `assertEquals()/assertNotEquals()` method parameters to new specific alternatives
2019-09-25 08:49:53 +00:00
```diff
final class SomeTest extends \PHPUnit\Framework\TestCase
2019-09-25 08:49:53 +00:00
{
public function test()
{
$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');
}
}
2018-12-31 11:50:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 11:50:32 +00:00
### `AssertEqualsToSameRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertEqualsToSameRector`](/rules/phpunit/src/Rector/MethodCall/AssertEqualsToSameRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertEqualsToSameRector/Fixture)
Turns `assertEquals()` into stricter `assertSame()` for scalar values in PHPUnit TestCase
```diff
-$this->assertEquals(2, $result, "message");
+$this->assertSame(2, $result, "message");
```
```diff
-$this->assertEquals($aString, $result, "message");
+$this->assertSame($aString, $result, "message");
```
<br><br>
### `AssertFalseStrposToContainsRector`
2019-05-24 20:30:15 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertFalseStrposToContainsRector`](/rules/phpunit/src/Rector/MethodCall/AssertFalseStrposToContainsRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertFalseStrposToContainsRector/Fixture)
2019-05-24 20:30:15 +00:00
Turns `strpos`/`stripos` comparisons to their method name alternatives in PHPUnit TestCase
2019-05-24 20:30:15 +00:00
```diff
-$this->assertFalse(strpos($anything, "foo"), "message");
+$this->assertNotContains("foo", $anything, "message");
```
2018-08-01 20:09:34 +00:00
```diff
-$this->assertNotFalse(stripos($anything, "foo"), "message");
+$this->assertContains("foo", $anything, "message");
2018-07-31 12:50:39 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AssertInstanceOfComparisonRector`
2018-07-31 12:50:39 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertInstanceOfComparisonRector`](/rules/phpunit/src/Rector/MethodCall/AssertInstanceOfComparisonRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertInstanceOfComparisonRector/Fixture)
2018-07-31 12:50:39 +00:00
Turns instanceof comparisons to their method name alternatives in PHPUnit TestCase
```diff
-$this->assertTrue($foo instanceof Foo, "message");
+$this->assertInstanceOf("Foo", $foo, "message");
```
```diff
-$this->assertFalse($foo instanceof Foo, "message");
+$this->assertNotInstanceOf("Foo", $foo, "message");
2019-09-25 08:49:53 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `AssertIssetToSpecificMethodRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertIssetToSpecificMethodRector`](/rules/phpunit/src/Rector/MethodCall/AssertIssetToSpecificMethodRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertIssetToSpecificMethodRector/Fixture)
Turns isset comparisons to their method name alternatives in PHPUnit TestCase
```diff
-$this->assertTrue(isset($anything->foo));
+$this->assertObjectHasAttribute("foo", $anything);
```
```diff
-$this->assertFalse(isset($anything["foo"]), "message");
+$this->assertArrayNotHasKey("foo", $anything, "message");
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AssertNotOperatorRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertNotOperatorRector`](/rules/phpunit/src/Rector/MethodCall/AssertNotOperatorRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertNotOperatorRector/Fixture)
2019-09-25 08:49:53 +00:00
Turns not-operator comparisons to their method name alternatives in PHPUnit TestCase
2019-09-25 08:49:53 +00:00
```diff
-$this->assertTrue(!$foo, "message");
+$this->assertFalse($foo, "message");
```
```diff
-$this->assertFalse(!$foo, "message");
+$this->assertTrue($foo, "message");
2018-10-21 22:26:45 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-21 22:26:45 +00:00
### `AssertPropertyExistsRector`
2018-10-21 22:26:45 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertPropertyExistsRector`](/rules/phpunit/src/Rector/MethodCall/AssertPropertyExistsRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertPropertyExistsRector/Fixture)
2018-10-21 22:26:45 +00:00
Turns `property_exists` comparisons to their method name alternatives in PHPUnit TestCase
2018-10-21 22:26:45 +00:00
2018-10-23 18:58:57 +00:00
```diff
-$this->assertTrue(property_exists(new Class, "property"), "message");
+$this->assertClassHasAttribute("property", "Class", "message");
```
```diff
-$this->assertFalse(property_exists(new Class, "property"), "message");
+$this->assertClassNotHasAttribute("property", "Class", "message");
2018-10-23 18:58:57 +00:00
```
2018-10-21 22:26:45 +00:00
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-21 22:26:45 +00:00
### `AssertRegExpRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertRegExpRector`](/rules/phpunit/src/Rector/MethodCall/AssertRegExpRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertRegExpRector/Fixture)
Turns `preg_match` comparisons to their method name alternatives in PHPUnit TestCase
```diff
-$this->assertSame(1, preg_match("/^Message for ".*"\.$/", $string), $message);
+$this->assertRegExp("/^Message for ".*"\.$/", $string, $message);
```
```diff
-$this->assertEquals(false, preg_match("/^Message for ".*"\.$/", $string), $message);
+$this->assertNotRegExp("/^Message for ".*"\.$/", $string, $message);
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AssertResourceToClosedResourceRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertResourceToClosedResourceRector`](/rules/phpunit/src/Rector/MethodCall/AssertResourceToClosedResourceRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertResourceToClosedResourceRector/Fixture)
Turns `assertIsNotResource()` into stricter `assertIsClosedResource()` for resource values in PHPUnit TestCase
```diff
-$this->assertIsNotResource($aResource, "message");
+$this->assertIsClosedResource($aResource, "message");
```
<br><br>
### `AssertSameBoolNullToSpecificMethodRector`
2019-02-21 14:36:16 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertSameBoolNullToSpecificMethodRector`](/rules/phpunit/src/Rector/MethodCall/AssertSameBoolNullToSpecificMethodRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertSameBoolNullToSpecificMethodRector/Fixture)
2019-02-21 14:36:16 +00:00
Turns same bool and null comparisons to their method name alternatives in PHPUnit TestCase
2019-02-21 14:36:16 +00:00
```diff
-$this->assertSame(null, $anything);
+$this->assertNull($anything);
```
```diff
-$this->assertNotSame(false, $anything);
+$this->assertNotFalse($anything);
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AssertTrueFalseInternalTypeToSpecificMethodRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertTrueFalseInternalTypeToSpecificMethodRector`](/rules/phpunit/src/Rector/MethodCall/AssertTrueFalseInternalTypeToSpecificMethodRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertTrueFalseInternalTypeToSpecificMethodRector/Fixture)
Turns true/false with internal type comparisons to their method name alternatives in PHPUnit TestCase
```diff
-$this->assertTrue(is_{internal_type}($anything), "message");
+$this->assertInternalType({internal_type}, $anything, "message");
2019-09-25 08:49:53 +00:00
```
```diff
-$this->assertFalse(is_{internal_type}($anything), "message");
+$this->assertNotInternalType({internal_type}, $anything, "message");
2019-02-21 14:36:16 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-21 14:36:16 +00:00
### `AssertTrueFalseToSpecificMethodRector`
2018-12-31 11:50:32 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector`](/rules/phpunit/src/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture)
2018-12-31 11:50:32 +00:00
Turns true/false comparisons to their method name alternatives in PHPUnit TestCase when possible
2018-12-31 11:50:32 +00:00
```diff
-$this->assertTrue(is_readable($readmeFile), "message");
+$this->assertIsReadable($readmeFile, "message");
2018-12-31 11:50:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 11:50:32 +00:00
### `CreateMockToCreateStubRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\CreateMockToCreateStubRector`](/rules/phpunit/src/Rector/MethodCall/CreateMockToCreateStubRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/CreateMockToCreateStubRector/Fixture)
2019-05-29 13:40:20 +00:00
Replaces `createMock()` with `createStub()` when relevant
2019-05-29 13:40:20 +00:00
```diff
use PHPUnit\Framework\TestCase
2019-09-25 08:49:53 +00:00
class MyTest extends TestCase
{
public function testItBehavesAsExpected(): void
2019-05-29 13:40:20 +00:00
{
- $stub = $this->createMock(\Exception::class);
+ $stub = $this->createStub(\Exception::class);
$stub->method('getMessage')
->willReturn('a message');
$mock = $this->createMock(\Exception::class);
$mock->expects($this->once())
->method('getMessage')
->willReturn('a message');
self::assertSame('a message', $stub->getMessage());
self::assertSame('a message', $mock->getMessage());
}
}
```
<br><br>
### `DelegateExceptionArgumentsRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\DelegateExceptionArgumentsRector`](/rules/phpunit/src/Rector/MethodCall/DelegateExceptionArgumentsRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/DelegateExceptionArgumentsRector/Fixture)
2019-05-29 13:40:20 +00:00
Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.
2019-09-25 08:49:53 +00:00
```diff
-$this->setExpectedException(Exception::class, "Message", "CODE");
+$this->setExpectedException(Exception::class);
+$this->expectExceptionMessage('Message');
+$this->expectExceptionCode('CODE');
2019-09-25 08:49:53 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `ExceptionAnnotationRector`
2019-05-19 08:27:38 +00:00
- class: [`Rector\PHPUnit\Rector\ClassMethod\ExceptionAnnotationRector`](/rules/phpunit/src/Rector/ClassMethod/ExceptionAnnotationRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/ClassMethod/ExceptionAnnotationRector/Fixture)
2019-05-19 08:27:38 +00:00
Changes `@expectedException annotations to expectException*() methods
2019-05-19 08:27:38 +00:00
```diff
-/**
- * @expectedException Exception
- * @expectedExceptionMessage Message
- */
public function test()
{
+ $this->expectException('Exception');
+ $this->expectExceptionMessage('Message');
// tested code
}
2019-05-19 08:27:38 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-19 08:27:38 +00:00
### `ExplicitPhpErrorApiRector`
2018-10-23 18:58:57 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\ExplicitPhpErrorApiRector`](/rules/phpunit/src/Rector/MethodCall/ExplicitPhpErrorApiRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/ExplicitPhpErrorApiRector/Fixture)
Use explicit API for expecting PHP errors, warnings, and notices
2018-10-21 22:26:45 +00:00
```diff
final class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
- $this->expectException(\PHPUnit\Framework\TestCase\Deprecated::class);
- $this->expectException(\PHPUnit\Framework\TestCase\Error::class);
- $this->expectException(\PHPUnit\Framework\TestCase\Notice::class);
- $this->expectException(\PHPUnit\Framework\TestCase\Warning::class);
+ $this->expectDeprecation();
+ $this->expectError();
+ $this->expectNotice();
+ $this->expectWarning();
}
}
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2020-06-16 16:14:51 +00:00
<br><br>
### `GetMockBuilderGetMockToCreateMockRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector`](/rules/phpunit/src/Rector/MethodCall/GetMockBuilderGetMockToCreateMockRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/GetMockBuilderGetMockToCreateMockRector/Fixture)
Remove `getMockBuilder()` to `createMock()`
```diff
class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
- $applicationMock = $this->getMockBuilder('SomeClass')
- ->disableOriginalConstructor()
- ->getMock();
+ $applicationMock = $this->createMock('SomeClass');
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `GetMockRector`
- class: [`Rector\PHPUnit\Rector\StaticCall\GetMockRector`](/rules/phpunit/src/Rector/StaticCall/GetMockRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/StaticCall/GetMockRector/Fixture)
2018-07-31 12:50:39 +00:00
Turns getMock*() methods to `createMock()`
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
-$this->getMock("Class");
+$this->createMock("Class");
2018-07-31 12:50:39 +00:00
```
```diff
-$this->getMockWithoutInvokingTheOriginalConstructor("Class");
+$this->createMock("Class");
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveDataProviderTestPrefixRector`
- class: [`Rector\PHPUnit\Rector\Class_\RemoveDataProviderTestPrefixRector`](/rules/phpunit/src/Rector/Class_/RemoveDataProviderTestPrefixRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/Class_/RemoveDataProviderTestPrefixRector/Fixture)
2018-07-31 12:50:39 +00:00
Data provider methods cannot start with "test" prefix
2018-07-31 12:50:39 +00:00
```diff
class SomeClass extends PHPUnit\Framework\TestCase
{
/**
- * @dataProvider testProvideData()
+ * @dataProvider provideData()
*/
public function test()
{
$nothing = 5;
}
- public function testProvideData()
+ public function provideData()
{
return ['123'];
}
}
2018-08-01 20:09:34 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-12 23:15:00 +00:00
### `RemoveEmptyTestMethodRector`
2018-10-12 23:15:00 +00:00
- class: [`Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector`](/rules/phpunit/src/Rector/ClassMethod/RemoveEmptyTestMethodRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/ClassMethod/RemoveEmptyTestMethodRector/Fixture)
Remove empty test methods
2018-10-12 23:15:00 +00:00
2018-08-01 20:09:34 +00:00
```diff
class SomeTest extends \PHPUnit\Framework\TestCase
2019-09-25 08:49:53 +00:00
{
- /**
- * testGetTranslatedModelField method
- *
- * @return void
- */
- public function testGetTranslatedModelField()
- {
- }
2019-09-25 08:49:53 +00:00
}
2018-07-31 12:50:39 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveExpectAnyFromMockRector`
2019-03-09 13:24:30 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\RemoveExpectAnyFromMockRector`](/rules/phpunit/src/Rector/MethodCall/RemoveExpectAnyFromMockRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/RemoveExpectAnyFromMockRector/Fixture)
2019-09-25 08:49:53 +00:00
Remove `expect($this->any())` from mocks as it has no added value
2019-03-09 13:24:30 +00:00
```diff
use PHPUnit\Framework\TestCase;
class SomeClass extends TestCase
{
public function test()
{
$translator = $this->getMock('SomeClass');
- $translator->expects($this->any())
- ->method('trans')
+ $translator->method('trans')
->willReturn('translated max {{ max }}!');
}
}
2019-03-09 13:24:30 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-09 13:24:30 +00:00
### `ReplaceAssertArraySubsetRector`
2019-02-21 14:36:16 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\ReplaceAssertArraySubsetRector`](/rules/phpunit/src/Rector/MethodCall/ReplaceAssertArraySubsetRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture)
2019-02-21 14:36:16 +00:00
Replace deprecated "assertArraySubset()" method with alternative methods
2019-02-21 14:36:16 +00:00
```diff
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
2019-09-25 08:49:53 +00:00
{
public function test()
2019-09-25 08:49:53 +00:00
{
$checkedArray = [];
- $this->assertArraySubset([
- 'cache_directory' => 'new_value',
- ], $checkedArray, true);
+ $this->assertArrayHasKey('cache_directory', $checkedArray);
+ $this->assertSame('new_value', $checkedArray['cache_directory']);
2019-09-25 08:49:53 +00:00
}
}
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-12 23:15:00 +00:00
### `ReplaceAssertArraySubsetWithDmsPolyfillRector`
2018-12-31 11:50:32 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\ReplaceAssertArraySubsetWithDmsPolyfillRector`](/rules/phpunit/src/Rector/MethodCall/ReplaceAssertArraySubsetWithDmsPolyfillRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetWithDmsPolyfillRector/Fixture)
2018-12-31 11:50:32 +00:00
Change `assertArraySubset()` to static call of DMS\PHPUnitExtensions\ArraySubset\Assert
2018-12-31 11:50:32 +00:00
```diff
use PHPUnit\Framework\TestCase;
class SomeClass extends TestCase
2019-09-25 08:49:53 +00:00
{
public function test()
2019-09-25 08:49:53 +00:00
{
- self::assertArraySubset(['bar' => 0], ['bar' => '0'], true);
+ \DMS\PHPUnitExtensions\ArraySubset\Assert::assertArraySubset(['bar' => 0], ['bar' => '0'], true);
- $this->assertArraySubset(['bar' => 0], ['bar' => '0'], true);
+ \DMS\PHPUnitExtensions\ArraySubset\Assert::assertArraySubset(['bar' => 0], ['bar' => '0'], true);
2019-09-25 08:49:53 +00:00
}
}
2018-12-31 11:50:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 11:50:32 +00:00
### `SelfContainerGetMethodCallFromTestToInjectPropertyRector`
2018-10-12 23:15:00 +00:00
- class: [`Rector\PHPUnit\Rector\Class_\SelfContainerGetMethodCallFromTestToInjectPropertyRector`](/rules/phpunit/src/Rector/Class_/SelfContainerGetMethodCallFromTestToInjectPropertyRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/Class_/SelfContainerGetMethodCallFromTestToInjectPropertyRector/Fixture)
Change `$container->get()` calls in PHPUnit to @inject properties autowired by jakzal/phpunit-injector
2018-10-12 23:15:00 +00:00
2018-08-01 20:09:34 +00:00
```diff
use PHPUnit\Framework\TestCase;
class SomeClassTest extends TestCase {
+ /**
+ * @var SomeService
+ * @inject
+ */
+ private $someService;
public function test()
{
- $someService = $this->getContainer()->get(SomeService::class);
+ $someService = $this->someService;
}
}
class SomeService
{
}
2018-05-05 12:48:33 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SimplifyForeachInstanceOfRector`
2019-05-01 23:56:58 +00:00
- class: [`Rector\PHPUnit\Rector\Foreach_\SimplifyForeachInstanceOfRector`](/rules/phpunit/src/Rector/Foreach_/SimplifyForeachInstanceOfRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/Foreach_/SimplifyForeachInstanceOfRector/Fixture)
2019-05-01 23:56:58 +00:00
Simplify unnecessary foreach check of instances
2019-05-01 23:56:58 +00:00
```diff
-foreach ($foos as $foo) {
- $this->assertInstanceOf(SplFileInfo::class, $foo);
-}
+$this->assertContainsOnlyInstancesOf(\SplFileInfo::class, $foos);
2019-05-01 23:56:58 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
### `SpecificAssertContainsRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsRector`](/rules/phpunit/src/Rector/MethodCall/SpecificAssertContainsRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/SpecificAssertContainsRector/Fixture)
Change `assertContains()/assertNotContains()` method to new string and iterable alternatives
```diff
<?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');
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SpecificAssertContainsWithoutIdentityRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector`](/rules/phpunit/src/Rector/MethodCall/SpecificAssertContainsWithoutIdentityRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/SpecificAssertContainsWithoutIdentityRector/Fixture)
Change `assertContains()/assertNotContains()` with non-strict comparison to new specific alternatives
```diff
<?php
-final class SomeTest extends \PHPUnit\Framework\TestCase
+final class SomeTest extends TestCase
{
public function test()
{
$objects = [ new \stdClass(), new \stdClass(), new \stdClass() ];
- $this->assertContains(new \stdClass(), $objects, 'message', false, false);
- $this->assertNotContains(new \stdClass(), $objects, 'message', false, false);
+ $this->assertContainsEquals(new \stdClass(), $objects, 'message');
+ $this->assertNotContainsEquals(new \stdClass(), $objects, 'message');
}
}
```
<br><br>
### `SpecificAssertInternalTypeRector`
2019-05-24 20:30:15 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector`](/rules/phpunit/src/Rector/MethodCall/SpecificAssertInternalTypeRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/Fixture)
2019-05-24 20:30:15 +00:00
Change `assertInternalType()/assertNotInternalType()` method to new specific alternatives
2019-05-24 20:30:15 +00:00
```diff
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);
}
}
2019-05-24 20:30:15 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-24 20:30:15 +00:00
### `TestListenerToHooksRector`
- class: [`Rector\PHPUnit\Rector\Class_\TestListenerToHooksRector`](/rules/phpunit/src/Rector/Class_/TestListenerToHooksRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/Class_/TestListenerToHooksRector/Fixture)
Refactor "*TestListener.php" to particular "*Hook.php" files
```diff
namespace App\Tests;
-use PHPUnit\Framework\TestListener;
-
-final class BeforeListHook implements TestListener
+final class BeforeListHook implements \PHPUnit\Runner\BeforeTestHook, \PHPUnit\Runner\AfterTestHook
2019-09-25 08:49:53 +00:00
{
- public function addError(Test $test, \Throwable $t, float $time): void
+ public function executeBeforeTest(Test $test): void
{
- }
-
- 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-09-25 08:49:53 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `TryCatchToExpectExceptionRector`
- class: [`Rector\PHPUnit\Rector\ClassMethod\TryCatchToExpectExceptionRector`](/rules/phpunit/src/Rector/ClassMethod/TryCatchToExpectExceptionRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/ClassMethod/TryCatchToExpectExceptionRector/Fixture)
Turns try/catch to `expectException()` call
```diff
-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();
```
2020-06-16 16:14:51 +00:00
<br><br>
### `UseSpecificWillMethodRector`
- class: [`Rector\PHPUnit\Rector\MethodCall\UseSpecificWillMethodRector`](/rules/phpunit/src/Rector/MethodCall/UseSpecificWillMethodRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/UseSpecificWillMethodRector/Fixture)
Changes ->will($this->xxx()) to one specific method
```diff
class SomeClass extends PHPUnit\Framework\TestCase
{
public function test()
{
$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 }}!');
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `WithConsecutiveArgToArrayRector`
2018-08-01 20:09:34 +00:00
- class: [`Rector\PHPUnit\Rector\MethodCall\WithConsecutiveArgToArrayRector`](/rules/phpunit/src/Rector/MethodCall/WithConsecutiveArgToArrayRector.php)
- [test fixtures](/rules/phpunit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/Fixture)
2019-09-25 08:49:53 +00:00
Split `withConsecutive()` arg to array
2018-08-01 20:09:34 +00:00
```diff
class SomeClass
{
public function run($one, $two)
{
}
}
2019-09-25 08:49:53 +00:00
class SomeTestCase extends \PHPUnit\Framework\TestCase
{
public function test()
{
$someClassMock = $this->createMock(SomeClass::class);
$someClassMock
->expects($this->exactly(2))
->method('run')
- ->withConsecutive(1, 2, 3, 5);
+ ->withConsecutive([1, 2], [3, 5]);
}
}
2018-08-01 20:09:34 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
## PHPUnitSymfony
2019-05-01 23:56:58 +00:00
### `AddMessageToEqualsResponseCodeRector`
2019-05-01 23:56:58 +00:00
- class: [`Rector\PHPUnitSymfony\Rector\StaticCall\AddMessageToEqualsResponseCodeRector`](/rules/phpunit-symfony/src/Rector/StaticCall/AddMessageToEqualsResponseCodeRector.php)
- [test fixtures](/rules/phpunit-symfony/tests/Rector/StaticCall/AddMessageToEqualsResponseCodeRector/Fixture)
Add response content to response code assert, so it is easier to debug
2019-05-01 23:56:58 +00:00
```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()
);
}
}
2019-05-01 23:56:58 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
## PSR4
2018-12-31 19:29:12 +00:00
### `MultipleClassFileToPsr4ClassesRector`
2018-12-31 19:29:12 +00:00
- class: [`Rector\PSR4\Rector\MultipleClassFileToPsr4ClassesRector`](/rules/psr4/src/Rector/MultipleClassFileToPsr4ClassesRector.php)
Turns namespaced classes in one file to standalone PSR-4 classes.
```diff
+// new file: "app/Exceptions/FirstException.php"
namespace App\Exceptions;
2018-12-31 19:29:12 +00:00
use Exception;
2018-12-31 19:29:12 +00:00
final class FirstException extends Exception
{
}
+
+// new file: "app/Exceptions/SecondException.php"
+namespace App\Exceptions;
+
+use Exception;
final class SecondException extends Exception
{
}
2018-08-01 20:09:34 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 11:50:32 +00:00
### `NormalizeNamespaceByPSR4ComposerAutoloadRector`
2019-02-21 14:36:16 +00:00
- class: [`Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector`](/rules/psr4/src/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php)
- [test fixtures](/rules/psr4/tests/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture)
2019-02-21 14:36:16 +00:00
Adds namespace to namespace-less files or correct namespace to match PSR-4 in `composer.json` autoload section. Run with combination with `Rector\PSR4\Rector\MultipleClassFileToPsr4ClassesRector`
2019-02-21 14:36:16 +00:00
```diff
// src/SomeClass.php
2019-05-29 13:40:20 +00:00
+namespace App\CustomNamespace;
+
class SomeClass
{
}
```
composer.json
```json
{
"autoload": {
"psr-4": {
"App\\CustomNamespace\\": "src"
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## Performance
2020-01-21 14:55:01 +00:00
### `PreslashSimpleFunctionRector`
2020-01-21 14:55:01 +00:00
- class: [`Rector\Performance\Rector\FuncCall\PreslashSimpleFunctionRector`](/rules/performance/src/Rector/FuncCall/PreslashSimpleFunctionRector.php)
- [test fixtures](/rules/performance/tests/Rector/FuncCall/PreslashSimpleFunctionRector/Fixture)
2020-01-21 14:55:01 +00:00
Add pre-slash to short named functions to improve performance
2020-01-21 14:55:01 +00:00
```diff
class SomeClass
{
public function shorten($value)
{
- return trim($value);
+ return \trim($value);
}
}
2020-01-21 14:55:01 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-21 14:55:01 +00:00
## Phalcon
2019-05-29 13:40:20 +00:00
### `AddRequestToHandleMethodCallRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Phalcon\Rector\MethodCall\AddRequestToHandleMethodCallRector`](/rules/phalcon/src/Rector/MethodCall/AddRequestToHandleMethodCallRector.php)
- [test fixtures](/rules/phalcon/tests/Rector/MethodCall/AddRequestToHandleMethodCallRector/Fixture)
Add $_SERVER REQUEST_URI to method call
2019-05-29 13:40:20 +00:00
```diff
class SomeClass
{
public function run($di)
{
$application = new \Phalcon\Mvc\Application();
- $response = $application->handle();
+ $response = $application->handle($_SERVER["REQUEST_URI"]);
}
}
2019-02-21 14:36:16 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-21 14:36:16 +00:00
### `DecoupleSaveMethodCallWithArgumentToAssignRector`
2018-12-31 19:29:12 +00:00
- class: [`Rector\Phalcon\Rector\MethodCall\DecoupleSaveMethodCallWithArgumentToAssignRector`](/rules/phalcon/src/Rector/MethodCall/DecoupleSaveMethodCallWithArgumentToAssignRector.php)
- [test fixtures](/rules/phalcon/tests/Rector/MethodCall/DecoupleSaveMethodCallWithArgumentToAssignRector/Fixture)
2019-09-25 08:49:53 +00:00
Decouple `Phalcon\Mvc\Model::save()` with argument to `assign()`
2018-12-31 19:29:12 +00:00
2019-05-29 13:40:20 +00:00
```diff
2019-09-25 08:49:53 +00:00
class SomeClass
{
public function run(\Phalcon\Mvc\Model $model, $data)
2019-09-25 08:49:53 +00:00
{
- $model->save($data);
+ $model->save();
+ $model->assign($data);
2019-09-25 08:49:53 +00:00
}
}
2019-05-29 13:40:20 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `FlashWithCssClassesToExtraCallRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Phalcon\Rector\Assign\FlashWithCssClassesToExtraCallRector`](/rules/phalcon/src/Rector/Assign/FlashWithCssClassesToExtraCallRector.php)
- [test fixtures](/rules/phalcon/tests/Rector/Assign/FlashWithCssClassesToExtraCallRector/Fixture)
2019-05-29 13:40:20 +00:00
Add `$cssClasses` in Flash to separated method call
2019-05-29 13:40:20 +00:00
```diff
class SomeClass
{
public function run()
{
$cssClasses = [];
- $flash = new Phalcon\Flash($cssClasses);
+ $flash = new Phalcon\Flash();
+ $flash->setCssClasses($cssClasses);
}
2019-09-25 08:49:53 +00:00
}
2018-12-31 19:29:12 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 19:29:12 +00:00
### `NewApplicationToToFactoryWithDefaultContainerRector`
2018-08-01 20:09:34 +00:00
- class: [`Rector\Phalcon\Rector\Assign\NewApplicationToToFactoryWithDefaultContainerRector`](/rules/phalcon/src/Rector/Assign/NewApplicationToToFactoryWithDefaultContainerRector.php)
- [test fixtures](/rules/phalcon/tests/Rector/Assign/NewApplicationToToFactoryWithDefaultContainerRector/Fixture)
2018-10-12 23:15:00 +00:00
Change new application to default factory with application
2018-08-01 20:09:34 +00:00
```diff
2019-09-25 08:49:53 +00:00
class SomeClass
{
public function run($di)
{
- $application = new \Phalcon\Mvc\Application($di);
+ $container = new \Phalcon\Di\FactoryDefault();
+ $application = new \Phalcon\Mvc\Application($container);
2018-08-01 20:09:34 +00:00
- $response = $application->handle();
+ $response = $application->handle($_SERVER["REQUEST_URI"]);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## Php52
2019-11-06 23:52:19 +00:00
### `ContinueToBreakInSwitchRector`
2019-11-06 23:52:19 +00:00
- class: [`Rector\Php52\Rector\Switch_\ContinueToBreakInSwitchRector`](/rules/php52/src/Rector/Switch_/ContinueToBreakInSwitchRector.php)
- [test fixtures](/rules/php52/tests/Rector/Switch_/ContinueToBreakInSwitchRector/Fixture)
Use break instead of continue in switch statements
2019-11-06 23:52:19 +00:00
```diff
function some_run($value)
2019-11-06 23:52:19 +00:00
{
switch ($value) {
case 1:
echo 'Hi';
- continue;
+ break;
case 2:
echo 'Hello';
break;
2019-11-06 23:52:19 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-11-06 23:52:19 +00:00
### `VarToPublicPropertyRector`
2018-08-01 20:09:34 +00:00
- class: [`Rector\Php52\Rector\Property\VarToPublicPropertyRector`](/rules/php52/src/Rector/Property/VarToPublicPropertyRector.php)
- [test fixtures](/rules/php52/tests/Rector/Property/VarToPublicPropertyRector/Fixture)
2018-10-12 23:15:00 +00:00
Remove unused private method
2018-08-01 20:09:34 +00:00
```diff
final class SomeController
{
- var $name = 'Tom';
+ public $name = 'Tom';
}
2018-08-01 20:09:34 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
## Php53
2018-12-31 19:29:12 +00:00
### `ClearReturnNewByReferenceRector`
2018-12-31 19:29:12 +00:00
- class: [`Rector\Php53\Rector\AssignRef\ClearReturnNewByReferenceRector`](/rules/php53/src/Rector/AssignRef/ClearReturnNewByReferenceRector.php)
- [test fixtures](/rules/php53/tests/Rector/AssignRef/ClearReturnNewByReferenceRector/Fixture)
Remove reference from "$assign = &new Value;"
```diff
-$assign = &new Value;
+$assign = new Value;
```
<br><br>
### `DirNameFileConstantToDirConstantRector`
- class: [`Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector`](/rules/php53/src/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php)
- [test fixtures](/rules/php53/tests/Rector/FuncCall/DirNameFileConstantToDirConstantRector/Fixture)
Convert dirname(__FILE__) to __DIR__
2018-12-31 19:29:12 +00:00
```diff
class SomeClass
2019-05-29 13:40:20 +00:00
{
public function run()
{
- return dirname(__FILE__);
+ return __DIR__;
}
2019-05-29 13:40:20 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `ReplaceHttpServerVarsByServerRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Php53\Rector\Variable\ReplaceHttpServerVarsByServerRector`](/rules/php53/src/Rector/Variable/ReplaceHttpServerVarsByServerRector.php)
- [test fixtures](/rules/php53/tests/Rector/Variable/ReplaceHttpServerVarsByServerRector/Fixture)
2019-05-29 13:40:20 +00:00
Rename old `$HTTP_*` variable names to new replacements
2019-05-29 13:40:20 +00:00
```diff
-$serverVars = $HTTP_SERVER_VARS;
+$serverVars = $_SERVER;
```
2018-12-31 19:29:12 +00:00
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `TernaryToElvisRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Php53\Rector\Ternary\TernaryToElvisRector`](/rules/php53/src/Rector/Ternary/TernaryToElvisRector.php)
- [test fixtures](/rules/php53/tests/Rector/Ternary/TernaryToElvisRector/Fixture)
2019-05-29 13:40:20 +00:00
Use ?: instead of ?, where useful
2019-05-29 13:40:20 +00:00
```diff
function elvis()
{
- $value = $a ? $a : false;
+ $value = $a ?: false;
}
2018-12-31 19:29:12 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-12-31 19:29:12 +00:00
## Php54
2019-03-31 12:25:39 +00:00
### `RemoveReferenceFromCallRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\Php54\Rector\FuncCall\RemoveReferenceFromCallRector`](/rules/php54/src/Rector/FuncCall/RemoveReferenceFromCallRector.php)
- [test fixtures](/rules/php54/tests/Rector/FuncCall/RemoveReferenceFromCallRector/Fixture)
2019-03-31 12:25:39 +00:00
Remove & from function and method calls
```diff
final class SomeClass
2019-03-31 12:25:39 +00:00
{
public function run($one)
{
- return strlen(&$one);
+ return strlen($one);
}
2019-03-31 12:25:39 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `RemoveZeroBreakContinueRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\Php54\Rector\Break_\RemoveZeroBreakContinueRector`](/rules/php54/src/Rector/Break_/RemoveZeroBreakContinueRector.php)
- [test fixtures](/rules/php54/tests/Rector/Break_/RemoveZeroBreakContinueRector/Fixture)
2019-03-31 12:25:39 +00:00
Remove 0 from break and continue
2019-03-31 12:25:39 +00:00
```diff
class SomeClass
{
public function run($random)
{
- continue 0;
- break 0;
+ continue;
+ break;
2019-09-25 08:49:53 +00:00
$five = 5;
- continue $five;
+ continue 5;
2019-09-25 08:49:53 +00:00
- break $random;
+ break;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## Php55
### `PregReplaceEModifierRector`
- class: [`Rector\Php55\Rector\FuncCall\PregReplaceEModifierRector`](/rules/php55/src/Rector/FuncCall/PregReplaceEModifierRector.php)
- [test fixtures](/rules/php55/tests/Rector/FuncCall/PregReplaceEModifierRector/Fixture)
The /e modifier is no longer supported, use `preg_replace_callback` instead
```diff
2019-09-25 08:49:53 +00:00
class SomeClass
{
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-09-25 08:49:53 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `StringClassNameToClassConstantRector`
- class: [`Rector\Php55\Rector\String_\StringClassNameToClassConstantRector`](/rules/php55/src/Rector/String_/StringClassNameToClassConstantRector.php)
- [test fixtures](/rules/php55/tests/Rector/String_/StringClassNameToClassConstantRector/Fixture)
Replace string class names by <class>::class constant
```php
<?php
declare(strict_types=1);
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(StringClassNameToClassConstantRector::class)
->call('configure', [[
StringClassNameToClassConstantRector::CLASSES_TO_SKIP => ['ClassName', 'AnotherClassName'],
]]);
};
```
```diff
class AnotherClass
{
}
class SomeClass
{
2019-09-25 08:49:53 +00:00
public function run()
{
- return 'AnotherClass';
+ return \AnotherClass::class;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## Php56
### `AddDefaultValueForUndefinedVariableRector`
- class: [`Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector`](/rules/php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php)
- [test fixtures](/rules/php56/tests/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector/Fixture)
Adds default value for undefined variable
```diff
class SomeClass
{
public function run()
{
+ $a = null;
if (rand(0, 1)) {
$a = 5;
}
echo $a;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `PowToExpRector`
- class: [`Rector\Php56\Rector\FuncCall\PowToExpRector`](/rules/php56/src/Rector/FuncCall/PowToExpRector.php)
- [test fixtures](/rules/php56/tests/Rector/FuncCall/PowToExpRector/Fixture)
Changes pow(val, val2) to ** `(exp)` parameter
```diff
-pow(1, 2);
+1**2;
```
2020-06-16 16:14:51 +00:00
<br><br>
## Php70
### `BreakNotInLoopOrSwitchToReturnRector`
2020-04-24 22:57:13 +00:00
- class: [`Rector\Php70\Rector\Break_\BreakNotInLoopOrSwitchToReturnRector`](/rules/php70/src/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector.php)
- [test fixtures](/rules/php70/tests/Rector/Break_/BreakNotInLoopOrSwitchToReturnRector/Fixture)
2020-04-24 22:57:13 +00:00
Convert break outside for/foreach/switch context to return
2020-04-24 22:57:13 +00:00
```diff
class SomeClass
{
public function run()
{
if ($isphp5)
return 1;
else
return 2;
- break;
+ return;
}
2020-04-24 22:57:13 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-04-24 22:57:13 +00:00
### `CallUserMethodRector`
2020-04-29 20:01:42 +00:00
- class: [`Rector\Php70\Rector\FuncCall\CallUserMethodRector`](/rules/php70/src/Rector/FuncCall/CallUserMethodRector.php)
- [test fixtures](/rules/php70/tests/Rector/FuncCall/CallUserMethodRector/Fixture)
2020-04-29 20:01:42 +00:00
Changes `call_user_method()/call_user_method_array()` to `call_user_func()/call_user_func_array()`
2020-04-29 20:01:42 +00:00
```diff
-call_user_method($method, $obj, "arg1", "arg2");
+call_user_func(array(&$obj, "method"), "arg1", "arg2");
2020-04-29 20:01:42 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-04-29 20:01:42 +00:00
### `EmptyListRector`
- class: [`Rector\Php70\Rector\List_\EmptyListRector`](/rules/php70/src/Rector/List_/EmptyListRector.php)
- [test fixtures](/rules/php70/tests/Rector/List_/EmptyListRector/Fixture)
`list()` cannot be empty
```diff
-'list() = $values;'
+'list($unusedGenerated) = $values;'
```
<br><br>
### `EregToPregMatchRector`
2020-04-29 21:23:14 +00:00
- class: [`Rector\Php70\Rector\FuncCall\EregToPregMatchRector`](/rules/php70/src/Rector/FuncCall/EregToPregMatchRector.php)
- [test fixtures](/rules/php70/tests/Rector/FuncCall/EregToPregMatchRector/Fixture)
2020-04-29 21:23:14 +00:00
Changes ereg*() to preg*() calls
2020-04-29 21:23:14 +00:00
```diff
-ereg("hi")
+preg_match("#hi#");
2020-04-29 21:23:14 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-04-29 21:23:14 +00:00
### `ExceptionHandlerTypehintRector`
2020-06-05 10:33:30 +00:00
- class: [`Rector\Php70\Rector\FunctionLike\ExceptionHandlerTypehintRector`](/rules/php70/src/Rector/FunctionLike/ExceptionHandlerTypehintRector.php)
- [test fixtures](/rules/php70/tests/Rector/FunctionLike/ExceptionHandlerTypehintRector/Fixture)
2020-06-05 10:33:30 +00:00
Changes property `@var` annotations from annotation to type.
2020-06-05 10:33:30 +00:00
```diff
-function handler(Exception $exception) { ... }
+function handler(Throwable $exception) { ... }
set_exception_handler('handler');
2020-06-05 10:33:30 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-06-05 10:33:30 +00:00
### `IfToSpaceshipRector`
2020-03-26 22:22:29 +00:00
- class: [`Rector\Php70\Rector\If_\IfToSpaceshipRector`](/rules/php70/src/Rector/If_/IfToSpaceshipRector.php)
- [test fixtures](/rules/php70/tests/Rector/If_/IfToSpaceshipRector/Fixture)
2020-03-26 22:22:29 +00:00
Changes if/else to spaceship <=> where useful
2020-03-26 22:22:29 +00:00
```diff
class SomeClass
{
public function run()
{
usort($languages, function ($a, $b) {
- if ($a[0] === $b[0]) {
- return 0;
- }
-
- return ($a[0] < $b[0]) ? 1 : -1;
+ return $b[0] <=> $a[0];
});
}
}
```
2020-03-26 22:22:29 +00:00
2020-06-16 16:14:51 +00:00
<br><br>
2020-03-26 22:22:29 +00:00
### `ListSplitStringRector`
2020-04-23 22:28:33 +00:00
- class: [`Rector\Php70\Rector\Assign\ListSplitStringRector`](/rules/php70/src/Rector/Assign/ListSplitStringRector.php)
- [test fixtures](/rules/php70/tests/Rector/Assign/ListSplitStringRector/Fixture)
2020-04-23 22:28:33 +00:00
`list()` cannot split string directly anymore, use `str_split()`
2020-04-23 22:28:33 +00:00
```diff
-list($foo) = "string";
+list($foo) = str_split("string");
2020-04-23 22:28:33 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-04-23 22:28:33 +00:00
### `ListSwapArrayOrderRector`
2020-04-23 12:34:27 +00:00
- class: [`Rector\Php70\Rector\Assign\ListSwapArrayOrderRector`](/rules/php70/src/Rector/Assign/ListSwapArrayOrderRector.php)
- [test fixtures](/rules/php70/tests/Rector/Assign/ListSwapArrayOrderRector/Fixture)
2020-04-23 12:34:27 +00:00
`list()` assigns variables in reverse order - relevant in array assign
2020-04-23 12:34:27 +00:00
```diff
-list($a[], $a[]) = [1, 2];
+list($a[], $a[]) = array_reverse([1, 2]);
2020-04-23 12:34:27 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-04-23 12:34:27 +00:00
### `MultiDirnameRector`
2020-04-24 12:00:49 +00:00
- class: [`Rector\Php70\Rector\FuncCall\MultiDirnameRector`](/rules/php70/src/Rector/FuncCall/MultiDirnameRector.php)
- [test fixtures](/rules/php70/tests/Rector/FuncCall/MultiDirnameRector/Fixture)
2020-04-24 12:00:49 +00:00
Changes multiple `dirname()` calls to one with nesting level
2020-04-24 12:00:49 +00:00
```diff
-dirname(dirname($path));
+dirname($path, 2);
2020-04-24 12:00:49 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-04-24 12:00:49 +00:00
### `NonVariableToVariableOnFunctionCallRector`
2020-06-05 10:33:30 +00:00
- class: [`Rector\Php70\Rector\FuncCall\NonVariableToVariableOnFunctionCallRector`](/rules/php70/src/Rector/FuncCall/NonVariableToVariableOnFunctionCallRector.php)
- [test fixtures](/rules/php70/tests/Rector/FuncCall/NonVariableToVariableOnFunctionCallRector/Fixture)
2020-06-05 10:33:30 +00:00
Transform non variable like arguments to variable where a function or method expects an argument passed by reference
2020-06-05 10:33:30 +00:00
```diff
-reset(a());
+$a = a(); reset($a);
2020-06-05 10:33:30 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-06-05 10:33:30 +00:00
### `Php4ConstructorRector`
- class: [`Rector\Php70\Rector\ClassMethod\Php4ConstructorRector`](/rules/php70/src/Rector/ClassMethod/Php4ConstructorRector.php)
- [test fixtures](/rules/php70/tests/Rector/ClassMethod/Php4ConstructorRector/Fixture)
Changes PHP 4 style constructor to __construct.
```diff
class SomeClass
{
- public function SomeClass()
+ public function __construct()
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RandomFunctionRector`
- class: [`Rector\Php70\Rector\FuncCall\RandomFunctionRector`](/rules/php70/src/Rector/FuncCall/RandomFunctionRector.php)
- [test fixtures](/rules/php70/tests/Rector/FuncCall/RandomFunctionRector/Fixture)
Changes rand, `srand` and `getrandmax` by new mt_* alternatives.
```diff
-rand();
+mt_rand();
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ReduceMultipleDefaultSwitchRector`
- class: [`Rector\Php70\Rector\Switch_\ReduceMultipleDefaultSwitchRector`](/rules/php70/src/Rector/Switch_/ReduceMultipleDefaultSwitchRector.php)
- [test fixtures](/rules/php70/tests/Rector/Switch_/ReduceMultipleDefaultSwitchRector/Fixture)
Remove first default switch, that is ignored
```diff
switch ($expr) {
default:
- echo "Hello World";
2019-09-25 08:49:53 +00:00
-
- default:
echo "Goodbye Moon!";
break;
2019-09-25 08:49:53 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameMktimeWithoutArgsToTimeRector`
- class: [`Rector\Php70\Rector\FuncCall\RenameMktimeWithoutArgsToTimeRector`](/rules/php70/src/Rector/FuncCall/RenameMktimeWithoutArgsToTimeRector.php)
- [test fixtures](/rules/php70/tests/Rector/FuncCall/RenameMktimeWithoutArgsToTimeRector/Fixture)
Renames `mktime()` without arguments to `time()`
```diff
class SomeClass
{
public function run()
2019-03-31 12:25:39 +00:00
{
$time = mktime(1, 2, 3);
- $nextTime = mktime();
+ $nextTime = time();
2019-03-31 12:25:39 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `StaticCallOnNonStaticToInstanceCallRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector`](/rules/php70/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php)
- [test fixtures](/rules/php70/tests/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector/Fixture)
2019-03-31 12:25:39 +00:00
Changes static call to instance call, where not useful
2019-03-31 12:25:39 +00:00
```diff
class Something
{
public function doWork()
{
}
}
2019-03-31 12:25:39 +00:00
class Another
2019-03-31 12:25:39 +00:00
{
public function run()
2019-03-31 12:25:39 +00:00
{
- return Something::doWork();
+ return (new Something)->doWork();
2019-03-31 12:25:39 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `TernaryToNullCoalescingRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\Php70\Rector\Ternary\TernaryToNullCoalescingRector`](/rules/php70/src/Rector/Ternary/TernaryToNullCoalescingRector.php)
- [test fixtures](/rules/php70/tests/Rector/Ternary/TernaryToNullCoalescingRector/Fixture)
2019-03-31 12:25:39 +00:00
Changes unneeded null check to ?? operator
2019-03-31 12:25:39 +00:00
```diff
-$value === null ? 10 : $value;
+$value ?? 10;
```
```diff
-isset($value) ? $value : 10;
+$value ?? 10;
2019-03-31 12:25:39 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `TernaryToSpaceshipRector`
2019-03-16 20:31:46 +00:00
- class: [`Rector\Php70\Rector\Ternary\TernaryToSpaceshipRector`](/rules/php70/src/Rector/Ternary/TernaryToSpaceshipRector.php)
- [test fixtures](/rules/php70/tests/Rector/Ternary/TernaryToSpaceshipRector/Fixture)
2019-03-16 20:31:46 +00:00
Use <=> spaceship instead of ternary with same effect
2019-03-16 20:31:46 +00:00
```diff
function order_func($a, $b) {
- return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
+ return $a <=> $b;
}
```
2019-03-16 20:31:46 +00:00
<br><br>
### `ThisCallOnStaticMethodToStaticCallRector`
- class: [`Rector\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector`](/rules/php70/src/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector.php)
- [test fixtures](/rules/php70/tests/Rector/MethodCall/ThisCallOnStaticMethodToStaticCallRector/Fixture)
Changes `$this->call()` to static method to static call
```diff
class SomeClass
2019-03-16 20:31:46 +00:00
{
public static function run()
{
- $this->eat();
+ static::eat();
}
public static function eat()
2019-03-16 20:31:46 +00:00
{
}
2019-03-31 12:25:39 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
### `WrapVariableVariableNameInCurlyBracesRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\Php70\Rector\Variable\WrapVariableVariableNameInCurlyBracesRector`](/rules/php70/src/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector.php)
- [test fixtures](/rules/php70/tests/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector/Fixture)
2019-03-16 20:31:46 +00:00
Ensure variable variables are wrapped in curly braces
2019-03-31 12:25:39 +00:00
```diff
function run($foo)
2019-03-31 12:25:39 +00:00
{
- global $$foo->bar;
+ global ${$foo->bar};
2019-03-16 20:31:46 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-16 20:31:46 +00:00
## Php71
### `AssignArrayToStringRector`
- class: [`Rector\Php71\Rector\Assign\AssignArrayToStringRector`](/rules/php71/src/Rector/Assign/AssignArrayToStringRector.php)
- [test fixtures](/rules/php71/tests/Rector/Assign/AssignArrayToStringRector/Fixture)
String cannot be turned into array by assignment anymore
```diff
-$string = '';
+$string = [];
$string[] = 1;
```
2020-06-16 16:14:51 +00:00
<br><br>
### `BinaryOpBetweenNumberAndStringRector`
2020-01-06 15:06:07 +00:00
- class: [`Rector\Php71\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector`](/rules/php71/src/Rector/BinaryOp/BinaryOpBetweenNumberAndStringRector.php)
- [test fixtures](/rules/php71/tests/Rector/BinaryOp/BinaryOpBetweenNumberAndStringRector/Fixture)
2020-01-06 15:06:07 +00:00
Change binary operation between some number + string to PHP 7.1 compatible version
2020-01-06 15:06:07 +00:00
```diff
class SomeClass
{
public function run()
{
- $value = 5 + '';
- $value = 5.0 + 'hi';
+ $value = 5 + 0;
+ $value = 5.0 + 0
2020-01-06 15:06:07 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-06 15:06:07 +00:00
### `CountOnNullRector`
- class: [`Rector\Php71\Rector\FuncCall\CountOnNullRector`](/rules/php71/src/Rector/FuncCall/CountOnNullRector.php)
- [test fixtures](/rules/php71/tests/Rector/FuncCall/CountOnNullRector/Fixture)
Changes `count()` on null to safe ternary check
```diff
$values = null;
-$count = count($values);
+$count = is_array($values) || $values instanceof Countable ? count($values) : 0;
```
2020-06-16 16:14:51 +00:00
<br><br>
### `IsIterableRector`
2020-03-28 23:06:05 +00:00
- class: [`Rector\Php71\Rector\BinaryOp\IsIterableRector`](/rules/php71/src/Rector/BinaryOp/IsIterableRector.php)
- [test fixtures](/rules/php71/tests/Rector/BinaryOp/IsIterableRector/Fixture)
2020-05-12 15:09:29 +00:00
Changes `is_array` + Traversable check to `is_iterable`
2020-05-12 15:09:29 +00:00
```diff
-is_array($foo) || $foo instanceof Traversable;
+is_iterable($foo);
```
<br><br>
### `ListToArrayDestructRector`
- class: [`Rector\Php71\Rector\List_\ListToArrayDestructRector`](/rules/php71/src/Rector/List_/ListToArrayDestructRector.php)
- [test fixtures](/rules/php71/tests/Rector/List_/ListToArrayDestructRector/Fixture)
Remove & from new &X
2020-05-12 15:09:29 +00:00
```diff
class SomeClass
{
public function run()
{
- list($id1, $name1) = $data;
+ [$id1, $name1] = $data;
- foreach ($data as list($id, $name)) {
+ foreach ($data as [$id, $name]) {
}
2020-05-12 15:09:29 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-05-12 15:09:29 +00:00
### `MultiExceptionCatchRector`
2020-05-29 10:41:25 +00:00
- class: [`Rector\Php71\Rector\TryCatch\MultiExceptionCatchRector`](/rules/php71/src/Rector/TryCatch/MultiExceptionCatchRector.php)
- [test fixtures](/rules/php71/tests/Rector/TryCatch/MultiExceptionCatchRector/Fixture)
2020-05-29 10:41:25 +00:00
Changes multi catch of same exception to single one | separated.
2020-05-29 10:41:25 +00:00
```diff
try {
- // Some code...
-} catch (ExceptionType1 $exception) {
- $sameCode;
-} catch (ExceptionType2 $exception) {
- $sameCode;
+ // Some code...
+} catch (ExceptionType1 | ExceptionType2 $exception) {
+ $sameCode;
2020-05-29 10:41:25 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-05-29 10:41:25 +00:00
### `PublicConstantVisibilityRector`
- class: [`Rector\Php71\Rector\ClassConst\PublicConstantVisibilityRector`](/rules/php71/src/Rector/ClassConst/PublicConstantVisibilityRector.php)
- [test fixtures](/rules/php71/tests/Rector/ClassConst/PublicConstantVisibilityRector/Fixture)
Add explicit public constant visibility.
```diff
class SomeClass
{
- const HEY = 'you';
+ public const HEY = 'you';
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveExtraParametersRector`
- class: [`Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector`](/rules/php71/src/Rector/FuncCall/RemoveExtraParametersRector.php)
- [test fixtures](/rules/php71/tests/Rector/FuncCall/RemoveExtraParametersRector/Fixture)
Remove extra parameters
```diff
-strlen("asdf", 1);
+strlen("asdf");
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ReservedObjectRector`
- class: [`Rector\Php71\Rector\Name\ReservedObjectRector`](/rules/php71/src/Rector/Name/ReservedObjectRector.php)
- [test fixtures](/rules/php71/tests/Rector/Name/ReservedObjectRector/Fixture)
Changes reserved "Object" name to "<Smart>Object" where <Smart> can be configured
```php
<?php
declare(strict_types=1);
use Rector\Php71\Rector\Name\ReservedObjectRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ReservedObjectRector::class)
->call('configure', [[
ReservedObjectRector::RESERVED_KEYWORDS_TO_REPLACEMENTS => [
'ReservedObject' => 'SmartObject',
'Object' => 'AnotherSmartObject',
],
]]);
};
```
```diff
-class Object
+class SmartObject
{
}
```
<br><br>
## Php72
### `BarewordStringRector`
- class: [`Rector\Php72\Rector\ConstFetch\BarewordStringRector`](/rules/php72/src/Rector/ConstFetch/BarewordStringRector.php)
- [test fixtures](/rules/php72/tests/Rector/ConstFetch/BarewordStringRector/Fixture)
Changes unquoted non-existing constants to strings
```diff
-var_dump(VAR);
+var_dump("VAR");
```
2020-06-16 16:14:51 +00:00
<br><br>
### `CreateFunctionToAnonymousFunctionRector`
2020-03-28 23:06:05 +00:00
- class: [`Rector\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector`](/rules/php72/src/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php)
- [test fixtures](/rules/php72/tests/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector/Fixture)
2020-03-28 23:06:05 +00:00
Use anonymous functions instead of deprecated `create_function()`
2020-03-28 23:06:05 +00:00
```diff
class ClassWithCreateFunction
2020-03-28 23:06:05 +00:00
{
public function run()
{
- $callable = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
+ $callable = function($matches) use ($delimiter) {
+ return $delimiter . strtolower($matches[1]);
+ };
2020-03-28 23:06:05 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-03-28 23:06:05 +00:00
### `GetClassOnNullRector`
- class: [`Rector\Php72\Rector\FuncCall\GetClassOnNullRector`](/rules/php72/src/Rector/FuncCall/GetClassOnNullRector.php)
- [test fixtures](/rules/php72/tests/Rector/FuncCall/GetClassOnNullRector/Fixture)
Null is no more allowed in `get_class()`
```diff
final class SomeClass
{
public function getItem()
{
$value = null;
- return get_class($value);
+ return $value !== null ? get_class($value) : self::class;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `IsObjectOnIncompleteClassRector`
- class: [`Rector\Php72\Rector\FuncCall\IsObjectOnIncompleteClassRector`](/rules/php72/src/Rector/FuncCall/IsObjectOnIncompleteClassRector.php)
- [test fixtures](/rules/php72/tests/Rector/FuncCall/IsObjectOnIncompleteClassRector/Fixture)
Incomplete class returns inverted bool on `is_object()`
```diff
$incompleteObject = new __PHP_Incomplete_Class;
-$isObject = is_object($incompleteObject);
+$isObject = ! is_object($incompleteObject);
```
<br><br>
### `ListEachRector`
- class: [`Rector\Php72\Rector\Assign\ListEachRector`](/rules/php72/src/Rector/Assign/ListEachRector.php)
- [test fixtures](/rules/php72/tests/Rector/Assign/ListEachRector/Fixture)
`each()` function is deprecated, use `key()` and `current()` instead
```diff
-list($key, $callback) = each($callbacks);
+$key = key($callbacks);
+$callback = current($callbacks);
+next($callbacks);
```
<br><br>
### `ParseStrWithResultArgumentRector`
- class: [`Rector\Php72\Rector\FuncCall\ParseStrWithResultArgumentRector`](/rules/php72/src/Rector/FuncCall/ParseStrWithResultArgumentRector.php)
- [test fixtures](/rules/php72/tests/Rector/FuncCall/ParseStrWithResultArgumentRector/Fixture)
Use `$result` argument in `parse_str()` function
```diff
-parse_str($this->query);
-$data = get_defined_vars();
+parse_str($this->query, $result);
+$data = $result;
```
<br><br>
### `ReplaceEachAssignmentWithKeyCurrentRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php72\Rector\Assign\ReplaceEachAssignmentWithKeyCurrentRector`](/rules/php72/src/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php)
- [test fixtures](/rules/php72/tests/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector/Fixture)
2019-09-25 08:49:53 +00:00
Replace `each()` assign outside loop
2019-09-25 08:49:53 +00:00
```diff
$array = ['b' => 1, 'a' => 2];
-$eachedArray = each($array);
+$eachedArray[1] = current($array);
+$eachedArray['value'] = current($array);
+$eachedArray[0] = key($array);
+$eachedArray['key'] = key($array);
+next($array);
```
<br><br>
### `StringifyDefineRector`
- class: [`Rector\Php72\Rector\FuncCall\StringifyDefineRector`](/rules/php72/src/Rector/FuncCall/StringifyDefineRector.php)
- [test fixtures](/rules/php72/tests/Rector/FuncCall/StringifyDefineRector/Fixture)
Make first argument of `define()` string
2019-09-25 08:49:53 +00:00
```diff
class SomeClass
{
public function run(int $a)
2019-09-25 08:49:53 +00:00
{
- define(CONSTANT_2, 'value');
+ define('CONSTANT_2', 'value');
define('CONSTANT', 'value');
2019-09-25 08:49:53 +00:00
}
}
```
2019-09-25 08:49:53 +00:00
<br><br>
### `StringsAssertNakedRector`
- class: [`Rector\Php72\Rector\FuncCall\StringsAssertNakedRector`](/rules/php72/src/Rector/FuncCall/StringsAssertNakedRector.php)
- [test fixtures](/rules/php72/tests/Rector/FuncCall/StringsAssertNakedRector/Fixture)
String asserts must be passed directly to `assert()`
```diff
function nakedAssert()
2019-09-25 08:49:53 +00:00
{
- assert('true === true');
- assert("true === true");
+ assert(true === true);
+ assert(true === true);
2019-09-25 08:49:53 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `UnsetCastRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php72\Rector\Unset_\UnsetCastRector`](/rules/php72/src/Rector/Unset_/UnsetCastRector.php)
- [test fixtures](/rules/php72/tests/Rector/Unset_/UnsetCastRector/Fixture)
2019-09-25 08:49:53 +00:00
Removes (unset) cast
2020-07-24 11:46:57 +00:00
```diff
-$different = (unset) $value;
+$different = null;
2020-07-24 11:46:57 +00:00
-$value = (unset) $value;
+unset($value);
2019-09-25 08:49:53 +00:00
```
<br><br>
2019-09-25 08:49:53 +00:00
### `WhileEachToForeachRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php72\Rector\While_\WhileEachToForeachRector`](/rules/php72/src/Rector/While_/WhileEachToForeachRector.php)
- [test fixtures](/rules/php72/tests/Rector/While_/WhileEachToForeachRector/Fixture)
2019-11-06 23:52:19 +00:00
`each()` function is deprecated, use `foreach()` instead.
```diff
-while (list($key, $callback) = each($callbacks)) {
+foreach ($callbacks as $key => $callback) {
// ...
2019-09-25 08:49:53 +00:00
}
```
```diff
-while (list($key) = each($callbacks)) {
+foreach (array_keys($callbacks) as $key) {
// ...
}
```
2019-09-25 08:49:53 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
## Php73
2019-09-25 08:49:53 +00:00
### `ArrayKeyFirstLastRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector`](/rules/php73/src/Rector/FuncCall/ArrayKeyFirstLastRector.php)
- [test fixtures](/rules/php73/tests/Rector/FuncCall/ArrayKeyFirstLastRector/Fixture)
2020-07-24 11:46:57 +00:00
Make use of `array_key_first()` and `array_key_last()`
2020-07-24 11:46:57 +00:00
```diff
-reset($items);
-$firstKey = key($items);
+$firstKey = array_key_first($items);
2019-09-25 08:49:53 +00:00
```
```diff
-end($items);
-$lastKey = key($items);
+$lastKey = array_key_last($items);
```
2020-06-16 16:14:51 +00:00
<br><br>
### `IsCountableRector`
- class: [`Rector\Php73\Rector\BinaryOp\IsCountableRector`](/rules/php73/src/Rector/BinaryOp/IsCountableRector.php)
- [test fixtures](/rules/php73/tests/Rector/BinaryOp/IsCountableRector/Fixture)
Changes `is_array` + Countable check to `is_countable`
2019-09-25 08:49:53 +00:00
```diff
-is_array($foo) || $foo instanceof Countable;
+is_countable($foo);
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `JsonThrowOnErrorRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector`](/rules/php73/src/Rector/FuncCall/JsonThrowOnErrorRector.php)
- [test fixtures](/rules/php73/tests/Rector/FuncCall/JsonThrowOnErrorRector/Fixture)
2019-09-25 08:49:53 +00:00
Adds JSON_THROW_ON_ERROR to `json_encode()` and `json_decode()` to throw JsonException on error
2019-09-25 08:49:53 +00:00
```diff
-json_encode($content);
-json_decode($json);
+json_encode($content, JSON_THROW_ON_ERROR);
+json_decode($json, null, null, JSON_THROW_ON_ERROR);
2019-09-25 08:49:53 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `RegexDashEscapeRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php73\Rector\FuncCall\RegexDashEscapeRector`](/rules/php73/src/Rector/FuncCall/RegexDashEscapeRector.php)
- [test fixtures](/rules/php73/tests/Rector/FuncCall/RegexDashEscapeRector/Fixture)
2019-09-25 08:49:53 +00:00
Escape - in some cases
2019-09-25 08:49:53 +00:00
```diff
-preg_match("#[\w-()]#", 'some text');
+preg_match("#[\w\-()]#", 'some text');
```
2019-09-25 08:49:53 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `RemoveMissingCompactVariableRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Php73\Rector\FuncCall\RemoveMissingCompactVariableRector`](/rules/php73/src/Rector/FuncCall/RemoveMissingCompactVariableRector.php)
- [test fixtures](/rules/php73/tests/Rector/FuncCall/RemoveMissingCompactVariableRector/Fixture)
2019-09-25 08:49:53 +00:00
Remove non-existing vars from `compact()`
2019-09-25 08:49:53 +00:00
```diff
class SomeClass
2019-09-25 08:49:53 +00:00
{
public function run()
2019-09-25 08:49:53 +00:00
{
$value = 'yes';
- compact('value', 'non_existing');
+ compact('value');
2019-09-25 08:49:53 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `SensitiveConstantNameRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector`](/rules/php73/src/Rector/ConstFetch/SensitiveConstantNameRector.php)
- [test fixtures](/rules/php73/tests/Rector/ConstFetch/SensitiveConstantNameRector/Fixture)
2019-09-25 08:49:53 +00:00
Changes case insensitive constants to sensitive ones.
2019-09-25 08:49:53 +00:00
```diff
define('FOO', 42, true);
var_dump(FOO);
-var_dump(foo);
+var_dump(FOO);
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `SensitiveDefineRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php73\Rector\FuncCall\SensitiveDefineRector`](/rules/php73/src/Rector/FuncCall/SensitiveDefineRector.php)
- [test fixtures](/rules/php73/tests/Rector/FuncCall/SensitiveDefineRector/Fixture)
Changes case insensitive constants to sensitive ones.
2019-09-25 08:49:53 +00:00
```diff
-define('FOO', 42, true);
+define('FOO', 42);
2019-09-25 08:49:53 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `SensitiveHereNowDocRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php73\Rector\String_\SensitiveHereNowDocRector`](/rules/php73/src/Rector/String_/SensitiveHereNowDocRector.php)
- [test fixtures](/rules/php73/tests/Rector/String_/SensitiveHereNowDocRector/Fixture)
2019-09-25 08:49:53 +00:00
Changes heredoc/nowdoc that contains closing word to safe wrapper name
2019-09-25 08:49:53 +00:00
```diff
-$value = <<<A
+$value = <<<A_WRAP
A
-A
+A_WRAP
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `SetCookieRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php73\Rector\FuncCall\SetCookieRector`](/rules/php73/src/Rector/FuncCall/SetCookieRector.php)
- [test fixtures](/rules/php73/tests/Rector/FuncCall/SetcookieRector/Fixture)
2019-09-25 08:49:53 +00:00
Convert `setcookie` argument to PHP7.3 option array
2019-09-25 08:49:53 +00:00
```diff
-setcookie('name', $value, 360);
+setcookie('name', $value, ['expires' => 360]);
```
```diff
-setcookie('name', $name, 0, '', '', true, true);
+setcookie('name', $name, ['expires' => 0, 'path' => '', 'domain' => '', 'secure' => true, 'httponly' => true]);
2019-09-25 08:49:53 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `StringifyStrNeedlesRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector`](/rules/php73/src/Rector/FuncCall/StringifyStrNeedlesRector.php)
- [test fixtures](/rules/php73/tests/Rector/FuncCall/StringifyStrNeedlesRector/Fixture)
2019-09-25 08:49:53 +00:00
Makes needles explicit strings
2019-09-25 08:49:53 +00:00
```diff
$needle = 5;
-$fivePosition = strpos('725', $needle);
+$fivePosition = strpos('725', (string) $needle);
2019-09-25 08:49:53 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
## Php74
2020-07-24 11:46:57 +00:00
### `AddLiteralSeparatorToNumberRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector`](/rules/php74/src/Rector/LNumber/AddLiteralSeparatorToNumberRector.php)
- [test fixtures](/rules/php74/tests/Rector/LNumber/AddLiteralSeparatorToNumberRector/Fixture)
2020-02-13 21:48:16 +00:00
Add "_" as thousands separator in numbers
2020-02-13 21:48:16 +00:00
```diff
class SomeClass
{
public function run()
{
- $int = 1000;
- $float = 1000500.001;
+ $int = 1_000;
+ $float = 1_000_500.001;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ArrayKeyExistsOnPropertyRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\Php74\Rector\FuncCall\ArrayKeyExistsOnPropertyRector`](/rules/php74/src/Rector/FuncCall/ArrayKeyExistsOnPropertyRector.php)
- [test fixtures](/rules/php74/tests/Rector/FuncCall/ArrayKeyExistsOnPropertyRector/Fixture)
2019-09-25 08:49:53 +00:00
Change `array_key_exists()` on property to `property_exists()`
2020-07-24 11:46:57 +00:00
```diff
class SomeClass
{
public $value;
}
$someClass = new SomeClass;
2020-07-24 11:46:57 +00:00
-array_key_exists('value', $someClass);
+property_exists($someClass, 'value');
2019-09-25 08:49:53 +00:00
```
<br><br>
2019-09-25 08:49:53 +00:00
### `ArraySpreadInsteadOfArrayMergeRector`
- class: [`Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector`](/rules/php74/src/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php)
- [test fixtures](/rules/php74/tests/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/Fixture)
Change `array_merge()` to spread operator, except values with possible string `key` values
```diff
class SomeClass
{
public function run($iter1, $iter2)
{
- $values = array_merge(iterator_to_array($iter1), iterator_to_array($iter2));
+ $values = [...$iter1, ...$iter2];
// Or to generalize to all iterables
- $anotherValues = array_merge(
- is_array($iter1) ? $iter1 : iterator_to_array($iter1),
- is_array($iter2) ? $iter2 : iterator_to_array($iter2)
- );
+ $anotherValues = [...$iter1, ...$iter2];
}
}
```
<br><br>
2020-07-24 11:46:57 +00:00
### `ChangeReflectionTypeToStringToGetNameRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector`](/rules/php74/src/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php)
- [test fixtures](/rules/php74/tests/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector/Fixture)
Change string calls on ReflectionType
```diff
class SomeClass
{
public function go(ReflectionFunction $reflectionFunction)
{
$parameterReflection = $reflectionFunction->getParameters()[0];
- $paramType = (string) $parameterReflection->getType();
+ $paramType = (string) ($parameterReflection->getType() ? $parameterReflection->getType()->getName() : null);
2019-05-01 23:56:58 +00:00
- $stringValue = 'hey' . $reflectionFunction->getReturnType();
+ $stringValue = 'hey' . ($reflectionFunction->getReturnType() ? $reflectionFunction->getReturnType()->getName() : null);
2019-05-01 23:56:58 +00:00
// keep
return $reflectionFunction->getReturnType();
}
}
```
2019-05-01 23:56:58 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `ClassConstantToSelfClassRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Php74\Rector\Class_\ClassConstantToSelfClassRector`](/rules/php74/src/Rector/Class_/ClassConstantToSelfClassRector.php)
- [test fixtures](/rules/php74/tests/Rector/Class_/ClassConstantToSelfClassRector/Fixture)
2019-05-01 23:56:58 +00:00
Change `__CLASS__` to self::class
2019-05-01 23:56:58 +00:00
```diff
class SomeClass
{
public function callOnMe()
{
- var_dump(__CLASS__);
+ var_dump(self::class);
}
}
2019-05-01 23:56:58 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
### `ClosureToArrowFunctionRector`
2019-05-01 23:56:58 +00:00
- class: [`Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector`](/rules/php74/src/Rector/Closure/ClosureToArrowFunctionRector.php)
- [test fixtures](/rules/php74/tests/Rector/Closure/ClosureToArrowFunctionRector/Fixture)
2019-05-01 23:56:58 +00:00
Change closure to arrow function
2019-05-01 23:56:58 +00:00
```diff
class SomeClass
{
public function run($meetups)
{
- return array_filter($meetups, function (Meetup $meetup) {
- return is_object($meetup);
- });
+ return array_filter($meetups, fn(Meetup $meetup) => is_object($meetup));
}
}
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `ExportToReflectionFunctionRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Php74\Rector\StaticCall\ExportToReflectionFunctionRector`](/rules/php74/src/Rector/StaticCall/ExportToReflectionFunctionRector.php)
- [test fixtures](/rules/php74/tests/Rector/StaticCall/ExportToReflectionFunctionRector/Fixture)
Change `export()` to ReflectionFunction alternatives
2019-05-01 23:56:58 +00:00
```diff
-$reflectionFunction = ReflectionFunction::export('foo');
-$reflectionFunctionAsString = ReflectionFunction::export('foo', true);
+$reflectionFunction = new ReflectionFunction('foo');
+$reflectionFunctionAsString = (string) new ReflectionFunction('foo');
2019-09-25 08:49:53 +00:00
```
2019-05-29 13:40:20 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `FilterVarToAddSlashesRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector`](/rules/php74/src/Rector/FuncCall/FilterVarToAddSlashesRector.php)
- [test fixtures](/rules/php74/tests/Rector/FuncCall/FilterVarToAddSlashesRector/Fixture)
2019-09-25 08:49:53 +00:00
Change `filter_var()` with slash escaping to `addslashes()`
2019-09-25 08:49:53 +00:00
```diff
$var= "Satya's here!";
-filter_var($var, FILTER_SANITIZE_MAGIC_QUOTES);
+addslashes($var);
2019-05-29 13:40:20 +00:00
```
2019-05-01 23:56:58 +00:00
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `GetCalledClassToStaticClassRector`
2019-08-05 21:10:47 +00:00
- class: [`Rector\Php74\Rector\FuncCall\GetCalledClassToStaticClassRector`](/rules/php74/src/Rector/FuncCall/GetCalledClassToStaticClassRector.php)
- [test fixtures](/rules/php74/tests/Rector/FuncCall/GetCalledClassToStaticClassRector/Fixture)
2019-08-05 21:10:47 +00:00
Change `get_called_class()` to static::class
2019-08-05 21:10:47 +00:00
```diff
class SomeClass
{
public function callOnMe()
{
- var_dump(get_called_class());
+ var_dump(static::class);
}
}
```
2019-08-05 21:10:47 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `MbStrrposEncodingArgumentPositionRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector`](/rules/php74/src/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php)
- [test fixtures](/rules/php74/tests/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector/Fixture)
2019-08-05 21:10:47 +00:00
Change `mb_strrpos()` encoding argument position
2019-08-05 21:10:47 +00:00
```diff
-mb_strrpos($text, "abc", "UTF-8");
+mb_strrpos($text, "abc", 0, "UTF-8");
2019-08-05 21:10:47 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `NullCoalescingOperatorRector`
- class: [`Rector\Php74\Rector\Assign\NullCoalescingOperatorRector`](/rules/php74/src/Rector/Assign/NullCoalescingOperatorRector.php)
- [test fixtures](/rules/php74/tests/Rector/Assign/NullCoalescingOperatorRector/Fixture)
Use null coalescing operator ??=
```diff
$array = [];
-$array['user_id'] = $array['user_id'] ?? 'value';
+$array['user_id'] ??= 'value';
```
<br><br>
2020-07-24 11:46:57 +00:00
### `RealToFloatTypeCastRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Php74\Rector\Double\RealToFloatTypeCastRector`](/rules/php74/src/Rector/Double/RealToFloatTypeCastRector.php)
- [test fixtures](/rules/php74/tests/Rector/Double/RealToFloatTypeCastRector/Fixture)
Change deprecated (real) to (float)
```diff
class SomeClass
{
public function run()
{
- $number = (real) 5;
+ $number = (float) 5;
$number = (float) 5;
$number = (double) 5;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `ReservedFnFunctionRector`
- class: [`Rector\Php74\Rector\Function_\ReservedFnFunctionRector`](/rules/php74/src/Rector/Function_/ReservedFnFunctionRector.php)
- [test fixtures](/rules/php74/tests/Rector/Function_/ReservedFnFunctionRector/Fixture)
Change `fn()` function name, since it will be reserved keyword
```php
<?php
declare(strict_types=1);
use Rector\Php74\Rector\Function_\ReservedFnFunctionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ReservedFnFunctionRector::class)
->call('configure', [[
ReservedFnFunctionRector::RESERVED_NAMES_TO_NEW_ONES => [
'fn' => 'someFunctionName',
],
]]);
};
```
```diff
class SomeClass
{
public function run()
{
- function fn($value)
+ function f($value)
{
return $value;
}
- fn(5);
+ f(5);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RestoreDefaultNullToNullableTypePropertyRector`
- class: [`Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector`](/rules/php74/src/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php)
- [test fixtures](/rules/php74/tests/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture)
Add null default to properties with PHP 7.4 property nullable type
```diff
class SomeClass
{
- public ?string $name;
+ public ?string $name = null;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `TypedPropertyRector`
- class: [`Rector\Php74\Rector\Property\TypedPropertyRector`](/rules/php74/src/Rector/Property/TypedPropertyRector.php)
- [test fixtures](/rules/php74/tests/Rector/Property/TypedPropertyRector/Fixture)
Changes property `@var` annotations from annotation to type.
```php
<?php
declare(strict_types=1);
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(TypedPropertyRector::class)
->call('configure', [[
TypedPropertyRector::CLASS_LIKE_TYPE_ONLY => false,
]]);
};
```
```diff
final class SomeClass
{
- /**
- * @var int
- */
- private count;
+ private int count;
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## Php80
### `AnnotationToAttributeRector`
- class: [`Rector\Php80\Rector\Class_\AnnotationToAttributeRector`](/rules/php80/src/Rector/Class_/AnnotationToAttributeRector.php)
- [test fixtures](/rules/php80/tests/Rector/Class_/AnnotationToAttributeRector/Fixture)
Change annotation to attribute
```diff
use Doctrine\ORM\Attributes as ORM;
-/**
- * @ORM\Entity
- */
2020-09-28 12:29:21 +00:00
+#[ORM\Entity]
class SomeClass
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ChangeSwitchToMatchRector`
- class: [`Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector`](/rules/php80/src/Rector/Switch_/ChangeSwitchToMatchRector.php)
- [test fixtures](/rules/php80/tests/Rector/Switch_/ChangeSwitchToMatchRector/Fixture)
Change `switch()` to `match()`
```diff
class SomeClass
{
public function run()
{
- $statement = switch ($this->lexer->lookahead['type']) {
- case Lexer::T_SELECT:
- $statement = $this->SelectStatement();
- break;
-
- case Lexer::T_UPDATE:
- $statement = $this->UpdateStatement();
- break;
-
- case Lexer::T_DELETE:
- $statement = $this->DeleteStatement();
- break;
-
- default:
- $this->syntaxError('SELECT, UPDATE or DELETE');
- break;
- }
+ $statement = match ($this->lexer->lookahead['type']) {
+ Lexer::T_SELECT => $this->SelectStatement(),
+ Lexer::T_UPDATE => $this->UpdateStatement(),
+ Lexer::T_DELETE => $this->DeleteStatement(),
+ default => $this->syntaxError('SELECT, UPDATE or DELETE'),
+ };
}
}
```
<br><br>
### `ClassOnObjectRector`
- class: [`Rector\Php80\Rector\FuncCall\ClassOnObjectRector`](/rules/php80/src/Rector/FuncCall/ClassOnObjectRector.php)
- [test fixtures](/rules/php80/tests/Rector/FuncCall/ClassOnObjectRector/Fixture)
Change get_class($object) to faster `$object::class`
```diff
class SomeClass
{
public function run($object)
{
- return get_class($object);
+ return $object::class;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ClassPropertyAssignToConstructorPromotionRector`
- class: [`Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector`](/rules/php80/src/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php)
- [test fixtures](/rules/php80/tests/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture)
Change simple property init and assign to constructor promotion
```diff
class SomeClass
{
- public float $x;
- public float $y;
- public float $z;
-
public function __construct(
- float $x = 0.0,
- float $y = 0.0,
- float $z = 0.0
- ) {
- $this->x = $x;
- $this->y = $y;
- $this->z = $z;
- }
+ public float $x = 0.0,
+ public float $y = 0.0,
+ public float $z = 0.0,
+ ) {}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `GetDebugTypeRector`
2020-01-06 15:06:07 +00:00
- class: [`Rector\Php80\Rector\Ternary\GetDebugTypeRector`](/rules/php80/src/Rector/Ternary/GetDebugTypeRector.php)
- [test fixtures](/rules/php80/tests/Rector/Ternary/GetDebugTypeRector/Fixture)
2020-01-06 15:06:07 +00:00
Change ternary type resolve to `get_debug_type()`
2020-01-06 15:06:07 +00:00
```diff
class SomeClass
{
public function run($value)
2020-01-06 15:06:07 +00:00
{
- return is_object($value) ? get_class($value) : gettype($value);
+ return get_debug_type($value);
2020-01-06 15:06:07 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-06 15:06:07 +00:00
### `RemoveUnusedVariableInCatchRector`
- class: [`Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector`](/rules/php80/src/Rector/Catch_/RemoveUnusedVariableInCatchRector.php)
- [test fixtures](/rules/php80/tests/Rector/Catch_/RemoveUnusedVariableInCatchRector/Fixture)
Remove unused variable in `catch()`
```diff
final class SomeClass
{
public function run()
{
try {
- } catch (Throwable $notUsedThrowable) {
+ } catch (Throwable) {
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `StrContainsRector`
2020-01-06 15:06:07 +00:00
- class: [`Rector\Php80\Rector\NotIdentical\StrContainsRector`](/rules/php80/src/Rector/NotIdentical/StrContainsRector.php)
- [test fixtures](/rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture)
2020-01-06 15:06:07 +00:00
Replace `strpos()` !== false and `strstr()` with `str_contains()`
2020-01-06 15:06:07 +00:00
```diff
class SomeClass
{
public function run()
{
- return strpos('abc', 'a') !== false;
+ return str_contains('abc', 'a');
2020-01-06 15:06:07 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-06 15:06:07 +00:00
### `StrEndsWithRector`
- class: [`Rector\Php80\Rector\Identical\StrEndsWithRector`](/rules/php80/src/Rector/Identical/StrEndsWithRector.php)
- [test fixtures](/rules/php80/tests/Rector/Identical/StrEndsWithRector/Fixture)
Change helper functions to `str_ends_with()`
```diff
class SomeClass
{
public function run()
{
- $isMatch = substr($haystack, -strlen($needle)) === $needle;
+ $isMatch = str_ends_with($haystack, $needle);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `StrStartsWithRector`
- class: [`Rector\Php80\Rector\Identical\StrStartsWithRector`](/rules/php80/src/Rector/Identical/StrStartsWithRector.php)
- [test fixtures](/rules/php80/tests/Rector/Identical/StrStartsWithRector/Fixture)
Change helper functions to `str_starts_with()`
```diff
class SomeClass
{
public function run()
{
- $isMatch = substr($haystack, 0, strlen($needle)) === $needle;
+ $isMatch = str_starts_with($haystack, $needle);
- $isNotMatch = substr($haystack, 0, strlen($needle)) !== $needle;
+ $isNotMatch = ! str_starts_with($haystack, $needle);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `StringableForToStringRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Php80\Rector\Class_\StringableForToStringRector`](/rules/php80/src/Rector/Class_/StringableForToStringRector.php)
- [test fixtures](/rules/php80/tests/Rector/Class_/StringableForToStringRector/Fixture)
2019-05-29 13:40:20 +00:00
Add `Stringable` interface to classes with `__toString()` method
2019-05-29 13:40:20 +00:00
```diff
-class SomeClass
+class SomeClass implements Stringable
{
- public function __toString()
+ public function __toString(): string
{
return 'I can stringz';
}
2019-05-01 23:56:58 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
### `TokenGetAllToObjectRector`
- class: [`Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector`](/rules/php80/src/Rector/FuncCall/TokenGetAllToObjectRector.php)
- [test fixtures](/rules/php80/tests/Rector/FuncCall/TokenGetAllToObjectRector/Fixture)
Complete missing constructor dependency instance by type
```diff
final class SomeClass
{
public function run()
{
- $tokens = token_get_all($code);
- foreach ($tokens as $token) {
- if (is_array($token)) {
- $name = token_name($token[0]);
- $text = $token[1];
- } else {
- $name = null;
- $text = $token;
- }
+ $tokens = \PhpToken::getAll($code);
+ foreach ($tokens as $phpToken) {
+ $name = $phpToken->getTokenName();
+ $text = $phpToken->text;
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `UnionTypesRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Php80\Rector\FunctionLike\UnionTypesRector`](/rules/php80/src/Rector/FunctionLike/UnionTypesRector.php)
- [test fixtures](/rules/php80/tests/Rector/FunctionLike/UnionTypesRector/Fixture)
Change docs types to union types, where possible (properties are covered by TypedPropertiesRector)
```diff
class SomeClass
{
/**
* @param array|int $number
* @return bool|float
*/
- public function go($number)
+ public function go(array|int $number): bool|float
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## PhpDeglobalize
2020-01-03 18:20:13 +00:00
### `ChangeGlobalVariablesToPropertiesRector`
2020-01-03 18:20:13 +00:00
- class: [`Rector\PhpDeglobalize\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector`](/rules/php-deglobalize/src/Rector/ClassMethod/ChangeGlobalVariablesToPropertiesRector.php)
- [test fixtures](/rules/php-deglobalize/tests/Rector/ClassMethod/ChangeGlobalVariablesToPropertiesRector/Fixture)
Change global `$variables` to private properties
2020-01-03 18:20:13 +00:00
```diff
class SomeClass
{
+ private $variable;
public function go()
2020-01-03 18:20:13 +00:00
{
- global $variable;
- $variable = 5;
+ $this->variable = 5;
}
public function run()
{
- global $variable;
- var_dump($variable);
+ var_dump($this->variable);
2020-01-03 18:20:13 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-01-03 18:20:13 +00:00
## PhpSpecToPHPUnit
### `AddMockPropertiesRector`
- class: [`Rector\PhpSpecToPHPUnit\Rector\Class_\AddMockPropertiesRector`](/rules/php-spec-to-phpunit/src/Rector/Class_/AddMockPropertiesRector.php)
Migrate PhpSpec behavior to PHPUnit test
```diff
-
namespace spec\SomeNamespaceForThisTest;
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod): void
+ /**
+ * @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);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MockVariableToPropertyFetchRector`
2019-10-15 14:46:31 +00:00
- class: [`Rector\PhpSpecToPHPUnit\Rector\Variable\MockVariableToPropertyFetchRector`](/rules/php-spec-to-phpunit/src/Rector/Variable/MockVariableToPropertyFetchRector.php)
2019-10-15 14:46:31 +00:00
Migrate PhpSpec behavior to PHPUnit test
2019-10-15 14:46:31 +00:00
```diff
-
namespace spec\SomeNamespaceForThisTest;
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
2019-10-15 14:46:31 +00:00
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod): void
+ /**
+ * @var \SomeNamespaceForThisTest\Order
+ */
+ private $order;
+ protected function setUp()
2019-10-15 14:46:31 +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-10-15 14:46:31 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-15 14:46:31 +00:00
### `PhpSpecClassToPHPUnitClassRector`
- class: [`Rector\PhpSpecToPHPUnit\Rector\Class_\PhpSpecClassToPHPUnitClassRector`](/rules/php-spec-to-phpunit/src/Rector/Class_/PhpSpecClassToPHPUnitClassRector.php)
Migrate PhpSpec behavior to PHPUnit test
```diff
-
namespace spec\SomeNamespaceForThisTest;
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod): void
+ /**
+ * @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);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `PhpSpecMethodToPHPUnitMethodRector`
2020-06-06 13:07:56 +00:00
- class: [`Rector\PhpSpecToPHPUnit\Rector\ClassMethod\PhpSpecMethodToPHPUnitMethodRector`](/rules/php-spec-to-phpunit/src/Rector/ClassMethod/PhpSpecMethodToPHPUnitMethodRector.php)
2020-06-06 13:07:56 +00:00
Migrate PhpSpec behavior to PHPUnit test
2020-06-06 13:07:56 +00:00
```diff
-
namespace spec\SomeNamespaceForThisTest;
2020-06-06 13:07:56 +00:00
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
2020-06-06 13:07:56 +00:00
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod): void
+ /**
+ * @var \SomeNamespaceForThisTest\Order
+ */
+ private $order;
+ protected function setUp()
2020-06-06 13:07:56 +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);
2020-06-06 13:07:56 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-06-06 13:07:56 +00:00
### `PhpSpecMocksToPHPUnitMocksRector`
2018-12-31 11:50:32 +00:00
- class: [`Rector\PhpSpecToPHPUnit\Rector\MethodCall\PhpSpecMocksToPHPUnitMocksRector`](/rules/php-spec-to-phpunit/src/Rector/MethodCall/PhpSpecMocksToPHPUnitMocksRector.php)
Migrate PhpSpec behavior to PHPUnit test
2018-12-31 11:50:32 +00:00
```diff
-
namespace spec\SomeNamespaceForThisTest;
2019-10-15 14:46:31 +00:00
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
2019-10-15 14:46:31 +00:00
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod): void
+ /**
+ * @var \SomeNamespaceForThisTest\Order
+ */
+ private $order;
+ protected function setUp()
2019-10-15 14:46:31 +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-10-15 14:46:31 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-15 14:46:31 +00:00
### `PhpSpecPromisesToPHPUnitAssertRector`
2018-10-21 22:26:45 +00:00
- class: [`Rector\PhpSpecToPHPUnit\Rector\MethodCall\PhpSpecPromisesToPHPUnitAssertRector`](/rules/php-spec-to-phpunit/src/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php)
2018-10-21 22:26:45 +00:00
Migrate PhpSpec behavior to PHPUnit test
2018-10-21 22:26:45 +00:00
```diff
-
namespace spec\SomeNamespaceForThisTest;
-use PhpSpec\ObjectBehavior;
-
class OrderSpec extends ObjectBehavior
{
- public function let(OrderFactory $factory, ShippingMethod $shippingMethod): void
+ /**
+ * @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);
}
}
2018-10-21 22:26:45 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameSpecFileToTestFileRector`
2018-08-01 20:09:34 +00:00
- class: [`Rector\PhpSpecToPHPUnit\Rector\FileSystem\RenameSpecFileToTestFileRector`](/rules/php-spec-to-phpunit/src/Rector/FileSystem/RenameSpecFileToTestFileRector.php)
2018-08-01 20:09:34 +00:00
Rename "*Spec.php" file to "*Test.php" file
2018-08-01 20:09:34 +00:00
```diff
-// tests/SomeSpec.php
+// tests/SomeTest.php
2018-08-01 20:09:34 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
## Polyfill
2018-09-28 16:33:35 +00:00
### `UnwrapFutureCompatibleIfFunctionExistsRector`
2018-09-28 16:33:35 +00:00
- class: [`Rector\Polyfill\Rector\If_\UnwrapFutureCompatibleIfFunctionExistsRector`](/rules/polyfill/src/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php)
- [test fixtures](/rules/polyfill/tests/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector/Fixture)
Remove functions exists if with else for always existing
2018-09-28 16:33:35 +00:00
```diff
class SomeClass
{
public function run()
{
// session locking trough other addons
- if (function_exists('session_abort')) {
- session_abort();
- } else {
- session_write_close();
- }
+ session_abort();
}
}
2018-09-28 16:33:35 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `UnwrapFutureCompatibleIfPhpVersionRector`
- class: [`Rector\Polyfill\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector`](/rules/polyfill/src/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php)
- [test fixtures](/rules/polyfill/tests/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector/Fixture)
Remove php version checks if they are passed
```diff
// current PHP: 7.2
-if (version_compare(PHP_VERSION, '7.2', '<')) {
- return 'is PHP 7.1-';
-} else {
- return 'is PHP 7.2+';
-}
+return 'is PHP 7.2+';
```
<br><br>
## Privatization
### `ChangeLocalPropertyToVariableRector`
- class: [`Rector\Privatization\Rector\Class_\ChangeLocalPropertyToVariableRector`](/rules/privatization/src/Rector/Class_/ChangeLocalPropertyToVariableRector.php)
- [test fixtures](/rules/privatization/tests/Rector/Class_/ChangeLocalPropertyToVariableRector/Fixture)
Change local property used in single method to local variable
```diff
class SomeClass
{
- private $count;
public function run()
{
- $this->count = 5;
- return $this->count;
+ $count = 5;
+ return $count;
}
}
```
<br><br>
### `PrivatizeFinalClassMethodRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector`](/rules/privatization/src/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php)
- [test fixtures](/rules/privatization/tests/Rector/ClassMethod/PrivatizeFinalClassMethodRector/Fixture)
Change protected class method to private if possible
```diff
final class SomeClass
{
- protected function someMethod()
+ private function someMethod()
{
}
}
```
<br><br>
### `PrivatizeFinalClassPropertyRector`
2019-05-01 23:56:58 +00:00
- class: [`Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector`](/rules/privatization/src/Rector/Property/PrivatizeFinalClassPropertyRector.php)
- [test fixtures](/rules/privatization/tests/Rector/Property/PrivatizeFinalClassPropertyRector/Fixture)
2019-05-01 23:56:58 +00:00
Change property to private if possible
2019-05-01 23:56:58 +00:00
2019-05-29 13:40:20 +00:00
```diff
final class SomeClass
{
- protected $value;
+ private $value;
}
2019-05-01 23:56:58 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-01 23:56:58 +00:00
### `PrivatizeLocalClassConstantRector`
2019-10-15 14:46:31 +00:00
- class: [`Rector\Privatization\Rector\ClassConst\PrivatizeLocalClassConstantRector`](/rules/privatization/src/Rector/ClassConst/PrivatizeLocalClassConstantRector.php)
- [test fixtures](/rules/privatization/tests/Rector/ClassConst/PrivatizeLocalClassConstantRector/Fixture)
2019-10-15 14:46:31 +00:00
Finalize every class constant that is used only locally
2019-10-15 14:46:31 +00:00
```diff
class ClassWithConstantUsedOnlyHere
2019-10-15 14:46:31 +00:00
{
- const LOCAL_ONLY = true;
+ private const LOCAL_ONLY = true;
public function isLocalOnly()
2019-10-15 14:46:31 +00:00
{
return self::LOCAL_ONLY;
2019-10-15 14:46:31 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-10-15 14:46:31 +00:00
### `PrivatizeLocalGetterToPropertyRector`
- class: [`Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector`](/rules/privatization/src/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector.php)
- [test fixtures](/rules/privatization/tests/Rector/MethodCall/PrivatizeLocalGetterToPropertyRector/Fixture)
Privatize getter of local property to property
```diff
class SomeClass
{
private $some;
public function run()
{
- return $this->getSome() + 5;
+ return $this->some + 5;
}
private function getSome()
{
return $this->some;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `PrivatizeLocalOnlyMethodRector`
- class: [`Rector\Privatization\Rector\ClassMethod\PrivatizeLocalOnlyMethodRector`](/rules/privatization/src/Rector/ClassMethod/PrivatizeLocalOnlyMethodRector.php)
- [test fixtures](/rules/privatization/tests/Rector/ClassMethod/PrivatizeLocalOnlyMethodRector/Fixture)
Privatize local-only use methods
2018-07-31 12:50:39 +00:00
```diff
class SomeClass
{
/**
* @api
*/
public function run()
{
return $this->useMe();
}
2020-05-02 09:51:01 +00:00
- public function useMe()
+ private function useMe()
{
}
}
2018-07-31 12:50:39 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `PrivatizeLocalPropertyToPrivatePropertyRector`
2019-03-31 12:25:39 +00:00
- class: [`Rector\Privatization\Rector\Property\PrivatizeLocalPropertyToPrivatePropertyRector`](/rules/privatization/src/Rector/Property/PrivatizeLocalPropertyToPrivatePropertyRector.php)
- [test fixtures](/rules/privatization/tests/Rector/Property/PrivatizeLocalPropertyToPrivatePropertyRector/Fixture)
2019-03-31 12:25:39 +00:00
Privatize local-only property to private property
2019-03-31 12:25:39 +00:00
```diff
class SomeClass
2019-03-31 12:25:39 +00:00
{
- public $value;
+ private $value;
public function run()
2019-03-31 12:25:39 +00:00
{
return $this->value;
2019-03-31 12:25:39 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-31 12:25:39 +00:00
## RectorGenerator
2018-05-04 22:30:32 +00:00
### `AddNewServiceToSymfonyPhpConfigRector`
- class: [`Rector\RectorGenerator\Rector\Closure\AddNewServiceToSymfonyPhpConfigRector`](/packages/rector-generator/src/Rector/Closure/AddNewServiceToSymfonyPhpConfigRector.php)
Adds a new `$services->set(...)` call to PHP Config
2018-05-04 22:30:32 +00:00
```diff
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
+ $services->set(AddNewServiceToSymfonyPhpConfigRector::class);
};
2018-05-04 22:30:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
## RemovingStatic
### `LocallyCalledStaticMethodToNonStaticRector`
- class: [`Rector\RemovingStatic\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector`](/rules/removing-static/src/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector.php)
- [test fixtures](/rules/removing-static/tests/Rector/ClassMethod/LocallyCalledStaticMethodToNonStaticRector/Fixture)
Change static method and local-only calls to non-static
```diff
class SomeClass
{
public function run()
{
- self::someStatic();
+ $this->someStatic();
}
- private static function someStatic()
+ private function someStatic()
{
}
}
```
<br><br>
### `NewUniqueObjectToEntityFactoryRector`
2018-05-04 22:30:32 +00:00
- class: [`Rector\RemovingStatic\Rector\Class_\NewUniqueObjectToEntityFactoryRector`](/rules/removing-static/src/Rector/Class_/NewUniqueObjectToEntityFactoryRector.php)
2018-05-04 22:30:32 +00:00
Convert new X to new factories
```php
<?php
declare(strict_types=1);
use Rector\RemovingStatic\Rector\Class_\NewUniqueObjectToEntityFactoryRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(NewUniqueObjectToEntityFactoryRector::class)
->call('configure', [[
NewUniqueObjectToEntityFactoryRector::TYPES_TO_SERVICES => ['ClassName'],
]]);
};
```
```diff
-<?php
-
class SomeClass
{
+ public function __construct(AnotherClassFactory $anotherClassFactory)
+ {
+ $this->anotherClassFactory = $anotherClassFactory;
+ }
+
public function run()
{
- return new AnotherClass;
+ return $this->anotherClassFactory->create();
}
}
2018-05-04 22:30:32 +00:00
class AnotherClass
{
public function someFun()
{
return StaticClass::staticMethod();
}
}
2018-05-04 22:30:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `PHPUnitStaticToKernelTestCaseGetRector`
2018-05-04 22:30:32 +00:00
- class: [`Rector\RemovingStatic\Rector\Class_\PHPUnitStaticToKernelTestCaseGetRector`](/rules/removing-static/src/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector.php)
- [test fixtures](/rules/removing-static/tests/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture)
2018-05-04 22:30:32 +00:00
Convert static calls in PHPUnit test cases, to `get()` from the container of KernelTestCase
2018-05-04 22:30:32 +00:00
```php
<?php
declare(strict_types=1);
use Rector\RemovingStatic\Rector\Class_\PHPUnitStaticToKernelTestCaseGetRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2018-10-23 18:58:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(PHPUnitStaticToKernelTestCaseGetRector::class)
->call('configure', [[
PHPUnitStaticToKernelTestCaseGetRector::STATIC_CLASS_TYPES => ['EntityFactory'],
]]);
};
```
2018-10-23 18:58:57 +00:00
2018-10-23 18:58:57 +00:00
```diff
-<?php
+use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
-use PHPUnit\Framework\TestCase;
+final class SomeTestCase extends KernelTestCase
+{
+ /**
+ * @var EntityFactory
+ */
+ private $entityFactory;
-final class SomeTestCase extends TestCase
-{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->entityFactory = self::$container->get(EntityFactory::class);
+ }
+
public function test()
{
- $product = EntityFactory::create('product');
+ $product = $this->entityFactory->create('product');
}
2019-05-29 13:40:20 +00:00
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-23 18:58:57 +00:00
### `PassFactoryToUniqueObjectRector`
2018-10-23 18:58:57 +00:00
- class: [`Rector\RemovingStatic\Rector\Class_\PassFactoryToUniqueObjectRector`](/rules/removing-static/src/Rector/Class_/PassFactoryToUniqueObjectRector.php)
2018-10-23 18:58:57 +00:00
Convert new `X/Static::call()` to factories in entities, pass them via constructor to `each` other
```php
<?php
declare(strict_types=1);
use Rector\RemovingStatic\Rector\Class_\PassFactoryToUniqueObjectRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(PassFactoryToUniqueObjectRector::class)
->call('configure', [[
PassFactoryToUniqueObjectRector::TYPES_TO_SERVICES => ['StaticClass'],
]]);
};
```
2018-10-23 18:58:57 +00:00
```diff
-<?php
-
class SomeClass
2019-05-29 13:40:20 +00:00
{
+ public function __construct(AnotherClassFactory $anotherClassFactory)
+ {
+ $this->anotherClassFactory = $anotherClassFactory;
+ }
+
public function run()
2019-05-29 13:40:20 +00:00
{
- return new AnotherClass;
+ return $this->anotherClassFactory->create();
2019-05-29 13:40:20 +00:00
}
}
class AnotherClass
{
+ public function __construct(StaticClass $staticClass)
+ {
+ $this->staticClass = $staticClass;
+ }
+
public function someFun()
2019-05-29 13:40:20 +00:00
{
- 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);
2019-05-29 13:40:20 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `SingleStaticServiceToDynamicRector`
- class: [`Rector\RemovingStatic\Rector\Class_\SingleStaticServiceToDynamicRector`](/rules/removing-static/src/Rector/Class_/SingleStaticServiceToDynamicRector.php)
- [test fixtures](/rules/removing-static/tests/Rector/Class_/SingleStaticServiceToDynamicRector/Fixture)
Change full static service, to dynamic one
```php
<?php
declare(strict_types=1);
use Rector\RemovingStatic\Rector\Class_\SingleStaticServiceToDynamicRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(SingleStaticServiceToDynamicRector::class)
->call('configure', [[
SingleStaticServiceToDynamicRector::CLASS_TYPES => ['SomeClass'],
]]);
};
```
```diff
class AnotherClass
{
+ /**
+ * @var SomeClass
+ */
+ private $someClass;
+
+ public fuction __construct(SomeClass $someClass)
+ {
+ $this->someClass = $someClass;
+ }
+
public function run()
{
SomeClass::someStatic();
}
}
class SomeClass
{
- public static function run()
+ public function run()
{
- self::someStatic();
+ $this->someStatic();
}
- private static function someStatic()
+ private function someStatic()
{
}
}
```
<br><br>
### `StaticTypeToSetterInjectionRector`
2019-06-06 13:01:53 +00:00
- class: [`Rector\RemovingStatic\Rector\Class_\StaticTypeToSetterInjectionRector`](/rules/removing-static/src/Rector/Class_/StaticTypeToSetterInjectionRector.php)
- [test fixtures](/rules/removing-static/tests/Rector/Class_/StaticTypeToSetterInjectionRector/Fixture)
2019-06-06 13:01:53 +00:00
Changes types to setter injection
```php
<?php
declare(strict_types=1);
use Rector\RemovingStatic\Rector\Class_\StaticTypeToSetterInjectionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(StaticTypeToSetterInjectionRector::class)
->call('configure', [[
StaticTypeToSetterInjectionRector::STATIC_TYPES => ['SomeStaticClass'],
]]);
};
```
2019-06-06 13:01:53 +00:00
```diff
<?php
2019-06-06 13:01:53 +00:00
final class CheckoutEntityFactory
2019-06-06 13:01:53 +00:00
{
+ /**
+ * @var SomeStaticClass
+ */
+ private $someStaticClass;
+
+ public function setSomeStaticClass(SomeStaticClass $someStaticClass)
+ {
+ $this->someStaticClass = $someStaticClass;
+ }
+
public function run()
2019-06-06 13:01:53 +00:00
{
- return SomeStaticClass::go();
+ return $this->someStaticClass->go();
2019-06-06 13:01:53 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-06-06 13:01:53 +00:00
## Renaming
### `PseudoNamespaceToNamespaceRector`
- class: [`Rector\Renaming\Rector\FileWithoutNamespace\PseudoNamespaceToNamespaceRector`](/rules/renaming/src/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector.php)
- [test fixtures](/rules/renaming/tests/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector/Fixture)
Replaces defined Pseudo_Namespaces by Namespace\Ones.
```php
<?php
declare(strict_types=1);
use Rector\Generic\ValueObject\PseudoNamespaceToNamespace;
use Rector\Renaming\Rector\FileWithoutNamespace\PseudoNamespaceToNamespaceRector;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(PseudoNamespaceToNamespaceRector::class)
->call('configure', [[
PseudoNamespaceToNamespaceRector::NAMESPACE_PREFIXES_WITH_EXCLUDED_CLASSES => inline_value_objects(
[new PseudoNamespaceToNamespace('Some_', ['Some_Class_To_Keep'])]
),
]]);
};
```
```diff
-/** @var Some_Chicken $someService */
-$someService = new Some_Chicken;
+/** @var Some\Chicken $someService */
+$someService = new Some\Chicken;
$someClassToKeep = new Some_Class_To_Keep;
```
<br><br>
### `RenameAnnotationRector`
- class: [`Rector\Renaming\Rector\ClassMethod\RenameAnnotationRector`](/rules/renaming/src/Rector/ClassMethod/RenameAnnotationRector.php)
- [test fixtures](/rules/renaming/tests/Rector/ClassMethod/RenameAnnotationRector/Fixture)
2019-05-29 13:40:20 +00:00
Turns defined annotations above properties and methods to their new values.
```php
<?php
2019-05-29 13:40:20 +00:00
declare(strict_types=1);
use Rector\Renaming\Rector\ClassMethod\RenameAnnotationRector;
use Rector\Renaming\ValueObject\RenameAnnotation;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-09-25 08:49:53 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RenameAnnotationRector::class)
->call('configure', [[
RenameAnnotationRector::RENAMED_ANNOTATIONS_IN_TYPES => inline_value_objects(
[new RenameAnnotation('PHPUnit\Framework\TestCase', 'test', 'scenario')]
),
]]);
};
```
2019-09-25 08:49:53 +00:00
2019-09-25 08:49:53 +00:00
```diff
class SomeTest extends PHPUnit\Framework\TestCase
2019-09-25 08:49:53 +00:00
{
/**
- * @test
+ * @scenario
2019-09-25 08:49:53 +00:00
*/
public function someMethod()
2019-09-25 08:49:53 +00:00
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `RenameClassConstantRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Renaming\Rector\ClassConstFetch\RenameClassConstantRector`](/rules/renaming/src/Rector/ClassConstFetch/RenameClassConstantRector.php)
- [test fixtures](/rules/renaming/tests/Rector/ClassConstFetch/RenameClassConstantRector/Fixture)
2019-05-29 13:40:20 +00:00
Replaces defined class constants in their calls.
2019-05-29 13:40:20 +00:00
```php
<?php
2019-05-29 13:40:20 +00:00
declare(strict_types=1);
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstantRector;
use Rector\Renaming\ValueObject\RenameClassConstant;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-05-29 13:40:20 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RenameClassConstantRector::class)
->call('configure', [[
RenameClassConstantRector::CLASS_CONSTANT_RENAME => inline_value_objects(
[new RenameClassConstant('SomeClass', 'OLD_CONSTANT', 'NEW_CONSTANT'), new RenameClassConstant(
'SomeClass',
'OTHER_OLD_CONSTANT',
'DifferentClass::NEW_CONSTANT'
)]
),
]]);
};
2019-05-29 13:40:20 +00:00
```
2019-05-29 13:40:20 +00:00
```diff
-$value = SomeClass::OLD_CONSTANT;
-$value = SomeClass::OTHER_OLD_CONSTANT;
+$value = SomeClass::NEW_CONSTANT;
+$value = DifferentClass::NEW_CONSTANT;
```
2018-10-23 18:58:57 +00:00
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameClassRector`
2018-05-04 22:30:32 +00:00
- class: [`Rector\Renaming\Rector\Name\RenameClassRector`](/rules/renaming/src/Rector/Name/RenameClassRector.php)
- [test fixtures](/rules/renaming/tests/Rector/Name/RenameClassRector/Fixture)
2018-07-31 12:50:39 +00:00
Replaces defined classes by new ones.
```php
<?php
declare(strict_types=1);
use Rector\Renaming\Rector\Name\RenameClassRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RenameClassRector::class)
->call('configure', [[
RenameClassRector::OLD_TO_NEW_CLASSES => [
'App\SomeOldClass' => 'App\SomeNewClass',
],
]]);
};
```
```diff
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;
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameConstantRector`
- class: [`Rector\Renaming\Rector\ConstFetch\RenameConstantRector`](/rules/renaming/src/Rector/ConstFetch/RenameConstantRector.php)
- [test fixtures](/rules/renaming/tests/Rector/ConstFetch/RenameConstantRector/Fixture)
Replace constant by new ones
```php
<?php
declare(strict_types=1);
use Rector\Renaming\Rector\ConstFetch\RenameConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RenameConstantRector::class)
->call('configure', [[
RenameConstantRector::OLD_TO_NEW_CONSTANTS => [
'MYSQL_ASSOC' => 'MYSQLI_ASSOC',
'OLD_CONSTANT' => 'NEW_CONSTANT',
],
]]);
};
```
```diff
final class SomeClass
{
public function run()
{
- return MYSQL_ASSOC;
+ return MYSQLI_ASSOC;
}
}
2018-05-04 22:30:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameFunctionRector`
- class: [`Rector\Renaming\Rector\FuncCall\RenameFunctionRector`](/rules/renaming/src/Rector/FuncCall/RenameFunctionRector.php)
- [test fixtures](/rules/renaming/tests/Rector/FuncCall/RenameFunctionRector/Fixture)
Turns defined function call new one.
```php
<?php
2019-05-29 13:40:20 +00:00
declare(strict_types=1);
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RenameFunctionRector::class)
->call('configure', [[
RenameFunctionRector::OLD_FUNCTION_TO_NEW_FUNCTION => [
'view' => 'Laravel\Templating\render',
],
]]);
};
```
```diff
-view("...", []);
+Laravel\Templating\render("...", []);
2019-05-29 13:40:20 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `RenameMethodRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Renaming\Rector\MethodCall\RenameMethodRector`](/rules/renaming/src/Rector/MethodCall/RenameMethodRector.php)
- [test fixtures](/rules/renaming/tests/Rector/MethodCall/RenameMethodRector/Fixture)
2019-05-29 13:40:20 +00:00
Turns method names to new ones.
2019-05-29 13:40:20 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RenameMethodRector::class)
->call('configure', [[
RenameMethodRector::METHOD_CALL_RENAMES => inline_value_objects(
[new MethodCallRename('SomeExampleClass', 'oldMethod', 'newMethod')]
),
]]);
};
```
```diff
$someObject = new SomeExampleClass;
-$someObject->oldMethod();
+$someObject->newMethod();
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenameNamespaceRector`
- class: [`Rector\Renaming\Rector\Namespace_\RenameNamespaceRector`](/rules/renaming/src/Rector/Namespace_/RenameNamespaceRector.php)
- [test fixtures](/rules/renaming/tests/Rector/Namespace_/RenameNamespaceRector/Fixture)
Replaces old namespace by new one.
```php
<?php
declare(strict_types=1);
use Rector\Renaming\Rector\Namespace_\RenameNamespaceRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RenameNamespaceRector::class)
->call('configure', [[
RenameNamespaceRector::OLD_TO_NEW_NAMESPACES => [
'SomeOldNamespace' => 'SomeNewNamespace',
],
]]);
};
```
```diff
-$someObject = new SomeOldNamespace\SomeClass;
+$someObject = new SomeNewNamespace\SomeClass;
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RenamePropertyRector`
- class: [`Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector`](/rules/renaming/src/Rector/PropertyFetch/RenamePropertyRector.php)
- [test fixtures](/rules/renaming/tests/Rector/PropertyFetch/RenamePropertyRector/Fixture)
Replaces defined old properties by new ones.
```php
<?php
declare(strict_types=1);
use Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector;
use Rector\Renaming\ValueObject\RenameProperty;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RenamePropertyRector::class)
->call('configure', [[
RenamePropertyRector::RENAMED_PROPERTIES => inline_value_objects(
[new RenameProperty('SomeClass', 'someOldProperty', 'someNewProperty')]
),
]]);
};
```
```diff
-$someObject->someOldProperty;
+$someObject->someNewProperty;
```
<br><br>
### `RenameStaticMethodRector`
- class: [`Rector\Renaming\Rector\StaticCall\RenameStaticMethodRector`](/rules/renaming/src/Rector/StaticCall/RenameStaticMethodRector.php)
- [test fixtures](/rules/renaming/tests/Rector/StaticCall/RenameStaticMethodRector/Fixture)
Turns method names to new ones.
```php
<?php
declare(strict_types=1);
use Rector\Renaming\Rector\StaticCall\RenameStaticMethodRector;
use Rector\Renaming\ValueObject\RenameStaticMethod;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RenameStaticMethodRector::class)
->call('configure', [[
RenameStaticMethodRector::OLD_TO_NEW_METHODS_BY_CLASSES => inline_value_objects(
[new RenameStaticMethod('SomeClass', 'oldMethod', 'AnotherExampleClass', 'newStaticMethod')]
),
]]);
};
```
```diff
-SomeClass::oldStaticMethod();
+AnotherExampleClass::newStaticMethod();
```
```php
<?php
declare(strict_types=1);
use Rector\Renaming\Rector\StaticCall\RenameStaticMethodRector;
use Rector\Renaming\ValueObject\RenameStaticMethod;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(RenameStaticMethodRector::class)
->call('configure', [[
RenameStaticMethodRector::OLD_TO_NEW_METHODS_BY_CLASSES => inline_value_objects(
[new RenameStaticMethod('SomeClass', 'oldMethod', 'SomeClass', 'newStaticMethod')]
),
]]);
};
```
```diff
-SomeClass::oldStaticMethod();
+SomeClass::newStaticMethod();
```
2020-06-16 16:14:51 +00:00
<br><br>
## Restoration
### `CompleteImportForPartialAnnotationRector`
- class: [`Rector\Restoration\Rector\Namespace_\CompleteImportForPartialAnnotationRector`](/rules/restoration/src/Rector/Namespace_/CompleteImportForPartialAnnotationRector.php)
- [test fixtures](/rules/restoration/tests/Rector/Namespace_/CompleteImportForPartialAnnotationRector/Fixture)
In case you have accidentally removed use imports but code still contains partial use statements, this will save you
```php
<?php
2019-08-05 21:10:47 +00:00
declare(strict_types=1);
use Rector\Restoration\Rector\Namespace_\CompleteImportForPartialAnnotationRector;
use Rector\Restoration\ValueObject\UseWithAlias;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2019-08-05 21:10:47 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(CompleteImportForPartialAnnotationRector::class)
->call('configure', [[
CompleteImportForPartialAnnotationRector::USE_IMPORTS_TO_RESTORE => inline_value_objects(
[new UseWithAlias('Doctrine\ORM\Mapping', 'ORM')]
),
]]);
};
```
2019-08-05 21:10:47 +00:00
2019-08-05 21:10:47 +00:00
```diff
+use Doctrine\ORM\Mapping as ORM;
2019-08-05 21:10:47 +00:00
+
class SomeClass
2019-08-05 21:10:47 +00:00
{
/**
* @ORM\Id
*/
public $id;
}
2019-08-05 21:10:47 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `CompleteMissingDependencyInNewRector`
- class: [`Rector\Restoration\Rector\New_\CompleteMissingDependencyInNewRector`](/rules/restoration/src/Rector/New_/CompleteMissingDependencyInNewRector.php)
- [test fixtures](/rules/restoration/tests/Rector/New_/CompleteMissingDependencyInNewRector/Fixture)
Complete missing constructor dependency instance by type
```php
<?php
2019-09-21 22:14:49 +00:00
declare(strict_types=1);
use Rector\Restoration\Rector\New_\CompleteMissingDependencyInNewRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(CompleteMissingDependencyInNewRector::class)
->call('configure', [[
CompleteMissingDependencyInNewRector::CLASS_TO_INSTANTIATE_BY_TYPE => [
'RandomDependency' => 'RandomDependency',
],
]]);
};
```
```diff
final class SomeClass
{
public function run()
{
- $valueObject = new RandomValueObject();
+ $valueObject = new RandomValueObject(new RandomDependency());
}
}
class RandomValueObject
{
public function __construct(RandomDependency $randomDependency)
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MakeTypedPropertyNullableIfCheckedRector`
- class: [`Rector\Restoration\Rector\Property\MakeTypedPropertyNullableIfCheckedRector`](/rules/restoration/src/Rector/Property/MakeTypedPropertyNullableIfCheckedRector.php)
- [test fixtures](/rules/restoration/tests/Rector/Property/MakeTypedPropertyNullableIfCheckedRector/Fixture)
Make typed property nullable if checked
```diff
final class SomeClass
{
- private AnotherClass $anotherClass;
+ private ?AnotherClass $anotherClass = null;
public function run()
{
if ($this->anotherClass === null) {
$this->anotherClass = new AnotherClass;
}
}
}
```
<br><br>
### `MissingClassConstantReferenceToStringRector`
2018-09-28 16:33:35 +00:00
- class: [`Rector\Restoration\Rector\ClassConstFetch\MissingClassConstantReferenceToStringRector`](/rules/restoration/src/Rector/ClassConstFetch/MissingClassConstantReferenceToStringRector.php)
- [test fixtures](/rules/restoration/tests/Rector/ClassConstFetch/MissingClassConstantReferenceToStringRector/Fixture)
2018-09-28 16:33:35 +00:00
Convert missing class reference to string
2018-09-28 16:33:35 +00:00
```diff
class SomeClass
2018-09-28 16:33:35 +00:00
{
public function run()
2018-09-28 16:33:35 +00:00
{
- return NonExistingClass::class;
+ return 'NonExistingClass';
2018-09-28 16:33:35 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RemoveFinalFromEntityRector`
2019-08-17 13:06:02 +00:00
- class: [`Rector\Restoration\Rector\Class_\RemoveFinalFromEntityRector`](/rules/restoration/src/Rector/Class_/RemoveFinalFromEntityRector.php)
- [test fixtures](/rules/restoration/tests/Rector/Class_/RemoveFinalFromEntityRector/Fixture)
2019-08-17 13:06:02 +00:00
Remove final from Doctrine entities
2019-08-17 13:06:02 +00:00
```diff
use Doctrine\ORM\Mapping as ORM;
2019-08-17 13:06:02 +00:00
/**
* @ORM\Entity
*/
-final class SomeClass
+class SomeClass
{
2019-08-17 13:06:02 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-17 13:06:02 +00:00
### `RemoveUselessJustForSakeInterfaceRector`
2019-08-17 13:06:02 +00:00
- class: [`Rector\Restoration\Rector\Class_\RemoveUselessJustForSakeInterfaceRector`](/rules/restoration/src/Rector/Class_/RemoveUselessJustForSakeInterfaceRector.php)
- [test fixtures](/rules/restoration/tests/Rector/Class_/RemoveUselessJustForSakeInterfaceRector/Fixture)
2019-08-17 13:06:02 +00:00
Remove interface, that are added just for its sake, but nowhere useful
2019-08-17 13:06:02 +00:00
```diff
-class SomeClass implements OnlyHereUsedInterface
+class SomeClass
2019-08-17 13:06:02 +00:00
{
}
2019-08-17 13:06:02 +00:00
-interface OnlyHereUsedInterface
-{
-}
-
class SomePresenter
{
- public function __construct(OnlyHereUsedInterface $onlyHereUsed)
+ public function __construct(SomeClass $onlyHereUsed)
2019-08-17 13:06:02 +00:00
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-17 13:06:02 +00:00
### `RestoreFullyQualifiedNameRector`
- class: [`Rector\Restoration\Rector\Use_\RestoreFullyQualifiedNameRector`](/rules/restoration/src/Rector/Use_/RestoreFullyQualifiedNameRector.php)
- [test fixtures](/rules/restoration/tests/Rector/Use_/RestoreFullyQualifiedNameRector/Fixture)
Restore accidentally shortened class names to its fully qualified form.
```diff
-use ShortClassOnly;
+use App\Whatever\ShortClassOnly;
class AnotherClass
{
}
```
<br><br>
### `UpdateFileNameByClassNameFileSystemRector`
- class: [`Rector\Restoration\Rector\ClassLike\UpdateFileNameByClassNameFileSystemRector`](/rules/restoration/src/Rector/ClassLike/UpdateFileNameByClassNameFileSystemRector.php)
- [test fixtures](/rules/restoration/tests/Rector/ClassLike/UpdateFileNameByClassNameFileSystemRector/Fixture)
Rename file to respect class name
```diff
-// app/SomeClass.php
+// app/AnotherClass.php
class AnotherClass
{
}
```
<br><br>
## SOLID
2019-05-19 08:27:38 +00:00
### `AddFalseDefaultToBoolPropertyRector`
2019-05-19 08:27:38 +00:00
- class: [`Rector\SOLID\Rector\Property\AddFalseDefaultToBoolPropertyRector`](/rules/solid/src/Rector/Property/AddFalseDefaultToBoolPropertyRector.php)
- [test fixtures](/rules/solid/tests/Rector/Property/AddFalseDefaultToBoolPropertyRector/Fixture)
Add false default to bool properties, to prevent null compare errors
2019-05-19 08:27:38 +00:00
```diff
class SomeClass
{
/**
* @var bool
*/
- private $isDisabled;
+ private $isDisabled = false;
2019-09-25 08:49:53 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `ChangeAndIfToEarlyReturnRector`
- class: [`Rector\SOLID\Rector\If_\ChangeAndIfToEarlyReturnRector`](/rules/solid/src/Rector/If_/ChangeAndIfToEarlyReturnRector.php)
- [test fixtures](/rules/solid/tests/Rector/If_/ChangeAndIfToEarlyReturnRector/Fixture)
Changes if && to early return
```diff
class SomeClass
{
public function canDrive(Car $car)
{
- if ($car->hasWheels && $car->hasFuel) {
- return true;
+ if (!$car->hasWheels) {
+ return false;
}
- return false;
+ if (!$car->hasFuel) {
+ return false;
+ }
+
+ return true;
}
}
```
<br><br>
### `ChangeIfElseValueAssignToEarlyReturnRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\SOLID\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector`](/rules/solid/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php)
- [test fixtures](/rules/solid/tests/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector/Fixture)
2019-09-25 08:49:53 +00:00
Change if/else value to early return
2019-09-25 08:49:53 +00:00
```diff
class SomeClass
{
public function run()
{
if ($this->hasDocBlock($tokens, $index)) {
- $docToken = $tokens[$this->getDocBlockIndex($tokens, $index)];
- } else {
- $docToken = null;
+ return $tokens[$this->getDocBlockIndex($tokens, $index)];
}
-
- return $docToken;
+ return null;
2019-09-25 08:49:53 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `ChangeNestedForeachIfsToEarlyContinueRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\SOLID\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector`](/rules/solid/src/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php)
- [test fixtures](/rules/solid/tests/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector/Fixture)
2019-12-18 09:53:46 +00:00
Change nested ifs to foreach with continue
2019-12-18 09:53:46 +00:00
```diff
class SomeClass
{
public function run()
2019-12-18 09:53:46 +00:00
{
$items = [];
foreach ($values as $value) {
- if ($value === 5) {
- if ($value2 === 10) {
- $items[] = 'maybe';
- }
+ if ($value !== 5) {
+ continue;
}
+ if ($value2 !== 10) {
+ continue;
+ }
+
+ $items[] = 'maybe';
}
2019-12-18 09:53:46 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-18 09:53:46 +00:00
### `ChangeNestedIfsToEarlyReturnRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\SOLID\Rector\If_\ChangeNestedIfsToEarlyReturnRector`](/rules/solid/src/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php)
- [test fixtures](/rules/solid/tests/Rector/If_/ChangeNestedIfsToEarlyReturnRector/Fixture)
2019-09-25 08:49:53 +00:00
Change nested ifs to early return
2019-09-25 08:49:53 +00:00
```diff
class SomeClass
2019-09-25 08:49:53 +00:00
{
public function run()
2019-09-25 08:49:53 +00:00
{
- if ($value === 5) {
- if ($value2 === 10) {
- return 'yes';
- }
+ if ($value !== 5) {
+ return 'no';
+ }
+
+ if ($value2 === 10) {
+ return 'yes';
}
return 'no';
2019-05-19 08:27:38 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-19 08:27:38 +00:00
### `ChangeReadOnlyPropertyWithDefaultValueToConstantRector`
2019-05-19 08:27:38 +00:00
- class: [`Rector\SOLID\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector`](/rules/solid/src/Rector/Property/ChangeReadOnlyPropertyWithDefaultValueToConstantRector.php)
- [test fixtures](/rules/solid/tests/Rector/Property/ChangeReadOnlyPropertyWithDefaultValueToConstantRector/Fixture)
2019-05-19 08:27:38 +00:00
Change property with read only status with default value to constant
2019-05-19 08:27:38 +00:00
```diff
class SomeClass
2019-05-19 08:27:38 +00:00
{
/**
* @var string[]
2019-05-19 08:27:38 +00:00
*/
- private $magicMethods = [
+ private const MAGIC_METHODS = [
'__toString',
'__wakeup',
];
2019-05-19 08:27:38 +00:00
public function run()
2019-05-19 08:27:38 +00:00
{
- foreach ($this->magicMethods as $magicMethod) {
+ foreach (self::MAGIC_METHODS as $magicMethod) {
echo $magicMethod;
}
2019-05-19 08:27:38 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-19 08:27:38 +00:00
### `ChangeReadOnlyVariableWithDefaultValueToConstantRector`
2019-08-05 21:10:47 +00:00
- class: [`Rector\SOLID\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector`](/rules/solid/src/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php)
- [test fixtures](/rules/solid/tests/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector/Fixture)
2019-08-05 21:10:47 +00:00
Change variable with read only status with default value to constant
2019-08-05 21:10:47 +00:00
```diff
class SomeClass
{
+ /**
+ * @var string[]
+ */
+ private const REPLACEMENTS = [
+ 'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
+ 'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
+ ];
+
public function run()
{
- $replacements = [
- 'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
- 'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
- ];
-
- foreach ($replacements as $class => $method) {
+ foreach (self::REPLACEMENTS as $class => $method) {
}
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `FinalizeClassesWithoutChildrenRector`
2018-05-04 22:30:32 +00:00
- class: [`Rector\SOLID\Rector\Class_\FinalizeClassesWithoutChildrenRector`](/rules/solid/src/Rector/Class_/FinalizeClassesWithoutChildrenRector.php)
- [test fixtures](/rules/solid/tests/Rector/Class_/FinalizeClassesWithoutChildrenRector/Fixture)
2018-08-01 20:09:34 +00:00
Finalize every class that has no children
2018-05-04 22:30:32 +00:00
```diff
-class FirstClass
+final class FirstClass
{
}
class SecondClass
{
}
-class ThirdClass extends SecondClass
+final class ThirdClass extends SecondClass
2019-05-29 13:40:20 +00:00
{
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MakeUnusedClassesWithChildrenAbstractRector`
- class: [`Rector\SOLID\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector`](/rules/solid/src/Rector/Class_/MakeUnusedClassesWithChildrenAbstractRector.php)
- [test fixtures](/rules/solid/tests/Rector/Class_/MakeUnusedClassesWithChildrenAbstractRector/Fixture)
Classes that have no children nor are used, should have abstract
```diff
class SomeClass extends PossibleAbstractClass
{
}
-class PossibleAbstractClass
+abstract class PossibleAbstractClass
2018-08-01 20:09:34 +00:00
{
}
2018-05-04 22:30:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MultiParentingToAbstractDependencyRector`
2019-12-19 22:07:06 +00:00
- class: [`Rector\SOLID\Rector\Class_\MultiParentingToAbstractDependencyRector`](/rules/solid/src/Rector/Class_/MultiParentingToAbstractDependencyRector.php)
- [test fixtures](/rules/solid/tests/Rector/Class_/MultiParentingToAbstractDependencyRector/Fixture)
2019-12-19 22:07:06 +00:00
Move dependency passed to all children to parent as @inject/@required dependency
2019-12-19 22:07:06 +00:00
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use Rector\SOLID\Rector\Class_\MultiParentingToAbstractDependencyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(MultiParentingToAbstractDependencyRector::class)
->call('configure', [[
MultiParentingToAbstractDependencyRector::FRAMEWORK => 'nette',
]]);
2020-07-24 11:46:57 +00:00
};
2019-12-19 22:07:06 +00:00
```
```diff
abstract class AbstractParentClass
2019-12-19 22:07:06 +00:00
{
- private $someDependency;
-
- public function __construct(SomeDependency $someDependency)
- {
- $this->someDependency = $someDependency;
- }
+ /**
+ * @inject
+ * @var SomeDependency
+ */
+ public $someDependency;
}
class FirstChild extends AbstractParentClass
{
- public function __construct(SomeDependency $someDependency)
- {
- parent::__construct($someDependency);
- }
}
class SecondChild extends AbstractParentClass
{
- public function __construct(SomeDependency $someDependency)
- {
- parent::__construct($someDependency);
- }
2019-12-19 22:07:06 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-19 22:07:06 +00:00
### `RemoveAlwaysElseRector`
2019-06-06 13:01:53 +00:00
- class: [`Rector\SOLID\Rector\If_\RemoveAlwaysElseRector`](/rules/solid/src/Rector/If_/RemoveAlwaysElseRector.php)
- [test fixtures](/rules/solid/tests/Rector/If_/RemoveAlwaysElseRector/Fixture)
2019-06-06 13:01:53 +00:00
Split if statement, when if condition always break execution flow
2019-06-06 13:01:53 +00:00
```diff
class SomeClass
2019-06-06 13:01:53 +00:00
{
public function run($value)
2019-06-06 13:01:53 +00:00
{
if ($value) {
throw new \InvalidStateException;
- } else {
- return 10;
}
2019-06-06 13:01:53 +00:00
+
+ return 10;
2019-06-06 13:01:53 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-06-06 13:01:53 +00:00
### `RepeatedLiteralToClassConstantRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\SOLID\Rector\Class_\RepeatedLiteralToClassConstantRector`](/rules/solid/src/Rector/Class_/RepeatedLiteralToClassConstantRector.php)
- [test fixtures](/rules/solid/tests/Rector/Class_/RepeatedLiteralToClassConstantRector/Fixture)
2018-05-04 22:30:32 +00:00
Replace repeated strings with constant
2018-08-01 20:09:34 +00:00
```diff
class SomeClass
2019-05-29 13:40:20 +00:00
{
+ /**
+ * @var string
+ */
+ private const REQUIRES = 'requires';
public function run($key, $items)
2019-12-26 10:21:09 +00:00
{
- if ($key === 'requires') {
- return $items['requires'];
+ if ($key === self::REQUIRES) {
+ return $items[self::REQUIRES];
}
2019-12-26 10:21:09 +00:00
}
2019-05-29 13:40:20 +00:00
}
2018-08-01 20:09:34 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `UseInterfaceOverImplementationInConstructorRector`
- class: [`Rector\SOLID\Rector\ClassMethod\UseInterfaceOverImplementationInConstructorRector`](/rules/solid/src/Rector/ClassMethod/UseInterfaceOverImplementationInConstructorRector.php)
- [test fixtures](/rules/solid/tests/Rector/ClassMethod/UseInterfaceOverImplementationInConstructorRector/Fixture)
Use interface instead of specific class
2018-05-04 22:30:32 +00:00
```diff
class SomeClass
{
- public function __construct(SomeImplementation $someImplementation)
+ public function __construct(SomeInterface $someImplementation)
{
}
}
class SomeImplementation implements SomeInterface
{
}
interface SomeInterface
{
}
```
<br><br>
## Sensio
2020-07-24 11:46:57 +00:00
### `RemoveServiceFromSensioRouteRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Sensio\Rector\ClassMethod\RemoveServiceFromSensioRouteRector`](/rules/sensio/src/Rector/ClassMethod/RemoveServiceFromSensioRouteRector.php)
- [test fixtures](/rules/sensio/tests/Rector/ClassMethod/RemoveServiceFromSensioRouteRector/Fixture)
2018-05-04 22:30:32 +00:00
Remove service from Sensio @Route
2018-05-04 22:30:32 +00:00
```diff
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
final class SomeClass
{
/**
- * @Route(service="some_service")
+ * @Route()
*/
public function run()
{
}
}
2018-05-04 22:30:32 +00:00
```
<br><br>
2020-07-24 11:46:57 +00:00
### `ReplaceSensioRouteAnnotationWithSymfonyRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Sensio\Rector\ClassMethod\ReplaceSensioRouteAnnotationWithSymfonyRector`](/rules/sensio/src/Rector/ClassMethod/ReplaceSensioRouteAnnotationWithSymfonyRector.php)
- [test fixtures](/rules/sensio/tests/Rector/ClassMethod/ReplaceSensioRouteAnnotationWithSymfonyRector/Fixture)
2018-08-01 20:09:34 +00:00
Replace Sensio @Route annotation with Symfony one
2018-05-04 22:30:32 +00:00
```diff
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Symfony\Component\Routing\Annotation\Route;
final class SomeClass
2019-05-29 13:40:20 +00:00
{
/**
* @Route()
*/
public function run()
2019-05-29 13:40:20 +00:00
{
}
}
2018-05-04 22:30:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `TemplateAnnotationToThisRenderRector`
2019-03-16 20:31:46 +00:00
- class: [`Rector\Sensio\Rector\ClassMethod\TemplateAnnotationToThisRenderRector`](/rules/sensio/src/Rector/ClassMethod/TemplateAnnotationToThisRenderRector.php)
- [test fixtures](/rules/sensio/tests/Rector/ClassMethod/TemplateAnnotationToThisRenderRector/Fixture)
2019-03-16 20:31:46 +00:00
Turns `@Template` annotation to explicit method call in Controller of FrameworkExtraBundle in Symfony
2019-03-16 20:31:46 +00:00
```diff
-/**
- * @Template()
- */
public function indexAction()
{
+ return $this->render('index.html.twig');
}
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
## StrictCodeQuality
2019-03-16 20:31:46 +00:00
### `VarInlineAnnotationToAssertRector`
- class: [`Rector\StrictCodeQuality\Rector\Stmt\VarInlineAnnotationToAssertRector`](/rules/strict-code-quality/src/Rector/Stmt/VarInlineAnnotationToAssertRector.php)
- [test fixtures](/rules/strict-code-quality/tests/Rector/Stmt/VarInlineAnnotationToAssertRector/Fixture)
Turn @var inline checks above code to `assert()` of the type
2019-03-16 20:31:46 +00:00
```diff
class SomeClass
{
public function run()
{
/** @var SpecificClass $value */
+ assert($value instanceof SpecificClass);
$value->call();
}
}
2019-03-16 20:31:46 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-03-16 20:31:46 +00:00
## Symfony
2020-07-24 11:46:57 +00:00
### `ActionSuffixRemoverRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\ClassMethod\ActionSuffixRemoverRector`](/rules/symfony/src/Rector/ClassMethod/ActionSuffixRemoverRector.php)
- [test fixtures](/rules/symfony/tests/Rector/ClassMethod/ActionSuffixRemoverRector/Fixture)
2018-08-01 20:09:34 +00:00
Removes Action suffixes from methods in Symfony Controllers
2018-05-04 22:30:32 +00:00
```diff
class SomeController
{
- public function indexAction()
+ public function index()
{
}
}
2018-05-04 22:30:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AddFlashRector`
- class: [`Rector\Symfony\Rector\MethodCall\AddFlashRector`](/rules/symfony/src/Rector/MethodCall/AddFlashRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/AddFlashRector/Fixture)
Turns long flash adding to short helper method in Controller in Symfony
```diff
class SomeController extends Controller
{
public function some(Request $request)
{
- $request->getSession()->getFlashBag()->add("success", "something");
+ $this->addFlash("success", "something");
}
}
```
<br><br>
### `AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector`
- class: [`Rector\Symfony\Rector\ClassMethod\AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector`](/rules/symfony/src/Rector/ClassMethod/AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector.php)
- [test fixtures](/rules/symfony/tests/Rector/ClassMethod/AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector/Fixture)
Use autowire + class name suffix for method with @required annotation
```diff
class SomeClass
{
/** @required */
- public function foo()
+ public function autowireSomeClass()
{
}
}
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `CascadeValidationFormBuilderRector`
- class: [`Rector\Symfony\Rector\MethodCall\CascadeValidationFormBuilderRector`](/rules/symfony/src/Rector/MethodCall/CascadeValidationFormBuilderRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/CascadeValidationFormBuilderRector/Fixture)
Change "cascade_validation" option to specific node attribute
```diff
class SomeController
2018-10-23 18:58:57 +00:00
{
public function someMethod()
{
- $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-05-04 22:30:32 +00:00
protected function createFormBuilder()
{
return new FormBuilder();
}
2018-10-21 22:26:45 +00:00
}
2018-10-12 23:15:00 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ChangeCollectionTypeOptionNameFromTypeToEntryTypeRector`
- class: [`Rector\Symfony\Rector\MethodCall\ChangeCollectionTypeOptionNameFromTypeToEntryTypeRector`](/rules/symfony/src/Rector/MethodCall/ChangeCollectionTypeOptionNameFromTypeToEntryTypeRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/ChangeCollectionTypeOptionNameFromTypeToEntryTypeRector/Fixture)
Rename `type` option to `entry_type` in CollectionType
2020-07-24 11:46:57 +00:00
```diff
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
2020-07-24 11:46:57 +00:00
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('tags', CollectionType::class, [
- 'type' => ChoiceType::class,
- 'options' => [1, 2, 3],
+ 'entry_type' => ChoiceType::class,
+ 'entry_options' => [1, 2, 3],
]);
}
}
```
<br><br>
### `ChangeCollectionTypeOptionTypeFromStringToClassReferenceRector`
- class: [`Rector\Symfony\Rector\MethodCall\ChangeCollectionTypeOptionTypeFromStringToClassReferenceRector`](/rules/symfony/src/Rector/MethodCall/ChangeCollectionTypeOptionTypeFromStringToClassReferenceRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/ChangeCollectionTypeOptionTypeFromStringToClassReferenceRector/Fixture)
Change type in CollectionType from alias string to class reference
```diff
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('tags', CollectionType::class, [
- 'type' => 'choice',
+ 'type' => \Symfony\Component\Form\Extension\Core\Type\ChoiceType::class,
]);
$builder->add('tags', 'collection', [
- 'type' => 'choice',
+ 'type' => \Symfony\Component\Form\Extension\Core\Type\ChoiceType::class,
]);
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ChangeFileLoaderInExtensionAndKernelRector`
- class: [`Rector\Symfony\Rector\Class_\ChangeFileLoaderInExtensionAndKernelRector`](/rules/symfony/src/Rector/Class_/ChangeFileLoaderInExtensionAndKernelRector.php)
- [test fixtures](/rules/symfony/tests/Rector/Class_/ChangeFileLoaderInExtensionAndKernelRector/Fixture)
2018-07-31 12:50:39 +00:00
Change XML loader to YAML in Bundle Extension
2018-08-01 20:09:34 +00:00
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Symfony\Rector\Class_\ChangeFileLoaderInExtensionAndKernelRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(ChangeFileLoaderInExtensionAndKernelRector::class)
->call('configure', [[
ChangeFileLoaderInExtensionAndKernelRector::FROM => 'xml',
ChangeFileLoaderInExtensionAndKernelRector::TO => 'yaml',
]]);
2020-07-24 11:46:57 +00:00
};
2018-08-01 20:09:34 +00:00
```
2018-07-31 12:50:39 +00:00
```diff
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
+use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
final class SomeExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
- $loader = new XmlFileLoader($container, new FileLocator());
- $loader->load(__DIR__ . '/../Resources/config/controller.xml');
- $loader->load(__DIR__ . '/../Resources/config/events.xml');
+ $loader = new YamlFileLoader($container, new FileLocator());
+ $loader->load(__DIR__ . '/../Resources/config/controller.yaml');
+ $loader->load(__DIR__ . '/../Resources/config/events.yaml');
}
}
2018-07-31 12:50:39 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ConsoleExceptionToErrorEventConstantRector`
2018-07-31 12:50:39 +00:00
- class: [`Rector\Symfony\Rector\ClassConstFetch\ConsoleExceptionToErrorEventConstantRector`](/rules/symfony/src/Rector/ClassConstFetch/ConsoleExceptionToErrorEventConstantRector.php)
- [test fixtures](/rules/symfony/tests/Rector/ClassConstFetch/ConsoleExceptionToErrorEventConstantRector/Fixture)
Turns old event name with EXCEPTION to ERROR constant in Console in Symfony
2020-07-24 11:46:57 +00:00
```diff
-"console.exception"
+Symfony\Component\Console\ConsoleEvents::ERROR
```
```diff
-Symfony\Component\Console\ConsoleEvents::EXCEPTION
+Symfony\Component\Console\ConsoleEvents::ERROR
2018-08-01 20:09:34 +00:00
```
<br><br>
2018-07-31 12:50:39 +00:00
### `ConsoleExecuteReturnIntRector`
- class: [`Rector\Symfony\Rector\ClassMethod\ConsoleExecuteReturnIntRector`](/rules/symfony/src/Rector/ClassMethod/ConsoleExecuteReturnIntRector.php)
- [test fixtures](/rules/symfony/tests/Rector/ClassMethod/ConsoleExecuteReturnIntRector/Fixture)
Returns int from Command::execute command
```diff
class SomeCommand extends Command
{
- public function execute(InputInterface $input, OutputInterface $output)
+ public function execute(InputInterface $input, OutputInterface $output): int
{
- return null;
+ return 0;
}
}
2018-08-01 20:09:34 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `ConstraintUrlOptionRector`
- class: [`Rector\Symfony\Rector\ConstFetch\ConstraintUrlOptionRector`](/rules/symfony/src/Rector/ConstFetch/ConstraintUrlOptionRector.php)
- [test fixtures](/rules/symfony/tests/Rector/ConstFetch/ConstraintUrlOptionRector/Fixture)
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]);
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `ContainerBuilderCompileEnvArgumentRector`
- class: [`Rector\Symfony\Rector\MethodCall\ContainerBuilderCompileEnvArgumentRector`](/rules/symfony/src/Rector/MethodCall/ContainerBuilderCompileEnvArgumentRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/ContainerBuilderCompileEnvArgumentRector/Fixture)
Turns old default value to parameter in `ContainerBuilder->build()` method in DI in Symfony
```diff
use Symfony\Component\DependencyInjection\ContainerBuilder;
$containerBuilder = new ContainerBuilder();
-$containerBuilder->compile();
+$containerBuilder->compile(true);
2018-08-01 20:09:34 +00:00
```
<br><br>
### `ContainerGetToConstructorInjectionRector`
- class: [`Rector\Symfony\Rector\MethodCall\ContainerGetToConstructorInjectionRector`](/rules/symfony/src/Rector/MethodCall/ContainerGetToConstructorInjectionRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/ContainerGetToConstructorInjectionRector/Fixture)
Turns fetching of dependencies via `$container->get()` in ContainerAware to constructor injection in Command and Controller in Symfony
```php
<?php
declare(strict_types=1);
use Rector\Symfony\Rector\MethodCall\ContainerGetToConstructorInjectionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ContainerGetToConstructorInjectionRector::class)
->call('configure', [[
ContainerGetToConstructorInjectionRector::CONTAINER_AWARE_PARENT_TYPES => [
'ContainerAwareParentClassName',
'ContainerAwareParentCommandClassName',
'ThisClassCallsMethodInConstructorClassName',
],
]]);
};
```
2018-08-01 20:09:34 +00:00
```diff
-final class SomeCommand extends ContainerAwareCommand
+final class SomeCommand extends Command
{
+ public function __construct(SomeService $someService)
+ {
+ $this->someService = $someService;
+ }
+
public function someMethod()
{
// ...
- $this->getContainer()->get('some_service');
- $this->container->get('some_service');
+ $this->someService;
+ $this->someService;
}
}
2018-08-01 20:09:34 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `FormIsValidRector`
2018-09-28 16:33:35 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\FormIsValidRector`](/rules/symfony/src/Rector/MethodCall/FormIsValidRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/FormIsValidRector/Fixture)
2018-09-28 16:33:35 +00:00
Adds `$form->isSubmitted()` validation to all `$form->isValid()` calls in Form in Symfony
2018-08-01 20:09:34 +00:00
```diff
-if ($form->isValid()) {
+if ($form->isSubmitted() && $form->isValid()) {
2018-08-01 20:09:34 +00:00
}
2018-05-04 22:30:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2018-10-23 18:58:57 +00:00
### `FormTypeGetParentRector`
2019-02-18 15:51:24 +00:00
- class: [`Rector\Symfony\Rector\ClassMethod\FormTypeGetParentRector`](/rules/symfony/src/Rector/ClassMethod/FormTypeGetParentRector.php)
- [test fixtures](/rules/symfony/tests/Rector/ClassMethod/FormTypeGetParentRector/Fixture)
2019-02-18 15:51:24 +00:00
Turns string Form Type references to their `CONSTANT` alternatives in `getParent()` and `getExtendedType()` methods in Form in Symfony
2020-07-24 11:46:57 +00:00
```diff
use Symfony\Component\Form\AbstractType;
2020-07-24 11:46:57 +00:00
class SomeType extends AbstractType
{
public function getParent()
{
- return 'collection';
+ return \Symfony\Component\Form\Extension\Core\Type\CollectionType::class;
}
}
2019-02-18 15:51:24 +00:00
```
```diff
use Symfony\Component\Form\AbstractTypeExtension;
class SomeExtension extends AbstractTypeExtension
{
public function getExtendedType()
{
- return 'collection';
+ return \Symfony\Component\Form\Extension\Core\Type\CollectionType::class;
}
}
2019-02-18 15:51:24 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-02-18 15:51:24 +00:00
### `FormTypeInstanceToClassConstRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\FormTypeInstanceToClassConstRector`](/rules/symfony/src/Rector/MethodCall/FormTypeInstanceToClassConstRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/FormTypeInstanceToClassConstRector/Fixture)
2018-05-04 22:30:32 +00:00
Changes createForm(new FormType), add(new FormType) to ones with "FormType::class"
2018-07-31 12:50:39 +00:00
2018-08-01 20:09:34 +00:00
```diff
class SomeController
{
public function action()
{
- $form = $this->createForm(new TeamType, $entity);
+ $form = $this->createForm(TeamType::class, $entity);
}
}
2018-05-04 22:30:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `GetParameterToConstructorInjectionRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\GetParameterToConstructorInjectionRector`](/rules/symfony/src/Rector/MethodCall/GetParameterToConstructorInjectionRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/GetParameterToConstructorInjectionRector/Fixture)
2018-05-05 00:04:41 +00:00
Turns fetching of parameters via `getParameter()` in ContainerAware to constructor injection in Command and Controller in Symfony
2018-08-01 20:09:34 +00:00
```diff
-class MyCommand extends ContainerAwareCommand
+class MyCommand extends Command
{
+ private $someParameter;
+
+ public function __construct($someParameter)
+ {
+ $this->someParameter = $someParameter;
+ }
+
public function someMethod()
{
- $this->getParameter('someParameter');
+ $this->someParameter;
}
}
2018-05-05 00:04:41 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `GetRequestRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\ClassMethod\GetRequestRector`](/rules/symfony/src/Rector/ClassMethod/GetRequestRector.php)
- [test fixtures](/rules/symfony/tests/Rector/ClassMethod/GetRequestRector/Fixture)
Turns fetching of dependencies via `$this->get()` to constructor injection in Command and Controller in Symfony
```diff
+use Symfony\Component\HttpFoundation\Request;
+
class SomeController
{
- public function someAction()
+ public function someAction(Request $request)
{
- $this->getRequest()->...();
+ $request->...();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `GetToConstructorInjectionRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\GetToConstructorInjectionRector`](/rules/symfony/src/Rector/MethodCall/GetToConstructorInjectionRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/GetToConstructorInjectionRector/Fixture)
2019-11-06 23:52:19 +00:00
Turns fetching of dependencies via `$this->get()` to constructor injection in Command and Controller in Symfony
2019-11-06 23:52:19 +00:00
```php
<?php
declare(strict_types=1);
use Rector\Symfony\Rector\MethodCall\GetToConstructorInjectionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(GetToConstructorInjectionRector::class)
->call('configure', [[
2020-10-07 14:20:53 +00:00
GetToConstructorInjectionRector::GET_METHOD_AWARE_TYPES => [
'SymfonyControllerClassName',
'GetTraitClassName',
],
]]);
};
```
2019-11-06 23:52:19 +00:00
```diff
-class MyCommand extends ContainerAwareCommand
+class MyCommand extends Command
2019-11-06 23:52:19 +00:00
{
+ public function __construct(SomeService $someService)
+ {
+ $this->someService = $someService;
+ }
+
public function someMethod()
2019-11-06 23:52:19 +00:00
{
- // ...
- $this->get('some_service');
+ $this->someService;
2019-11-06 23:52:19 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-11-06 23:52:19 +00:00
### `MakeCommandLazyRector`
2020-07-28 15:15:30 +00:00
- class: [`Rector\Symfony\Rector\Class_\MakeCommandLazyRector`](/rules/symfony/src/Rector/Class_/MakeCommandLazyRector.php)
- [test fixtures](/rules/symfony/tests/Rector/Class_/MakeCommandLazyRector/Fixture)
2020-07-28 15:15:30 +00:00
Make Symfony commands lazy
2020-07-28 15:15:30 +00:00
```diff
use Symfony\Component\Console\Command\Command
2020-07-28 15:15:30 +00:00
class SunshineCommand extends Command
{
+ protected static $defaultName = 'sunshine';
public function configure()
2020-07-28 15:15:30 +00:00
{
- $this->setName('sunshine');
2020-07-28 15:15:30 +00:00
}
}
```
<br><br>
### `MakeDispatchFirstArgumentEventRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\MakeDispatchFirstArgumentEventRector`](/rules/symfony/src/Rector/MethodCall/MakeDispatchFirstArgumentEventRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/MakeDispatchFirstArgumentEventRector/Fixture)
Make event object a first argument of `dispatch()` method, event name as second
```diff
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class SomeClass
2019-05-29 13:40:20 +00:00
{
public function run(EventDispatcherInterface $eventDispatcher)
{
- $eventDispatcher->dispatch('event_name', new Event());
+ $eventDispatcher->dispatch(new Event(), 'event_name');
}
2019-05-29 13:40:20 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `MergeMethodAnnotationToRouteAnnotationRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\ClassMethod\MergeMethodAnnotationToRouteAnnotationRector`](/rules/symfony/src/Rector/ClassMethod/MergeMethodAnnotationToRouteAnnotationRector.php)
- [test fixtures](/rules/symfony/tests/Rector/ClassMethod/MergeMethodAnnotationToRouteAnnotationRector/Fixture)
Merge removed @Method annotation to @Route one
```diff
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends Controller
2019-05-29 13:40:20 +00:00
{
/**
- * @Route("/show/{id}")
- * @Method({"GET", "HEAD"})
+ * @Route("/show/{id}", methods={"GET","HEAD"})
*/
public function show($id)
{
}
2019-05-29 13:40:20 +00:00
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `OptionNameRector`
2018-05-04 22:30:32 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\OptionNameRector`](/rules/symfony/src/Rector/MethodCall/OptionNameRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/OptionNameRector/Fixture)
2018-05-04 22:30:32 +00:00
Turns old option names to new ones in FormTypes in Form in Symfony
2018-07-31 12:50:39 +00:00
```diff
$builder = new FormBuilder;
-$builder->add("...", ["precision" => "...", "virtual" => "..."];
+$builder->add("...", ["scale" => "...", "inherit_data" => "..."];
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `ParseFileRector`
2018-05-04 22:30:32 +00:00
- class: [`Rector\Symfony\Rector\StaticCall\ParseFileRector`](/rules/symfony/src/Rector/StaticCall/ParseFileRector.php)
- [test fixtures](/rules/symfony/tests/Rector/StaticCall/ParseFileRector/Fixture)
session > use_strict_mode is true by default and can be removed
2018-08-01 20:09:34 +00:00
```diff
-session > use_strict_mode: true
+session:
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `ProcessBuilderGetProcessRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\ProcessBuilderGetProcessRector`](/rules/symfony/src/Rector/MethodCall/ProcessBuilderGetProcessRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/ProcessBuilderGetProcessRector/Fixture)
2019-05-29 13:40:20 +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
$processBuilder = new Symfony\Component\Process\ProcessBuilder;
-$process = $processBuilder->getProcess();
-$commamdLine = $processBuilder->getProcess()->getCommandLine();
+$process = $processBuilder;
+$commamdLine = $processBuilder->getCommandLine();
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `ProcessBuilderInstanceRector`
- class: [`Rector\Symfony\Rector\StaticCall\ProcessBuilderInstanceRector`](/rules/symfony/src/Rector/StaticCall/ProcessBuilderInstanceRector.php)
- [test fixtures](/rules/symfony/tests/Rector/StaticCall/ProcessBuilderInstanceRector/Fixture)
Turns `ProcessBuilder::instance()` to new ProcessBuilder in Process in Symfony. Part of multi-step Rector.
```diff
-$processBuilder = Symfony\Component\Process\ProcessBuilder::instance($args);
+$processBuilder = new Symfony\Component\Process\ProcessBuilder($args);
2018-09-28 16:33:35 +00:00
```
<br><br>
### `ReadOnlyOptionToAttributeRector`
- class: [`Rector\Symfony\Rector\MethodCall\ReadOnlyOptionToAttributeRector`](/rules/symfony/src/Rector/MethodCall/ReadOnlyOptionToAttributeRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/ReadOnlyOptionToAttributeRector/Fixture)
Change "read_only" option in form to attribute
2018-09-28 16:33:35 +00:00
```diff
use Symfony\Component\Form\FormBuilderInterface;
function buildForm(FormBuilderInterface $builder, array $options)
2019-05-29 13:40:20 +00:00
{
- $builder->add('cuid', TextType::class, ['read_only' => true]);
+ $builder->add('cuid', TextType::class, ['attr' => ['read_only' => true]]);
2019-05-29 13:40:20 +00:00
}
2018-09-28 16:33:35 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `RedirectToRouteRector`
- class: [`Rector\Symfony\Rector\MethodCall\RedirectToRouteRector`](/rules/symfony/src/Rector/MethodCall/RedirectToRouteRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/RedirectToRouteRector/Fixture)
Turns redirect to route to short helper method in Controller in Symfony
```diff
-$this->redirect($this->generateUrl("homepage"));
+$this->redirectToRoute("homepage");
```
2020-07-24 11:46:57 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `RemoveDefaultGetBlockPrefixRector`
- class: [`Rector\Symfony\Rector\ClassMethod\RemoveDefaultGetBlockPrefixRector`](/rules/symfony/src/Rector/ClassMethod/RemoveDefaultGetBlockPrefixRector.php)
- [test fixtures](/rules/symfony/tests/Rector/ClassMethod/RemoveDefaultGetBlockPrefixRector/Fixture)
Rename `getBlockPrefix()` if it returns the default value - class to underscore, e.g. UserFormType = user_form
```diff
use Symfony\Component\Form\AbstractType;
class TaskType extends AbstractType
{
- public function getBlockPrefix()
- {
- return 'task';
- }
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ResponseStatusCodeRector`
- class: [`Rector\Symfony\Rector\BinaryOp\ResponseStatusCodeRector`](/rules/symfony/src/Rector/BinaryOp/ResponseStatusCodeRector.php)
- [test fixtures](/rules/symfony/tests/Rector/BinaryOp/ResponseStatusCodeRector/Fixture)
Turns status code numbers to constants
2020-07-24 11:46:57 +00:00
```diff
class SomeController
{
public function index()
{
$response = new \Symfony\Component\HttpFoundation\Response();
- $response->setStatusCode(200);
+ $response->setStatusCode(\Symfony\Component\HttpFoundation\Response::HTTP_OK);
2020-07-24 11:46:57 +00:00
- if ($response->getStatusCode() === 200) {}
+ if ($response->getStatusCode() === \Symfony\Component\HttpFoundation\Response::HTTP_OK) {}
}
}
```
<br><br>
### `RootNodeTreeBuilderRector`
- class: [`Rector\Symfony\Rector\New_\RootNodeTreeBuilderRector`](/rules/symfony/src/Rector/New_/RootNodeTreeBuilderRector.php)
- [test fixtures](/rules/symfony/tests/Rector/New_/RootNodeTreeBuilderRector/Fixture)
Changes Process string argument to an array
2019-05-29 13:40:20 +00:00
```diff
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><br>
2020-07-24 11:46:57 +00:00
### `SimplifyWebTestCaseAssertionsRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\SimplifyWebTestCaseAssertionsRector`](/rules/symfony/src/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/SimplifyWebTestCaseAssertionsRector/Fixture)
Simplify use of assertions in WebTestCase
```diff
use PHPUnit\Framework\TestCase;
class SomeClass extends TestCase
{
public function test()
{
- $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');
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
### `StringFormTypeToClassRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\StringFormTypeToClassRector`](/rules/symfony/src/Rector/MethodCall/StringFormTypeToClassRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/StringFormTypeToClassRector/Fixture)
2020-10-07 13:41:19 +00:00
Turns string Form Type references to their `CONSTANT` alternatives in FormTypes in Form in Symfony. To enable custom types, add `link` to your container XML `dump` in "$parameters->set(Option::SYMFONY_CONTAINER_XML_PATH_PARAMETER, ...);"
```diff
$formBuilder = new Symfony\Component\Form\FormBuilder;
-$formBuilder->add('name', 'form.type.text');
+$formBuilder->add('name', \Symfony\Component\Form\Extension\Core\Type\TextType::class);
2019-05-29 13:40:20 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-05-29 13:40:20 +00:00
### `StringToArrayArgumentProcessRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\Symfony\Rector\New_\StringToArrayArgumentProcessRector`](/rules/symfony/src/Rector/New_/StringToArrayArgumentProcessRector.php)
- [test fixtures](/rules/symfony/tests/Rector/New_/StringToArrayArgumentProcessRector/Fixture)
2020-05-31 15:26:08 +00:00
Changes Process string argument to an array
2020-05-31 15:26:08 +00:00
```diff
use Symfony\Component\Process\Process;
-$process = new Process('ls -l');
+$process = new Process(['ls', '-l']);
2020-05-31 15:26:08 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-05-31 15:26:08 +00:00
### `VarDumperTestTraitMethodArgsRector`
2020-05-31 15:26:08 +00:00
- class: [`Rector\Symfony\Rector\MethodCall\VarDumperTestTraitMethodArgsRector`](/rules/symfony/src/Rector/MethodCall/VarDumperTestTraitMethodArgsRector.php)
- [test fixtures](/rules/symfony/tests/Rector/MethodCall/VarDumperTestTraitMethodArgsRector/Fixture)
2020-07-24 11:46:57 +00:00
Adds a new `$filter` argument in `VarDumperTestTrait->assertDumpEquals()` and `VarDumperTestTrait->assertDumpMatchesFormat()` in Validator in Symfony.
2020-07-24 11:46:57 +00:00
```diff
-$varDumperTestTrait->assertDumpEquals($dump, $data, $message = "");
+$varDumperTestTrait->assertDumpEquals($dump, $data, $filter = 0, $message = "");
2020-05-31 15:26:08 +00:00
```
```diff
-$varDumperTestTrait->assertDumpMatchesFormat($dump, $data, $message = "");
+$varDumperTestTrait->assertDumpMatchesFormat($dump, $data, $filter = 0, $message = "");
2020-05-31 15:26:08 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
2020-05-31 15:26:08 +00:00
## SymfonyCodeQuality
2020-07-24 11:46:57 +00:00
### `EventListenerToEventSubscriberRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\SymfonyCodeQuality\Rector\Class_\EventListenerToEventSubscriberRector`](/rules/symfony-code-quality/src/Rector/Class_/EventListenerToEventSubscriberRector.php)
- [test fixtures](/rules/symfony-code-quality/tests/Rector/Class_/EventListenerToEventSubscriberRector/Fixture)
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' }
+}
```
2020-06-16 16:14:51 +00:00
<br><br>
## SymfonyPHPUnit
### `SelfContainerGetMethodCallFromTestToSetUpMethodRector`
- class: [`Rector\SymfonyPHPUnit\Rector\Class_\SelfContainerGetMethodCallFromTestToSetUpMethodRector`](/rules/symfony-phpunit/src/Rector/Class_/SelfContainerGetMethodCallFromTestToSetUpMethodRector.php)
- [test fixtures](/rules/symfony-phpunit/tests/Rector/Class_/SelfContainerGetMethodCallFromTestToSetUpMethodRector/Fixture)
Move self::$container service fetching from test methods up to setUp method
```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();
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
## SymfonyPhpConfig
2020-07-24 11:46:57 +00:00
2020-07-29 23:39:41 +00:00
### `ChangeServiceArgumentsToMethodCallRector`
- class: [`Rector\SymfonyPhpConfig\Rector\MethodCall\ChangeServiceArgumentsToMethodCallRector`](/rules/symfony-php-config/src/Rector/MethodCall/ChangeServiceArgumentsToMethodCallRector.php)
- [test fixtures](/rules/symfony-php-config/tests/Rector/MethodCall/ChangeServiceArgumentsToMethodCallRector/Fixture)
2020-07-29 23:39:41 +00:00
Change `$service->arg(...)` to `$service->call(...)`
```php
<?php
declare(strict_types=1);
2020-07-29 23:39:41 +00:00
use Rector\SymfonyPhpConfig\Rector\MethodCall\ChangeServiceArgumentsToMethodCallRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-29 23:39:41 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 23:39:41 +00:00
$services = $containerConfigurator->services();
2020-07-29 23:39:41 +00:00
$services->set(ChangeServiceArgumentsToMethodCallRector::class)
->call('configure', [[
ChangeServiceArgumentsToMethodCallRector::CLASS_TYPE_TO_METHOD_NAME => [
'SomeClass' => 'configure',
],
]]);
2020-07-29 23:39:41 +00:00
};
```
```diff
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(SomeClass::class)
- ->arg('$key', 'value');
+ ->call('configure', [[
+ '$key' => 'value
+ ]]);
}
```
<br><br>
### `ReplaceArrayWithObjectRector`
- class: [`Rector\SymfonyPhpConfig\Rector\ArrayItem\ReplaceArrayWithObjectRector`](/rules/symfony-php-config/src/Rector/ArrayItem/ReplaceArrayWithObjectRector.php)
- [test fixtures](/rules/symfony-php-config/tests/Rector/ArrayItem/ReplaceArrayWithObjectRector/Fixture)
Replace complex array configuration in configs with value object
```php
<?php
declare(strict_types=1);
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\SymfonyPhpConfig\Rector\ArrayItem\ReplaceArrayWithObjectRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ReplaceArrayWithObjectRector::class)
->call('configure', [[
ReplaceArrayWithObjectRector::CONSTANT_NAMES_TO_VALUE_OBJECTS => [
RenameMethodRector::OLD_TO_NEW_METHODS_BY_CLASS => MethodCallRename::class,
],
]]);
};
```
```diff
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RenameMethodRector::class)
->call('configure', [[
- RenameMethodRector::OLD_TO_NEW_METHODS_BY_CLASS => [
- 'Illuminate\Auth\Access\Gate' => [
- 'access' => 'inspect',
- ]
- ]]
- ]);
+ RenameMethodRector::OLD_TO_NEW_METHODS_BY_CLASS => \Rector\SymfonyPhpConfig\inline_value_objects([
+ new \Rector\Renaming\ValueObject\MethodCallRename('Illuminate\Auth\Access\Gate', 'access', 'inspect'),
+ ])
+ ]]);
}
```
<br><br>
## Transform
### `ArgumentFuncCallToMethodCallRector`
- class: [`Rector\Transform\Rector\FuncCall\ArgumentFuncCallToMethodCallRector`](/rules/transform/src/Rector/FuncCall/ArgumentFuncCallToMethodCallRector.php)
- [test fixtures](/rules/transform/tests/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture)
Move help facade-like function calls to constructor injection
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\FuncCall\ArgumentFuncCallToMethodCallRector;
use Rector\Transform\ValueObject\ArgumentFuncCallToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ArgumentFuncCallToMethodCallRector::class)
->call('configure', [[
ArgumentFuncCallToMethodCallRector::FUNCTIONS_TO_METHOD_CALLS => inline_value_objects(
[new ArgumentFuncCallToMethodCall('view', 'Illuminate\Contracts\View\Factory', null, 'make')]
),
]]);
};
```
```diff
class SomeController
{
+ /**
+ * @var \Illuminate\Contracts\View\Factory
+ */
+ private $viewFactory;
+
+ public function __construct(\Illuminate\Contracts\View\Factory $viewFactory)
+ {
+ $this->viewFactory = $viewFactory;
+ }
+
public function action()
{
- $template = view('template.blade');
- $viewFactory = view();
+ $template = $this->viewFactory->make('template.blade');
+ $viewFactory = $this->viewFactory;
}
}
```
<br><br>
### `FuncCallToMethodCallRector`
- class: [`Rector\Transform\Rector\FuncCall\FuncCallToMethodCallRector`](/rules/transform/src/Rector/FuncCall/FuncCallToMethodCallRector.php)
- [test fixtures](/rules/transform/tests/Rector/FuncCall/FuncCallToMethodCallRector/Fixture)
Turns defined function calls to local method calls.
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\FuncCall\FuncCallToMethodCallRector;
use Rector\Transform\ValueObject\FuncNameToMethodCallName;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(FuncCallToMethodCallRector::class)
->call('configure', [[
FuncCallToMethodCallRector::FUNC_CALL_TO_CLASS_METHOD_CALL => inline_value_objects(
[new FuncNameToMethodCallName('view', 'Namespaced\SomeRenderer', 'render')]
),
]]);
};
```
```diff
class SomeClass
{
+ /**
+ * @var \Namespaced\SomeRenderer
+ */
+ private $someRenderer;
+
+ public function __construct(\Namespaced\SomeRenderer $someRenderer)
+ {
+ $this->someRenderer = $someRenderer;
+ }
+
public function run()
{
- view('...');
+ $this->someRenderer->view('...');
}
}
```
<br><br>
### `MethodCallToAnotherMethodCallWithArgumentsRector`
- class: [`Rector\Transform\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector`](/rules/transform/src/Rector/MethodCall/MethodCallToAnotherMethodCallWithArgumentsRector.php)
- [test fixtures](/rules/transform/tests/Rector/MethodCall/MethodCallToAnotherMethodCallWithArgumentsRector/Fixture)
Turns old method call with specific types to new one with arguments
```php
<?php
declare(strict_types=1);
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\MethodCall\MethodCallToAnotherMethodCallWithArgumentsRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(MethodCallToAnotherMethodCallWithArgumentsRector::class)
->call('configure', [[
MethodCallToAnotherMethodCallWithArgumentsRector::METHOD_CALL_RENAMES_WITH_ADDED_ARGUMENTS => inline_value_objects(
[new MethodCallRenameWithArrayKey('Nette\DI\ServiceDefinition', 'setInject', 'addTag', 'inject')]
),
]]);
};
```
```diff
$serviceDefinition = new Nette\DI\ServiceDefinition;
-$serviceDefinition->setInject();
+$serviceDefinition->addTag('inject');
```
<br><br>
### `MethodCallToPropertyFetchRector`
- class: [`Rector\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector`](/rules/transform/src/Rector/MethodCall/MethodCallToPropertyFetchRector.php)
- [test fixtures](/rules/transform/tests/Rector/MethodCall/MethodCallToPropertyFetchRector/Fixture)
Turns method call "$this->something()" to property fetch "$this->something"
```php
<?php
declare(strict_types=1);
use Rector\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(MethodCallToPropertyFetchRector::class)
->call('configure', [[
MethodCallToPropertyFetchRector::METHOD_CALL_TO_PROPERTY_FETCHES => [
'someMethod' => 'someProperty',
],
]]);
};
```
```diff
class SomeClass
{
public function run()
{
- $this->someMethod();
+ $this->someProperty;
}
}
```
<br><br>
### `MethodCallToStaticCallRector`
- class: [`Rector\Transform\Rector\MethodCall\MethodCallToStaticCallRector`](/rules/transform/src/Rector/MethodCall/MethodCallToStaticCallRector.php)
- [test fixtures](/rules/transform/tests/Rector/MethodCall/MethodCallToStaticCallRector/Fixture)
Change method call to desired static call
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\MethodCall\MethodCallToStaticCallRector;
use Rector\Transform\ValueObject\MethodCallToStaticCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(MethodCallToStaticCallRector::class)
->call('configure', [[
MethodCallToStaticCallRector::METHOD_CALLS_TO_STATIC_CALLS => inline_value_objects(
[new MethodCallToStaticCall('AnotherDependency', 'process', 'StaticCaller', 'anotherMethod')]
),
]]);
};
```
```diff
final class SomeClass
{
private $anotherDependency;
public function __construct(AnotherDependency $anotherDependency)
{
$this->anotherDependency = $anotherDependency;
}
public function loadConfiguration()
{
- return $this->anotherDependency->process('value');
+ return StaticCaller::anotherMethod('value');
}
}
```
<br><br>
### `NewToStaticCallRector`
- class: [`Rector\Transform\Rector\New_\NewToStaticCallRector`](/rules/transform/src/Rector/New_/NewToStaticCallRector.php)
- [test fixtures](/rules/transform/tests/Rector/New_/NewToStaticCallRector/Fixture)
Change new Object to static call
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\New_\NewToStaticCallRector;
use Rector\Transform\ValueObject\NewToStaticCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(NewToStaticCallRector::class)
->call('configure', [[
2020-10-07 14:20:53 +00:00
NewToStaticCallRector::TYPE_TO_STATIC_CALLS => inline_value_objects(
[new NewToStaticCall('Cookie', 'Cookie', 'create')]
),
]]);
};
```
```diff
class SomeClass
{
public function run()
{
- new Cookie($name);
+ Cookie::create($name);
}
}
```
<br><br>
### `PropertyAssignToMethodCallRector`
- class: [`Rector\Transform\Rector\Assign\PropertyAssignToMethodCallRector`](/rules/transform/src/Rector/Assign/PropertyAssignToMethodCallRector.php)
- [test fixtures](/rules/transform/tests/Rector/Assign/PropertyAssignToMethodCallRector/Fixture)
Turns property assign of specific type and property name to method call
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\Assign\PropertyAssignToMethodCallRector;
use Rector\Transform\ValueObject\PropertyAssignToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(PropertyAssignToMethodCallRector::class)
->call('configure', [[
PropertyAssignToMethodCallRector::PROPERTY_ASSIGNS_TO_METHODS_CALLS => inline_value_objects(
[new PropertyAssignToMethodCall('SomeClass', 'oldProperty', 'newMethodCall')]
),
]]);
};
```
```diff
$someObject = new SomeClass;
-$someObject->oldProperty = false;
+$someObject->newMethodCall(false);
```
<br><br>
### `PropertyToMethodRector`
- class: [`Rector\Transform\Rector\Assign\PropertyToMethodRector`](/rules/transform/src/Rector/Assign/PropertyToMethodRector.php)
- [test fixtures](/rules/transform/tests/Rector/Assign/PropertyToMethodRector/Fixture)
Replaces properties assign calls be defined methods.
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\Assign\PropertyToMethodRector;
use Rector\Transform\ValueObject\PropertyToMethod;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(PropertyToMethodRector::class)
->call('configure', [[
PropertyToMethodRector::PROPERTIES_TO_METHOD_CALLS => inline_value_objects(
[new PropertyToMethod('SomeObject', 'property', 'getProperty', [], 'setProperty')]
),
]]);
};
```
```diff
-$result = $object->property;
-$object->property = $value;
+$result = $object->getProperty();
+$object->setProperty($value);
```
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\Assign\PropertyToMethodRector;
use Rector\Transform\ValueObject\PropertyToMethod;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(PropertyToMethodRector::class)
->call('configure', [[
PropertyToMethodRector::PROPERTIES_TO_METHOD_CALLS => inline_value_objects(
[new PropertyToMethod('SomeObject', 'property', 'getConfig', ['someArg'], null)]
),
]]);
};
```
```diff
-$result = $object->property;
+$result = $object->getProperty('someArg');
```
<br><br>
### `ServiceGetterToConstructorInjectionRector`
- class: [`Rector\Transform\Rector\MethodCall\ServiceGetterToConstructorInjectionRector`](/rules/transform/src/Rector/MethodCall/ServiceGetterToConstructorInjectionRector.php)
- [test fixtures](/rules/transform/tests/Rector/MethodCall/ServiceGetterToConstructorInjectionRector/Fixture)
Get service call to constructor injection
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\MethodCall\ServiceGetterToConstructorInjectionRector;
use Rector\Transform\ValueObject\ServiceGetterToConstructorInjection;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ServiceGetterToConstructorInjectionRector::class)
->call('configure', [[
ServiceGetterToConstructorInjectionRector::METHOD_CALL_TO_SERVICES => inline_value_objects(
[new ServiceGetterToConstructorInjection('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><br>
### `StaticCallToFuncCallRector`
- class: [`Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector`](/rules/transform/src/Rector/StaticCall/StaticCallToFuncCallRector.php)
- [test fixtures](/rules/transform/tests/Rector/StaticCall/StaticCallToFuncCallRector/Fixture)
Turns static call to function call.
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
use Rector\Transform\ValueObject\StaticCallToFuncCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(StaticCallToFuncCallRector::class)
->call('configure', [[
StaticCallToFuncCallRector::STATIC_CALLS_TO_FUNCTIONS => inline_value_objects(
[new StaticCallToFuncCall('OldClass', 'oldMethod', 'new_function')]
),
]]);
};
```
```diff
-OldClass::oldMethod("args");
+new_function("args");
```
<br><br>
### `StaticCallToMethodCallRector`
- class: [`Rector\Transform\Rector\StaticCall\StaticCallToMethodCallRector`](/rules/transform/src/Rector/StaticCall/StaticCallToMethodCallRector.php)
- [test fixtures](/rules/transform/tests/Rector/StaticCall/StaticCallToMethodCallRector/Fixture)
Change static call to service method via constructor injection
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\Transform\Rector\StaticCall\StaticCallToMethodCallRector;
use Rector\Transform\ValueObject\StaticCallToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(StaticCallToMethodCallRector::class)
->call('configure', [[
StaticCallToMethodCallRector::STATIC_CALLS_TO_METHOD_CALLS => inline_value_objects(
[new StaticCallToMethodCall(
'Nette\Utils\FileSystem',
'write',
'Symplify\SmartFileSystem\SmartFileSystem',
'dumpFile'
)]
),
]]);
};
```
```diff
-use Nette\Utils\FileSystem;
+use Symplify\SmartFileSystem\SmartFileSystem;
class SomeClass
{
+ /**
+ * @var SmartFileSystem
+ */
+ private $smartFileSystem;
+
+ public function __construct(SmartFileSystem $smartFileSystem)
+ {
+ $this->smartFileSystem = $smartFileSystem;
+ }
+
public function run()
{
- return FileSystem::write('file', 'content');
+ return $this->smartFileSystem->dumpFile('file', 'content');
}
}
```
<br><br>
## Twig
2019-05-29 13:40:20 +00:00
### `SimpleFunctionAndFilterRector`
2019-05-29 13:40:20 +00:00
- class: [`Rector\Twig\Rector\Return_\SimpleFunctionAndFilterRector`](/rules/twig/src/Rector/Return_/SimpleFunctionAndFilterRector.php)
- [test fixtures](/rules/twig/tests/Rector/Return_/SimpleFunctionAndFilterRector/Fixture)
Changes Twig_Function_Method to Twig_SimpleFunction calls in Twig_Extension.
2020-07-24 11:46:57 +00:00
```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']),
];
}
2020-07-24 11:46:57 +00:00
public function getFilters()
{
return [
- 'is_mobile' => new Twig_Filter_Method($this, 'isMobile'),
+ new Twig_SimpleFilter('is_mobile', [$this, 'isMobile']),
];
}
}
```
<br><br>
## TypeDeclaration
### `AddArrayParamDocTypeRector`
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector`](/rules/type-declaration/src/Rector/ClassMethod/AddArrayParamDocTypeRector.php)
- [test fixtures](/rules/type-declaration/tests/Rector/ClassMethod/AddArrayParamDocTypeRector/Fixture)
Adds @param annotation to array parameters inferred from the rest of the code
2019-05-29 13:40:20 +00:00
```diff
class SomeClass
{
/**
* @var int[]
*/
private $values;
+ /**
+ * @param int[] $values
+ */
public function __construct(array $values)
{
$this->values = $values;
}
}
2018-07-31 12:50:39 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AddArrayReturnDocTypeRector`
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector`](/rules/type-declaration/src/Rector/ClassMethod/AddArrayReturnDocTypeRector.php)
- [test fixtures](/rules/type-declaration/tests/Rector/ClassMethod/AddArrayReturnDocTypeRector/Fixture)
2018-05-04 22:30:32 +00:00
Adds @return annotation to array parameters inferred from the rest of the code
2018-08-01 20:09:34 +00:00
```diff
class SomeClass
{
/**
* @var int[]
*/
private $values;
2018-05-04 22:30:32 +00:00
+ /**
+ * @return int[]
+ */
public function getValues(): array
{
return $this->values;
}
}
2018-08-01 20:09:34 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `AddClosureReturnTypeRector`
- class: [`Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector`](/rules/type-declaration/src/Rector/Closure/AddClosureReturnTypeRector.php)
- [test fixtures](/rules/type-declaration/tests/Rector/Closure/AddClosureReturnTypeRector/Fixture)
2019-08-05 21:10:47 +00:00
Add known return type to functions
2019-08-05 21:10:47 +00:00
```diff
class SomeClass
{
public function run($meetups)
{
- return array_filter($meetups, function (Meetup $meetup) {
+ return array_filter($meetups, function (Meetup $meetup): bool {
return is_object($meetup);
});
}
}
```
2019-08-05 21:10:47 +00:00
<br><br>
2020-07-24 11:46:57 +00:00
### `AddMethodCallBasedParamTypeRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector`](/rules/type-declaration/src/Rector/ClassMethod/AddMethodCallBasedParamTypeRector.php)
- [test fixtures](/rules/type-declaration/tests/Rector/ClassMethod/AddMethodCallBasedParamTypeRector/Fixture)
2019-08-05 21:10:47 +00:00
Change param type of passed `getId()` to UuidInterface type declaration
2019-08-05 21:10:47 +00:00
```diff
class SomeClass
2019-08-05 21:10:47 +00:00
{
- public function getById($id)
+ public function getById(\Ramsey\Uuid\UuidInterface $id)
2019-08-05 21:10:47 +00:00
{
}
}
2019-08-05 21:10:47 +00:00
class CallerClass
{
public function run()
2019-08-05 21:10:47 +00:00
{
$building = new Building();
$someClass = new SomeClass();
$someClass->getById($building->getId());
2019-08-05 21:10:47 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-08-05 21:10:47 +00:00
### `AddParamTypeDeclarationRector`
2018-05-04 22:30:32 +00:00
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector`](/rules/type-declaration/src/Rector/ClassMethod/AddParamTypeDeclarationRector.php)
- [test fixtures](/rules/type-declaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Fixture)
Add param types where needed
2018-05-04 22:30:32 +00:00
2020-07-24 11:46:57 +00:00
```php
<?php
declare(strict_types=1);
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddParamTypeDeclaration;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2020-07-24 11:46:57 +00:00
return static function (ContainerConfigurator $containerConfigurator): void {
2020-07-29 08:33:10 +00:00
$services = $containerConfigurator->services();
$services->set(AddParamTypeDeclarationRector::class)
->call('configure', [[
AddParamTypeDeclarationRector::PARAMETER_TYPEHINTS => inline_value_objects(
[new AddParamTypeDeclaration('SomeClass', 'process', 0, 'string')]
),
]]);
2020-07-24 11:46:57 +00:00
};
2018-08-01 20:09:34 +00:00
```
2018-05-04 22:30:32 +00:00
```diff
class SomeClass
{
- public function process($name)
+ public function process(string $name)
{
}
}
2018-07-31 06:38:48 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `CompleteVarDocTypePropertyRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector`](/rules/type-declaration/src/Rector/Property/CompleteVarDocTypePropertyRector.php)
- [test fixtures](/rules/type-declaration/tests/Rector/Property/CompleteVarDocTypePropertyRector/Fixture)
2018-08-01 20:09:34 +00:00
Complete property `@var` annotations or correct the old ones
2018-08-01 20:09:34 +00:00
2018-05-04 22:30:32 +00:00
```diff
final class SomeClass
{
+ /**
+ * @var EventDispatcher
+ */
private $eventDispatcher;
public function __construct(EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}
2018-05-04 22:30:32 +00:00
```
2020-06-16 16:14:51 +00:00
<br><br>
### `ParamTypeDeclarationRector`
2019-12-18 09:53:46 +00:00
- class: [`Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector`](/rules/type-declaration/src/Rector/FunctionLike/ParamTypeDeclarationRector.php)
- [test fixtures](/rules/type-declaration/tests/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture)
2020-07-24 11:46:57 +00:00
Change @param types to type declarations if not a BC-break
2020-07-24 11:46:57 +00:00
```diff
<?php
2019-12-18 09:53:46 +00:00
class ParentClass
{
/**
* @param int $number
*/
public function keep($number)
{
}
}
2019-12-18 09:53:46 +00:00
final class ChildClass extends ParentClass
2019-12-18 09:53:46 +00:00
{
/**
* @param int $number
*/
public function keep($number)
{
}
/**
* @param int $number
*/
- public function change($number)
+ public function change(int $number)
2019-12-18 09:53:46 +00:00
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-12-18 09:53:46 +00:00
### `PropertyTypeDeclarationRector`
2019-09-25 08:49:53 +00:00
- class: [`Rector\TypeDeclaration\Rector\Property\PropertyTypeDeclarationRector`](/rules/type-declaration/src/Rector/Property/PropertyTypeDeclarationRector.php)
- [test fixtures](/rules/type-declaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture)
2019-09-25 08:49:53 +00:00
Add @var to properties that are missing it
2019-09-25 08:49:53 +00:00
```diff
class SomeClass
2019-09-25 08:49:53 +00:00
{
+ /**
+ * @var int
+ */
private $value;
public function run()
2019-09-25 08:49:53 +00:00
{
$this->value = 123;
2019-09-25 08:49:53 +00:00
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>
2019-09-25 08:49:53 +00:00
### `ReturnTypeDeclarationRector`
2020-07-24 11:46:57 +00:00
- class: [`Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector`](/rules/type-declaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php)
- [test fixtures](/rules/type-declaration/tests/Rector/FunctionLike/ReturnTypeDeclarationRector/Fixture)
Change @return types and type from static analysis to type declarations if not a BC-break
```diff
<?php
class SomeClass
{
- /**
- * @return int
- */
- public function getCount()
+ public function getCount(): int
{
}
}
```
2020-06-16 16:14:51 +00:00
<br><br>