Completed
Push — master ( b16976...401561 )
by Juliette
10s
created

Sniff.php (11 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * PHPCompatibility_Sniff.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 * @copyright 2014 Cu.be Solutions bvba
11
 */
12
13
/**
14
 * PHPCompatibility_Sniff.
15
 *
16
 * @category  PHP
17
 * @package   PHPCompatibility
18
 * @author    Wim Godden <[email protected]>
19
 * @version   1.1.0
20
 * @copyright 2014 Cu.be Solutions bvba
21
 */
22
abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
23
{
24
25
    const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`';
26
27
    /**
28
     * List of functions using hash algorithm as parameter (always the first parameter).
29
     *
30
     * Used by the new/removed hash algorithm sniffs.
31
     * Key is the function name, value is the 1-based parameter position in the function call.
32
     *
33
     * @var array
34
     */
35
    protected $hashAlgoFunctions = array(
36
        'hash_file'      => 1,
37
        'hash_hmac_file' => 1,
38
        'hash_hmac'      => 1,
39
        'hash_init'      => 1,
40
        'hash_pbkdf2'    => 1,
41
        'hash'           => 1,
42
    );
43
44
45
    /**
46
     * List of functions which take an ini directive as parameter (always the first parameter).
47
     *
48
     * Used by the new/removed ini directives sniffs.
49
     * Key is the function name, value is the 1-based parameter position in the function call.
50
     *
51
     * @var array
52
     */
53
    protected $iniFunctions = array(
54
        'ini_get' => 1,
55
        'ini_set' => 1,
56
    );
57
58
59
/* The testVersion configuration variable may be in any of the following formats:
60
 * 1) Omitted/empty, in which case no version is specified.  This effectively
61
 *    disables all the checks provided by this standard.
62
 * 2) A single PHP version number, e.g. "5.4" in which case the standard checks that
63
 *    the code will run on that version of PHP (no deprecated features or newer
64
 *    features being used).
65
 * 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run
66
 *    on all PHP versions in that range, and that it doesn't use any features that
67
 *    were deprecated by the final version in the list, or which were not available
68
 *    for the first version in the list.
69
 * PHP version numbers should always be in Major.Minor format.  Both "5", "5.3.2"
70
 * would be treated as invalid, and ignored.
71
 * This standard doesn't support checking against PHP4, so the minimum version that
72
 * is recognised is "5.0".
73
 */
74
75
    private function getTestVersion()
76
    {
77
        /**
78
         * var $arrTestVersions will hold an array containing min/max version of PHP
79
         *   that we are checking against (see above).  If only a single version
80
         *   number is specified, then this is used as both the min and max.
81
         */
82
        static $arrTestVersions = array();
83
84
        $testVersion = trim(PHP_CodeSniffer::getConfigData('testVersion'));
85
86
        if (!isset($arrTestVersions[$testVersion]) && !empty($testVersion)) {
87
88
            $arrTestVersions[$testVersion] = array(null, null);
89
            if (preg_match('/^\d+\.\d+$/', $testVersion)) {
90
                $arrTestVersions[$testVersion] = array($testVersion, $testVersion);
91
            }
92
            elseif (preg_match('/^(\d+\.\d+)\s*-\s*(\d+\.\d+)$/', $testVersion,
93
                               $matches))
94
            {
95
                if (version_compare($matches[1], $matches[2], '>')) {
96
                    trigger_error("Invalid range in testVersion setting: '"
97
                                  . $testVersion . "'", E_USER_WARNING);
98
                }
99
                else {
100
                    $arrTestVersions[$testVersion] = array($matches[1], $matches[2]);
101
                }
102
            }
103
            elseif (!$testVersion == '') {
104
                trigger_error("Invalid testVersion setting: '" . $testVersion
105
                              . "'", E_USER_WARNING);
106
            }
107
        }
108
109
        if (isset($arrTestVersions[$testVersion])) {
110
            return $arrTestVersions[$testVersion];
111
        }
112
        else {
113
            return array(null, null);
114
        }
115
    }
116
117 View Code Duplication
    public function supportsAbove($phpVersion)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        $testVersion = $this->getTestVersion();
120
        $testVersion = $testVersion[1];
121
122
        if (is_null($testVersion)
123
            || version_compare($testVersion, $phpVersion) >= 0
124
        ) {
125
            return true;
126
        } else {
127
            return false;
128
        }
129
    }//end supportsAbove()
130
131 View Code Duplication
    public function supportsBelow($phpVersion)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $testVersion = $this->getTestVersion();
134
        $testVersion = $testVersion[0];
135
136
        if (!is_null($testVersion)
137
            && version_compare($testVersion, $phpVersion) <= 0
138
        ) {
139
            return true;
140
        } else {
141
            return false;
142
        }
143
    }//end supportsBelow()
144
145
146
    /**
147
     * Add a PHPCS message to the output stack as either a warning or an error.
148
     *
149
     * @param PHP_CodeSniffer_File $phpcsFile The file the message applies to.
150
     * @param string               $message   The message.
151
     * @param int                  $stackPtr  The position of the token
152
     *                                        the message relates to.
153
     * @param bool                 $isError   Whether to report the message as an
154
     *                                        'error' or 'warning'.
155
     *                                        Defaults to true (error).
156
     * @param string               $code      The error code for the message.
157
     *                                        Defaults to 'Found'.
158
     * @param array                $data      Optional input for the data replacements.
159
     *
160
     * @return void
161
     */
162
    public function addMessage($phpcsFile, $message, $stackPtr, $isError, $code = 'Found', $data = array())
163
    {
164
        if ($isError === true) {
165
            $phpcsFile->addError($message, $stackPtr, $code, $data);
166
        } else {
167
            $phpcsFile->addWarning($message, $stackPtr, $code, $data);
168
        }
169
    }
170
171
172
    /**
173
     * Convert an arbitrary string to an alphanumeric string with underscores.
174
     *
175
     * Pre-empt issues with arbitrary strings being used as error codes in XML and PHP.
176
     *
177
     * @param string $baseString Arbitrary string.
178
     *
179
     * @return string
180
     */
181
    public function stringToErrorCode($baseString)
182
    {
183
        return preg_replace('`[^a-z0-9_]`i', '_', strtolower($baseString));
184
    }
185
186
187
    /**
188
     * Strip quotes surrounding an arbitrary string.
189
     *
190
     * Intended for use with the content of a T_CONSTANT_ENCAPSED_STRING / T_DOUBLE_QUOTED_STRING.
191
     *
192
     * @param string $string The raw string.
193
     *
194
     * @return string String without quotes around it.
195
     */
196
    public function stripQuotes($string) {
197
        return preg_replace('`^([\'"])(.*)\1$`Ds', '$2', $string);
198
    }
199
200
201
    /**
202
     * Strip variables from an arbitrary double quoted string.
203
     *
204
     * Intended for use with the content of a T_DOUBLE_QUOTED_STRING.
205
     *
206
     * @param string $string The raw string.
207
     *
208
     * @return string String without variables in it.
209
     */
210
    public function stripVariables($string) {
211
        if (strpos($string, '$') === false) {
212
            return $string;
213
        }
214
215
        return preg_replace( self::REGEX_COMPLEX_VARS, '', $string );
216
    }
217
218
219
    /**
220
     * Make all top level array keys in an array lowercase.
221
     *
222
     * @param array $array Initial array.
223
     *
224
     * @return array Same array, but with all lowercase top level keys.
225
     */
226
    public function arrayKeysToLowercase($array)
227
    {
228
        $keys = array_keys($array);
229
        $keys = array_map('strtolower', $keys);
230
        return array_combine($keys, $array);
231
    }
232
233
234
    /**
235
     * Returns the name(s) of the interface(s) that the specified class implements.
236
     *
237
     * Returns FALSE on error or if there are no implemented interface names.
238
     *
239
     * {@internal Duplicate of same method as introduced in PHPCS 2.7.
240
     * Once the minimum supported PHPCS version for this sniff library goes beyond
241
     * that, this method can be removed and call to it replaced with
242
     * `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}}
243
     *
244
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
245
     * @param int                  $stackPtr  The position of the class token.
246
     *
247
     * @return array|false
248
     */
249
    public function findImplementedInterfaceNames(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
250
    {
251
        if (method_exists($phpcsFile, 'findImplementedInterfaceNames')) {
252
            return $phpcsFile->findImplementedInterfaceNames($stackPtr);
253
        }
254
255
        $tokens = $phpcsFile->getTokens();
256
257
        // Check for the existence of the token.
258
        if (isset($tokens[$stackPtr]) === false) {
259
            return false;
260
        }
261
262
        if ($tokens[$stackPtr]['code'] !== T_CLASS) {
263
            return false;
264
        }
265
266
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
267
            return false;
268
        }
269
270
        $classOpenerIndex = $tokens[$stackPtr]['scope_opener'];
271
        $implementsIndex  = $phpcsFile->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex);
272
        if ($implementsIndex === false) {
273
            return false;
274
        }
275
276
        $find = array(
277
                 T_NS_SEPARATOR,
278
                 T_STRING,
279
                 T_WHITESPACE,
280
                 T_COMMA,
281
                );
282
283
        $end  = $phpcsFile->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true);
284
        $name = $phpcsFile->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1));
285
        $name = trim($name);
286
287
        if ($name === '') {
288
            return false;
289
        } else {
290
            $names = explode(',', $name);
291
            $names = array_map('trim', $names);
292
            return $names;
293
        }
294
295
    }//end findImplementedInterfaceNames()
296
297
298
    /**
299
     * Checks if a function call has parameters.
300
     *
301
     * Expects to be passed the T_STRING stack pointer for the function call.
302
     * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.
303
     *
304
     * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer, it
305
     * will detect whether the array has values or is empty.
306
     *
307
     * @link https://github.com/wimg/PHPCompatibility/issues/120
308
     * @link https://github.com/wimg/PHPCompatibility/issues/152
309
     *
310
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
311
     * @param int                  $stackPtr  The position of the function call token.
312
     *
313
     * @return bool
314
     */
315
    public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
316
    {
317
        $tokens = $phpcsFile->getTokens();
318
319
        // Check for the existence of the token.
320
        if (isset($tokens[$stackPtr]) === false) {
321
            return false;
322
        }
323
324
        // Is this one of the tokens this function handles ?
325
        if (in_array($tokens[$stackPtr]['code'], array(T_STRING, T_ARRAY, T_OPEN_SHORT_ARRAY), true) === false) {
326
            return false;
327
        }
328
329
        $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
330
331
        // Deal with short array syntax.
332
        if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) {
333
            if (isset($tokens[$stackPtr]['bracket_closer']) === false) {
334
                return false;
335
            }
336
337
            if ($nextNonEmpty === $tokens[$stackPtr]['bracket_closer']) {
338
                // No parameters.
339
                return false;
340
            }
341
            else {
342
                return true;
343
            }
344
        }
345
346
        // Deal with function calls & long arrays.
347
        // Next non-empty token should be the open parenthesis.
348
        if ($nextNonEmpty === false && $tokens[$nextNonEmpty]['code'] !== T_OPEN_PARENTHESIS) {
349
            return false;
350
        }
351
352
        if (isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false) {
353
            return false;
354
        }
355
356
        $closeParenthesis = $tokens[$nextNonEmpty]['parenthesis_closer'];
357
        $nextNextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $nextNonEmpty + 1, $closeParenthesis + 1, true);
358
359
        if ($nextNextNonEmpty === $closeParenthesis) {
360
            // No parameters.
361
            return false;
362
        }
363
364
        return true;
365
    }
366
367
368
    /**
369
     * Count the number of parameters a function call has been passed.
370
     *
371
     * Expects to be passed the T_STRING stack pointer for the function call.
372
     * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.
373
     *
374
     * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer,
375
     * it will return the number of values in the array.
376
     *
377
     * @link https://github.com/wimg/PHPCompatibility/issues/111
378
     * @link https://github.com/wimg/PHPCompatibility/issues/114
379
     * @link https://github.com/wimg/PHPCompatibility/issues/151
380
     *
381
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
382
     * @param int                  $stackPtr  The position of the function call token.
383
     *
384
     * @return int
385
     */
386
    public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
387
    {
388
        if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) {
389
            return 0;
390
        }
391
392
        return count($this->getFunctionCallParameters($phpcsFile, $stackPtr));
393
    }
394
395
396
    /**
397
     * Get information on all parameters passed to a function call.
398
     *
399
     * Expects to be passed the T_STRING stack pointer for the function call.
400
     * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.
401
     *
402
     * Will return an multi-dimentional array with the start token pointer, end token
403
     * pointer and raw parameter value for all parameters. Index will be 1-based.
404
     * If no parameters are found, will return an empty array.
405
     *
406
     * Extra feature: If passed an T_ARRAY or T_OPEN_SHORT_ARRAY stack pointer,
407
     * it will tokenize the values / key/value pairs contained in the array call.
408
     *
409
     * @param PHP_CodeSniffer_File $phpcsFile     The file being scanned.
410
     * @param int                  $stackPtr      The position of the function call token.
411
     *
412
     * @return array
413
     */
414
    public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
415
    {
416
        if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) {
417
            return array();
418
        }
419
420
        // Ok, we know we have a T_STRING, T_ARRAY or T_OPEN_SHORT_ARRAY with parameters
421
        // and valid open & close brackets/parenthesis.
422
        $tokens = $phpcsFile->getTokens();
423
424
        // Mark the beginning and end tokens.
425
        if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) {
426
            $opener = $stackPtr;
427
            $closer = $tokens[$stackPtr]['bracket_closer'];
428
429
            $nestedParenthesisCount = 0;
430
        }
431
        else {
432
            $opener = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
433
            $closer = $tokens[$opener]['parenthesis_closer'];
434
435
            $nestedParenthesisCount = 1;
436
        }
437
438
        // Which nesting level is the one we are interested in ?
439 View Code Duplication
        if (isset($tokens[$opener]['nested_parenthesis'])) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
440
            $nestedParenthesisCount += count($tokens[$opener]['nested_parenthesis']);
441
        }
442
443
        $parameters = array();
444
        $nextComma  = $opener;
445
        $paramStart = $opener + 1;
446
        $cnt        = 1;
447
        while ($nextComma = $phpcsFile->findNext(array(T_COMMA, $tokens[$closer]['code'], T_OPEN_SHORT_ARRAY), $nextComma + 1, $closer + 1)) {
448
            // Ignore anything within short array definition brackets.
449
            if (
450
                $tokens[$nextComma]['type'] === 'T_OPEN_SHORT_ARRAY'
451
                &&
452
                ( isset($tokens[$nextComma]['bracket_opener']) && $tokens[$nextComma]['bracket_opener'] === $nextComma )
453
                &&
454
                isset($tokens[$nextComma]['bracket_closer'])
455
            ) {
456
                // Skip forward to the end of the short array definition.
457
                $nextComma = $tokens[$nextComma]['bracket_closer'];
458
                continue;
459
            }
460
461
            // Ignore comma's at a lower nesting level.
462
            if (
463
                $tokens[$nextComma]['type'] === 'T_COMMA'
464
                &&
465
                isset($tokens[$nextComma]['nested_parenthesis'])
466
                &&
467
                count($tokens[$nextComma]['nested_parenthesis']) !== $nestedParenthesisCount
468
            ) {
469
                continue;
470
            }
471
472
            // Ignore closing parenthesis/bracket if not 'ours'.
473
            if ($tokens[$nextComma]['type'] === $tokens[$closer]['type'] && $nextComma !== $closer) {
474
                continue;
475
            }
476
477
            // Ok, we've reached the end of the parameter.
478
            $parameters[$cnt]['start'] = $paramStart;
479
            $parameters[$cnt]['end']   = $nextComma - 1;
480
            $parameters[$cnt]['raw']   = trim($phpcsFile->getTokensAsString($paramStart, ($nextComma - $paramStart)));
481
482
            // Check if there are more tokens before the closing parenthesis.
483
            // Prevents code like the following from setting a third parameter:
484
            // functionCall( $param1, $param2, );
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
485
            $hasNextParam = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $nextComma + 1, $closer, true, null, true);
486
            if ($hasNextParam === false) {
487
                break;
488
            }
489
490
            // Prepare for the next parameter.
491
            $paramStart = $nextComma + 1;
492
            $cnt++;
493
        }
494
495
        return $parameters;
496
    }
497
498
499
    /**
500
     * Get information on a specific parameter passed to a function call.
501
     *
502
     * Expects to be passed the T_STRING stack pointer for the function call.
503
     * If passed a T_STRING which is *not* a function call, the behaviour is unreliable.
504
     *
505
     * Will return a array with the start token pointer, end token pointer and the raw value
506
     * of the parameter at a specific offset.
507
     * If the specified parameter is not found, will return false.
508
     *
509
     * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
510
     * @param int                  $stackPtr    The position of the function call token.
511
     * @param int                  $paramOffset The 1-based index position of the parameter to retrieve.
512
     *
513
     * @return array|false
514
     */
515
    public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset)
516
    {
517
        $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr);
518
519
        if (isset($parameters[$paramOffset]) === false) {
520
            return false;
521
        }
522
        else {
523
            return $parameters[$paramOffset];
524
        }
525
    }
526
527
528
    /**
529
     * Verify whether a token is within a scoped condition.
530
     *
531
     * If the optional $validScopes parameter has been passed, the function
532
     * will check that the token has at least one condition which is of a
533
     * type defined in $validScopes.
534
     *
535
     * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
536
     * @param int                  $stackPtr    The position of the token.
537
     * @param array|int            $validScopes Optional. Array of valid scopes
538
     *                                          or int value of a valid scope.
539
     *                                          Pass the T_.. constant(s) for the
540
     *                                          desired scope to this parameter.
541
     *
542
     * @return bool Without the optional $scopeTypes: True if within a scope, false otherwise.
543
     *              If the $scopeTypes are set: True if *one* of the conditions is a
544
     *              valid scope, false otherwise.
545
     */
546
    public function tokenHasScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $validScopes = null)
547
    {
548
        $tokens = $phpcsFile->getTokens();
549
550
        // Check for the existence of the token.
551
        if (isset($tokens[$stackPtr]) === false) {
552
            return false;
553
        }
554
555
        // No conditions = no scope.
556
        if (empty($tokens[$stackPtr]['conditions'])) {
557
            return false;
558
        }
559
560
        // Ok, there are conditions, do we have to check for specific ones ?
561
        if (isset($validScopes) === false) {
562
            return true;
563
        }
564
565
        return $phpcsFile->hasCondition($stackPtr, $validScopes);
566
    }
567
568
569
    /**
570
     * Verify whether a token is within a class scope.
571
     *
572
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
573
     * @param int                  $stackPtr  The position of the token.
574
     * @param bool                 $strict    Whether to strictly check for the T_CLASS
575
     *                                        scope or also accept interfaces and traits
576
     *                                        as scope.
577
     *
578
     * @return bool True if within class scope, false otherwise.
579
     */
580
    public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true)
581
    {
582
        $validScopes = array(T_CLASS);
583
        if (defined('T_ANON_CLASS') === true) {
584
            $validScopes[] = T_ANON_CLASS;
585
        }
586
587
        if ($strict === false) {
588
            $validScopes[] = T_INTERFACE;
589
            $validScopes[] = T_TRAIT;
590
        }
591
592
        return $phpcsFile->hasCondition($stackPtr, $validScopes);
593
    }
594
595
596
    /**
597
     * Verify whether a token is within a scoped use statement.
598
     *
599
     * PHPCS cross-version compatibility method.
600
     *
601
     * In PHPCS 1.x no conditions are set for a scoped use statement.
602
     * This method works around that limitation.
603
     *
604
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
605
     * @param int                  $stackPtr  The position of the token.
606
     *
607
     * @return bool True if within use scope, false otherwise.
608
     */
609
    public function inUseScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
610
    {
611
        static $isLowPHPCS, $ignoreTokens;
612
613
        if (isset($isLowPHPCS) === false) {
614
            $isLowPHPCS = version_compare(PHP_CodeSniffer::VERSION, '2.0', '<');
615
        }
616
        if (isset($ignoreTokens) === false) {
617
            $ignoreTokens              = PHP_CodeSniffer_Tokens::$emptyTokens;
618
            $ignoreTokens[T_STRING]    = T_STRING;
619
            $ignoreTokens[T_AS]        = T_AS;
620
            $ignoreTokens[T_PUBLIC]    = T_PUBLIC;
621
            $ignoreTokens[T_PROTECTED] = T_PROTECTED;
622
            $ignoreTokens[T_PRIVATE]   = T_PRIVATE;
623
        }
624
625
        // PHPCS 2.0.
626
        if ($isLowPHPCS === false) {
627
            return $phpcsFile->hasCondition($stackPtr, T_USE);
628
        } else {
629
            // PHPCS 1.x.
630
            $tokens         = $phpcsFile->getTokens();
631
            $maybeCurlyOpen = $phpcsFile->findPrevious($ignoreTokens, ($stackPtr - 1), null, true);
632
            if ($tokens[$maybeCurlyOpen]['code'] === T_OPEN_CURLY_BRACKET) {
633
                $maybeUseStatement = $phpcsFile->findPrevious($ignoreTokens, ($maybeCurlyOpen - 1), null, true);
634
                if ($tokens[$maybeUseStatement]['code'] === T_USE) {
635
                    return true;
636
                }
637
            }
638
            return false;
639
        }
640
    }
641
642
643
    /**
644
     * Returns the fully qualified class name for a new class instantiation.
645
     *
646
     * Returns an empty string if the class name could not be reliably inferred.
647
     *
648
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
649
     * @param int                  $stackPtr  The position of a T_NEW token.
650
     *
651
     * @return string
652
     */
653
    public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
654
    {
655
        $tokens = $phpcsFile->getTokens();
656
657
        // Check for the existence of the token.
658
        if (isset($tokens[$stackPtr]) === false) {
659
            return '';
660
        }
661
662
        if ($tokens[$stackPtr]['code'] !== T_NEW) {
663
            return '';
664
        }
665
666
        $find = array(
667
                 T_NS_SEPARATOR,
668
                 T_STRING,
669
                 T_NAMESPACE,
670
                 T_WHITESPACE,
671
                );
672
673
        $start = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
674
        // Bow out if the next token is a variable as we don't know where it was defined.
675
        if ($tokens[$start]['code'] === T_VARIABLE) {
676
            return '';
677
        }
678
679
        $end       = $phpcsFile->findNext($find, ($start + 1), null, true, null, true);
680
        $className = $phpcsFile->getTokensAsString($start, ($end - $start));
681
        $className = trim($className);
682
683
        return $this->getFQName($phpcsFile, $stackPtr, $className);
684
    }
685
686
687
    /**
688
     * Returns the fully qualified name of the class that the specified class extends.
689
     *
690
     * Returns an empty string if the class does not extend another class or if
691
     * the class name could not be reliably inferred.
692
     *
693
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
694
     * @param int                  $stackPtr  The position of a T_CLASS token.
695
     *
696
     * @return string
697
     */
698
    public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
699
    {
700
        $tokens = $phpcsFile->getTokens();
701
702
        // Check for the existence of the token.
703
        if (isset($tokens[$stackPtr]) === false) {
704
            return '';
705
        }
706
707
        if ($tokens[$stackPtr]['code'] !== T_CLASS) {
708
            return '';
709
        }
710
711
        $extends = $phpcsFile->findExtendedClassName($stackPtr);
712
        if (empty($extends) || is_string($extends) === false) {
713
            return '';
714
        }
715
716
        return $this->getFQName($phpcsFile, $stackPtr, $extends);
717
    }
718
719
720
    /**
721
     * Returns the class name for the static usage of a class.
722
     * This can be a call to a method, the use of a property or constant.
723
     *
724
     * Returns an empty string if the class name could not be reliably inferred.
725
     *
726
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
727
     * @param int                  $stackPtr  The position of a T_NEW token.
728
     *
729
     * @return string
730
     */
731
    public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
732
    {
733
        $tokens = $phpcsFile->getTokens();
734
735
        // Check for the existence of the token.
736
        if (isset($tokens[$stackPtr]) === false) {
737
            return '';
738
        }
739
740
        if ($tokens[$stackPtr]['code'] !== T_DOUBLE_COLON) {
741
            return '';
742
        }
743
744
        // Nothing to do if previous token is a variable as we don't know where it was defined.
745
        if ($tokens[$stackPtr - 1]['code'] === T_VARIABLE) {
746
            return '';
747
        }
748
749
        // Nothing to do if 'parent' or 'static' as we don't know how far the class tree extends.
750
        if (in_array($tokens[$stackPtr - 1]['code'], array(T_PARENT, T_STATIC), true)) {
751
            return '';
752
        }
753
754
        // Get the classname from the class declaration if self is used.
755
        if ($tokens[$stackPtr - 1]['code'] === T_SELF) {
756
            $classDeclarationPtr = $phpcsFile->findPrevious(T_CLASS, $stackPtr - 1);
757
            if ($classDeclarationPtr === false) {
758
                return '';
759
            }
760
            $className = $phpcsFile->getDeclarationName($classDeclarationPtr);
761
            return $this->getFQName($phpcsFile, $classDeclarationPtr, $className);
762
        }
763
764
        $find = array(
765
                 T_NS_SEPARATOR,
766
                 T_STRING,
767
                 T_NAMESPACE,
768
                 T_WHITESPACE,
769
                );
770
771
        $start     = ($phpcsFile->findPrevious($find, $stackPtr - 1, null, true, null, true) + 1);
772
        $className = $phpcsFile->getTokensAsString($start, ($stackPtr - $start));
773
        $className = trim($className);
774
775
        return $this->getFQName($phpcsFile, $stackPtr, $className);
776
    }
777
778
779
    /**
780
     * Get the Fully Qualified name for a class/function/constant etc.
781
     *
782
     * Checks if a class/function/constant name is already fully qualified and
783
     * if not, enrich it with the relevant namespace information.
784
     *
785
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
786
     * @param int                  $stackPtr  The position of the token.
787
     * @param string               $name      The class / function / constant name.
788
     *
789
     * @return string
790
     */
791
    public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name)
792
    {
793
        if (strpos($name, '\\' ) === 0) {
794
            // Already fully qualified.
795
            return $name;
796
        }
797
798
        // Remove the namespace keyword if used.
799
        if (strpos($name, 'namespace\\') === 0) {
800
            $name = substr($name, 10);
801
        }
802
803
        $namespace = $this->determineNamespace($phpcsFile, $stackPtr);
804
805
        if ($namespace === '') {
806
            return '\\' . $name;
807
        }
808
        else {
809
            return '\\' . $namespace . '\\' . $name;
810
        }
811
    }
812
813
814
    /**
815
     * Is the class/function/constant name namespaced or global ?
816
     *
817
     * @param string $FQName Fully Qualified name of a class, function etc.
818
     *                       I.e. should always start with a `\` !
819
     *
820
     * @return bool True if namespaced, false if global.
821
     */
822
    public function isNamespaced($FQName) {
823
        if (strpos($FQName, '\\') !== 0) {
824
            throw new PHP_CodeSniffer_Exception('$FQName must be a fully qualified name');
825
        }
826
827
        return (strpos(substr($FQName, 1), '\\') !== false);
828
    }
829
830
831
    /**
832
     * Determine the namespace name an arbitrary token lives in.
833
     *
834
     * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.
835
     * @param int                  $stackPtr  The token position for which to determine the namespace.
836
     *
837
     * @return string Namespace name or empty string if it couldn't be determined or no namespace applies.
838
     */
839
    public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
840
    {
841
        $tokens = $phpcsFile->getTokens();
842
843
        // Check for the existence of the token.
844
        if (isset($tokens[$stackPtr]) === false) {
845
            return '';
846
        }
847
848
        // Check for scoped namespace {}.
849
        if (empty($tokens[$stackPtr]['conditions']) === false) {
850
            foreach ($tokens[$stackPtr]['conditions'] as $pointer => $type) {
851
                if ($type === T_NAMESPACE) {
852
                    $namespace = $this->getDeclaredNamespaceName($phpcsFile, $pointer);
853
                    if ($namespace !== false) {
854
                        return $namespace;
855
                    }
856
                    break; // Nested namespaces is not possible.
857
                }
858
            }
859
        }
860
861
        /*
862
         * Not in a scoped namespace, so let's see if we can find a non-scoped namespace instead.
863
         * Keeping in mind that:
864
         * - there can be multiple non-scoped namespaces in a file (bad practice, but it happens).
865
         * - the namespace keyword can also be used as part of a function/method call and such.
866
         * - that a non-named namespace resolves to the global namespace.
867
         */
868
        $previousNSToken = $stackPtr;
869
        $namespace       = false;
870
        do {
871
            $previousNSToken = $phpcsFile->findPrevious(T_NAMESPACE, $previousNSToken -1);
872
873
            // Stop if we encounter a scoped namespace declaration as we already know we're not in one.
874
            if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] = $previousNSToken) {
875
                break;
876
            }
877
            $namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken);
878
879
        } while ($namespace === false && $previousNSToken !== false);
880
881
        // If we still haven't got a namespace, return an empty string.
882
        if ($namespace === false) {
883
            return '';
884
        }
885
        else {
886
            return $namespace;
887
        }
888
    }
889
890
    /**
891
     * Get the complete namespace name for a namespace declaration.
892
     *
893
     * For hierarchical namespaces, the name will be composed of several tokens,
894
     * i.e. MyProject\Sub\Level which will be returned together as one string.
895
     *
896
     * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.
897
     * @param int|bool             $stackPtr  The position of a T_NAMESPACE token.
898
     *
899
     * @return string|false Namespace name or false if not a namespace declaration.
900
     *                      Namespace name can be an empty string for global namespace declaration.
901
     */
902
    public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr )
903
    {
904
        $tokens = $phpcsFile->getTokens();
905
906
        // Check for the existence of the token.
907
        if ($stackPtr === false || isset($tokens[$stackPtr]) === false) {
908
            return false;
909
        }
910
911
        if ($tokens[$stackPtr]['code'] !== T_NAMESPACE) {
912
            return false;
913
        }
914
915
        if ($tokens[$stackPtr + 1]['code'] === T_NS_SEPARATOR) {
916
            // Not a namespace declaration, but use of, i.e. namespace\someFunction();
917
            return false;
918
        }
919
920
        $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
921
        if ($tokens[$nextToken]['code'] === T_OPEN_CURLY_BRACKET) {
922
            // Declaration for global namespace when using multiple namespaces in a file.
923
            // I.e.: namespace {}
924
            return '';
925
        }
926
927
        // Ok, this should be a namespace declaration, so get all the parts together.
928
        $validTokens = array(
929
                        T_STRING,
930
                        T_NS_SEPARATOR,
931
                        T_WHITESPACE,
932
                       );
933
934
        $namespaceName = '';
935
        while(in_array($tokens[$nextToken]['code'], $validTokens, true) === true) {
936
            $namespaceName .= trim($tokens[$nextToken]['content']);
937
            $nextToken++;
938
        }
939
940
        return $namespaceName;
941
    }
942
943
944
    /**
945
     * Get the stack pointer for a return type token for a given function.
946
     *
947
     * Compatible layer for older PHPCS versions which don't recognize
948
     * return type hints correctly.
949
     *
950
     * Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token.
951
     *
952
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
953
     * @param int                  $stackPtr  The position of the token.
954
     *
955
     * @return int|false Stack pointer to the return type token or false if
956
     *                   no return type was found or the passed token was
957
     *                   not of the correct type.
958
     */
959
    public function getReturnTypeHintToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
960
    {
961
        $tokens = $phpcsFile->getTokens();
962
963
        if (defined('T_RETURN_TYPE') && $tokens[$stackPtr]['code'] === T_RETURN_TYPE) {
964
            return $tokens[$stackPtr]['code'];
965
        }
966
967 View Code Duplication
        if ($tokens[$stackPtr]['code'] !== T_FUNCTION && $tokens[$stackPtr]['code'] !== T_CLOSURE) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
968
            return false;
969
        }
970
971
        if (isset($tokens[$stackPtr]['parenthesis_closer'], $tokens[$stackPtr]['scope_opener']) === false
972
            || ($tokens[$stackPtr]['parenthesis_closer'] + 1) === $tokens[$stackPtr]['scope_opener']
973
        ) {
974
            return false;
975
        }
976
977
        $hasColon = $phpcsFile->findNext(array(T_COLON, T_INLINE_ELSE), ($tokens[$stackPtr]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
978
        if ($hasColon === false) {
979
            return false;
980
        }
981
982
        // `self` and `callable` are not being recognized as return types in PHPCS < 2.6.0.
983
        $unrecognizedTypes = array(
984
            T_CALLABLE,
985
            T_SELF,
986
        );
987
988
        // Return types are not recognized at all in PHPCS < 2.4.0.
989
        if (defined('T_RETURN_TYPE') === false) {
990
            $unrecognizedTypes[] = T_ARRAY;
991
            $unrecognizedTypes[] = T_STRING;
992
        }
993
994
        return $phpcsFile->findNext($unrecognizedTypes, ($hasColon + 1), $tokens[$stackPtr]['scope_opener']);
995
    }
996
997
998
    /**
999
     * Returns the method parameters for the specified function token.
1000
     *
1001
     * Each parameter is in the following format:
1002
     *
1003
     * <code>
1004
     *   0 => array(
1005
     *         'name'              => '$var',  // The variable name.
1006
     *         'content'           => string,  // The full content of the variable definition.
1007
     *         'pass_by_reference' => boolean, // Is the variable passed by reference?
1008
     *         'type_hint'         => string,  // The type hint for the variable.
1009
     *         'nullable_type'     => boolean, // Is the variable using a nullable type?
1010
     *        )
1011
     * </code>
1012
     *
1013
     * Parameters with default values have an additional array index of
1014
     * 'default' with the value of the default as a string.
1015
     *
1016
     * {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File`
1017
     * class, but with some improvements which will probably be introduced in
1018
     * PHPCS 2.7.2. {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1117},
1019
     * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1193} and
1020
     * {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1293}.
1021
     *
1022
     * Once the minimum supported PHPCS version for this standard goes beyond
1023
     * that, this method can be removed and calls to it replaced with
1024
     * `$phpcsFile->getMethodParameters($stackPtr)` calls.
1025
     *
1026
     * NOTE: This version does not deal with the new T_NULLABLE token type.
1027
     * This token is included upstream only in 2.7.2+ and as we defer to upstream
1028
     * in that case, no need to deal with it here.
1029
     *
1030
     * Last synced with PHPCS version: PHPCS 2.7.2-alpha.}}
1031
     *
1032
     * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.
1033
     * @param int                  $stackPtr  The position in the stack of the
1034
     *                                        function token to acquire the
1035
     *                                        parameters for.
1036
     *
1037
     * @return array|false
1038
     * @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of
1039
     *                                   type T_FUNCTION or T_CLOSURE.
1040
     */
1041
    public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
1042
    {
1043
        if (version_compare(PHP_CodeSniffer::VERSION, '2.7.1', '>') === true) {
1044
            return $phpcsFile->getMethodParameters($stackPtr);
1045
        }
1046
1047
        $tokens = $phpcsFile->getTokens();
1048
1049
        // Check for the existence of the token.
1050
        if (isset($tokens[$stackPtr]) === false) {
1051
            return false;
1052
        }
1053
1054 View Code Duplication
        if ($tokens[$stackPtr]['code'] !== T_FUNCTION && $tokens[$stackPtr]['code'] !== T_CLOSURE) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1055
            throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
1056
        }
1057
1058
        $opener = $tokens[$stackPtr]['parenthesis_opener'];
1059
        $closer = $tokens[$stackPtr]['parenthesis_closer'];
1060
1061
        $vars            = array();
1062
        $currVar         = null;
1063
        $paramStart      = ($opener + 1);
1064
        $defaultStart    = null;
1065
        $paramCount      = 0;
1066
        $passByReference = false;
1067
        $variableLength  = false;
1068
        $typeHint        = '';
1069
        $nullableType    = false;
1070
1071
        for ($i = $paramStart; $i <= $closer; $i++) {
1072
            // Check to see if this token has a parenthesis or bracket opener. If it does
1073
            // it's likely to be an array which might have arguments in it. This
1074
            // could cause problems in our parsing below, so lets just skip to the
1075
            // end of it.
1076 View Code Duplication
            if (isset($tokens[$i]['parenthesis_opener']) === true) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1077
                // Don't do this if it's the close parenthesis for the method.
1078
                if ($i !== $tokens[$i]['parenthesis_closer']) {
1079
                    $i = ($tokens[$i]['parenthesis_closer'] + 1);
1080
                }
1081
            }
1082
1083 View Code Duplication
            if (isset($tokens[$i]['bracket_opener']) === true) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1084
                // Don't do this if it's the close parenthesis for the method.
1085
                if ($i !== $tokens[$i]['bracket_closer']) {
1086
                    $i = ($tokens[$i]['bracket_closer'] + 1);
1087
                }
1088
            }
1089
1090
            switch ($tokens[$i]['code']) {
1091
            case T_BITWISE_AND:
1092
                $passByReference = true;
1093
                break;
1094
            case T_VARIABLE:
1095
                $currVar = $i;
1096
                break;
1097
            case T_ELLIPSIS:
1098
                $variableLength = true;
1099
                break;
1100
            case T_ARRAY_HINT:
1101
            case T_CALLABLE:
1102
                $typeHint .= $tokens[$i]['content'];
1103
                break;
1104
            case T_SELF:
1105
            case T_PARENT:
1106
            case T_STATIC:
1107
                // Self is valid, the others invalid, but were probably intended as type hints.
1108
                if (isset($defaultStart) === false) {
1109
                    $typeHint .= $tokens[$i]['content'];
1110
                }
1111
                break;
1112
            case T_STRING:
1113
                // This is a string, so it may be a type hint, but it could
1114
                // also be a constant used as a default value.
1115
                $prevComma = false;
1116 View Code Duplication
                for ($t = $i; $t >= $opener; $t--) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1117
                    if ($tokens[$t]['code'] === T_COMMA) {
1118
                        $prevComma = $t;
1119
                        break;
1120
                    }
1121
                }
1122
1123
                if ($prevComma !== false) {
1124
                    $nextEquals = false;
1125 View Code Duplication
                    for ($t = $prevComma; $t < $i; $t++) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1126
                        if ($tokens[$t]['code'] === T_EQUAL) {
1127
                            $nextEquals = $t;
1128
                            break;
1129
                        }
1130
                    }
1131
1132
                    if ($nextEquals !== false) {
1133
                        break;
1134
                    }
1135
                }
1136
1137
                if ($defaultStart === null) {
1138
                    $typeHint .= $tokens[$i]['content'];
1139
                }
1140
                break;
1141
            case T_NS_SEPARATOR:
1142
                // Part of a type hint or default value.
1143
                if ($defaultStart === null) {
1144
                    $typeHint .= $tokens[$i]['content'];
1145
                }
1146
                break;
1147
            case T_INLINE_THEN:
1148
                if ($defaultStart === null) {
1149
                    $nullableType = true;
1150
                    $typeHint    .= $tokens[$i]['content'];
1151
                }
1152
                break;
1153
            case T_CLOSE_PARENTHESIS:
1154
            case T_COMMA:
1155
                // If it's null, then there must be no parameters for this
1156
                // method.
1157
                if ($currVar === null) {
1158
                    continue;
1159
                }
1160
1161
                $vars[$paramCount]            = array();
1162
                $vars[$paramCount]['name']    = $tokens[$currVar]['content'];
1163
                $vars[$paramCount]['content'] = trim($phpcsFile->getTokensAsString($paramStart, ($i - $paramStart)));
1164
1165
                if ($defaultStart !== null) {
1166
                    $vars[$paramCount]['default']
1167
                        = $phpcsFile->getTokensAsString(
1168
                            $defaultStart,
1169
                            ($i - $defaultStart)
1170
                        );
1171
                }
1172
1173
                $vars[$paramCount]['pass_by_reference'] = $passByReference;
1174
                $vars[$paramCount]['variable_length']   = $variableLength;
1175
                $vars[$paramCount]['type_hint']         = $typeHint;
1176
                $vars[$paramCount]['nullable_type']     = $nullableType;
1177
1178
                // Reset the vars, as we are about to process the next parameter.
1179
                $defaultStart    = null;
1180
                $paramStart      = ($i + 1);
1181
                $passByReference = false;
1182
                $variableLength  = false;
1183
                $typeHint        = '';
1184
                $nullableType    = false;
1185
1186
                $paramCount++;
1187
                break;
1188
            case T_EQUAL:
1189
                $defaultStart = ($i + 1);
1190
                break;
1191
            }//end switch
1192
        }//end for
1193
1194
        return $vars;
1195
1196
    }//end getMethodParameters()
1197
1198
1199
    /**
1200
     * Get the hash algorithm name from the parameter in a hash function call.
1201
     *
1202
     * @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.
1203
     * @param int                  $stackPtr  The position of the T_STRING function token.
1204
     *
1205
     * @return string|false The algorithm name without quotes if this was a relevant hash
1206
     *                      function call or false if it was not.
1207
     */
1208
    public function getHashAlgorithmParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
1209
    {
1210
        $tokens = $phpcsFile->getTokens();
1211
1212
        // Check for the existence of the token.
1213
        if (isset($tokens[$stackPtr]) === false) {
1214
            return false;
1215
        }
1216
1217
        if ($tokens[$stackPtr]['code'] !== T_STRING) {
1218
            return false;
1219
        }
1220
1221
        $functionName   = $tokens[$stackPtr]['content'];
1222
        $functionNameLc = strtolower($functionName);
1223
1224
        // Bow out if not one of the functions we're targetting.
1225
        if (isset($this->hashAlgoFunctions[$functionNameLc]) === false) {
1226
            return false;
1227
        }
1228
1229
        // Get the parameter from the function call which should contain the algorithm name.
1230
        $algoParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, $this->hashAlgoFunctions[$functionNameLc]);
1231
        if ($algoParam === false) {
1232
            return false;
1233
        }
1234
1235
        /**
1236
         * Algorithm is a text string, so we need to remove the quotes.
1237
         */
1238
        $algo = strtolower(trim($algoParam['raw']));
1239
        $algo = $this->stripQuotes($algo);
1240
1241
        return $algo;
1242
    }
1243
1244
}//end class
1245