Completed
Pull Request — master (#209)
by
unknown
03:12
created

process()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 35
rs 4.909
cc 9
eloc 21
nc 13
nop 2
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
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...
26
{
27
28
    /**
29
     * Returns an array of tokens this test wants to listen for.
30
     *
31
     * @return array
32
     */
33
    public function register()
34
    {
35
        return array(T_BREAK, T_CONTINUE);
36
37
    }//end register()
38
39
    /**
40
     * Processes this test, when one of its tokens is encountered.
41
     *
42
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
43
     * @param int                  $stackPtr  The position of the current token in the
44
     *                                        stack passed in $tokens.
45
     *
46
     * @return void
47
     */
48
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
49
    {
50
        if ($this->supportsAbove('5.4') === false) {
51
            return;
52
        }
53
54
        $tokens             = $phpcsFile->getTokens();
55
        $nextSemicolonToken = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr), null, false);
56
        $isError            = false;
57
        for ($curToken = $stackPtr + 1; $curToken < $nextSemicolonToken; $curToken++) {
58
            if ($tokens[$curToken]['type'] === 'T_STRING') {
59
                // If the next non-whitespace token after the string
60
                // is an opening parenthesis then it's a function call.
61
                $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $curToken + 1, null, true);
62
                if ($tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS) {
63
                    $isError = true;
64
                    break;
65
                }
66
            }
67
            else if (in_array($tokens[$curToken]['type'], array('T_VARIABLE', 'T_FUNCTION', 'T_CLOSURE'), true)) {
68
                $isError = true;
69
                break;
70
            }
71
            else if ($tokens[$curToken]['type'] === 'T_LNUMBER' && $tokens[$curToken]['content'] === '0') {
72
                $isError = true;
73
                break;
74
            }
75
        }
76
77
        if ($isError === true) {
78
            $error = 'Using a variable argument or zero value on break or continue is forbidden since PHP 5.4';
79
            $phpcsFile->addError($error, $stackPtr);
80
        }
81
82
    }//end process()
83
84
}//end class
85