| Conditions | 23 |
| Paths | 51 |
| Total Lines | 83 |
| Lines | 3 |
| Ratio | 3.61 % |
| 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 |
||
| 43 | function l( $stuff = null ) { |
||
| 44 | // Do nothing when debugging is off. |
||
| 45 | if ( ! defined( 'WP_DEBUG' ) || WP_DEBUG === false ) { |
||
| 46 | return $stuff; |
||
| 47 | } |
||
| 48 | static $pageload; |
||
| 49 | // Call l() on each argument. |
||
| 50 | if ( func_num_args() > 1 ) { |
||
| 51 | foreach ( func_get_args() as $arg ) { // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
||
| 52 | l( $arg ); |
||
| 53 | } |
||
| 54 | return $stuff; |
||
| 55 | } |
||
| 56 | if ( ! isset( $pageload ) ) { |
||
| 57 | $pageload = substr( md5( wp_rand() ), 0, 4 ); |
||
| 58 | if ( ! empty( $_SERVER['argv'] ) ) { |
||
| 59 | $hint = implode( ' ', $_SERVER['argv'] ); |
||
| 60 | } elseif ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { |
||
| 61 | $hint = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
| 62 | } else { |
||
| 63 | $hint = php_sapi_name(); |
||
| 64 | } |
||
| 65 | error_log( sprintf( '[%s-%s => %s]', $pageload, getmypid(), $hint ) ); |
||
| 66 | } |
||
| 67 | $pid = $pageload . '-' . getmypid(); |
||
| 68 | if ( is_null( $stuff ) ) { |
||
| 69 | // Log the file and line number. |
||
| 70 | $backtrace = debug_backtrace( false ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
||
| 71 | View Code Duplication | while ( isset( $backtrace[1]['function'] ) && __FUNCTION__ === $backtrace[1]['function'] ) { |
|
| 72 | array_shift( $backtrace ); |
||
| 73 | } |
||
| 74 | $log = sprintf( '%s line %d', $backtrace[0]['file'], $backtrace[0]['line'] ); |
||
| 75 | } elseif ( is_bool( $stuff ) ) { |
||
| 76 | $log = $stuff ? 'TRUE' : 'FALSE'; |
||
| 77 | } elseif ( is_scalar( $stuff ) ) { |
||
| 78 | // Strings and numbers can be logged exactly. |
||
| 79 | $log = $stuff; |
||
| 80 | } else { |
||
| 81 | /* |
||
| 82 | * Are we in an output buffer handler? |
||
| 83 | * If so, print_r($stuff, true) is fatal so we must avoid that. |
||
| 84 | * This is not as slow as it looks: <1ms when !$in_ob_handler. |
||
| 85 | * Using json_encode_pretty() all the time is much slower. |
||
| 86 | */ |
||
| 87 | do { |
||
| 88 | $in_ob_handler = false; |
||
| 89 | $ob_status = ob_get_status( true ); |
||
| 90 | $obs = array(); |
||
| 91 | |||
| 92 | if ( ! $ob_status ) { |
||
|
|
|||
| 93 | break; |
||
| 94 | } |
||
| 95 | |||
| 96 | foreach ( $ob_status as $ob ) { |
||
| 97 | $obs[] = $ob['name']; |
||
| 98 | } |
||
| 99 | // This is not perfect: anonymous handlers appear as default. |
||
| 100 | if ( array( 'default output handler' ) === $obs ) { |
||
| 101 | break; |
||
| 102 | } |
||
| 103 | $backtrace = debug_backtrace( false ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
||
| 104 | $bts = array(); |
||
| 105 | foreach ( $backtrace as $level ) { |
||
| 106 | $caller = ''; |
||
| 107 | if ( isset( $level['class'] ) ) { |
||
| 108 | $caller = $level['class'] . '::'; |
||
| 109 | } |
||
| 110 | $caller .= $level['function']; |
||
| 111 | $bts[] = $caller; |
||
| 112 | } |
||
| 113 | if ( array_intersect( $obs, $bts ) ) { |
||
| 114 | $in_ob_handler = true; |
||
| 115 | } |
||
| 116 | } while ( false ); |
||
| 117 | if ( $in_ob_handler ) { |
||
| 118 | $log = l_json_encode_pretty( $stuff ); |
||
| 119 | } else { |
||
| 120 | $log = print_r( $stuff, true ); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | error_log( sprintf( '[%s] %s', $pid, $log ) ); |
||
| 124 | return $stuff; |
||
| 125 | } |
||
| 126 | |||
| 226 |
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.