Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP8.3] add new rectors for get_class()/get_parent_class() without arguments #6405

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};
79 changes: 79 additions & 0 deletions rules/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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 \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;
}
}
79 changes: 79 additions & 0 deletions rules/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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 \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;
}
}
7 changes: 7 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,11 @@ 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
* @see https://php.watch/versions/8.3/get_class-get_parent_class-parameterless-deprecated
* @var int
*/
public const DEPRECATE_GET_CLASS_WITHOUT_ARGS = PhpVersion::PHP_83;
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
}