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

Source for file DisallowBooleanStatementSniff.php

Documentation is available at DisallowBooleanStatementSniff.php

  1. <?php
  2. /**
  3.  * Ensures that boolean operators are only used inside control structure conditions.
  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\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class DisallowBooleanStatementSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         return Tokens::$booleanOperators;
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Processes this test, when one of its tokens is encountered.
  34.      *
  35.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  36.      * @param int                         $stackPtr  The position of the current token
  37.      *                                                in the stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.         if (isset($tokens[$stackPtr]['nested_parenthesis']=== true{
  45.             $foundOwner = false;
  46.             foreach ($tokens[$stackPtr]['nested_parenthesis'as $open => $close{
  47.                 if (isset($tokens[$open]['parenthesis_owner']=== true{
  48.                     // Any owner means we are not just a simple statement.
  49.                     return;
  50.                 }
  51.             }
  52.         }
  53.  
  54.         $error 'Boolean operators are not allowed outside of control structure conditions';
  55.         $phpcsFile->addError($error$stackPtr'Found');
  56.  
  57.     }//end process()
  58.  
  59.  
  60. }//end class

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