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.      * The number of spaces code should be indented.
  20.      *
  21.      * @var integer 
  22.      */
  23.     public $indent = 4;
  24.  
  25.  
  26.     /**
  27.      * Returns an array of tokens this test wants to listen for.
  28.      *
  29.      * @return array 
  30.      */
  31.     public function register()
  32.     {
  33.         return array(
  34.                 T_CLASS,
  35.                 T_INTERFACE,
  36.                 T_TRAIT,
  37.                );
  38.  
  39.     }//end register()
  40.  
  41.  
  42.     /**
  43.      * Processes this test, when one of its tokens is encountered.
  44.      *
  45.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  46.      * @param integer              $stackPtr  The position of the current token in the
  47.      *                                         stack passed in $tokens.
  48.      *
  49.      * @return void 
  50.      */
  51.     public function process(File $phpcsFile$stackPtr)
  52.     {
  53.         $tokens    $phpcsFile->getTokens();
  54.         $errorData = array(strtolower($tokens[$stackPtr]['content']));
  55.  
  56.         if (isset($tokens[$stackPtr]['scope_opener']=== false{
  57.             $error 'Possible parse error: %s missing opening or closing brace';
  58.             $phpcsFile->addWarning($error$stackPtr'MissingBrace'$errorData);
  59.             return;
  60.         }
  61.  
  62.         $curlyBrace  $tokens[$stackPtr]['scope_opener'];
  63.         $lastContent $phpcsFile->findPrevious(T_WHITESPACE($curlyBrace - 1)$stackPtrtrue);
  64.         $classLine   $tokens[$lastContent]['line'];
  65.         $braceLine   $tokens[$curlyBrace]['line'];
  66.         if ($braceLine === $classLine{
  67.             $phpcsFile->recordMetric($stackPtr'Class opening brace placement''same line');
  68.             $error 'Opening brace of a %s must be on the line after the definition';
  69.             $fix   $phpcsFile->addFixableError($error$curlyBrace'OpenBraceNewLine'$errorData);
  70.             if ($fix === true{
  71.                 $phpcsFile->fixer->beginChangeset();
  72.                 if ($tokens[($curlyBrace - 1)]['code'=== T_WHITESPACE{
  73.                     $phpcsFile->fixer->replaceToken(($curlyBrace - 1)'');
  74.                 }
  75.  
  76.                 $phpcsFile->fixer->addNewlineBefore($curlyBrace);
  77.                 $phpcsFile->fixer->endChangeset();
  78.             }
  79.  
  80.             return;
  81.         else {
  82.             $phpcsFile->recordMetric($stackPtr'Class opening brace placement''new line');
  83.  
  84.             if ($braceLine ($classLine + 1)) {
  85.                 $error 'Opening brace of a %s must be on the line following the %s declaration; found %s line(s)';
  86.                 $data  = array(
  87.                           $tokens[$stackPtr]['content'],
  88.                           $tokens[$stackPtr]['content'],
  89.                           ($braceLine $classLine - 1),
  90.                          );
  91.                 $fix   $phpcsFile->addFixableError($error$curlyBrace'OpenBraceWrongLine'$data);
  92.                 if ($fix === true{
  93.                     $phpcsFile->fixer->beginChangeset();
  94.                     for ($i ($curlyBrace - 1)$i $lastContent$i--{
  95.                         if ($tokens[$i]['line'=== ($tokens[$curlyBrace]['line'+ 1)) {
  96.                             break;
  97.                         }
  98.  
  99.                         $phpcsFile->fixer->replaceToken($i'');
  100.                     }
  101.  
  102.                     $phpcsFile->fixer->endChangeset();
  103.                 }
  104.  
  105.                 return;
  106.             }//end if
  107.         }//end if
  108.  
  109.         if ($tokens[($curlyBrace + 1)]['content'!== $phpcsFile->eolChar{
  110.             $error 'Opening %s brace must be on a line by itself';
  111.             $fix   $phpcsFile->addFixableError($error$curlyBrace'OpenBraceNotAlone'$errorData);
  112.             if ($fix === true{
  113.                 $phpcsFile->fixer->addNewline($curlyBrace);
  114.             }
  115.         }
  116.  
  117.         if ($tokens[($curlyBrace - 1)]['code'=== T_WHITESPACE{
  118.             $prevContent $tokens[($curlyBrace - 1)]['content'];
  119.             if ($prevContent === $phpcsFile->eolChar{
  120.                 $spaces = 0;
  121.             else {
  122.                 $blankSpace substr($prevContentstrpos($prevContent$phpcsFile->eolChar));
  123.                 $spaces     strlen($blankSpace);
  124.             }
  125.  
  126.             $expected ($tokens[$stackPtr]['level'$this->indent);
  127.             if ($spaces !== $expected{
  128.                 $error 'Expected %s spaces before opening brace; %s found';
  129.                 $data  = array(
  130.                           $expected,
  131.                           $spaces,
  132.                          );
  133.  
  134.                 $fix $phpcsFile->addFixableError($error$curlyBrace'SpaceBeforeBrace'$data);
  135.                 if ($fix === true{
  136.                     $indent str_repeat(' '$expected);
  137.                     if ($spaces === 0{
  138.                         $phpcsFile->fixer->addContentBefore($curlyBrace$indent);
  139.                     else {
  140.                         $phpcsFile->fixer->replaceToken(($curlyBrace - 1)$indent);
  141.                     }
  142.                 }
  143.             }
  144.         }//end if
  145.  
  146.     }//end process()
  147.  
  148.  
  149. }//end class

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