Completed
Pull Request — master (#172)
by Juliette
03:08
created

process()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 25
rs 5.3846
cc 8
eloc 14
nc 8
nop 2
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
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...
24
{
25
26
    /**
27
     * Returns an array of tokens this test wants to listen for.
28
     *
29
     * @return array
30
     */
31
    public function register()
32
    {
33
        return array(T_SWITCH);
34
35
    }//end register()
36
37
    /**
38
     * Processes this test, when one of its tokens is encountered.
39
     *
40
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
41
     * @param int                  $stackPtr  The position of the current token
42
     *                                        in the stack passed in $tokens.
43
     *
44
     * @return void
45
     */
46
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
47
    {
48
        if ($this->supportsAbove('7.0') === false) {
49
            return;
50
        }
51
52
        $tokens = $phpcsFile->getTokens();
53
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
54
            return;
55
        }
56
57
        $defaultToken = $stackPtr;
58
        $defaultCount = 0;
59
        $targetLevel  = $tokens[$stackPtr]['level'] + 1;
60
        while ($defaultCount < 2 && $defaultToken = $phpcsFile->findNext(array(T_DEFAULT), $defaultToken + 1, $tokens[$stackPtr]['scope_closer'])) {
61
            // Same level or one below (= two default cases after each other).
62
            if ($tokens[$defaultToken]['level'] === $targetLevel || $tokens[$defaultToken]['level'] === ($targetLevel + 1)) {
63
                $defaultCount++;
64
            }
65
        }
66
67
        if ($defaultCount > 1) {
68
            $phpcsFile->addError('Switch statements can not have multiple default blocks since PHP 7.0', $stackPtr);
69
        }
70
    }//end process()
71
72
}//end class
73