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

Source for file EmptyStatementSniff.php

Documentation is available at EmptyStatementSniff.php

  1. <?php
  2. /**
  3.  * This sniff class detected empty statement.
  4.  *
  5.  * This sniff implements the common algorithm for empty statement body detection.
  6.  * A body is considered as empty if it is completely empty or it only contains
  7.  * whitespace characters and/or comments.
  8.  *
  9.  * <code>
  10.  * stmt {
  11.  *   // foo
  12.  * }
  13.  * stmt (conditions) {
  14.  *   // foo
  15.  * }
  16.  * </code>
  17.  *
  18.  * @author    Manuel Pichler <mapi@manuel-pichler.de>
  19.  * @author    Greg Sherwood <gsherwood@squiz.net>
  20.  * @copyright 2007-2014 Manuel Pichler. All rights reserved.
  21.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  22.  */
  23.  
  24. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
  25.  
  26. use PHP_CodeSniffer\Sniffs\Sniff;
  27. use PHP_CodeSniffer\Files\File;
  28. use PHP_CodeSniffer\Util\Tokens;
  29.  
  30. class EmptyStatementSniff implements Sniff
  31. {
  32.  
  33.  
  34.     /**
  35.      * Registers the tokens that this sniff wants to listen for.
  36.      *
  37.      * @return int[] 
  38.      */
  39.     public function register()
  40.     {
  41.         return array(
  42.                 T_CATCH,
  43.                 T_DO,
  44.                 T_ELSE,
  45.                 T_ELSEIF,
  46.                 T_FOR,
  47.                 T_FOREACH,
  48.                 T_IF,
  49.                 T_SWITCH,
  50.                 T_TRY,
  51.                 T_WHILE,
  52.                );
  53.  
  54.     }//end register()
  55.  
  56.  
  57.     /**
  58.      * Processes this test, when one of its tokens is encountered.
  59.      *
  60.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  61.      * @param int                         $stackPtr  The position of the current token
  62.      *                                                in the stack passed in $tokens.
  63.      *
  64.      * @return void 
  65.      */
  66.     public function process(File $phpcsFile$stackPtr)
  67.     {
  68.         $tokens $phpcsFile->getTokens();
  69.         $token  $tokens[$stackPtr];
  70.  
  71.         // Skip statements without a body.
  72.         if (isset($token['scope_opener']=== false{
  73.             return;
  74.         }
  75.  
  76.         $next $phpcsFile->findNext(
  77.             Tokens::$emptyTokens,
  78.             ($token['scope_opener'+ 1),
  79.             ($token['scope_closer'- 1),
  80.             true
  81.         );
  82.  
  83.         if ($next !== false{
  84.             return;
  85.         }
  86.  
  87.         // Get token identifier.
  88.         $name  strtoupper($token['content']);
  89.         $error 'Empty %s statement detected';
  90.         $phpcsFile->addError($error$stackPtr'Detected'.$namearray($name));
  91.  
  92.     }//end process()
  93.  
  94.  
  95. }//end class

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