Conditions | 40 |
Paths | 124 |
Total Lines | 66 |
Code Lines | 56 |
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 |
||
115 | public static function compare( $value1, $value2, $operator ) { |
||
116 | switch ( $operator ) { |
||
117 | case '===': |
||
118 | $show = ( $value1 === $value2 ) ? true : false; |
||
119 | break; |
||
120 | case '==': |
||
121 | case '=': |
||
122 | case 'equals': |
||
123 | case 'equal': |
||
124 | $show = ( $value1 == $value2 ) ? true : false; |
||
125 | break; |
||
126 | case '!==': |
||
127 | $show = ( $value1 !== $value2 ) ? true : false; |
||
128 | break; |
||
129 | case '!=': |
||
130 | case 'not equal': |
||
131 | $show = ( $value1 != $value2 ) ? true : false; |
||
132 | break; |
||
133 | case '>=': |
||
134 | case 'greater or equal': |
||
135 | case 'equal or greater': |
||
136 | $show = ( $value1 >= $value2 ) ? true : false; |
||
137 | break; |
||
138 | case '<=': |
||
139 | case 'smaller or equal': |
||
140 | case 'equal or smaller': |
||
141 | $show = ( $value1 <= $value2 ) ? true : false; |
||
142 | break; |
||
143 | case '>': |
||
144 | case 'greater': |
||
145 | $show = ( $value1 > $value2 ) ? true : false; |
||
146 | break; |
||
147 | case '<': |
||
148 | case 'smaller': |
||
149 | $show = ( $value1 < $value2 ) ? true : false; |
||
150 | break; |
||
151 | case 'contains': |
||
152 | case 'in': |
||
153 | if ( is_array( $value1 ) && ! is_array( $value2 ) ) { |
||
154 | $array = $value1; |
||
155 | $string = $value2; |
||
156 | } elseif ( is_array( $value2 ) && ! is_array( $value1 ) ) { |
||
157 | $array = $value2; |
||
158 | $string = $value1; |
||
159 | } |
||
160 | if ( isset( $array ) && isset( $string ) ) { |
||
161 | if ( ! in_array( $string, $array ) ) { |
||
162 | $show = false; |
||
163 | } |
||
164 | } else { |
||
165 | if ( false === strrpos( $value1, $value2 ) && false === strpos( $value2, $value1 ) ) { |
||
166 | $show = false; |
||
167 | } |
||
168 | } |
||
169 | break; |
||
170 | default: |
||
171 | $show = ( $value1 == $value2 ) ? true : false; |
||
172 | |||
173 | } |
||
174 | |||
175 | if ( isset( $show ) ) { |
||
176 | return $show; |
||
177 | } |
||
178 | |||
179 | return true; |
||
180 | } |
||
181 | } |
||
182 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: