| Conditions | 11 |
| Paths | 19 |
| Total Lines | 45 |
| 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 |
||
| 125 | private static function get_calling_importer_class() { |
||
| 126 | // If WP_Importer doesn't exist, neither will any importer that extends it. |
||
| 127 | if ( ! class_exists( '\WP_Importer', false ) ) { |
||
| 128 | return 'unknown'; |
||
| 129 | } |
||
| 130 | |||
| 131 | $action = current_filter(); |
||
| 132 | $backtrace = debug_backtrace( false ); //phpcs:ignore PHPCompatibility.FunctionUse.NewFunctionParameters.debug_backtrace_optionsFound,WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
||
| 133 | |||
| 134 | $do_action_pos = -1; |
||
| 135 | $backtrace_len = count( $backtrace ); |
||
| 136 | for ( $i = 0; $i < $backtrace_len; $i++ ) { |
||
| 137 | // Find the location in the stack of the calling action. |
||
| 138 | if ( 'do_action' === $backtrace[ $i ]['function'] && $action === $backtrace[ $i ]['args'][0] ) { |
||
| 139 | $do_action_pos = $i; |
||
| 140 | break; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | // If the action wasn't called, the calling class is unknown. |
||
| 145 | if ( -1 === $do_action_pos ) { |
||
| 146 | return 'unknown'; |
||
| 147 | } |
||
| 148 | |||
| 149 | // Continue iterating the stack looking for a caller that extends WP_Importer. |
||
| 150 | for ( $i = $do_action_pos + 1; $i < $backtrace_len; $i++ ) { |
||
| 151 | // If there is no class on the trace, continue. |
||
| 152 | if ( ! isset( $backtrace[ $i ]['class'] ) ) { |
||
| 153 | continue; |
||
| 154 | } |
||
| 155 | |||
| 156 | $class_name = $backtrace[ $i ]['class']; |
||
| 157 | |||
| 158 | // Check if the class extends WP_Importer. |
||
| 159 | if ( class_exists( $class_name, false ) ) { |
||
| 160 | $parents = class_parents( $class_name, false ); |
||
| 161 | if ( $parents && in_array( '\WP_Importer', $parents, true ) ) { |
||
|
|
|||
| 162 | return $class_name; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | // If we've exhausted the stack without a match, the calling class is unknown. |
||
| 168 | return 'unknown'; |
||
| 169 | } |
||
| 170 | } |
||
| 171 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.