Completed
Push — master ( 01d73e...c6357c )
by Juliette
03:01
created

process()   D

Complexity

Conditions 10
Paths 13

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 39
rs 4.8196
cc 10
eloc 25
nc 13
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_ForbiddenBreakContinueVariableArguments.
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_ForbiddenBreakContinueVariableArguments.
15
 *
16
 * Forbids variable arguments on break or continue statements.
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_ForbiddenBreakContinueVariableArgumentsSniff extends PHPCompatibility_Sniff
26
{
27
    const ERROR_TYPE_VARIABLE = 'a variable argument';
28
    const ERROR_TYPE_ZERO = '0 as an argument';
29
30
    /**
31
     * Returns an array of tokens this test wants to listen for.
32
     *
33
     * @return array
34
     */
35
    public function register()
36
    {
37
        return array(T_BREAK, T_CONTINUE);
38
39
    }//end register()
40
41
    /**
42
     * Processes this test, when one of its tokens is encountered.
43
     *
44
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
45
     * @param int                  $stackPtr  The position of the current token in the
46
     *                                        stack passed in $tokens.
47
     *
48
     * @return void
49
     */
50
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
51
    {
52
        if ($this->supportsAbove('5.4') === false) {
53
            return;
54
        }
55
56
        $tokens             = $phpcsFile->getTokens();
57
        $nextSemicolonToken = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr), null, false);
58
        $isError            = false;
59
        $errorType          = '';
60
        for ($curToken = $stackPtr + 1; $curToken < $nextSemicolonToken; $curToken++) {
61
            if ($tokens[$curToken]['type'] === 'T_STRING') {
62
                // If the next non-whitespace token after the string
63
                // is an opening parenthesis then it's a function call.
64
                $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $curToken + 1, null, true);
65
                if ($tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS) {
66
                    $isError = true;
67
                    $errorType = self::ERROR_TYPE_VARIABLE;
68
                    break;
69
                }
70
            }
71
            else if (in_array($tokens[$curToken]['type'], array('T_VARIABLE', 'T_FUNCTION', 'T_CLOSURE'), true)) {
72
                $isError = true;
73
                $errorType = self::ERROR_TYPE_VARIABLE;
74
                break;
75
            }
76
            else if ($tokens[$curToken]['type'] === 'T_LNUMBER' && $tokens[$curToken]['content'] === '0') {
77
                $isError = true;
78
                $errorType = self::ERROR_TYPE_ZERO;
79
                break;
80
            }
81
        }
82
83
        if ($isError === true && !empty($errorType)) {
84
            $error = 'Using ' . $errorType . ' on break or continue is forbidden since PHP 5.4';
85
            $phpcsFile->addError($error, $stackPtr);
86
        }
87
88
    }//end process()
89
90
}//end class
91