Conditions | 21 |
Paths | 114 |
Total Lines | 109 |
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 |
||
58 | public function process(File $phpcsFile, $stackPtr) |
||
59 | { |
||
60 | $tokens = $phpcsFile->getTokens(); |
||
61 | $arrayStart = $tokens[$stackPtr]['parenthesis_opener']; |
||
62 | $arrayEnd = $tokens[$arrayStart]['parenthesis_closer']; |
||
63 | |||
64 | if ($arrayStart !== ($stackPtr + 1)) { |
||
65 | $error = 'There must be no space between the Array keyword and the opening parenthesis'; |
||
66 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword'); |
||
67 | if ($fix === true) { |
||
68 | $phpcsFile->fixer->beginChangeset(); |
||
69 | |||
70 | for ($i = ($stackPtr + 1); $i < $arrayStart; $i++) { |
||
71 | $phpcsFile->fixer->replaceToken($i, ''); |
||
72 | } |
||
73 | |||
74 | $phpcsFile->fixer->endChangeset(); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | // Check for empty arrays. |
||
79 | $content = $phpcsFile->findNext(array(T_WHITESPACE), ($arrayStart + 1), ($arrayEnd + 1), true); |
||
80 | if ($content === $arrayEnd) { |
||
81 | // Empty array, but if the brackets aren't together, there's a problem. |
||
82 | if (($arrayEnd - $arrayStart) !== 1) { |
||
83 | $error = 'Empty array declaration must have no space between the parentheses'; |
||
84 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceInEmptyArray'); |
||
85 | if ($fix === true) { |
||
86 | $phpcsFile->fixer->beginChangeset(); |
||
87 | |||
88 | for ($i = ($arrayStart + 1); $i < $arrayEnd; $i++) { |
||
89 | $phpcsFile->fixer->replaceToken($i, ''); |
||
90 | } |
||
91 | |||
92 | $phpcsFile->fixer->endChangeset(); |
||
93 | } |
||
94 | |||
95 | // We can return here because there is nothing else to check. All code |
||
96 | // below can assume that the array is not empty. |
||
97 | return; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | $lastItem = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($arrayEnd - 1), $stackPtr, true); |
||
102 | |||
103 | // Empty array. |
||
104 | if ($lastItem === $arrayStart) { |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | // Inline array. |
||
109 | $isInlineArray = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line']; |
||
110 | |||
111 | // Check if the last item in a multiline array has a "closing" comma. |
||
112 | if ($tokens[$lastItem]['code'] !== T_COMMA && $isInlineArray === false) { |
||
113 | $error = 'A comma should follow the last multiline array item. Found: '.$tokens[$lastItem]['content']; |
||
114 | $fix = $phpcsFile->addFixableWarning($error, $lastItem, 'NoLastComma'); |
||
|
|||
115 | if ($fix === true) { |
||
116 | $phpcsFile->fixer->beginChangeset(); |
||
117 | $phpcsFile->fixer->addContent($lastItem, ','); |
||
118 | $phpcsFile->fixer->endChangeset(); |
||
119 | } |
||
120 | |||
121 | return; |
||
122 | } |
||
123 | |||
124 | if ($isInlineArray === true) { |
||
125 | if ($tokens[$lastItem]['code'] === T_COMMA) { |
||
126 | $error = 'Comma not allowed after last value in single-line array declaration'; |
||
127 | $fix = $phpcsFile->addFixableWarning($error, $lastItem, 'LastComma'); |
||
128 | if ($fix === true) { |
||
129 | $phpcsFile->fixer->beginChangeset(); |
||
130 | $phpcsFile->fixer->replaceToken($lastItem, ''); |
||
131 | $phpcsFile->fixer->endChangeset(); |
||
132 | } |
||
133 | |||
134 | return; |
||
135 | } |
||
136 | |||
137 | // Inline array must not have spaces within parenthesis. |
||
138 | if ($content !== ($arrayStart + 1)) { |
||
139 | $error = 'Space found after opening parenthesis of Array'; |
||
140 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen'); |
||
141 | if ($fix === true) { |
||
142 | $phpcsFile->fixer->beginChangeset(); |
||
143 | |||
144 | for ($i = ($arrayStart + 1); $i < $content; $i++) { |
||
145 | $phpcsFile->fixer->replaceToken($i, ''); |
||
146 | } |
||
147 | |||
148 | $phpcsFile->fixer->endChangeset(); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | if ($lastItem !== ($arrayEnd - 1)) { |
||
153 | $error = 'Space found before closing parenthesis of Array'; |
||
154 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose'); |
||
155 | if ($fix === true) { |
||
156 | $phpcsFile->fixer->beginChangeset(); |
||
157 | |||
158 | for ($i = ($lastItem + 1); $i < $arrayEnd; $i++) { |
||
159 | $phpcsFile->fixer->replaceToken($i, ''); |
||
160 | } |
||
161 | |||
162 | $phpcsFile->fixer->endChangeset(); |
||
163 | } |
||
164 | } |
||
165 | }//end if |
||
166 | }//end process() |
||
167 | }//end class |
||
168 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.