Conditions | 35 |
Paths | > 20000 |
Total Lines | 126 |
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 |
||
228 | function vp_scan_file( $file, $tmp_file = null, $use_parser = false ) { |
||
229 | $real_file = vp_get_real_file_path( $file, $tmp_file ); |
||
230 | $file_size = file_exists( $real_file ) ? @filesize( $real_file ) : 0; |
||
231 | if ( !is_readable( $real_file ) || !$file_size || $file_size > apply_filters( 'scan_max_file_size', 3 * 1024 * 1024 ) ) { // don't scan empty or files larger than 3MB. |
||
232 | return false; |
||
233 | } |
||
234 | |||
235 | $file_content = null; |
||
236 | $file_parsed = null; |
||
237 | $skip_file = apply_filters_ref_array( 'pre_scan_file', array ( false, $file, $real_file, &$file_content ) ); |
||
238 | if ( false !== $skip_file ) { // maybe detect malware without regular expressions. |
||
239 | return $skip_file; |
||
240 | } |
||
241 | |||
242 | if ( !vp_is_interesting_file( $file ) ) { // only scan relevant files. |
||
243 | return false; |
||
244 | } |
||
245 | |||
246 | if ( !isset( $GLOBALS['vp_signatures'] ) ) { |
||
247 | $GLOBALS['vp_signatures'] = array(); |
||
248 | } |
||
249 | |||
250 | $found = array (); |
||
251 | foreach ( $GLOBALS['vp_signatures'] as $signature ) { |
||
252 | if ( !is_object( $signature ) || !isset( $signature->patterns ) ) { |
||
253 | continue; |
||
254 | } |
||
255 | // if there is no filename_regex, we assume it's the same of vp_is_interesting_file(). |
||
256 | if ( empty( $signature->filename_regex ) || preg_match( '#' . addcslashes( $signature->filename_regex, '#' ) . '#i', $file ) ) { |
||
257 | if ( null === $file_content || !is_array( $file_content ) ) { |
||
258 | $file_content = @file( $real_file ); |
||
259 | |||
260 | if ( $file_content === false ) { |
||
261 | return false; |
||
262 | } |
||
263 | |||
264 | if ( $use_parser ) { |
||
265 | $file_parsed = split_file_to_php_html( $real_file ); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | $is_vulnerable = true; |
||
270 | |||
271 | $code = $file_content; |
||
272 | |||
273 | if ( $use_parser ) { |
||
274 | // use the language specified in the signature if it has one |
||
275 | if ( ! empty( $signature->target_language ) && array_key_exists( $signature->target_language, $file_parsed ) ) { |
||
276 | $code = $file_parsed[ $signature->target_language ]; |
||
277 | |||
278 | |||
279 | } |
||
280 | } |
||
281 | |||
282 | $matches = array(); |
||
283 | if ( ! empty( $signature->patterns ) ) { |
||
284 | foreach ( $signature->patterns as $pattern ) { |
||
285 | $match = preg_grep( '#' . addcslashes( $pattern, '#' ) . '#im', $code ); |
||
286 | if ( empty( $match ) ) { |
||
287 | $is_vulnerable = false; |
||
288 | break; |
||
289 | } |
||
290 | |||
291 | $matches += $match; |
||
292 | } |
||
293 | } |
||
294 | |||
295 | // convert the matched line to an array of details showing context around the lines |
||
296 | $lines = array(); |
||
297 | |||
298 | $lines_parsed = array(); |
||
299 | |||
300 | $line_indices_parsed = array(); |
||
301 | |||
302 | if ( $use_parser ) { |
||
303 | $line_indices_parsed = array_keys( $code ); |
||
304 | } |
||
305 | |||
306 | foreach ( $matches as $line => $text ) { |
||
307 | $lines = array_merge( $lines, range( $line - 1, $line + 1 ) ); |
||
308 | if ( $use_parser ) { |
||
309 | $idx = array_search( $line, $line_indices_parsed ); |
||
310 | |||
311 | // we might be looking at the first or last line; for the non-parsed case, array_intersect_key |
||
312 | // handles this transparently below; for the parsed case, since we have another layer of |
||
313 | // indirection, we have to handle that case here |
||
314 | $idx_around = array(); |
||
315 | if ( isset( $line_indices_parsed[ $idx - 1 ] ) ) { |
||
316 | $idx_around[] = $line_indices_parsed[ $idx - 1 ]; |
||
317 | } |
||
318 | $idx_around[] = $line_indices_parsed[ $idx ]; |
||
319 | if ( isset( $line_indices_parsed[ $idx + 1 ] ) ) { |
||
320 | $idx_around[] = $line_indices_parsed[ $idx + 1 ]; |
||
321 | } |
||
322 | $lines_parsed = array_merge( $lines_parsed, $idx_around ); |
||
323 | } |
||
324 | } |
||
325 | |||
326 | $details = array_intersect_key( $file_content, array_flip( $lines ) ); |
||
327 | |||
328 | $details_parsed = array(); |
||
329 | |||
330 | if ( $use_parser ) { |
||
331 | $details_parsed = array_intersect_key( $code, array_flip( $lines_parsed ) ); |
||
332 | } |
||
333 | |||
334 | // provide both 'matches' and 'details', as some places want 'matches' |
||
335 | // this matches the old behavior, which would add 'details' to some items, without replacing 'matches' |
||
336 | $debug_data = array( 'matches' => $matches, 'details' => $details ); |
||
337 | if ( $use_parser ) { |
||
338 | $debug_data['details_parsed'] = $details_parsed; |
||
339 | } |
||
340 | |||
341 | // Additional checking needed? |
||
342 | if ( method_exists( $signature, 'get_detailed_scanner' ) && $scanner = $signature->get_detailed_scanner() ) |
||
343 | $is_vulnerable = $scanner->scan( $is_vulnerable, $file, $real_file, $file_content, $debug_data ); |
||
344 | if ( $is_vulnerable ) { |
||
345 | $found[$signature->id] = $debug_data; |
||
346 | if ( isset( $signature->severity ) && $signature->severity > 8 ) // don't continue scanning |
||
347 | break; |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
352 | return apply_filters_ref_array( 'post_scan_file', array ( $found, $file, $real_file, &$file_content ) ); |
||
353 | } |
||
354 |
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: