Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions config/set/php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\Php85\Rector\FuncCall\ChrArgModuloRector;
use Rector\Php85\Rector\FuncCall\OrdSingleByteRector;
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
use Rector\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector;
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
use Rector\Removing\ValueObject\RemoveFuncCallArg;
Expand Down Expand Up @@ -43,6 +44,7 @@
SleepToSerializeRector::class,
OrdSingleByteRector::class,
WakeupToUnserializeRector::class,
ShellExecFunctionCallOverBackticksRector::class,
]
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\Fixture;

class Fixture
{
public function run()
{
$output = `ls -al`;
echo "<pre>$output</pre>";
}
}

?>
-----
<?php

namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\Fixture;

class Fixture
{
public function run()
{
$output = shell_exec('ls -al');
echo "<pre>$output</pre>";
}
}

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

namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\Fixture;

class MultiVariables
{
public function run()
{
$a = 'X';
$b = 'Y';

$output = `echo $a ' and ' $b`;
echo $output;

$output = `echo $a and $b`;
echo $output;
}
}

?>
-----
<?php

namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\Fixture;

class MultiVariables
{
public function run()
{
$a = 'X';
$b = 'Y';

$output = shell_exec('echo ' . $a . ' \' and \' ' . $b);
echo $output;

$output = shell_exec('echo ' . $a . ' and ' . $b);
echo $output;
}
}

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

namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\Fixture;

class SkipEmptyBackticks
{
public function run()
{
``;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\Fixture;

class WithVariable
{
public function run()
{
$dir = __DIR__;
$var = 'example';

$output = `ls $dir`;
$output2 = `echo "value: $var"`;
echo "<pre>$output</pre>";
echo "<pre>$output2</pre>";
}
}

?>
-----
<?php

namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\Fixture;

class WithVariable
{
public function run()
{
$dir = __DIR__;
$var = 'example';

$output = shell_exec('ls ' . $dir);
$output2 = shell_exec('echo "value: ' . $var . '"');
echo "<pre>$output</pre>";
echo "<pre>$output2</pre>";
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector;

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

final class ShellExecFunctionCallOverBackticksRectorTest 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\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector;
use Rector\ValueObject\PhpVersion;

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

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

declare(strict_types=1);

namespace Rector\Php85\Rector\ShellExec;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\ShellExec;
use PhpParser\Node\InterpolatedStringPart;
use PhpParser\Node\Scalar\String_;
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://wiki.php.net/rfc/deprecations_php_8_5#deprecate_backticks_as_an_alias_for_shell_exec
* @see \Rector\Tests\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector\ShellExecFunctionCallOverBackticksRectorTest
*/
final class ShellExecFunctionCallOverBackticksRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace backticks based with shell_exec() function calls',
[
new CodeSample(
<<<'CODE_SAMPLE'
$output = `ls -al`;
echo "<pre>$output</pre>";
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$output = shell_exec('ls -al');
echo "<pre>$output</pre>";
CODE_SAMPLE
),
]
);
}

public function getNodeTypes(): array
{
return [ShellExec::class];
}

/**
* @param ShellExec $node
*/
public function refactor(Node $node): ?Node
{
if ($node->parts === []) {
return null;
}

$exprs = [];
foreach ($node->parts as $part) {
if ($part instanceof InterpolatedStringPart) {
$exprs[] = new String_($part->value);
continue;
}

// other parts are Expr (variables, function calls, etc.)
// keep them as-is so they are concatenated
$exprs[] = $part;
}

// reduce to single concatenated expression
$argExpr = array_shift($exprs);
foreach ($exprs as $expr) {
$argExpr = new Concat($argExpr, $expr);
}

// create single Arg and call shell_exec
return $this->nodeFactory->createFuncCall('shell_exec', [new Arg($argExpr)]);
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATE_BACKTICKS;
}
}
5 changes: 3 additions & 2 deletions rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ private function renameProperty(ClassLike $classLike, RenameProperty $renameProp
$property->props[0]->name = new VarLikeIdentifier($newProperty);
}

private function refactorPropertyFetch(PropertyFetch|StaticPropertyFetch $propertyFetch): null|PropertyFetch|StaticPropertyFetch
{
private function refactorPropertyFetch(
PropertyFetch|StaticPropertyFetch $propertyFetch
): null|PropertyFetch|StaticPropertyFetch {
foreach ($this->renamedProperties as $renamedProperty) {
$oldProperty = $renamedProperty->getOldProperty();
if (! $this->isName($propertyFetch, $oldProperty)) {
Expand Down
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,4 +834,10 @@ final class PhpVersionFeature
* @var int
*/
public const DEPRECATE_ORD_WITH_MULTIBYTE_STRING = PhpVersion::PHP_85;

/**
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_backticks_as_an_alias_for_shell_exec
* @var int
*/
public const DEPRECATE_BACKTICKS = PhpVersion::PHP_85;
}