Skip to content

Commit

Permalink
add new rectors for php8.3's deprecation of get_class()/get_parent_cl…
Browse files Browse the repository at this point in the history
…ass() without arguments
  • Loading branch information
cabbey committed Oct 30, 2024
1 parent d246d27 commit 6ad00a7
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/set/php83.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
use Rector\Php83\Rector\FuncCall\CombineHostPortLdapUriRector;
use Rector\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector;
use Rector\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
AddOverrideAttributeToOverriddenMethodsRector::class,
AddTypeToConstRector::class,
CombineHostPortLdapUriRector::class,
RemoveGetClassNoArgsRector::class,
RemoveGetParentClassNoArgsRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\Fixture;

class Fixture
{
public function test()
{
echo get_class();
}
}

?>
-----
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\Fixture;

class Fixture
{
public function test()
{
echo __CLASS__;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\Fixture;

class SkipDifferentFuncCall
{
public function run($a, $b)
{
return str_contains($a, $b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\Fixture;

use Rector\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector;

class SkipWithArgs
{
public function test()
{
$obj = new RemoveGetClassNoArgsRector();
echo get_class($obj);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveGetClassNoArgsRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveGetClassNoArgsRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_83);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\Fixture;

class Fixture
{
public function test()
{
echo get_parent_class();
}
}

?>
-----
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\Fixture;

class Fixture
{
public function test()
{
echo parent::class;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\Fixture;

class SkipDifferentFuncCall
{
public function run($a, $b)
{
return str_contains($a, $b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\Fixture;

use Rector\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector;

class SkipWithArgs
{
public function test()
{
$obj = new RemoveGetClassNoArgsRector();
echo get_parent_class($obj);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveGetParentClassNoArgsRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveGetParentClassNoArgsRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_83);
};
81 changes: 81 additions & 0 deletions rules/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Rector\Php83\Rector\FuncCall;

use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://php.watch/versions/8.3/get_class-get_parent_class-parameterless-deprecated
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.core.get-class
* @see \Rector\Tests\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector\RemoveGetClassNoArgsRectorTest
*/
final class RemoveGetClassNoArgsRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
$r = new RuleDefinition(
'Replace calls to get_class() without arguments with __CLASS__ magic constant',
[
new CodeSample(
<<<'CODE_SAMPLE'
class Example {
public function doWork() {
return get_class();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class Example {
public function doWork() {
return __CLASS__;
}
}
CODE_SAMPLE
),
]
);

return $r;
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Expr\FuncCall::class];
}

/**
* @param Node\Expr\FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node->isFirstClassCallable()) {
return null;
}

if (! ($this->isName($node, 'get_class'))) {
return null;
}

if (count($node->getArgs()) !== 0) {
return null;
}

return new Node\Scalar\MagicConst\Class_();
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATE_GET_CLASS_WITHOUT_ARGS;
}
}
81 changes: 81 additions & 0 deletions rules/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Rector\Php83\Rector\FuncCall;

use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://php.watch/versions/8.3/get_class-get_parent_class-parameterless-deprecated
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.core.get-class
* @see \Rector\Tests\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector\RemoveGetParentClassNoArgsRectorTest
*/
final class RemoveGetParentClassNoArgsRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
$r = new RuleDefinition(
'Replace calls to get_parent_class() without arguments with parent::class constant',
[
new CodeSample(
<<<'CODE_SAMPLE'
class Example extends StdClass {
public function doWork() {
return get_parent_class();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class Example extends StdClass {
public function doWork() {
return parent::class;
}
}
CODE_SAMPLE
),
]
);

return $r;
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Expr\FuncCall::class];
}

/**
* @param Node\Expr\FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node->isFirstClassCallable()) {
return null;
}

if (! ($this->isName($node, 'get_parent_class'))) {
return null;
}

if (count($node->getArgs()) !== 0) {
return null;
}

return new Node\Expr\ClassConstFetch(new Node\Name(['parent']), new Node\VarLikeIdentifier('class'));
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATE_GET_CLASS_WITHOUT_ARGS;
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,10 @@ final class PhpVersionFeature
* @var int
*/
public const DEPRECATE_HOST_PORT_SEPARATE_ARGS = PhpVersion::PHP_83;

/**
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.core.get-class
* @var int
*/
public const DEPRECATE_GET_CLASS_WITHOUT_ARGS = PhpVersion::PHP_83;
}

0 comments on commit 6ad00a7

Please sign in to comment.