• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.89
/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php
1
<?php
2
/**
3
 * Discourages the use of alias functions.
4
 *
5
 * Alias functions are kept in PHP for compatibility
6
 * with older versions. Can be used to forbid the use of any function.
7
 *
8
 * @author    Greg Sherwood <gsherwood@squiz.net>
9
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
10
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
11
 */
12

13
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
14

15
use PHP_CodeSniffer\Files\File;
16
use PHP_CodeSniffer\Sniffs\Sniff;
17
use PHP_CodeSniffer\Util\Tokens;
18

19
class ForbiddenFunctionsSniff implements Sniff
20
{
21

22
    /**
23
     * A list of forbidden functions with their alternatives.
24
     *
25
     * The value is NULL if no alternative exists. IE, the
26
     * function should just not be used.
27
     *
28
     * @var array<string, string|null>
29
     */
30
    public $forbiddenFunctions = [
31
        'sizeof' => 'count',
32
        'delete' => 'unset',
33
    ];
34

35
    /**
36
     * A cache of forbidden function names, for faster lookups.
37
     *
38
     * @var string[]
39
     */
40
    protected $forbiddenFunctionNames = [];
41

42
    /**
43
     * If true, forbidden functions will be considered regular expressions.
44
     *
45
     * @var boolean
46
     */
47
    protected $patternMatch = false;
48

49
    /**
50
     * If true, an error will be thrown; otherwise a warning.
51
     *
52
     * @var boolean
53
     */
54
    public $error = true;
55

56

57
    /**
58
     * Returns an array of tokens this test wants to listen for.
59
     *
60
     * @return array<int|string>
61
     */
62
    public function register()
3✔
63
    {
64
        // Everyone has had a chance to figure out what forbidden functions
65
        // they want to check for, so now we can cache out the list.
66
        $this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
3✔
67

68
        if ($this->patternMatch === true) {
3✔
69
            foreach ($this->forbiddenFunctionNames as $i => $name) {
×
70
                $this->forbiddenFunctionNames[$i] = '/'.$name.'/i';
×
71
            }
72

73
            return [
74
                T_STRING,
×
75
                T_NAME_FULLY_QUALIFIED,
×
76
            ];
77
        }
78

79
        // If we are not pattern matching, we need to work out what
80
        // tokens to listen for.
81
        $hasHaltCompiler = false;
3✔
82
        $string          = '<?php ';
3✔
83
        foreach ($this->forbiddenFunctionNames as $name) {
3✔
84
            if ($name === '__halt_compiler') {
3✔
85
                $hasHaltCompiler = true;
×
86
            } else {
87
                $string .= $name.'();';
3✔
88
            }
89
        }
90

91
        if ($hasHaltCompiler === true) {
3✔
92
            $string .= '__halt_compiler();';
×
93
        }
94

95
        $register = [];
3✔
96

97
        $tokens = token_get_all($string);
3✔
98
        array_shift($tokens);
3✔
99
        foreach ($tokens as $token) {
3✔
100
            if (is_array($token) === true) {
3✔
101
                $register[] = $token[0];
3✔
102
            }
103
        }
104

105
        $this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
3✔
106
        $this->forbiddenFunctions     = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);
3✔
107

108
        $targets   = array_unique($register);
3✔
109
        $targets[] = T_NAME_FULLY_QUALIFIED;
3✔
110

111
        return $targets;
3✔
112

113
    }//end register()
114

115

116
    /**
117
     * Processes this test, when one of its tokens is encountered.
118
     *
119
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
120
     * @param int                         $stackPtr  The position of the current token in
121
     *                                               the stack passed in $tokens.
122
     *
123
     * @return void
124
     */
125
    public function process(File $phpcsFile, $stackPtr)
