Skip to content

Commit 1f028d5

Browse files
committed
[Php85] Add ShellExecFunctionCallOverBackticksRector
1 parent c410da9 commit 1f028d5

7 files changed

Lines changed: 150 additions & 2 deletions

File tree

config/set/php85.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Rector\Php85\Rector\FuncCall\ChrArgModuloRector;
1717
use Rector\Php85\Rector\FuncCall\OrdSingleByteRector;
1818
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
19+
use Rector\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector;
1920
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
2021
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
2122
use Rector\Removing\ValueObject\RemoveFuncCallArg;
@@ -43,6 +44,7 @@
4344
SleepToSerializeRector::class,
4445
OrdSingleByteRector::class,
4546
WakeupToUnserializeRector::class,
47+
ShellExecFunctionCallOverBackticksRector::class,
4648
]
4749
);
4850

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\Fixture;
4+
5+
class Fixture
6+
{
7+
public function run()
8+
{
9+
$output = `ls -al`;
10+
echo "<pre>$output</pre>";
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\Fixture;
19+
20+
class Fixture
21+
{
22+
public function run()
23+
{
24+
$output = shell_exec('ls -al');
25+
echo "<pre>$output</pre>";
26+
}
27+
}
28+
29+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\FuncCall\ShellExecFunctionCallOverBackticksRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ShellExecFunctionCallOverBackticksRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rule(ShellExecFunctionCallOverBackticksRector::class);
11+
12+
$rectorConfig->phpVersion(PhpVersion::PHP_85);
13+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Php85\Rector\ShellExec;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\ShellExec;
10+
use PhpParser\Node\InterpolatedStringPart;
11+
use PhpParser\Node\Scalar\String_;
12+
use Rector\Rector\AbstractRector;
13+
use Rector\ValueObject\PhpVersionFeature;
14+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
/**
19+
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_backticks_as_an_alias_for_shell_exec
20+
* @see \Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\ShellExecFunctionCallOverBackticksRectorTest
21+
*/
22+
final class ShellExecFunctionCallOverBackticksRector extends AbstractRector implements MinPhpVersionInterface
23+
{
24+
public function getRuleDefinition(): RuleDefinition
25+
{
26+
return new RuleDefinition(
27+
'Replace backticks based with shell_exec() function calls',
28+
[
29+
new CodeSample(
30+
<<<'CODE_SAMPLE'
31+
$output = `ls -al`;
32+
echo "<pre>$output</pre>";
33+
CODE_SAMPLE
34+
,
35+
<<<'CODE_SAMPLE'
36+
$output = shell_exec('ls -al');
37+
echo "<pre>$output</pre>";
38+
CODE_SAMPLE
39+
),
40+
]
41+
);
42+
}
43+
44+
public function getNodeTypes(): array
45+
{
46+
return [ShellExec::class];
47+
}
48+
49+
/**
50+
* @param ShellExec $node
51+
*/
52+
public function refactor(Node $node): Node
53+
{
54+
$args = array_map(function (Node $node): Node {
55+
if ($node instanceof InterpolatedStringPart) {
56+
return new Arg(new String_($node->value));
57+
}
58+
59+
return new Arg($node);
60+
}, $node->parts);
61+
62+
return $this->nodeFactory->createFuncCall('shell_exec', $args);
63+
}
64+
65+
public function provideMinPhpVersion(): int
66+
{
67+
return PhpVersionFeature::DEPRECATE_BACKTICKS;
68+
}
69+
}

rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ private function renameProperty(ClassLike $classLike, RenameProperty $renameProp
111111
$property->props[0]->name = new VarLikeIdentifier($newProperty);
112112
}
113113

114-
private function refactorPropertyFetch(PropertyFetch|StaticPropertyFetch $propertyFetch): null|PropertyFetch|StaticPropertyFetch
115-
{
114+
private function refactorPropertyFetch(
115+
PropertyFetch|StaticPropertyFetch $propertyFetch
116+
): null|PropertyFetch|StaticPropertyFetch {
116117
foreach ($this->renamedProperties as $renamedProperty) {
117118
$oldProperty = $renamedProperty->getOldProperty();
118119
if (! $this->isName($propertyFetch, $oldProperty)) {

src/ValueObject/PhpVersionFeature.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,4 +834,10 @@ final class PhpVersionFeature
834834
* @var int
835835
*/
836836
public const DEPRECATE_ORD_WITH_MULTIBYTE_STRING = PhpVersion::PHP_85;
837+
838+
/**
839+
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_backticks_as_an_alias_for_shell_exec
840+
* @var int
841+
*/
842+
public const DEPRECATE_BACKTICKS = PhpVersion::PHP_85;
837843
}

0 commit comments

Comments
 (0)