1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs\Functions; |
6
|
|
|
|
7
|
|
|
use BestIt\CodeSniffer\File as FileDecorator; |
8
|
|
|
use BestIt\Sniffs\SuppressingTrait; |
9
|
|
|
use PHP_CodeSniffer\Files\File; |
10
|
|
|
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff as BaseSniff; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* You SHOULD not use eval, die, sizeof, delete ... |
14
|
|
|
* |
15
|
|
|
* @author blange <[email protected]> |
16
|
|
|
* @package BestIt\Sniffs\Functions |
17
|
|
|
*/ |
18
|
|
|
class ForbiddenFunctionsSniff extends BaseSniff |
19
|
|
|
{ |
20
|
|
|
use SuppressingTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* If true, an error will be thrown; otherwise a warning. |
24
|
|
|
* |
25
|
|
|
* @var boolean |
26
|
|
|
*/ |
27
|
|
|
public $error = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The used file. |
31
|
|
|
* |
32
|
|
|
* @var FileDecorator|void |
33
|
|
|
*/ |
34
|
|
|
protected $file; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* A list of forbidden functions with their alternatives. |
38
|
|
|
* |
39
|
|
|
* The value is NULL if no alternative exists. IE, the function should just not be used. |
40
|
|
|
* |
41
|
|
|
* @var array<string, string|null> |
42
|
|
|
*/ |
43
|
|
|
public $forbiddenFunctions = [ |
44
|
|
|
'eval' => null, |
45
|
|
|
'die' => 'exit', |
46
|
|
|
'sizeof' => 'count', |
47
|
|
|
'delete' => 'unset', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Position of the listened token. |
52
|
|
|
* |
53
|
|
|
* @var int|void |
54
|
|
|
*/ |
55
|
|
|
protected $stackPos; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Type-safe getter for the file. |
59
|
|
|
* |
60
|
|
|
* @return FileDecorator |
61
|
|
|
*/ |
62
|
|
|
protected function getFile(): FileDecorator |
63
|
|
|
{ |
64
|
|
|
return $this->file; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Type-safe getter for the stack position. |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
protected function getStackPos(): int |
73
|
|
|
{ |
74
|
|
|
return $this->stackPos; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Processes this test, when one of its tokens is encountered. |
79
|
|
|
* |
80
|
|
|
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint |
81
|
|
|
* |
82
|
|
|
* @param File $phpcsFile The file being scanned. |
83
|
|
|
* @param int $stackPtr The position of the current token in the stack passed in $tokens. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function process(File $phpcsFile, $stackPtr): void |
88
|
|
|
{ |
89
|
|
|
$this->file = new FileDecorator($phpcsFile); |
90
|
|
|
$this->stackPos = $stackPtr; |
91
|
|
|
|
92
|
|
|
if (!$this->isSniffSuppressed('Discouraged') && |
93
|
|
|
!$this->isSniffSuppressed('DiscouragedWithAlternative')) { |
94
|
|
|
parent::process($phpcsFile, $stackPtr); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|