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