PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file ClassDeclarationSniff.php

Documentation is available at ClassDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Checks the declaration of the class is correct.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\Classes;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ClassDeclarationSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(
  27.                 T_CLASS,
  28.                 T_INTERFACE,
  29.                 T_TRAIT,
  30.                );
  31.  
  32.     }//end register()
  33.  
  34.  
  35.     /**
  36.      * Processes this test, when one of its tokens is encountered.
  37.      *
  38.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  39.      * @param integer                     $stackPtr  The position of the current token in the
  40.      *                                                stack passed in $tokens.
  41.      *
  42.      * @return void 
  43.      */
  44.     public function process(File $phpcsFile$stackPtr)
  45.     {
  46.         $tokens    $phpcsFile->getTokens();
  47.         $errorData = array(strtolower($tokens[$stackPtr]['content']));
  48.  
  49.         if (isset($tokens[$stackPtr]['scope_opener']=== false{
  50.             $error 'Possible parse error: %s missing opening or closing brace';
  51.             $phpcsFile->addWarning($error$stackPtr'MissingBrace'$errorData);
  52.             return;
  53.         }
  54.  
  55.         $curlyBrace  $tokens[$stackPtr]['scope_opener'];
  56.         $lastContent $phpcsFile->findPrevious(T_WHITESPACE($curlyBrace - 1)$stackPtrtrue);
  57.         $classLine   $tokens[$lastContent]['line'];
  58.         $braceLine   $tokens[$curlyBrace]['line'];
  59.         if ($braceLine === $classLine{
  60.             $phpcsFile->recordMetric($stackPtr'Class opening brace placement''same line');
  61.             $error 'Opening brace of a %s must be on the line after the definition';
  62.             $fix   $phpcsFile->addFixableError($error$curlyBrace'OpenBraceNewLine'$errorData);
  63.             if ($fix === true{
  64.                 $phpcsFile->fixer->beginChangeset();
  65.                 if ($tokens[($curlyBrace - 1)]['code'=== T_WHITESPACE{
  66.                     $phpcsFile->fixer->replaceToken(($curlyBrace - 1)'');
  67.                 }
  68.  
  69.                 $phpcsFile->fixer->addNewlineBefore($curlyBrace);
  70.                 $phpcsFile->fixer->endChangeset();
  71.             }
  72.  
  73.             return;
  74.         else {
  75.             $phpcsFile->recordMetric($stackPtr'Class opening brace placement''new line');
  76.  
  77.             if ($braceLine ($classLine + 1)) {
  78.                 $error 'Opening brace of a %s must be on the line following the %s declaration; found %s line(s)';
  79.                 $data  = array(
  80.                           $tokens[$stackPtr]['content'],
  81.                           $tokens[$stackPtr]['content'],
  82.                           ($braceLine $classLine - 1),
  83.                          );
  84.                 $fix   $phpcsFile->addFixableError($error$curlyBrace'OpenBraceWrongLine'$data);
  85.                 if ($fix === true{
  86.                     $phpcsFile->fixer->beginChangeset();
  87.                     for ($i ($curlyBrace - 1)$i $lastContent$i--{
  88.                         if ($tokens[$i]['line'=== ($tokens[$curlyBrace]['line'+ 1)) {
  89.                             break;
  90.                         }
  91.  
  92.                         $phpcsFile->fixer->replaceToken($i'');
  93.                     }
  94.  
  95.                     $phpcsFile->fixer->endChangeset();
  96.                 }
  97.  
  98.                 return;
  99.             }//end if
  100.         }//end if
  101.  
  102.         if ($tokens[($curlyBrace + 1)]['content'!== $phpcsFile->eolChar{
  103.             $error 'Opening %s brace must be on a line by itself';
  104.             $fix   $phpcsFile->addFixableError($error$curlyBrace'OpenBraceNotAlone'$errorData);
  105.             if ($fix === true{
  106.                 $phpcsFile->fixer->addNewline($curlyBrace);
  107.             }
  108.         }
  109.  
  110.         if ($tokens[($curlyBrace - 1)]['code'=== T_WHITESPACE{
  111.             $prevContent $tokens[($curlyBrace - 1)]['content'];
  112.             if ($prevContent === $phpcsFile->eolChar{
  113.                 $spaces = 0;
  114.             else {
  115.                 $blankSpace substr($prevContentstrpos($prevContent$phpcsFile->eolChar));
  116.                 $spaces     strlen($blankSpace);
  117.             }
  118.  
  119.             $first    $phpcsFile->findFirstOnLine(T_WHITESPACE$stackPtrtrue);
  120.             $expected ($tokens[$first]['column'- 1);
  121.             if ($spaces !== $expected{
  122.                 $error 'Expected %s spaces before opening brace; %s found';
  123.                 $data  = array(
  124.                           $expected,
  125.                           $spaces,
  126.                          );
  127.  
  128.                 $fix $phpcsFile->addFixableError($error$curlyBrace'SpaceBeforeBrace'$data);
  129.                 if ($fix === true{
  130.                     $indent str_repeat(' '$expected);
  131.                     if ($spaces === 0{
  132.                         $phpcsFile->fixer->addContentBefore($curlyBrace$indent);
  133.                     else {
  134.                         $phpcsFile->fixer->replaceToken(($curlyBrace - 1)$indent);
  135.                     }
  136.                 }
  137.             }
  138.         }//end if
  139.  
  140.     }//end process()
  141.  
  142.  
  143. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.