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

Source for file LanguageConstructSpacingSniff.php

Documentation is available at LanguageConstructSpacingSniff.php

  1. <?php
  2. /**
  3.  * Ensures all language constructs contain a single space between themselves and their content.
  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\WhiteSpace;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class LanguageConstructSpacingSniff 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_ECHO,
  28.                 T_PRINT,
  29.                 T_RETURN,
  30.                 T_INCLUDE,
  31.                 T_INCLUDE_ONCE,
  32.                 T_REQUIRE,
  33.                 T_REQUIRE_ONCE,
  34.                 T_NEW,
  35.                );
  36.  
  37.     }//end register()
  38.  
  39.  
  40.     /**
  41.      * Processes this test, when one of its tokens is encountered.
  42.      *
  43.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  44.      * @param int                         $stackPtr  The position of the current token in
  45.      *                                                the stack passed in $tokens.
  46.      *
  47.      * @return void 
  48.      */
  49.     public function process(File $phpcsFile$stackPtr)
  50.     {
  51.         $tokens $phpcsFile->getTokens();
  52.  
  53.         if ($tokens[($stackPtr + 1)]['code'=== T_SEMICOLON{
  54.             // No content for this language construct.
  55.             return;
  56.         }
  57.  
  58.         if ($tokens[($stackPtr + 1)]['code'=== T_WHITESPACE{
  59.             $content       $tokens[($stackPtr + 1)]['content'];
  60.             $contentLength strlen($content);
  61.             if ($contentLength !== 1{
  62.                 $error 'Language constructs must be followed by a single space; expected 1 space but found %s';
  63.                 $data  = array($contentLength);
  64.                 $fix   $phpcsFile->addFixableError($error$stackPtr'IncorrectSingle'$data);
  65.                 if ($fix === true{
  66.                     $phpcsFile->fixer->replaceToken(($stackPtr + 1)' ');
  67.                 }
  68.             }
  69.         else if ($tokens[($stackPtr + 1)]['code'!== T_OPEN_PARENTHESIS{
  70.             $error 'Language constructs must be followed by a single space; expected "%s" but found "%s"';
  71.             $data  = array(
  72.                       $tokens[$stackPtr]['content'].' '.$tokens[($stackPtr + 1)]['content'],
  73.                       $tokens[$stackPtr]['content'].$tokens[($stackPtr + 1)]['content'],
  74.                      );
  75.             $fix   $phpcsFile->addFixableError($error$stackPtr'Incorrect'$data);
  76.             if ($fix === true{
  77.                 $phpcsFile->fixer->addContent($stackPtr' ');
  78.             }
  79.         }//end if
  80.  
  81.     }//end process()
  82.  
  83.  
  84. }//end class

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