Completed
Push — php70 ( 2bfd9c...dafa05 )
by Wim
02:45
created

process()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 10
c 2
b 1
f 0
nc 6
nop 2
dl 0
loc 18
rs 8.8571
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_ForbiddenSwitchWithMultipleDefaultBlocksSniff.
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_ForbiddenSwitchWithMultipleDefaultBlocksSniff.
14
 *
15
 * Switch statements can not have multiple default blocks since PHP 7.0
16
 *
17
 * PHP version 7.0
18
 *
19
 * @category  PHP
20
 * @package   PHPCompatibility
21
 * @author    Wim Godden <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_ForbiddenSwitchWithMultipleDefaultBlocksSniff extends PHPCompatibility_Sniff
24
{
25
26
    /**
27
     * If true, an error will be thrown; otherwise a warning.
28
     *
29
     * @var bool
30
     */
31
    protected $error = true;
32
33
    /**
34
     * Returns an array of tokens this test wants to listen for.
35
     *
36
     * @return array
37
     */
38
    public function register()
39
    {
40
        return array(T_SWITCH);
41
42
    }//end register()
43
44
    /**
45
     * Processes this test, when one of its tokens is encountered.
46
     *
47
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
     * @param int                  $stackPtr  The position of the current token
49
     *                                        in the stack passed in $tokens.
50
     *
51
     * @return void
52
     */
53
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
    {
55
        if ($this->supportsAbove('7.0')) {
56
            $tokens = $phpcsFile->getTokens();
57
            
58
            $defaultToken = $stackPtr;
59
            $defaultCount = 0;
60
            if (isset($tokens[$stackPtr]['scope_closer'])) {
61
                while ($defaultToken = $phpcsFile->findNext(T_DEFAULT, $defaultToken + 1, $tokens[$stackPtr]['scope_closer'])) {
62
                    $defaultCount++;
63
                }
64
                
65
                if ($defaultCount > 1) {
66
                    $phpcsFile->addError('Switch statements can not have multiple default blocks since PHP 7.0', $stackPtr);
67
                }
68
            }
69
        }
70
    }//end process()
71
72
}//end class
73