mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
Merge pull request #3277 from rectorphp/php80-class
[PHP 8.0] Add ClassOnObjectRector
This commit is contained in:
commit
c5a1cc111e
@ -5,3 +5,4 @@ services:
|
|||||||
Rector\Php80\Rector\Identical\StrEndsWithRector: null
|
Rector\Php80\Rector\Identical\StrEndsWithRector: null
|
||||||
Rector\Php80\Rector\Class_\StringableForToStringRector: null
|
Rector\Php80\Rector\Class_\StringableForToStringRector: null
|
||||||
Rector\Php80\Rector\Class_\AnnotationToAttributeRector: null
|
Rector\Php80\Rector\Class_\AnnotationToAttributeRector: null
|
||||||
|
Rector\Php80\Rector\FuncCall\ClassOnObjectRector: null
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# All 505 Rectors Overview
|
# All 506 Rectors Overview
|
||||||
|
|
||||||
- [Projects](#projects)
|
- [Projects](#projects)
|
||||||
- [General](#general)
|
- [General](#general)
|
||||||
@ -7904,7 +7904,7 @@ Changes property `@var` annotations from annotation to type.
|
|||||||
- class: [`Rector\Php80\Rector\Class_\AnnotationToAttributeRector`](/../master/rules/php80/src/Rector/Class_/AnnotationToAttributeRector.php)
|
- class: [`Rector\Php80\Rector\Class_\AnnotationToAttributeRector`](/../master/rules/php80/src/Rector/Class_/AnnotationToAttributeRector.php)
|
||||||
- [test fixtures](/../master/rules/php80/tests/Rector/Class_/AnnotationToAttributeRector/Fixture)
|
- [test fixtures](/../master/rules/php80/tests/Rector/Class_/AnnotationToAttributeRector/Fixture)
|
||||||
|
|
||||||
Change annotation to attibute
|
Change annotation to attribute
|
||||||
|
|
||||||
```diff
|
```diff
|
||||||
use Doctrine\ORM\Attributes as ORM;
|
use Doctrine\ORM\Attributes as ORM;
|
||||||
@ -7920,6 +7920,26 @@ Change annotation to attibute
|
|||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
|
### `ClassOnObjectRector`
|
||||||
|
|
||||||
|
- class: [`Rector\Php80\Rector\FuncCall\ClassOnObjectRector`](/../master/rules/php80/src/Rector/FuncCall/ClassOnObjectRector.php)
|
||||||
|
- [test fixtures](/../master/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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
### `StrContainsRector`
|
### `StrContainsRector`
|
||||||
|
|
||||||
- class: [`Rector\Php80\Rector\NotIdentical\StrContainsRector`](/../master/rules/php80/src/Rector/NotIdentical/StrContainsRector.php)
|
- class: [`Rector\Php80\Rector\NotIdentical\StrContainsRector`](/../master/rules/php80/src/Rector/NotIdentical/StrContainsRector.php)
|
||||||
|
@ -33,7 +33,7 @@ final class AnnotationToAttributeRector extends AbstractRector
|
|||||||
|
|
||||||
public function getDefinition(): RectorDefinition
|
public function getDefinition(): RectorDefinition
|
||||||
{
|
{
|
||||||
return new RectorDefinition('Change annotation to attibute', [
|
return new RectorDefinition('Change annotation to attribute', [
|
||||||
new CodeSample(
|
new CodeSample(
|
||||||
<<<'PHP'
|
<<<'PHP'
|
||||||
use Doctrine\ORM\Attributes as ORM;
|
use Doctrine\ORM\Attributes as ORM;
|
||||||
|
75
rules/php80/src/Rector/FuncCall/ClassOnObjectRector.php
Normal file
75
rules/php80/src/Rector/FuncCall/ClassOnObjectRector.php
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rector\Php80\Rector\FuncCall;
|
||||||
|
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Expr\ClassConstFetch;
|
||||||
|
use PhpParser\Node\Expr\FuncCall;
|
||||||
|
use Rector\Core\Rector\AbstractRector;
|
||||||
|
use Rector\Core\RectorDefinition\CodeSample;
|
||||||
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||||
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://wiki.php.net/rfc/class_name_literal_on_object
|
||||||
|
*
|
||||||
|
* @see \Rector\Php80\Tests\Rector\FuncCall\ClassOnObjectRector\ClassOnObjectRectorTest
|
||||||
|
*/
|
||||||
|
final class ClassOnObjectRector extends AbstractRector
|
||||||
|
{
|
||||||
|
public function getDefinition(): RectorDefinition
|
||||||
|
{
|
||||||
|
return new RectorDefinition('Change get_class($object) to faster $object::class', [
|
||||||
|
new CodeSample(
|
||||||
|
<<<'PHP'
|
||||||
|
class SomeClass
|
||||||
|
{
|
||||||
|
public function run($object)
|
||||||
|
{
|
||||||
|
return get_class($object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PHP
|
||||||
|
,
|
||||||
|
<<<'PHP'
|
||||||
|
class SomeClass
|
||||||
|
{
|
||||||
|
public function run($object)
|
||||||
|
{
|
||||||
|
return $object::class;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PHP
|
||||||
|
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public function getNodeTypes(): array
|
||||||
|
{
|
||||||
|
return [FuncCall::class];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FuncCall $node
|
||||||
|
*/
|
||||||
|
public function refactor(Node $node): ?Node
|
||||||
|
{
|
||||||
|
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::CLASS_ON_OBJECT)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $this->isFuncCallName($node, 'get_class')) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$object = $node->args[0]->value;
|
||||||
|
|
||||||
|
return new ClassConstFetch($object, 'class');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rector\Php80\Tests\Rector\FuncCall\ClassOnObjectRector;
|
||||||
|
|
||||||
|
use Iterator;
|
||||||
|
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||||
|
use Rector\Php80\Rector\FuncCall\ClassOnObjectRector;
|
||||||
|
|
||||||
|
final class ClassOnObjectRectorTest extends AbstractRectorTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @dataProvider provideData()
|
||||||
|
*/
|
||||||
|
public function test(string $file): void
|
||||||
|
{
|
||||||
|
$this->doTestFile($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideData(): Iterator
|
||||||
|
{
|
||||||
|
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRectorClass(): string
|
||||||
|
{
|
||||||
|
return ClassOnObjectRector::class;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\Php80\Tests\Rector\FuncCall\ClassOnObjectRector\Fixture;
|
||||||
|
|
||||||
|
class SomeClass
|
||||||
|
{
|
||||||
|
public function run($object)
|
||||||
|
{
|
||||||
|
return get_class($object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
-----
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\Php80\Tests\Rector\FuncCall\ClassOnObjectRector\Fixture;
|
||||||
|
|
||||||
|
class SomeClass
|
||||||
|
{
|
||||||
|
public function run($object)
|
||||||
|
{
|
||||||
|
return $object::class;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -146,4 +146,9 @@ final class PhpVersionFeature
|
|||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public const UNION_TYPES = '8.0';
|
public const UNION_TYPES = '8.0';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public const CLASS_ON_OBJECT = '8.0';
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user