Completed
Pull Request — master (#219)
by Juliette
03:40
created

ForbiddenNegativeBitshiftSniff::process()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 12
nc 4
nop 2
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
22
{
23
24
    /**
25
     * Returns an array of tokens this test wants to listen for.
26
     *
27
     * @return array
28
     */
29
    public function register()
30
    {
31
        return array(T_SR);
32
33
    }//end register()
34
35
    /**
36
     * Processes this test, when one of its tokens is encountered.
37
     *
38
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
39
     * @param int                  $stackPtr  The position of the current token
40
     *                                        in the stack passed in $tokens.
41
     *
42
     * @return void
43
     */
44
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
45
    {
46
        if ($this->supportsAbove('7.0') === false) {
47
            return;
48
        }
49
50
        $tokens = $phpcsFile->getTokens();
0 ignored issues
show
Unused Code introduced by
$tokens is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
52
        $nextNumber = $phpcsFile->findNext(T_LNUMBER, $stackPtr + 1, null, false, null, true);
53
        if($nextNumber === false || ($stackPtr + 1) === $nextNumber) {
54
            return;
55
        }
56
57
        $hasMinusSign = $phpcsFile->findNext(T_MINUS, $stackPtr + 1, $nextNumber, false, null, true);
58
        if($hasMinusSign === false) {
59
            return;
60
        }
61
62
        $error = 'Bitwise shifts by negative number will throw an ArithmeticError in PHP 7.0';
63
        $phpcsFile->addError($error, $hasMinusSign);
64
65
    }//end process()
66
67
}//end class
68