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

Source for file NamespaceDeclarationSniff.php

Documentation is available at NamespaceDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Ensures namespaces 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 NamespaceDeclarationSniff 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_NAMESPACE);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this test, when one of its tokens is encountered.
  33.      *
  34.      * @param \PHP_CodeSniffer\Files\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.         $tokens $phpcsFile->getTokens();
  43.  
  44.         for ($i ($stackPtr + 1)$i ($phpcsFile->numTokens - 1)$i++{
  45.             if ($tokens[$i]['line'=== $tokens[$stackPtr]['line']{
  46.                 continue;
  47.             }
  48.  
  49.             break;
  50.         }
  51.  
  52.         // The $i var now points to the first token on the line after the
  53.         // namespace declaration, which must be a blank line.
  54.         $next $phpcsFile->findNext(T_WHITESPACE$i$phpcsFile->numTokenstrue);
  55.         if ($next === false{
  56.             return;
  57.         }
  58.  
  59.         $diff ($tokens[$next]['line'$tokens[$i]['line']);
  60.         if ($diff === 1{
  61.             return;
  62.         }
  63.  
  64.         if ($diff < 0{
  65.             $diff = 0;
  66.         }
  67.  
  68.         $error 'There must be one blank line after the namespace declaration';
  69.         $fix   $phpcsFile->addFixableError($error$stackPtr'BlankLineAfter');
  70.  
  71.         if ($fix === true{
  72.             if ($diff === 0{
  73.                 $phpcsFile->fixer->addNewlineBefore($i);
  74.             else {
  75.                 $phpcsFile->fixer->beginChangeset();
  76.                 for ($x $i$x $next$x++{
  77.                     if ($tokens[$x]['line'=== $tokens[$next]['line']{
  78.                         break;
  79.                     }
  80.  
  81.                     $phpcsFile->fixer->replaceToken($x'');
  82.                 }
  83.  
  84.                 $phpcsFile->fixer->addNewline($i);
  85.                 $phpcsFile->fixer->endChangeset();
  86.             }
  87.         }
  88.  
  89.     }//end process()
  90.  
  91.  
  92. }//end class

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