3✔
126
    {
127
        $tokens = $phpcsFile->getTokens();
3✔
128

129
        $ignore = [
2✔
130
            T_DOUBLE_COLON             => true,
3✔
131
            T_OBJECT_OPERATOR          => true,
3✔
132
            T_NULLSAFE_OBJECT_OPERATOR => true,
3✔
133
            T_FUNCTION                 => true,
3✔
134
            T_CONST                    => true,
3✔
135
            T_PUBLIC                   => true,
3✔
136
            T_PRIVATE                  => true,
3✔
137
            T_PROTECTED                => true,
3✔
138
            T_AS                       => true,
3✔
139
            T_NEW                      => true,
3✔
140
            T_INSTEADOF                => true,
3✔
141
            T_IMPLEMENTS               => true,
3✔
142
        ];
2✔
143

144
        $prevToken = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);
3✔
145

146
        if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
3✔
147
            // Not a call to a PHP function.
148
            return;
3✔
149
        }
150

151
        $nextToken = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($stackPtr + 1), null, true);
3✔
152
        if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
3✔
153
            // Not a call to a PHP function.
154
            return;
3✔
155
        }
156

157
        if (($tokens[$stackPtr]['code'] === T_STRING || $tokens[$stackPtr]['code'] === T_NAME_FULLY_QUALIFIED)
3✔
158
            && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS
3✔
159
        ) {
160
            // Not a call to a PHP function.
161
            return;
3✔
162
        }
163

164
        if (empty($tokens[$stackPtr]['nested_attributes']) === false) {
3✔
165
            // Class instantiation in attribute, not function call.
166
            return;
3✔
167
        }
168

169
        $function = strtolower($tokens[$stackPtr]['content']);
3✔
170
        if ($tokens[$stackPtr]['code'] === T_NAME_FULLY_QUALIFIED) {
3✔
171
            $function = ltrim($function, '\\');
3✔
172
        }
173

174
        $pattern = null;
3✔
175
        if ($this->patternMatch === true) {
3✔
176
            $count   = 0;
×
177
            $pattern = preg_replace(
×
178
                $this->forbiddenFunctionNames,
×
179
                $this->forbiddenFunctionNames,
×
180
                $function,
×
181
                1,
×
182
                $count
×
183
            );
184

185
            if ($count === 0) {
×
186
                return;
×
187
            }
188

189
            // Remove the pattern delimiters and modifier.
190
            $pattern = substr($pattern, 1, -2);
×
191
        } else {
192
            if (in_array($function, $this->forbiddenFunctionNames, true) === false) {
3✔
193
                return;
3✔
194
            }
195
        }//end if
196

197
        $this->addError($phpcsFile, $stackPtr, $function, $pattern);
3✔
198

199
    }//end process()
1✔
200

201

202
    /**
203
     * Generates the error or warning for this sniff.
204
     *
205
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
206
     * @param int                         $stackPtr  The position of the forbidden function
207
     *                                               in the token array.
208
     * @param string                      $function  The name of the forbidden function.
209
     * @param string                      $pattern   The pattern used for the match.
210
     *
211
     * @return void
212
     */
213
    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
3✔
214
    {
215
        $data  = [$function];
3✔
216
        $error = 'The use of function %s() is ';
3✔
217
        if ($this->error === true) {
3✔
218
            $type   = 'Found';
3✔
219
            $error .= 'forbidden';
3✔
220
        } else {
221
            $type   = 'Discouraged';
×
222
            $error .= 'discouraged';
×
223
        }
224

225
        if ($pattern === null) {
3✔
226
            $pattern = strtolower($function);
3✔
227
        }
228

229
        if ($this->forbiddenFunctions[$pattern] !== null) {
3✔
230
            $type  .= 'WithAlternative';
3✔
231
            $data[] = $this->forbiddenFunctions[$pattern];
3✔
232
            $error .= '; use %s() instead';
3✔
233
        }
234

235
        if ($this->error === true) {
3✔
236
            $phpcsFile->addError($error, $stackPtr, $type, $data);
3✔
237
        } else {
238
            $phpcsFile->addWarning($error, $stackPtr, $type, $data);
×
239
        }
240

241
    }//end addError()
1✔
242

243

244
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc