Completed
Pull Request — master (#456)
by
unknown
02:14
created

ForbiddenBreakContinueVariableArgumentsSniff   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 74
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 103.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
     * Error types this sniff handles for forbidden break/continue arguments.
29
     *
30
     * Array key is the error code. Array value will be used as part of the error message.
31
     *
32
     * @var array
33
     */
34
    private $errorTypes = array(
35
        'variableArgument' => 'a variable argument',
36
        'zeroArgument'     => '0 as an argument',
37
    );
38
39
    /**
40
     * Returns an array of tokens this test wants to listen for.
41
     *
42
     * @return array
43
     */
44
    public function register()
45
    {
46
        return array(T_BREAK, T_CONTINUE);
47
48
    }//end register()
49
50
    /**
51
     * Processes this test, when one of its tokens is encountered.
52
     *
53
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
54
     * @param int                  $stackPtr  The position of the current token in the
55
     *                                        stack passed in $tokens.
56
     *
57
     * @return void
58
     */
59
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
60
    {
61
        if ($this->supportsAbove('5.4') === false) {
62
            return;
63
        }
64
65
        $tokens             = $phpcsFile->getTokens();
66
        $nextSemicolonToken = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr), null, false);
67
        $errorType          = '';
68
        for ($curToken = $stackPtr + 1; $curToken < $nextSemicolonToken; $curToken++) {
69
            if ($tokens[$curToken]['type'] === 'T_STRING') {
70
                // If the next non-whitespace token after the string
71
                // is an opening parenthesis then it's a function call.
72
                $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $curToken + 1, null, true);
73
                if ($tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS) {
74
                    $errorType = 'variableArgument';
75
                    break;
76
                }
77
78
            } elseif (in_array($tokens[$curToken]['type'], array('T_VARIABLE', 'T_FUNCTION', 'T_CLOSURE'), true)) {
79
                $errorType = 'variableArgument';
80
                break;
81
82
            } elseif ($tokens[$curToken]['type'] === 'T_LNUMBER' && $tokens[$curToken]['content'] === '0') {
83
                $errorType = 'zeroArgument';
84
                break;
85
            }
86
            } elseif ($tokens[$curToken]['type'] === 'T_LNUMBER') {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ELSEIF
Loading history...
87
                $errorType = 'variableArgument';
88
                break;
89
            }
90
91
        }
92
93
        if ($errorType !== '') {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $errorType.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
94
            $error     = 'Using %s on break or continue is forbidden since PHP 5.4';
95
            $errorCode = $errorType.'Found';
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $errorCode.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
96
            $data      = array($this->errorTypes[$errorType]);
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $data.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
97
98
            $phpcsFile->addError($error, $stackPtr, $errorCode, $data);
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $phpcsFile.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
99
        }
100
101
    }//end process()
102
103
}//end class
104