ForbiddenBreakContinueOutsideLoopSniff::register()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\ForbiddenBreakContinueOutsideLoop.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\ForbiddenBreakContinueOutsideLoop.
18
 *
19
 * Forbids use of break or continue statements outside of looping structures.
20
 *
21
 * PHP version 7.0
22
 *
23
 * @category PHP
24
 * @package  PHPCompatibility
25
 * @author   Juliette Reinders Folmer <[email protected]>
26
 */
27
class ForbiddenBreakContinueOutsideLoopSniff extends Sniff
28
{
29
30
    /**
31
     * Token codes of control structure in which usage of break/continue is valid.
32
     *
33
     * @var array
34
     */
35
    protected $validLoopStructures = array(
36
        T_FOR     => true,
37
        T_FOREACH => true,
38
        T_WHILE   => true,
39
        T_DO      => true,
40
        T_SWITCH  => true,
41
    );
42
43
    /**
44
     * Token codes which did not correctly get a condition assigned in older PHPCS versions.
45
     *
46
     * @var array
47
     */
48
    protected $backCompat = array(
49
        T_CASE    => true,
50
        T_DEFAULT => true,
51
    );
52
53
    /**
54
     * Returns an array of tokens this test wants to listen for.
55
     *
56
     * @return array
57
     */
58
    public function register()
59
    {
60
        return array(
61
            T_BREAK,
62
            T_CONTINUE,
63
        );
64
65
    }//end register()
66
67
    /**
68
     * Processes this test, when one of its tokens is encountered.
69
     *
70
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
71
     * @param int                   $stackPtr  The position of the current token in the
72
     *                                         stack passed in $tokens.
73
     *
74
     * @return void
75
     */
76
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
77
    {
78
        $tokens = $phpcsFile->getTokens();
79
        $token  = $tokens[$stackPtr];
80
81
        // Check if the break/continue is within a valid loop structure.
82
        if (empty($token['conditions']) === false) {
83
            foreach ($token['conditions'] as $tokenCode) {
84
                if (isset($this->validLoopStructures[$tokenCode]) === true) {
85
                    return;
86
                }
87
            }
88
        } else {
89
            // Deal with older PHPCS versions.
90
            if (isset($token['scope_condition']) === true && isset($this->backCompat[$tokens[$token['scope_condition']]['code']]) === true) {
91
                return;
92
            }
93
        }
94
95
        // If we're still here, no valid loop structure container has been found, so throw an error.
96
        $error     = "Using '%s' outside of a loop or switch structure is invalid";
97
        $isError   = false;
98
        $errorCode = 'Found';
99
        $data      = array($token['content']);
100
101
        if ($this->supportsAbove('7.0')) {
102
            $error    .= ' and will throw a fatal error since PHP 7.0';
103
            $isError   = true;
104
            $errorCode = 'FatalError';
105
        }
106
107
        $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode, $data);
108
109
    }//end process()
110
111
}//end class
112