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 and its inheritance 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\Squiz\Sniffs\Classes;
  11.  
  12. use PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff as PSR2ClassDeclarationSniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ClassDeclarationSniff extends PSR2ClassDeclarationSniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Processes this test, when one of its tokens is encountered.
  21.      *
  22.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  23.      * @param int                         $stackPtr  The position of the current token
  24.      *                                                in the stack passed in $tokens.
  25.      *
  26.      * @return void 
  27.      */
  28.     public function process(File $phpcsFile$stackPtr)
  29.     {
  30.         // We want all the errors from the PSR2 standard, plus some of our own.
  31.         parent::process($phpcsFile$stackPtr);
  32.  
  33.         $tokens $phpcsFile->getTokens();
  34.  
  35.         // Check that this is the only class or interface in the file.
  36.         $nextClass $phpcsFile->findNext(array(T_CLASST_INTERFACE)($stackPtr + 1));
  37.         if ($nextClass !== false{
  38.             // We have another, so an error is thrown.
  39.             $error 'Only one interface or class is allowed in a file';
  40.             $phpcsFile->addError($error$nextClass'MultipleClasses');
  41.         }
  42.  
  43.     }//end process()
  44.  
  45.  
  46.     /**
  47.      * Processes the opening section of a class declaration.
  48.      *
  49.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  50.      * @param int                         $stackPtr  The position of the current token
  51.      *                                                in the stack passed in $tokens.
  52.      *
  53.      * @return void 
  54.      */
  55.     public function processOpen(File $phpcsFile$stackPtr)
  56.     {
  57.         parent::processOpen($phpcsFile$stackPtr);
  58.  
  59.         $tokens $phpcsFile->getTokens();
  60.  
  61.         if ($tokens[($stackPtr - 1)]['code'=== T_WHITESPACE{
  62.             $prevContent $tokens[($stackPtr - 1)]['content'];
  63.             if ($prevContent !== $phpcsFile->eolChar{
  64.                 $blankSpace substr($prevContentstrpos($prevContent$phpcsFile->eolChar));
  65.                 $spaces     strlen($blankSpace);
  66.  
  67.                 if ($tokens[($stackPtr - 2)]['code'!== T_ABSTRACT
  68.                     && $tokens[($stackPtr - 2)]['code'!== T_FINAL
  69.                 {
  70.                     if ($spaces !== 0{
  71.                         $type  strtolower($tokens[$stackPtr]['content']);
  72.                         $error 'Expected 0 spaces before %s keyword; %s found';
  73.                         $data  = array(
  74.                                   $type,
  75.                                   $spaces,
  76.                                  );
  77.  
  78.                         $fix $phpcsFile->addFixableError($error$stackPtr'SpaceBeforeKeyword'$data);
  79.                         if ($fix === true{
  80.                             $phpcsFile->fixer->replaceToken(($stackPtr - 1)'');
  81.                         }
  82.                     }
  83.                 }
  84.             }//end if
  85.         }//end if
  86.  
  87.     }//end processOpen()
  88.  
  89.  
  90.     /**
  91.      * Processes the closing section of a class declaration.
  92.      *
  93.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  94.      * @param int                         $stackPtr  The position of the current token
  95.      *                                                in the stack passed in $tokens.
  96.      *
  97.      * @return void 
  98.      */
  99.     public function processClose(File $phpcsFile$stackPtr)
  100.     {
  101.         $tokens $phpcsFile->getTokens();
  102.         if (isset($tokens[$stackPtr]['scope_closer']=== false{
  103.             return;
  104.         }
  105.  
  106.         $closeBrace $tokens[$stackPtr]['scope_closer'];
  107.  
  108.         // Check that the closing brace has one blank line after it.
  109.         for ($nextContent ($closeBrace + 1)$nextContent $phpcsFile->numTokens; $nextContent++{
  110.             // Ignore comments on the same lines as the brace.
  111.             if ($tokens[$nextContent]['line'=== $tokens[$closeBrace]['line']
  112.                 && ($tokens[$nextContent]['code'=== T_WHITESPACE
  113.                 || $tokens[$nextContent]['code'=== T_COMMENT)
  114.             {
  115.                 continue;
  116.             }
  117.  
  118.             if ($tokens[$nextContent]['code'!== T_WHITESPACE{
  119.                 break;
  120.             }
  121.         }
  122.  
  123.         if ($nextContent === $phpcsFile->numTokens{
  124.             // Ignore the line check as this is the very end of the file.
  125.             $difference = 1;
  126.         else {
  127.             $difference ($tokens[$nextContent]['line'$tokens[$closeBrace]['line'- 1);
  128.         }
  129.  
  130.         $lastContent $phpcsFile->findPrevious(T_WHITESPACE($closeBrace - 1)$stackPtrtrue);
  131.  
  132.         if ($difference === -1
  133.             || $tokens[$lastContent]['line'=== $tokens[$closeBrace]['line']
  134.         {
  135.             $error 'Closing %s brace must be on a line by itself';
  136.             $data  = array($tokens[$stackPtr]['content']);
  137.             $fix   $phpcsFile->addFixableError($error$closeBrace'CloseBraceSameLine'$data);
  138.             if ($fix === true{
  139.                 if ($difference === -1{
  140.                     $phpcsFile->fixer->addNewlineBefore($nextContent);
  141.                 }
  142.  
  143.                 if ($tokens[$lastContent]['line'=== $tokens[$closeBrace]['line']{
  144.                     $phpcsFile->fixer->addNewlineBefore($closeBrace);
  145.                 }
  146.             }
  147.         else if ($tokens[($closeBrace - 1)]['code'=== T_WHITESPACE{
  148.             $prevContent $tokens[($closeBrace - 1)]['content'];
  149.             if ($prevContent !== $phpcsFile->eolChar{
  150.                 $blankSpace substr($prevContentstrpos($prevContent$phpcsFile->eolChar));
  151.                 $spaces     strlen($blankSpace);
  152.                 if ($spaces !== 0{
  153.                     if ($tokens[($closeBrace - 1)]['line'!== $tokens[$closeBrace]['line']{
  154.                         $error 'Expected 0 spaces before closing brace; newline found';
  155.                         $phpcsFile->addError($error$closeBrace'NewLineBeforeCloseBrace');
  156.                     else {
  157.                         $error 'Expected 0 spaces before closing brace; %s found';
  158.                         $data  = array($spaces);
  159.                         $fix   $phpcsFile->addFixableError($error$closeBrace'SpaceBeforeCloseBrace'$data);
  160.                         if ($fix === true{
  161.                             $phpcsFile->fixer->replaceToken(($closeBrace - 1)'');
  162.                         }
  163.                     }
  164.                 }
  165.             }
  166.         }//end if
  167.  
  168.         if ($difference !== -1 && $difference !== 1{
  169.             $error 'Closing brace of a %s must be followed by a single blank line; found %s';
  170.             $data  = array(
  171.                       $tokens[$stackPtr]['content'],
  172.                       $difference,
  173.                      );
  174.             $fix   $phpcsFile->addFixableError($error$closeBrace'NewlinesAfterCloseBrace'$data);
  175.             if ($fix === true{
  176.                 if ($difference === 0{
  177.                     $first $phpcsFile->findFirstOnLine(array()$nextContenttrue);
  178.                     $phpcsFile->fixer->addNewlineBefore($first);
  179.                 else {
  180.                     $phpcsFile->fixer->beginChangeset();
  181.                     for ($i ($closeBrace + 1)$i $nextContent$i++{
  182.                         if ($tokens[$i]['line'<= ($tokens[$closeBrace]['line'+ 1)) {
  183.                             continue;
  184.                         else if ($tokens[$i]['line'=== $tokens[$nextContent]['line']{
  185.                             break;
  186.                         }
  187.  
  188.                         $phpcsFile->fixer->replaceToken($i'');
  189.                     }
  190.  
  191.                     $phpcsFile->fixer->endChangeset();
  192.                 }
  193.             }
  194.         }//end if
  195.  
  196.     }//end processClose()
  197.  
  198.  
  199. }//end class

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