Conditions | 14 |
Paths | 12 |
Total Lines | 52 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
60 | protected function getType(&$value) |
||
61 | { |
||
62 | $type = self::T_UNKNOWN; |
||
63 | |||
64 | switch (true) { |
||
65 | // Recognize numeric values |
||
66 | case is_numeric($value): |
||
67 | if (false !== strpos($value, '.') || false !== stripos($value, 'e')) { |
||
68 | return self::T_FLOAT; |
||
|
|||
69 | } |
||
70 | |||
71 | return self::T_INTEGER; |
||
72 | |||
73 | // Recognize quoted strings |
||
74 | case "'" === $value[0]: |
||
75 | $value = str_replace("''", "'", substr($value, 1, strlen($value) - 2)); |
||
76 | |||
77 | return self::T_STRING; |
||
78 | |||
79 | case '"' === $value[0]: |
||
80 | $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2)); |
||
81 | |||
82 | return self::T_STRING; |
||
83 | |||
84 | case 'null' === $value: |
||
85 | return self::T_NULL; |
||
86 | |||
87 | // Recognize identifiers, aliased or qualified names |
||
88 | case ctype_alpha($value[0]) || '\\' === $value[0]: |
||
89 | return self::T_IDENTIFIER; |
||
90 | |||
91 | case ',' === $value: |
||
92 | return self::T_COMMA; |
||
93 | |||
94 | case '>' === $value: |
||
95 | return self::T_TYPE_END; |
||
96 | |||
97 | case '<' === $value: |
||
98 | return self::T_TYPE_START; |
||
99 | |||
100 | case ']' === $value: |
||
101 | return self::T_ARRAY_END; |
||
102 | |||
103 | case '[' === $value: |
||
104 | return self::T_ARRAY_START; |
||
105 | |||
106 | // Default |
||
107 | default: |
||
108 | // Do nothing |
||
109 | } |
||
110 | |||
111 | return $type; |
||
112 | } |
||
114 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: