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\Files\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.         }
  75.  
  76.         $spaceBefore ($tokens[$stackPtr]['column'($tokens[$contentBefore]['column'$tokens[$contentBefore]['length']));
  77.         if ($spaceBefore !== 1{
  78.             $error 'Inline shorthand IF statement requires 1 space before THEN; %s found';
  79.             $data  = array($spaceBefore);
  80.             $fix   $phpcsFile->addFixableError($error$stackPtr'SpacingBeforeThen'$data);
  81.             if ($fix === true{
  82.                 if ($spaceBefore === 0{
  83.                     $phpcsFile->fixer->addContentBefore($stackPtr' ');
  84.                 else {
  85.                     $phpcsFile->fixer->replaceToken(($stackPtr - 1)' ');
  86.                 }
  87.             }
  88.         }
  89.  
  90.         // If there is no content between the ? and the : operators, then they are
  91.         // trying to replicate an elvis operator, even though PHP doesn't have one.
  92.         // In this case, we want no spaces between the two operators so ?: looks like
  93.         // an operator itself.
  94.         $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  95.         if ($tokens[$next]['code'=== T_INLINE_ELSE{
  96.             $inlineElse $next;
  97.             if ($inlineElse !== ($stackPtr + 1)) {
  98.                 $error 'Inline shorthand IF statement without THEN statement requires 0 spaces between THEN and ELSE';
  99.                 $fix   $phpcsFile->addFixableError($error$stackPtr'ElvisSpacing');
  100.                 if ($fix === true{
  101.                     $phpcsFile->fixer->replaceToken(($stackPtr + 1)'');
  102.                 }
  103.             }
  104.         else {
  105.             $spaceAfter (($tokens[$contentAfter]['column']($tokens[$stackPtr]['column'+ 1));
  106.             if ($spaceAfter !== 1{
  107.                 $error 'Inline shorthand IF statement requires 1 space after THEN; %s found';
  108.                 $data  = array($spaceAfter);
  109.                 $fix   $phpcsFile->addFixableError($error$stackPtr'SpacingAfterThen'$data);
  110.                 if ($fix === true{
  111.                     if ($spaceAfter === 0{
  112.                         $phpcsFile->fixer->addContent($stackPtr' ');
  113.                     else {
  114.                         $phpcsFile->fixer->replaceToken(($stackPtr + 1)' ');
  115.                     }
  116.                 }
  117.             }
  118.  
  119.             // Make sure the ELSE has the correct spacing.
  120.             $inlineElse    $phpcsFile->findNext(T_INLINE_ELSE($stackPtr + 1)$statementEndfalse);
  121.             $contentBefore $phpcsFile->findPrevious(T_WHITESPACE($inlineElse - 1)nulltrue);
  122.             $spaceBefore   ($tokens[$inlineElse]['column'($tokens[$contentBefore]['column'$tokens[$contentBefore]['length']));
  123.             if ($spaceBefore !== 1{
  124.                 $error 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
  125.                 $data  = array($spaceBefore);
  126.                 $fix   $phpcsFile->addFixableError($error$inlineElse'SpacingBeforeElse'$data);
  127.                 if ($fix === true{
  128.                     if ($spaceBefore === 0{
  129.                         $phpcsFile->fixer->addContentBefore($inlineElse' ');
  130.                     else {
  131.                         $phpcsFile->fixer->replaceToken(($inlineElse - 1)' ');
  132.                     }
  133.                 }
  134.             }
  135.         }//end if
  136.  
  137.         $contentAfter $phpcsFile->findNext(T_WHITESPACE($inlineElse + 1)nulltrue);
  138.         $spaceAfter   (($tokens[$contentAfter]['column']($tokens[$inlineElse]['column'+ 1));
  139.         if ($spaceAfter !== 1{
  140.             $error 'Inline shorthand IF statement requires 1 space after ELSE; %s found';
  141.             $data  = array($spaceAfter);
  142.             $fix   $phpcsFile->addFixableError($error$inlineElse'SpacingAfterElse'$data);
  143.             if ($fix === true{
  144.                 if ($spaceAfter === 0{
  145.                     $phpcsFile->fixer->addContent($inlineElse' ');
  146.                 else {
  147.                     $phpcsFile->fixer->replaceToken(($inlineElse + 1)' ');
  148.                 }
  149.             }
  150.         }
  151.  
  152.     }//end process()
  153.  
  154.  
  155. }//end class

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