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

Source for file UseDeclarationSniff.php

Documentation is available at UseDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Ensures USE blocks are declared correctly.
  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\PSR2\Sniffs\Namespaces;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class UseDeclarationSniff 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(T_USE);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this test, when one of its tokens is encountered.
  33.      *
  34.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  35.      * @param int                  $stackPtr  The position of the current token in
  36.      *                                         the stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         if ($this->shouldIgnoreUse($phpcsFile$stackPtr=== true{
  43.             return;
  44.         }
  45.  
  46.         $tokens $phpcsFile->getTokens();
  47.  
  48.         // One space after the use keyword.
  49.         if ($tokens[($stackPtr + 1)]['content'!== ' '{
  50.             $error 'There must be a single space after the USE keyword';
  51.             $fix   $phpcsFile->addFixableError($error$stackPtr'SpaceAfterUse');
  52.             if ($fix === true{
  53.                 $phpcsFile->fixer->replaceToken(($stackPtr + 1)' ');
  54.             }
  55.         }
  56.  
  57.         // Only one USE declaration allowed per statement.
  58.         $next $phpcsFile->findNext(array(T_COMMAT_SEMICOLON)($stackPtr + 1));
  59.         if ($tokens[$next]['code'=== T_COMMA{
  60.             $error 'There must be one USE keyword per declaration';
  61.             $fix   $phpcsFile->addFixableError($error$stackPtr'MultipleDeclarations');
  62.             if ($fix === true{
  63.                 $phpcsFile->fixer->replaceToken($next';'.$phpcsFile->eolChar.'use ');
  64.             }
  65.         }
  66.  
  67.         // Make sure this USE comes after the first namespace declaration.
  68.         $prev $phpcsFile->findPrevious(T_NAMESPACE($stackPtr - 1));
  69.         if ($prev !== false{
  70.             $first $phpcsFile->findNext(T_NAMESPACE1);
  71.             if ($prev !== $first{
  72.                 $error 'USE declarations must go after the first namespace declaration';
  73.                 $phpcsFile->addError($error$stackPtr'UseAfterNamespace');
  74.             }
  75.         }
  76.  
  77.         // Only interested in the last USE statement from here onwards.
  78.         $nextUse $phpcsFile->findNext(T_USE($stackPtr + 1));
  79.         while ($this->shouldIgnoreUse($phpcsFile$nextUse=== true{
  80.             $nextUse $phpcsFile->findNext(T_USE($nextUse + 1));
  81.             if ($nextUse === false{
  82.                 break;
  83.             }
  84.         }
  85.  
  86.         if ($nextUse !== false{
  87.             return;
  88.         }
  89.  
  90.         $end  $phpcsFile->findNext(T_SEMICOLON($stackPtr + 1));
  91.         $next $phpcsFile->findNext(T_WHITESPACE($end + 1)nulltrue);
  92.  
  93.         if ($tokens[$next]['code'=== T_CLOSE_TAG{
  94.             return;
  95.         }
  96.  
  97.         $diff ($tokens[$next]['line'$tokens[$end]['line'- 1);
  98.         if ($diff !== 1{
  99.             if ($diff < 0{
  100.                 $diff = 0;
  101.             }
  102.  
  103.             $error 'There must be one blank line after the last USE statement; %s found;';
  104.             $data  = array($diff);
  105.             $fix   $phpcsFile->addFixableError($error$stackPtr'SpaceAfterLastUse'$data);
  106.             if ($fix === true{
  107.                 if ($diff === 0{
  108.                     $phpcsFile->fixer->addNewline($end);
  109.                 else {
  110.                     $phpcsFile->fixer->beginChangeset();
  111.                     for ($i ($end + 1)$i $next$i++{
  112.                         if ($tokens[$i]['line'=== $tokens[$next]['line']{
  113.                             break;
  114.                         }
  115.  
  116.                         $phpcsFile->fixer->replaceToken($i'');
  117.                     }
  118.  
  119.                     $phpcsFile->fixer->addNewline($end);
  120.                     $phpcsFile->fixer->endChangeset();
  121.                 }
  122.             }
  123.         }//end if
  124.  
  125.     }//end process()
  126.  
  127.  
  128.     /**
  129.      * Check if this use statement is part of the namespace block.
  130.      *
  131.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  132.      * @param int                  $stackPtr  The position of the current token in
  133.      *                                         the stack passed in $tokens.
  134.      *
  135.      * @return void 
  136.      */
  137.     private function shouldIgnoreUse($phpcsFile$stackPtr)
  138.     {
  139.         $tokens $phpcsFile->getTokens();
  140.  
  141.         // Ignore USE keywords inside closures.
  142.         $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  143.         if ($tokens[$next]['code'=== T_OPEN_PARENTHESIS{
  144.             return true;
  145.         }
  146.  
  147.         // Ignore USE keywords for traits.
  148.         if ($phpcsFile->hasCondition($stackPtrarray(T_CLASST_TRAIT)) === true{
  149.             return true;
  150.         }
  151.  
  152.         return false;
  153.  
  154.     }//end shouldIgnoreUse()
  155.  
  156.  
  157. }//end class

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