Conditions | 16 |
Paths | 13 |
Total Lines | 66 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
47 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
48 | { |
||
49 | if ($this->supportsAbove('7.1') === false) { |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | $tokens = $phpcsFile->getTokens(); |
||
54 | |||
55 | // Verify this use statement is used with a closure - if so, it has to have parenthesis before it. |
||
56 | $previousNonEmpty = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
||
57 | if ($previousNonEmpty === false || $tokens[$previousNonEmpty]['code'] !== T_CLOSE_PARENTHESIS |
||
58 | || isset($tokens[$previousNonEmpty]['parenthesis_opener']) === false |
||
59 | ) { |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | // ... and (a variable within) parenthesis after it. |
||
64 | $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
||
65 | if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== T_OPEN_PARENTHESIS) { |
||
66 | return; |
||
67 | } |
||
68 | |||
69 | if (isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false) { |
||
70 | // Live coding. |
||
71 | return; |
||
72 | } |
||
73 | |||
74 | $closurePtr = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($tokens[$previousNonEmpty]['parenthesis_opener'] - 1), null, true ); |
||
75 | if ($tokens[$closurePtr]['code'] !== T_CLOSURE) { |
||
76 | return; |
||
77 | } |
||
78 | |||
79 | // Get the parameters declared by the closure. |
||
80 | $closureParams = $this->getMethodParameters($phpcsFile, $closurePtr); |
||
81 | |||
82 | $errorMsg = 'Variables bound to a closure via the use construct cannot use the same name as superglobals, $this, or a declared parameter since PHP 7.1. Found: %s'; |
||
83 | |||
84 | for ($i = ($nextNonEmpty + 1); $i < $tokens[$nextNonEmpty]['parenthesis_closer']; $i++) { |
||
85 | if ($tokens[$i]['code'] !== T_VARIABLE) { |
||
86 | continue; |
||
87 | } |
||
88 | |||
89 | $variableName = $tokens[$i]['content']; |
||
90 | |||
91 | if ($variableName === '$this') { |
||
92 | $phpcsFile->addError($errorMsg, $i, 'FoundThis', array($variableName)); |
||
93 | continue; |
||
94 | } |
||
95 | |||
96 | if (in_array($variableName, $this->superglobals, true) === true) { |
||
97 | $phpcsFile->addError($errorMsg, $i, 'FoundSuperglobal', array($variableName)); |
||
98 | continue; |
||
99 | } |
||
100 | |||
101 | // Check whether it is one of the parameters declared by the closure. |
||
102 | if (empty($closureParams) === false) { |
||
103 | foreach ($closureParams as $param) { |
||
104 | if ($param['name'] === $variableName) { |
||
105 | $phpcsFile->addError($errorMsg, $i, 'FoundShadowParam', array($variableName)); |
||
106 | continue 2; |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | }//end process() |
||
113 | |||
115 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.