Skip to content

Commit 9405233

Browse files
committed
Report deprecated backtick operator in PHP 8.5
1 parent d0a77d1 commit 9405233

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

src/Php/PhpVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,9 @@ public function deprecatesNonStandardCasts(): bool
444444
return $this->versionId >= 80500;
445445
}
446446

447+
public function deprecatesBacktickOperator(): bool
448+
{
449+
return $this->versionId >= 80500;
450+
}
451+
447452
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Operators;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\ShellExec;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\DependencyInjection\RegisteredRule;
9+
use PHPStan\Php\PhpVersion;
10+
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleErrorBuilder;
12+
13+
/**
14+
* @implements Rule<ShellExec>
15+
*/
16+
#[RegisteredRule(level: 0)]
17+
final class BacktickRule implements Rule
18+
{
19+
20+
public function __construct(
21+
private PhpVersion $phpVersion,
22+
)
23+
{
24+
}
25+
26+
public function getNodeType(): string
27+
{
28+
return ShellExec::class;
29+
}
30+
31+
public function processNode(Node $node, Scope $scope): array
32+
{
33+
if (!$this->phpVersion->deprecatesBacktickOperator()) {
34+
return [];
35+
}
36+
37+
return [
38+
RuleErrorBuilder::message('Backtick operator is deprecated in PHP 8.5. Use shell_exec() function call instead.')
39+
->identifier('backtick.deprecated')
40+
->build(),
41+
];
42+
}
43+
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Operators;
4+
5+
use PHPStan\Php\PhpVersion;
6+
use PHPStan\Rules\Rule;
7+
use PHPStan\Testing\RuleTestCase;
8+
use const PHP_VERSION_ID;
9+
10+
/**
11+
* @extends RuleTestCase<BacktickRule>
12+
*/
13+
class BacktickRuleTest extends RuleTestCase
14+
{
15+
16+
protected function getRule(): Rule
17+
{
18+
return new BacktickRule(new PhpVersion(PHP_VERSION_ID));
19+
}
20+
21+
public function testRule(): void
22+
{
23+
$errors = [];
24+
if (PHP_VERSION_ID >= 80500) {
25+
$errors = [
26+
[
27+
'Backtick operator is deprecated in PHP 8.5. Use shell_exec() function call instead.',
28+
4,
29+
],
30+
];
31+
}
32+
$this->analyse([__DIR__ . '/data/backtick.php'], $errors);
33+
}
34+
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
function (): void {
4+
`echo "foo";`;
5+
};

0 commit comments

Comments
 (0)