Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 89c7bf9

Browse files
BacktickRule made auto-fixable
1 parent 9405233 commit 89c7bf9

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

‎src/Rules/Operators/BacktickRule.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Php\PhpVersion;
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
12+
use PHPStan\ShouldNotHappenException;
1213

1314
/**
1415
* @implements Rule<ShellExec>
@@ -34,9 +35,32 @@ public function processNode(Node $node, Scope $scope): array
3435
return [];
3536
}
3637

38+
$argExpr = null;
39+
foreach ($node->parts as $part) {
40+
if ($part instanceof Node\InterpolatedStringPart) {
41+
$expr = new Node\Scalar\String_($part->value);
42+
} else {
43+
$expr = $part;
44+
}
45+
46+
if ($argExpr === null) {
47+
$argExpr = $expr;
48+
continue;
49+
}
50+
51+
$argExpr = new Node\Expr\BinaryOp\Concat($argExpr, $expr);
52+
}
53+
54+
if ($argExpr === null) {
55+
throw new ShouldNotHappenException();
56+
}
57+
3758
return [
3859
RuleErrorBuilder::message('Backtick operator is deprecated in PHP 8.5. Use shell_exec() function call instead.')
3960
->identifier('backtick.deprecated')
61+
->fixNode($node, static fn () => new Node\Expr\FuncCall(new Node\Name('shell_exec'), [
62+
new Node\Arg($argExpr),
63+
]))
4064
->build(),
4165
];
4266
}

‎tests/PHPStan/Rules/Operators/BacktickRuleTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Php\PhpVersion;
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
8+
use PHPUnit\Framework\Attributes\RequiresPhp;
89
use const PHP_VERSION_ID;
910

1011
/**
@@ -27,9 +28,23 @@ public function testRule(): void
2728
'Backtick operator is deprecated in PHP 8.5. Use shell_exec() function call instead.',
2829
4,
2930
],
31+
[
32+
'Backtick operator is deprecated in PHP 8.5. Use shell_exec() function call instead.',
33+
5,
34+
],
35+
[
36+
'Backtick operator is deprecated in PHP 8.5. Use shell_exec() function call instead.',
37+
6,
38+
],
3039
];
3140
}
3241
$this->analyse([__DIR__ . '/data/backtick.php'], $errors);
3342
}
3443

44+
#[RequiresPhp('>= 8.5')]
45+
public function testFix(): void
46+
{
47+
$this->fix(__DIR__ . '/data/backtick.php', __DIR__ . '/data/backtick.php.fixed');
48+
}
49+
3550
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3-
function (): void {
3+
function (string$s, string$t): void {
44
`echo "foo";`;
5+
`$s$a`;
6+
`$s$a foo $t`;
57
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
function (string $s, string $t): void {
4+
shell_exec('echo "foo";');
5+
shell_exec($s . ' ' . $a);
6+
shell_exec($s . ' ' . $a . ' foo ' . $t);
7+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /