Completed
Push — php70 ( 83abde...b6aa8a )
by Wim
05:28 queued 02:49
created

ForbiddenNegativeBitshiftSniff::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 12
rs 9.4285
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_ForbiddenNegativeBitshift.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_ForbiddenNegativeBitshift.
14
 *
15
 * Bitwise shifts by negative number will throw an ArithmeticError in PHP 7.0 
16
 *
17
 * @category  PHP
18
 * @package   PHPCompatibility
19
 * @author    Wim Godden <[email protected]>
20
 */
21
class PHPCompatibility_Sniffs_PHP_ForbiddenNegativeBitshiftSniff extends PHPCompatibility_Sniff
22
{
23
24
    /**
25
     * If true, an error will be thrown; otherwise a warning.
26
     *
27
     * @var bool
28
     */
29
    protected $error = true;
30
31
    /**
32
     * Returns an array of tokens this test wants to listen for.
33
     *
34
     * @return array
35
     */
36
    public function register()
37
    {
38
        return array(T_SR);
39
40
    }//end register()
41
42
    /**
43
     * Processes this test, when one of its tokens is encountered.
44
     *
45
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
46
     * @param int                  $stackPtr  The position of the current token
47
     *                                        in the stack passed in $tokens.
48
     *
49
     * @return void
50
     */
51
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
52
    {
53
        if ($this->supportsAbove('7.0')) {
54
            $tokens = $phpcsFile->getTokens();
55
            
56
            $nextNumber = $phpcsFile->findNext(T_LNUMBER, $stackPtr, null, false, null, true);
57
            if ($tokens[$nextNumber - 1]['code'] == T_MINUS) {
58
                $error = 'Bitwise shifts by negative number will throw an ArithmeticError in PHP 7.0';
59
                $phpcsFile->addError($error, $nextNumber - 1);
60
            }
61
        }
62
    }//end process()
63
64
}//end class
65