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

Source for file InlineIfDeclarationSniff.php

Documentation is available at InlineIfDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Tests the spacing of shorthand IF statements.
  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\ControlStructures;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class InlineIfDeclarationSniff 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_INLINE_THEN);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this sniff, 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 the
  36.      *                                         stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         $tokens $phpcsFile->getTokens();
  43.  
  44.         $openBracket  = null;
  45.         $closeBracket = null;
  46.         if (isset($tokens[$stackPtr]['nested_parenthesis']=== true{
  47.             $parens       $tokens[$stackPtr]['nested_parenthesis'];
  48.             $openBracket  array_pop($parens);
  49.             $closeBracket $tokens[$openBracket]['parenthesis_closer'];
  50.         }
  51.  
  52.         // Find the beginning of the statement. If we don't find a
  53.         // semicolon (end of statement) or comma (end of array value)
  54.         // then assume the content before the closing parenthesis is the end.
  55.         $else         $phpcsFile->findNext(T_INLINE_ELSE($stackPtr + 1));
  56.         $statementEnd $phpcsFile->findNext(array(T_SEMICOLONT_COMMA)($else + 1)$closeBracket);
  57.         if ($statementEnd === false{
  58.             $statementEnd $phpcsFile->findPrevious(T_WHITESPACE($closeBracket - 1)nulltrue);
  59.         }
  60.  
  61.         // Make sure it's all on the same line.
  62.         if ($tokens[$statementEnd]['line'!== $tokens[$stackPtr]['line']{
  63.             $error 'Inline shorthand IF statement must be declared on a single line';
  64.             $phpcsFile->addError($error$stackPtr'NotSingleLine');
  65.             return;
  66.         }
  67.  
  68.         // Make sure there are spaces around the question mark.
  69.         $contentBefore $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  70.         $contentAfter  $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  71.         if ($tokens[$contentBefore]['code'!== T_CLOSE_PARENTHESIS{
  72.             $error 'Inline shorthand IF statement requires brackets around comparison';
  73.             $phpcsFile->addError($error$stackPtr'NoBrackets');
  74.             return;
  75.         }
  76.  
  77.         $spaceBefore ($tokens[$stackPtr]['column'($tokens[$contentBefore]['column'$tokens[$contentBefore]['length']));
  78.         if ($spaceBefore !== 1{
  79.             $error 'Inline shorthand IF statement requires 1 space before THEN; %s found';
  80.             $data  = array($spaceBefore);
  81.             $phpcsFile->addError($error$stackPtr'SpacingBeforeThen'$data);
  82.         }
  83.  
  84.         $spaceAfter (($tokens[$contentAfter]['column']($tokens[$stackPtr]['column'+ 1));
  85.         if ($spaceAfter !== 1{
  86.             $error 'Inline shorthand IF statement requires 1 space after THEN; %s found';
  87.             $data  = array($spaceAfter);
  88.             $phpcsFile->addError($error$stackPtr'SpacingAfterThen'$data);
  89.         }
  90.  
  91.         // Make sure the ELSE has the correct spacing.
  92.         $inlineElse    $phpcsFile->findNext(T_INLINE_ELSE($stackPtr + 1)$statementEndfalse);
  93.         $contentBefore $phpcsFile->findPrevious(T_WHITESPACE($inlineElse - 1)nulltrue);
  94.         $contentAfter  $phpcsFile->findNext(T_WHITESPACE($inlineElse + 1)nulltrue);
  95.  
  96.         $spaceBefore ($tokens[$inlineElse]['column'($tokens[$contentBefore]['column'$tokens[$contentBefore]['length']));
  97.         if ($spaceBefore !== 1{
  98.             $error 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
  99.             $data  = array($spaceBefore);
  100.             $phpcsFile->addError($error$inlineElse'SpacingBeforeElse'$data);
  101.         }
  102.  
  103.         $spaceAfter (($tokens[$contentAfter]['column']($tokens[$inlineElse]['column'+ 1));
  104.         if ($spaceAfter !== 1{
  105.             $error 'Inline shorthand IF statement requires 1 space after ELSE; %s found';
  106.             $data  = array($spaceAfter);
  107.             $phpcsFile->addError($error$inlineElse'SpacingAfterElse'$data);
  108.         }
  109.  
  110.     }//end process()
  111.  
  112.  
  113. }//end class

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