[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_cs/DokuWiki/Sniffs/Functions/ -> OpeningFunctionBraceSniff.php (source)

   1  <?php
   2  /**
   3   * Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff.
   4   */
   5  
   6  class DokuWiki_Sniffs_Functions_OpeningFunctionBraceSniff implements PHP_CodeSniffer_Sniff {
   7  
   8  
   9      /**
  10       * Registers the tokens that this sniff wants to listen for.
  11       *
  12       * @return void
  13       */
  14      public function register()
  15      {
  16          return array(T_FUNCTION);
  17  
  18      }//end register()
  19  
  20  
  21      /**
  22       * Processes this test, when one of its tokens is encountered.
  23       *
  24       * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  25       * @param int                  $stackPtr  The position of the current token in the
  26       *                                        stack passed in $tokens.
  27       *
  28       * @return void
  29       */
  30      public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  31      {
  32          $tokens = $phpcsFile->getTokens();
  33  
  34          if (isset($tokens[$stackPtr]['scope_opener']) === false) {
  35              return;
  36          }
  37  
  38          $openingBrace = $tokens[$stackPtr]['scope_opener'];
  39  
  40          // The end of the function occurs at the end of the argument list. Its
  41          // like this because some people like to break long function declarations
  42          // over multiple lines.
  43          $functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
  44          $braceLine    = $tokens[$openingBrace]['line'];
  45  
  46          $lineDifference = ($braceLine - $functionLine);
  47  
  48          if ($lineDifference > 0) {
  49              $error = 'Opening brace should be on the same line as the declaration';
  50              $phpcsFile->addError($error, $openingBrace);
  51              return;
  52          }
  53  
  54          // Checks that the closing parenthesis and the opening brace are
  55          // separated by a whitespace character.
  56          $closerColumn = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['column'];
  57          $braceColumn  = $tokens[$openingBrace]['column'];
  58  
  59          $columnDifference = ($braceColumn - $closerColumn);
  60  
  61          if ($columnDifference > 2) {
  62              $error = 'Expected 0 or 1 space between the closing parenthesis and the opening brace; found '.($columnDifference - 1).'.';
  63              $phpcsFile->addError($error, $openingBrace);
  64              return;
  65          }
  66  
  67          // Check that a tab was not used instead of a space.
  68          $spaceTokenPtr = ($tokens[$stackPtr]['parenthesis_closer'] + 1);
  69          $spaceContent  = $tokens[$spaceTokenPtr]['content'];
  70          if ($columnDifference == 2 && $spaceContent !== ' ') {
  71              $error = 'Expected a none or a single space character between closing parenthesis and opening brace; found "'.$spaceContent.'".';
  72              $phpcsFile->addError($error, $openingBrace);
  73              return;
  74          }
  75  
  76      }//end process()
  77  
  78  
  79  }//end class
  80  
  81  ?>