File: ConditionSpacingSniff.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 (87 lines) | stat: -rw-r--r-- 3,640 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
 * FDStandard_Sniffs_WhiteSpace_AssignmentSpacingSniff.
 *
 * Checks that assignment blocks have aligned equals signs
 *
 * Modified version of PHP_CodeSniffer Generic_Sniffs_Function_FunctionCallArgumentSpacingSniff
 * by Greg Sherwood <gsherwood@squiz.net> and Marc McIntyre <mmcintyre@squiz.net>
 *
 * @author    Côme Bernigaud <come.bernigaud@laposte.net>
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 */

class FDStandard_Sniffs_WhiteSpace_ConditionSpacingSniff implements PHP_CodeSniffer_Sniff
{
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array
     */
    public function register()
    {
        return array( T_WHILE, T_FOR, T_FOREACH, T_IF, T_ELSE, T_ELSEIF,
                      T_DO, T_TRY, T_CATCH, T_SWITCH);

    }//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();

        //~ $blockClosers = array(T_CLOSE_CURLY_BRACKET, T_CLOSE_SQUARE_BRACKET, T_CLOSE_PARENTHESIS);

        if ($prevBracket = $phpcsFile->findPrevious(array(T_CLOSE_CURLY_BRACKET),$stackPtr-1)) {
          if ($tokens[$prevBracket]['line'] == $tokens[$stackPtr]['line']) {
              if ($tokens[$stackPtr-1]['code'] !== T_WHITESPACE) {
                  //Check that there is a space between this bracket and the condition token
                  $error = 'Condition token "'.$tokens[$stackPtr]['content'].'" should be preceded by a space';
                  $phpcsFile->addError($error, $stackPtr, 'ConditionBeforeSpace');
              }
          } elseif (in_array($tokens[$stackPtr]['code'],array(T_ELSE, T_ELSEIF, T_CATCH))) {
              //The brace should be there
              $error = 'The brace before "'.$tokens[$stackPtr]['content'].'" should be on the same line';
              $phpcsFile->addError($error, $prevBracket, 'ConditionBeforeBracket');
          }
        }

        if (isset($tokens[$stackPtr]['scope_opener'])) {
          $nextBracket = $tokens[$stackPtr]['scope_opener'];
          if (isset($tokens[$stackPtr]['parenthesis_closer'])) {
            $parenthesis = $tokens[$stackPtr]['parenthesis_closer'];
            $line = $tokens[$parenthesis]['line'];
          } else {
            $line = $tokens[$stackPtr]['line'];
          }
          if ($tokens[$nextBracket]['line'] != $line) {
              $error = 'The brace after "'.$tokens[$stackPtr]['content'].'" should be on the same line';
              $phpcsFile->addError($error, $stackPtr, 'ConditionAfterBracketLine');
          } elseif ($tokens[$nextBracket-1]['code'] !== T_WHITESPACE) {
              $error = 'The "'.$tokens[$nextBracket]['content'].'" after "'.$tokens[$stackPtr]['content'].'" should be preceded by a space';
              $phpcsFile->addError($error, $stackPtr, 'ConditionAfterBracketSpace');
              return;
          }
        }

        //Check that there is a space after the condition token
        if ($tokens[$stackPtr+1]['code'] !== T_WHITESPACE) {
            $error = 'Condition token "'.$tokens[$stackPtr]['content'].'" should be followed by a space';
            $phpcsFile->addError($error, $stackPtr, 'ConditionAfterSpace');
        }

    }//end process()


}//end class

?>