GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 699b70...879176 )
by Chris
13:23
created

ForEachLoopDeclarationSniff   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 223
Duplicated Lines 62.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 139
loc 223
rs 8.2608
c 0
b 0
f 0
wmc 40
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
F process() 139 180 39

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ForEachLoopDeclarationSniff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ForEachLoopDeclarationSniff, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <[email protected]>
10
 * @author    Marc McIntyre <[email protected]>
11
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
16
/**
17
 * Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff.
18
 *
19
 * Verifies that there is a space between each condition of foreach loops.
20
 *
21
 * @category  PHP
22
 * @package   PHP_CodeSniffer
23
 * @author    Greg Sherwood <[email protected]>
24
 * @author    Marc McIntyre <[email protected]>
25
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
26
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
27
 * @version   Release: @package_version@
28
 * @link      http://pear.php.net/package/PHP_CodeSniffer
29
 */
30
class Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff implements PHP_CodeSniffer_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...
31
{
32
33
34
    /**
35
     * How many spaces should follow the opening bracket.
36
     *
37
     * @var int
38
     */
39
    public $requiredSpacesAfterOpen = 0;
40
41
    /**
42
     * How many spaces should precede the closing bracket.
43
     *
44
     * @var int
45
     */
46
    public $requiredSpacesBeforeClose = 0;
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(T_FOREACH);
57
58
    }//end register()
59
60
61
    /**
62
     * Processes this test, when one of its tokens is encountered.
63
     *
64
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
65
     * @param int                  $stackPtr  The position of the current token in the
66
     *                                        stack passed in $tokens.
67
     *
68
     * @return void
69
     */
70
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
71
    {
72
        $this->requiredSpacesAfterOpen   = (int) $this->requiredSpacesAfterOpen;
73
        $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
74
        $tokens = $phpcsFile->getTokens();
75
76
        $openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
77
        if ($openingBracket === false) {
78
            $error = 'Possible parse error: FOREACH has no opening parenthesis';
79
            $phpcsFile->addWarning($error, $stackPtr, 'MissingOpenParenthesis');
80
            return;
81
        }
82
83
        if (isset($tokens[$openingBracket]['parenthesis_closer']) === false) {
84
            $error = 'Possible parse error: FOREACH has no closing parenthesis';
85
            $phpcsFile->addWarning($error, $stackPtr, 'MissingCloseParenthesis');
86
            return;
87
        }
88
89
        $closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
90
91 View Code Duplication
        if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
0 ignored issues
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...
92
            $error = 'Space found after opening bracket of FOREACH loop';
93
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen');
94
            if ($fix === true) {
95
                $phpcsFile->fixer->replaceToken(($openingBracket + 1), '');
96
            }
97
        } else if ($this->requiredSpacesAfterOpen > 0) {
98
            $spaceAfterOpen = 0;
99
            if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
100
                $spaceAfterOpen = strlen($tokens[($openingBracket + 1)]['content']);
101
            }
102
103
            if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
104
                $error = 'Expected %s spaces after opening bracket; %s found';
105
                $data  = array(
106
                          $this->requiredSpacesAfterOpen,
107
                          $spaceAfterOpen,
108
                         );
109
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data);
110
                if ($fix === true) {
111
                    $padding = str_repeat(' ', $this->requiredSpacesAfterOpen);
112
                    if ($spaceAfterOpen === 0) {
113
                        $phpcsFile->fixer->addContent($openingBracket, $padding);
114
                    } else {
115
                        $phpcsFile->fixer->replaceToken(($openingBracket + 1), $padding);
116
                    }
117
                }
118
            }
119
        }//end if
120
121 View Code Duplication
        if ($this->requiredSpacesBeforeClose === 0 && $tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
0 ignored issues
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...
122
            $error = 'Space found before closing bracket of FOREACH loop';
123
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose');
124
            if ($fix === true) {
125
                $phpcsFile->fixer->replaceToken(($closingBracket - 1), '');
126
            }
127
        } else if ($this->requiredSpacesBeforeClose > 0) {
128
            $spaceBeforeClose = 0;
129
            if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
130
                $spaceBeforeClose = strlen($tokens[($closingBracket - 1)]['content']);
131
            }
132
133
            if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
134
                $error = 'Expected %s spaces before closing bracket; %s found';
135
                $data  = array(
136
                          $this->requiredSpacesBeforeClose,
137
                          $spaceBeforeClose,
138
                         );
139
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose', $data);
140
                if ($fix === true) {
141
                    $padding = str_repeat(' ', $this->requiredSpacesBeforeClose);
142
                    if ($spaceBeforeClose === 0) {
143
                        $phpcsFile->fixer->addContentBefore($closingBracket, $padding);
144
                    } else {
145
                        $phpcsFile->fixer->replaceToken(($closingBracket - 1), $padding);
146
                    }
147
                }
