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

PHPCSStandards / PHP_CodeSniffer / 15036337869

15 May 2025 04:03AM UTC coverage: 78.375% (-0.2%) from 78.556%
15036337869

Pull #856

github

web-flow
Merge 93f570b46 into f5e7943d0
Pull Request #856: [Doc] Cover all errors of PEAR ClassDeclaration

25112 of 32041 relevant lines covered (78.37%)

69.4 hits per line

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

82.65
/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 [T_STRING];
×
74
        }
75

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

88
        if ($hasHaltCompiler === true) {
3✔
89
            $string .= '__halt_compiler();';
×
90
        }
91

92
        $register = [];
3✔
93

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

102
        $this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
3✔
103
        $this->forbiddenFunctions     = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);
3✔
104

105
        return array_unique($register);
3✔
106

107
    }//end register()
108

109

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

123
        $ignore = [
1✔
124
            T_DOUBLE_COLON             => true,
3✔
125
            T_OBJECT_OPERATOR          => true,
3✔
126
            T_NULLSAFE_OBJECT_OPERATOR => true,
3✔
127
            T_FUNCTION                 => true,
3✔
128
            T_CONST                    => true,
3✔
129
            T_PUBLIC                   => true,
3✔
130
            T_PRIVATE                  => true,
3✔
131
            T_PROTECTED                => true,
3✔
132
            T_AS                       => true,
3✔
133
            T_NEW                      => true,
3✔
134
            T_INSTEADOF                => true,
3✔
135
            T_NS_SEPARATOR             => true,
3✔
136
            T_IMPLEMENTS               => true,
3✔
137
        ];
2✔
138

139
        $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
3✔
140

141
        // If function call is directly preceded by a NS_SEPARATOR it points to the
142
        // global namespace, so we should still catch it.
143
        if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR) {
3✔
144
            $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), null, true);
3✔
145
            if ($tokens[$prevToken]['code'] === T_STRING) {
3✔
146
                // Not in the global namespace.
147
                return;
3✔
148
            }
149
        }
1✔
150

151
        if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
3✔
152
            // Not a call to a PHP function.
153
            return;
3✔
154
        }
155

156
        $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
3✔
157
        if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
3✔
158
            // Not a call to a PHP function.
159
            return;
3✔
160
        }
161

162
        if ($tokens[$stackPtr]['code'] === T_STRING && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) {
3✔
163
            // Not a call to a PHP function.
164
            return;
3✔
165
        }
166

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

172
        $function = strtolower($tokens[$stackPtr]['content']);
3✔
173
        $pattern  = null;
3✔
174

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, $tokens[$stackPtr]['content'], $pattern);
3✔
198

199
    }//end process()
2✔
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 {
1✔
221
            $type   = 'Discouraged';
×
222
            $error .= 'discouraged';
×
223
        }
224

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

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

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

243
    }//end addError()
2✔
244

245

246
}//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

© 2026 Coveralls, Inc