3
* Squiz_Sniffs_Objects_ObjectInstantiationSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
* @link http://pear.php.net/package/PHP_CodeSniffer
17
* Squiz_Sniffs_Objects_ObjectInstantiationSniff.
19
* Ensures objects are assigned to a variable when instantiated.
22
* @package PHP_CodeSniffer
23
* @author Greg Sherwood <gsherwood@squiz.net>
24
* @author Marc McIntyre <mmcintyre@squiz.net>
25
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
26
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
27
* @version Release: 1.5.5
28
* @link http://pear.php.net/package/PHP_CodeSniffer
30
class Squiz_Sniffs_Objects_ObjectInstantiationSniff implements PHP_CodeSniffer_Sniff
35
* Registers the token types that this sniff wishes to listen to.
39
public function register()
47
* Process the tokens that this sniff is listening for.
49
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
50
* @param int $stackPtr The position in the stack where
51
* the token was found.
55
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
57
$tokens = $phpcsFile->getTokens();
59
$allowedTokens = PHP_CodeSniffer_Tokens::$emptyTokens;
60
$allowedTokens[] = T_BITWISE_AND;
62
$prev = $phpcsFile->findPrevious($allowedTokens, ($stackPtr - 1), null, true);
64
$allowedTokens = array(
73
if (in_array($tokens[$prev]['code'], $allowedTokens) === false) {
74
$error = 'New objects must be assigned to a variable';
75
$phpcsFile->addError($error, $stackPtr, 'NotAssigned');