Skip to content

Commit b7ae6e0

Browse files
Michal Kralondrejmirtes
authored andcommitted
Added: disallow usage of backtick operator
1 parent 1bdf378 commit b7ae6e0

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Check LSP even for static methods
2929
* Check missing typehint in anonymous function when a native one could be added
3030
* Require calling parent constructor
31+
* Disallow usage of backtick operator (`` $ls = `ls -la` ``)
3132

3233
Additional rules are coming in subsequent releases!
3334

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\DisallowedConstructs;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
8+
class DisallowedBacktickRule implements \PHPStan\Rules\Rule
9+
{
10+
11+
public function getNodeType(): string
12+
{
13+
return \PhpParser\Node\Expr\ShellExec::class;
14+
}
15+
16+
/**
17+
* @param \PhpParser\Node\Expr\Empty_ $node
18+
* @param \PHPStan\Analyser\Scope $scope
19+
* @return string[]
20+
*/
21+
public function processNode(Node $node, Scope $scope): array
22+
{
23+
return [
24+
'Backtick operator is not allowed. Use shell_exec() instead.',
25+
];
26+
}
27+
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\DisallowedConstructs;
4+
5+
use PHPStan\Rules\Rule;
6+
7+
class DisallowedBacktickRuleTest extends \PHPStan\Testing\RuleTestCase
8+
{
9+
10+
protected function getRule(): Rule
11+
{
12+
return new DisallowedBacktickRule();
13+
}
14+
15+
public function testRule(): void
16+
{
17+
$this->analyse([__DIR__ . '/data/backtick.php'], [
18+
[
19+
'Backtick operator is not allowed. Use shell_exec() instead.',
20+
3,
21+
],
22+
]);
23+
}
24+
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$ls = `ls -la`;

0 commit comments

Comments
 (0)