Conditions | 15 |
Paths | 256 |
Total Lines | 105 |
Code Lines | 54 |
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 |
||
152 | public function format($source) |
||
153 | { |
||
154 | // We does not indent <script> body. Instead, it temporary removes it from the code, indents the input, and restores the script body. |
||
155 | $tempScriptElements = []; |
||
156 | |||
157 | if (preg_match_all('/<script\b[^>]*>([\s\S]*?)<\/script>/mi', $source, $matches)) { |
||
158 | $tempScriptElements = $matches[ 0 ]; |
||
159 | |||
160 | foreach ($matches[ 0 ] as $i => $match) { |
||
161 | $source = str_replace($match, '<script>' . ($i + 1) . '</script>', $source); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | // Removing double whitespaces to make the source code easier to read. |
||
166 | // With exception of <pre>/ CSS white-space changing the default behaviour, double whitespace is meaningless in HTML output. |
||
167 | // This reason alone is sufficient not to use indentation in production. |
||
168 | $source = str_replace("\t", '', $source); |
||
169 | $source = preg_replace('/\s{2,}/', ' ', $source); |
||
170 | |||
171 | // Remove inline elements and replace them with text entities. |
||
172 | $tempInlineElements = []; |
||
173 | |||
174 | if (preg_match_all( |
||
175 | '/<(' . implode('|', $this->inlineElements) . ')[^>]*>(?:[^<]*)<\/\1>/', |
||
176 | $source, |
||
177 | $matches |
||
178 | )) { |
||
179 | $tempInlineElements = $matches[ 0 ]; |
||
180 | |||
181 | foreach ($matches[ 0 ] as $i => $match) { |
||
182 | $source = str_replace($match, 'ᐃ' . ($i + 1) . 'ᐃ', $source); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $output = ''; |
||
187 | |||
188 | $nextLineIndentationLevel = 0; |
||
189 | |||
190 | do { |
||
191 | $indentationLevel = $nextLineIndentationLevel; |
||
192 | |||
193 | $patterns = [ |
||
194 | // block tag |
||
195 | '/^(<([a-z]+)(?:[^>]*)>(?:[^<]*)<\/(?:\2)>)/' => static::MATCH_INDENT_NO, |
||
196 | // DOCTYPE |
||
197 | '/^<!([^>]*)>/' => static::MATCH_INDENT_NO, |
||
198 | // tag with implied closing |
||
199 | '/^<(input|link|meta|base|br|img|hr)([^>]*)>/' => static::MATCH_INDENT_NO, |
||
200 | // opening tag |
||
201 | '/^<[^\/]([^>]*)>/' => static::MATCH_INDENT_INCREASE, |
||
202 | // closing tag |
||
203 | '/^<\/([^>]*)>/' => static::MATCH_INDENT_DECREASE, |
||
204 | // self-closing tag |
||
205 | '/^<(.+)\/>/' => static::MATCH_INDENT_DECREASE, |
||
206 | // whitespace |
||
207 | '/^(\s+)/' => static::MATCH_DISCARD, |
||
208 | // text node |
||
209 | '/([^<]+)/' => static::MATCH_INDENT_NO, |
||
210 | ]; |
||
211 | |||
212 | foreach ($patterns as $pattern => $rule) { |
||
213 | if ($match = preg_match($pattern, $source, $matches)) { |
||
214 | if (function_exists('mb_substr')) { |
||
215 | $source = mb_substr($source, mb_strlen($matches[ 0 ])); |
||
216 | } else { |
||
217 | $source = substr($source, strlen($matches[ 0 ])); |
||
218 | } |
||
219 | |||
220 | if ($rule === static::MATCH_DISCARD) { |
||
221 | break; |
||
222 | } |
||
223 | |||
224 | if ($rule === static::MATCH_INDENT_NO) { |
||
225 | |||
226 | } else { |
||
227 | if ($rule === static::MATCH_INDENT_DECREASE) { |
||
228 | $nextLineIndentationLevel--; |
||
229 | $indentationLevel--; |
||
230 | } else { |
||
231 | $nextLineIndentationLevel++; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | if ($indentationLevel < 0) { |
||
236 | $indentationLevel = 0; |
||
237 | } |
||
238 | |||
239 | $output .= str_repeat($this->indentCharacter, $indentationLevel) . $matches[ 0 ] . "\n"; |
||
240 | |||
241 | break; |
||
242 | } |
||
243 | } |
||
244 | } while ($match); |
||
245 | |||
246 | $output = preg_replace('/(<(\w+)[^>]*>)\s*(<\/\2>)/', '\\1\\3', $output); |
||
247 | |||
248 | foreach ($tempScriptElements as $i => $original) { |
||
249 | $output = str_replace('<script>' . ($i + 1) . '</script>', $original, $output); |
||
250 | } |
||
251 | |||
252 | foreach ($tempInlineElements as $i => $original) { |
||
253 | $output = str_replace('ᐃ' . ($i + 1) . 'ᐃ', $original, $output); |
||
254 | } |
||
255 | |||
256 | return trim($output); |
||
257 | } |
||
258 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.