Completed
Push — master ( 78e1c3...ce2ac5 )
by Juliette
8s
created

ForbiddenBreakContinueOutsideLoopSniff   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 5.62 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 5
loc 89
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 1
C process() 5 38 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
            $phpcsFile->addError($error, $stackPtr, 'Found', $data);
105
        } else {
106
            $phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
107
        }
108
109
    }//end process()
110
111
}//end class
112