Conditions | 15 |
Paths | 10 |
Total Lines | 50 |
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 |
||
8 | function indent($json) |
||
9 | { |
||
10 | $result = ''; |
||
11 | $pos = 0; |
||
12 | $strLen = strlen($json); |
||
13 | $indentStr = ' '; |
||
14 | $newLine = "\n"; |
||
15 | $prevChar = ''; |
||
16 | $outOfQuotes = true; |
||
17 | |||
18 | for ($i=0; $i<=$strLen; $i++) { |
||
19 | |||
20 | // Grab the next character in the string. |
||
21 | $char = substr($json, $i, 1); |
||
22 | |||
23 | // Are we inside a quoted string? |
||
24 | if ($char == '"' && $prevChar != '\\') { |
||
25 | $outOfQuotes = !$outOfQuotes; |
||
26 | |||
27 | // If this character is the end of an element, |
||
28 | // output a new line and indent the next line. |
||
29 | } elseif (($char == '}' || $char == ']') && $outOfQuotes) { |
||
30 | $result .= $newLine; |
||
31 | $pos --; |
||
32 | for ($j=0; $j<$pos; $j++) { |
||
33 | $result .= $indentStr; |
||
34 | } |
||
35 | } |
||
36 | |||
37 | // Add the character to the result string. |
||
38 | $result .= $char; |
||
39 | |||
40 | // If the last character was the beginning of an element, |
||
41 | // output a new line and indent the next line. |
||
42 | if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { |
||
43 | $result .= $newLine; |
||
44 | if ($char == '{' || $char == '[') { |
||
45 | $pos ++; |
||
46 | } |
||
47 | |||
48 | for ($j = 0; $j < $pos; $j++) { |
||
49 | $result .= $indentStr; |
||
50 | } |
||
51 | } |
||
52 | |||
53 | $prevChar = $char; |
||
54 | } |
||
55 | |||
56 | return $result; |
||
57 | } |
||
58 | |||
126 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.