Merge pull request #3277 from rectorphp/php80-class

[PHP 8.0] Add ClassOnObjectRector
This commit is contained in:
kodiakhq[bot] 2020-04-29 20:06:47 +00:00 committed by GitHub
commit c5a1cc111e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 161 additions and 3 deletions

View File

@ -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

View File

@ -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)

View File

@ -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;

View 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');
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}
?>

View File

@ -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';
} }