| Conditions | 10 |
| Paths | 21 |
| Total Lines | 42 |
| 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 |
||
| 27 | private static function get_calling_class() { |
||
| 28 | // If WP_Importer doesn't exist, neither will any importer that extends it |
||
| 29 | if ( ! class_exists( 'WP_Importer' ) ){ |
||
| 30 | return 'unknown'; |
||
| 31 | } |
||
| 32 | |||
| 33 | $action = current_filter(); |
||
| 34 | $backtrace = wp_debug_backtrace_summary( null, 0, false ); |
||
| 35 | |||
| 36 | $do_action_pos = -1; |
||
| 37 | for ( $i = 0; $i < count( $backtrace ); $i++ ) { |
||
|
|
|||
| 38 | // Find the location in the stack of the calling action |
||
| 39 | if ( preg_match( "/^do_action\\(\'([^\']+)/", $backtrace[ $i ], $matches ) ) { |
||
| 40 | if ( $matches[1] === $action ) { |
||
| 41 | $do_action_pos = $i; |
||
| 42 | break; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | // if the action wasn't called, the calling class is unknown |
||
| 48 | if ( -1 === $do_action_pos ) { |
||
| 49 | return 'unknown'; |
||
| 50 | } |
||
| 51 | |||
| 52 | // continue iterating the stack looking for a caller that extends WP_Import |
||
| 53 | for ( $i = $do_action_pos + 1; $i < count( $backtrace ); $i++ ) { |
||
| 54 | // grab only class_name from the trace |
||
| 55 | list( $class_name ) = explode( '->', $backtrace[ $i ] ); |
||
| 56 | |||
| 57 | // check if the class extends WP_Importer |
||
| 58 | if ( class_exists( $class_name ) ) { |
||
| 59 | $parents = class_parents( $class_name ); |
||
| 60 | if ( $parents && in_array( 'WP_Importer', $parents ) ) { |
||
| 61 | return $class_name; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | // If we've exhausted the stack without a match, the calling class is unknown |
||
| 67 | return 'unknown'; |
||
| 68 | } |
||
| 69 | |||
| 95 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: