Completed
Pull Request — master (#110)
by Wim
03:06
created

DeprecatedNewReferenceSniff::process()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 16
rs 8.8571
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_DeprecatedNewReferenceSniff.
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 * @copyright 2012 Cu.be Solutions bvba
11
 */
12
13
/**
14
 * PHPCompatibility_Sniffs_PHP_DeprecatedNewReferenceSniff.
15
 *
16
 * Discourages the use of assigning the return value of new by reference
17
 *
18
 * PHP version 5.4
19
 *
20
 * @category  PHP
21
 * @package   PHPCompatibility
22
 * @author    Wim Godden <[email protected]>
23
 * @copyright 2012 Cu.be Solutions bvba
24
 */
25
class PHPCompatibility_Sniffs_PHP_DeprecatedNewReferenceSniff extends PHPCompatibility_Sniff
26
{
27
28
    /**
29
     * If true, an error will be thrown; otherwise a warning.
30
     *
31
     * @var bool
32
     */
33
    protected $error = false;
34
35
    /**
36
     * Returns an array of tokens this test wants to listen for.
37
     *
38
     * @return array
39
     */
40
    public function register()
41
    {
42
        return array(T_NEW);
43
44
    }//end register()
45
46
    /**
47
     * Processes this test, when one of its tokens is encountered.
48
     *
49
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
50
     * @param int                  $stackPtr  The position of the current token in the
51
     *                                        stack passed in $tokens.
52
     *
53
     * @return void
54
     */
55
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
56
    {
57
        if ($this->supportsAbove('5.3')) {
58
            $tokens = $phpcsFile->getTokens();
59
            if ($tokens[$stackPtr - 1]['type'] == 'T_BITWISE_AND' || $tokens[$stackPtr - 2]['type'] == 'T_BITWISE_AND') {
60
                if ($this->supportsAbove('7.0')) {
61
                    $error = 'Assigning the return value of new by reference is deprecated in PHP 5.3 and forbidden in PHP 7.0';
62
                    $phpcsFile->addError($error, $stackPtr);
63
                } else {
64
                    $error = 'Assigning the return value of new by reference is deprecated in PHP 5.3';
65
                    $phpcsFile->addWarning($error, $stackPtr);
66
                }
67
            }
68
        }
69
70
    }//end process()
71
72
}//end class
73