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

Source for file ObjectInstantiationSniff.php

Documentation is available at ObjectInstantiationSniff.php

  1. <?php
  2. /**
  3.  * Ensures objects are assigned to a variable when instantiated.
  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\Objects;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class ObjectInstantiationSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Registers the token types that this sniff wishes to listen to.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         return array(T_NEW);
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Process the tokens that this sniff is listening for.
  34.      *
  35.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  36.      * @param int                         $stackPtr  The position in the stack where
  37.      *                                                the token was found.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.  
  45.         $allowedTokens   = Tokens::$emptyTokens;
  46.         $allowedTokens[= T_BITWISE_AND;
  47.  
  48.         $prev $phpcsFile->findPrevious($allowedTokens($stackPtr - 1)nulltrue);
  49.  
  50.         $allowedTokens = array(
  51.                           T_EQUAL        => true,
  52.                           T_DOUBLE_ARROW => true,
  53.                           T_THROW        => true,
  54.                           T_RETURN       => true,
  55.                           T_INLINE_THEN  => true,
  56.                           T_INLINE_ELSE  => true,
  57.                          );
  58.  
  59.         if (isset($allowedTokens[$tokens[$prev]['code']]=== false{
  60.             $error 'New objects must be assigned to a variable';
  61.             $phpcsFile->addError($error$stackPtr'NotAssigned');
  62.         }
  63.  
  64.     }//end process()
  65.  
  66.  
  67. }//end class

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