148
            }
149
        }//end if
150
151
        $asToken = $phpcsFile->findNext(T_AS, $openingBracket);
152
        if ($asToken === false) {
153
            $error = 'Possible parse error: FOREACH has no AS statement';
154
            $phpcsFile->addWarning($error, $stackPtr, 'MissingAs');
155
            return;
156
        }
157
158
        $content = $tokens[$asToken]['content'];
159 View Code Duplication
        if ($content !== strtolower($content)) {
0 ignored issues
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...
160
            $expected = strtolower($content);
161
            $error    = 'AS keyword must be lowercase; expected "%s" but found "%s"';
162
            $data     = array(
163
                         $expected,
164
                         $content,
165
                        );
166
167
            $fix = $phpcsFile->addFixableError($error, $asToken, 'AsNotLower', $data);
168
            if ($fix === true) {
169
                $phpcsFile->fixer->replaceToken($asToken, $expected);
170
            }
171
        }
172
173
        $doubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $asToken, $closingBracket);
174
175
        if ($doubleArrow !== false) {
176 View Code Duplication
            if ($tokens[($doubleArrow - 1)]['code'] !== T_WHITESPACE) {
0 ignored issues
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...
177
                $error = 'Expected 1 space before "=>"; 0 found';
178
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeArrow');
179
                if ($fix === true) {
180
                    $phpcsFile->fixer->addContentBefore($doubleArrow, ' ');
181
                }
182
            } else {
183
                if (strlen($tokens[($doubleArrow - 1)]['content']) !== 1) {
184
                    $spaces = strlen($tokens[($doubleArrow - 1)]['content']);
185
                    $error  = 'Expected 1 space before "=>"; %s found';
186
                    $data   = array($spaces);
187
                    $fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeArrow', $data);
188
                    if ($fix === true) {
189
                        $phpcsFile->fixer->replaceToken(($doubleArrow - 1), ' ');
190
                    }
191
                }
192
            }
193
194 View Code Duplication
            if ($tokens[($doubleArrow + 1)]['code'] !== T_WHITESPACE) {
0 ignored issues
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...
195
                $error = 'Expected 1 space after "=>"; 0 found';
196
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterArrow');
197
                if ($fix === true) {
198
                    $phpcsFile->fixer->addContent($doubleArrow, ' ');
199
                }
200
            } else {
201
                if (strlen($tokens[($doubleArrow + 1)]['content']) !== 1) {
202
                    $spaces = strlen($tokens[($doubleArrow + 1)]['content']);
203
                    $error  = 'Expected 1 space after "=>"; %s found';
204
                    $data   = array($spaces);
205
                    $fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterArrow', $data);
206
                    if ($fix === true) {
207
                        $phpcsFile->fixer->replaceToken(($doubleArrow + 1), ' ');
208
                    }
209
                }
210
            }
211
        }//end if
212
213 View Code Duplication
        if ($tokens[($asToken - 1)]['code'] !== T_WHITESPACE) {
0 ignored issues
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...
214
            $error = 'Expected 1 space before "as"; 0 found';
215
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeAs');
216
            if ($fix === true) {
217
                $phpcsFile->fixer->addContentBefore($asToken, ' ');
218
            }
219
        } else {
220
            if (strlen($tokens[($asToken - 1)]['content']) !== 1) {
221
                $spaces = strlen($tokens[($asToken - 1)]['content']);
222
                $error  = 'Expected 1 space before "as"; %s found';
223
                $data   = array($spaces);
224
                $fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeAs', $data);
225
                if ($fix === true) {
226
                    $phpcsFile->fixer->replaceToken(($asToken - 1), ' ');
227
                }
228
            }
229
        }
230
231 View Code Duplication
        if ($tokens[($asToken + 1)]['code'] !== T_WHITESPACE) {
0 ignored issues
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...
232
            $error = 'Expected 1 space after "as"; 0 found';
233
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterAs');
234
            if ($fix === true) {
235
                $phpcsFile->fixer->addContent($asToken, ' ');
236
            }
237
        } else {
238
            if (strlen($tokens[($asToken + 1)]['content']) !== 1) {
239
                $spaces = strlen($tokens[($asToken + 1)]['content']);
240
                $error  = 'Expected 1 space after "as"; %s found';
241
                $data   = array($spaces);
242
                $fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterAs', $data);
243
                if ($fix === true) {
244
                    $phpcsFile->fixer->replaceToken(($asToken + 1), ' ');
245
                }
246
            }
247
        }
248
249
    }//end process()
250
251
252
}//end class
253