| Conditions | 23 |
| Paths | 226 |
| Total Lines | 102 |
| 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 |
||
| 55 | function jp_custom_error_handler( $whether_i_may_die, $type, $message, $file, $line ) { |
||
| 56 | if ( ! ( $type & ini_get( 'error_reporting' ) ) ) { |
||
| 57 | return true; |
||
| 58 | } |
||
| 59 | |||
| 60 | $die = false; |
||
| 61 | switch ( $type ) { |
||
| 62 | case E_CORE_ERROR: // we may not be able to capture this one. |
||
| 63 | $string = 'Core error'; |
||
| 64 | $die = true; |
||
| 65 | break; |
||
| 66 | case E_COMPILE_ERROR: // or this one. |
||
| 67 | $string = 'Compile error'; |
||
| 68 | $die = true; |
||
| 69 | break; |
||
| 70 | case E_PARSE: // we can't actually capture this one. |
||
| 71 | $string = 'Parse error'; |
||
| 72 | $die = true; |
||
| 73 | break; |
||
| 74 | case E_ERROR: |
||
| 75 | case E_USER_ERROR: |
||
| 76 | $string = 'Fatal error'; |
||
| 77 | $die = true; |
||
| 78 | break; |
||
| 79 | case E_WARNING: |
||
| 80 | case E_USER_WARNING: |
||
| 81 | $string = 'Warning'; |
||
| 82 | break; |
||
| 83 | case E_NOTICE: |
||
| 84 | case E_USER_NOTICE: |
||
| 85 | $string = 'Notice'; |
||
| 86 | break; |
||
| 87 | case E_STRICT: |
||
| 88 | $string = 'Strict Standards'; |
||
| 89 | break; |
||
| 90 | case E_RECOVERABLE_ERROR: |
||
| 91 | $string = 'Catchable fatal error'; |
||
| 92 | $die = true; |
||
| 93 | break; |
||
| 94 | case E_DEPRECATED: |
||
| 95 | case E_USER_DEPRECATED: |
||
| 96 | $string = 'Deprecated'; |
||
| 97 | break; |
||
| 98 | case 0: |
||
| 99 | return true; |
||
| 100 | } |
||
| 101 | |||
| 102 | $log_json = array(); |
||
| 103 | |||
| 104 | // @ error suppression |
||
| 105 | if ( 0 === error_reporting() ) { |
||
| 106 | $string = '[Suppressed] ' . $string; |
||
|
|
|||
| 107 | } |
||
| 108 | |||
| 109 | $backtrace = jp_get_error_backtrace( $file, $type ); |
||
| 110 | |||
| 111 | if ( ! empty( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { |
||
| 112 | $source = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
| 113 | } else { |
||
| 114 | // What's happening here? |
||
| 115 | $source = '$ ' . @join( ' ', $GLOBALS['argv'] ); |
||
| 116 | } |
||
| 117 | |||
| 118 | // Maybe not needed, since it should be handled by default error_handler, and it might not be needed for shutdown handler. |
||
| 119 | // $display_errors = ini_get( 'display_errors' ); |
||
| 120 | // if ( $display_errors ) { |
||
| 121 | // if ( ini_get( 'html_errors' ) ) { |
||
| 122 | // $display_errors_format = "<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%d</b><br />\n[%s]<br />\n[%s]<br /><br />\n"; |
||
| 123 | // } else { |
||
| 124 | // $display_errors_format = "\n%s: %s in %s on line %d [%s] [%s]\n"; |
||
| 125 | // } |
||
| 126 | |||
| 127 | // if ( 'stderr' === $display_errors && defined( 'STDERR' ) && is_resource( STDERR ) ) { |
||
| 128 | // fwrite( STDERR, sprintf( $display_errors_format, $string, $message, $file, $line, htmlspecialchars( $source ), htmlspecialchars( $backtrace ) ) ); |
||
| 129 | // } else { |
||
| 130 | // printf( $display_errors_format, $string, $message, $file, $line, htmlspecialchars( $source ), htmlspecialchars( $backtrace ) ); |
||
| 131 | // } |
||
| 132 | // } |
||
| 133 | |||
| 134 | if ( ini_get( 'log_errors' ) ) { |
||
| 135 | error_log( |
||
| 136 | sprintf( |
||
| 137 | 'XXXXX ::: %s: %s in %s on line %d [%s] [%s]%s', |
||
| 138 | $string, |
||
| 139 | $message, |
||
| 140 | $file, |
||
| 141 | $line, |
||
| 142 | $source, |
||
| 143 | $backtrace, |
||
| 144 | empty( $log_json ) ? '' : ' log-json:' . json_encode( $log_json ) |
||
| 145 | ) |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | do_stuff(); |
||
| 150 | |||
| 151 | // When we run at shutdown we must not die as then the pretty printing of the Error doesn't happen which is lame sauce. |
||
| 152 | if ( $die && $whether_i_may_die ) { |
||
| 153 | die( 1 ); |
||
| 154 | } |
||
| 155 | return true; |
||
| 156 | } |
||
| 157 | |||
| 211 |
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: