File: ReferenceSniff.php

package info (click to toggle)
fusiondirectory 1.0.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 28,984 kB
  • sloc: php: 74,645; xml: 3,645; perl: 1,555; pascal: 705; sh: 135; sql: 45; makefile: 19
file content (64 lines) | stat: -rw-r--r-- 1,941 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
 * FDStandard_Sniffs_Foreach_ReferenceSniff.
 *
 * Checks that foreach using references are followed by an unset
 *
 * @author    Côme Bernigaud <come.bernigaud@laposte.net>
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 */

class FDStandard_Sniffs_Foreach_ReferenceSniff implements PHP_CodeSniffer_Sniff
{
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array
     */
    public function register()
    {
        return array(T_FOREACH);

    }//end register()


    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
     * @param int                  $stackPtr  The position of the current token in the
     *                                        stack passed in $tokens.
     *
     * @return void
     */
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();

        /* Find the as token of the foreach */
        $as = $phpcsFile->findNext(T_AS, $tokens[$stackPtr]['parenthesis_opener']+1, $tokens[$stackPtr]['parenthesis_closer']);
        if (!$as) {
          return;
        }
        /* Search for a reference between as and closing parenthesis  */
        $ref = $phpcsFile->findNext(T_BITWISE_AND, $as+1, $tokens[$stackPtr]['parenthesis_closer']);
        if (!$ref) {
          return;
        }

        $error = 'Foreach token using a reference should unset it afterwards';
        if ($next = $phpcsFile->findNext(T_WHITESPACE, $tokens[$stackPtr]['scope_closer']+1, NULL, TRUE)) {
          if ($tokens[$next]['content'] == 'unset') {
            return;
          }
          $error .= ' (Expected unset, found '.$tokens[$next]['content'].')';
        }

        $phpcsFile->addError($error, $stackPtr, 'UnsetReference');

    }//end process()


}//end class

?>