Conditions | 34 |
Paths | 384 |
Total Lines | 161 |
Code Lines | 106 |
Lines | 8 |
Ratio | 4.97 % |
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 |
||
37 | protected function displayException( \Exception $exception ) { |
||
38 | |||
39 | $error_code = ''; |
||
40 | $trace_details = ''; |
||
41 | $time = time(); |
||
42 | $trace = $exception->getTrace(); |
||
43 | // get separate user and developer messages if they exist |
||
44 | $msg = explode( '||', $exception->getMessage() ); |
||
45 | $user_msg = $msg[0]; |
||
46 | $dev_msg = isset( $msg[1] ) ? $msg[1] : $msg[0]; |
||
47 | $msg = WP_DEBUG ? $dev_msg : $user_msg; |
||
48 | // start gathering output |
||
49 | $output = $this->exceptionStyles(); |
||
50 | $output .= ' |
||
51 | <div id="ee-error-message" class="error">'; |
||
52 | if ( ! WP_DEBUG ) { |
||
53 | $output .= ' |
||
54 | <p>'; |
||
55 | } |
||
56 | // process trace info |
||
57 | if ( empty( $trace ) ) { |
||
58 | $trace_details .= __( |
||
59 | 'Sorry, but no trace information was available for this exception.', |
||
60 | 'event_espresso' |
||
61 | ); |
||
62 | } else { |
||
63 | $trace_details .= ' |
||
64 | <div id="ee-trace-details"> |
||
65 | <table width="100%" border="0" cellpadding="5" cellspacing="0"> |
||
66 | <tr> |
||
67 | <th scope="col" align="right" style="width:2.5%;">#</th> |
||
68 | <th scope="col" align="right" style="width:3.5%;">Line</th> |
||
69 | <th scope="col" align="left" style="width:40%;">File</th> |
||
70 | <th scope="col" align="left">' . __( 'Class', 'event_espresso' ) . '->' . __( 'Method( arguments )', 'event_espresso' ) . '</th> |
||
71 | </tr>'; |
||
72 | $last_on_stack = count( $trace ) - 1; |
||
73 | // reverse array so that stack is in proper chronological order |
||
74 | $sorted_trace = array_reverse( $trace ); |
||
75 | foreach ( $sorted_trace as $nmbr => $trace ) { |
||
76 | $file = isset( $trace['file'] ) ? $trace['file'] : ''; |
||
77 | $class = isset( $trace['class'] ) ? $trace['class'] : ''; |
||
78 | $type = isset( $trace['type'] ) ? $trace['type'] : ''; |
||
79 | $function = isset( $trace['function'] ) ? $trace['function'] : ''; |
||
80 | $args = isset( $trace['args'] ) ? $this->_convert_args_to_string( $trace['args'] ) : ''; |
||
81 | $args = isset( $trace['args'] ) && count( $trace['args'] ) > 4 ? ' <br />' . $args . '<br />' : $args; |
||
82 | $line = isset( $trace['line'] ) ? $trace['line'] : ''; |
||
83 | $zebra = $nmbr % 2 !== 0 ? ' odd' : ''; |
||
84 | View Code Duplication | if ( empty( $file ) && ! empty( $class ) ) { |
|
85 | $a = new \ReflectionClass( $class ); |
||
86 | $file = $a->getFileName(); |
||
87 | if ( empty( $line ) && ! empty( $function ) ) { |
||
88 | $b = new \ReflectionMethod( $class, $function ); |
||
89 | $line = $b->getStartLine(); |
||
90 | } |
||
91 | } |
||
92 | if ( $nmbr === $last_on_stack ) { |
||
93 | $file = $exception->getFile() !== '' ? $exception->getFile() : $file; |
||
94 | $line = $exception->getLine() !== '' ? $exception->getLine() : $line; |
||
95 | $error_code = $this->generate_error_code( $file, $trace['function'], $line ); |
||
96 | } |
||
97 | $file = \EEH_File::standardise_directory_separators( $file ); |
||
98 | $nmbr = ! empty( $nmbr ) ? $nmbr : ' '; |
||
99 | $line = ! empty( $line ) ? $line : ' '; |
||
100 | $file = ! empty( $file ) ? $file : ' '; |
||
101 | $class_display = ! empty( $class ) ? $class : ''; |
||
102 | $type = ! empty( $type ) ? $type : ''; |
||
103 | $function = ! empty( $function ) ? $function : ''; |
||
104 | $args = ! empty( $args ) ? '( ' . $args . ' )' : '()'; |
||
105 | $trace_details .= ' |
||
106 | <tr> |
||
107 | <td align="right" valign="top" class="' . $zebra . '">' . $nmbr . '</td> |
||
108 | <td align="right" valign="top" class="' . $zebra . '">' . $line . '</td> |
||
109 | <td align="left" valign="top" class="' . $zebra . '">' . $file . '</td> |
||
110 | <td align="left" valign="top" class="' . $zebra . '">' . $class_display . $type . $function . $args . '</td> |
||
111 | </tr>'; |
||
112 | } |
||
113 | $trace_details .= ' |
||
114 | </table> |
||
115 | </div>'; |
||
116 | } |
||
117 | $code = $exception->getCode() ? $exception->getCode() : $error_code; |
||
118 | // add generic non-identifying messages for non-privileged users |
||
119 | if ( ! WP_DEBUG ) { |
||
120 | $output .= '<span class="ee-error-user-msg-spn">' |
||
121 | . trim( $msg ) |
||
122 | . '</span> <sup>' |
||
123 | . $code |
||
124 | . '</sup><br />'; |
||
125 | } else { |
||
126 | // or helpful developer messages if debugging is on |
||
127 | $output .= ' |
||
128 | <div class="ee-error-dev-msg-dv"> |
||
129 | <p class="ee-error-dev-msg-pg"> |
||
130 | ' |
||
131 | . sprintf( |
||
132 | __( '%1$sAn %2$s was thrown!%3$s code: %4$s', 'event_espresso' ), |
||
133 | '<strong class="ee-error-dev-msg-str">', |
||
134 | get_class( $exception ), |
||
135 | '</strong> <span>', |
||
136 | $code . '</span>' |
||
137 | ) |
||
138 | . '<br /> |
||
139 | <span class="big-text">"' |
||
140 | . trim( $msg ) |
||
141 | . '"</span><br/> |
||
142 | <a id="display-ee-error-trace-1' |
||
143 | . $time |
||
144 | . '" class="display-ee-error-trace-lnk small-text" rel="ee-error-trace-1' |
||
145 | . $time |
||
146 | . '"> |
||
147 | ' |
||
148 | . __( 'click to view backtrace and class/method details', 'event_espresso' ) |
||
149 | . ' |
||
150 | </a><br /> |
||
151 | ' |
||
152 | . $exception->getFile() |
||
153 | . sprintf( |
||
154 | __( '%1$s( line no: %2$s )%3$s', 'event_espresso' ), |
||
155 | ' <span class="small-text lt-grey-text">', |
||
156 | $exception->getLine(), |
||
157 | '</span>' |
||
158 | ) |
||
159 | . ' |
||
160 | </p> |
||
161 | <div id="ee-error-trace-1' |
||
162 | . $time |
||
163 | . '-dv" class="ee-error-trace-dv" style="display: none;"> |
||
164 | ' |
||
165 | . $trace_details; |
||
166 | if ( ! empty( $class ) ) { |
||
167 | $output .= ' |
||
168 | <div style="padding:3px; margin:0 0 1em; border:1px solid #999; background:#fff; border-radius:3px;"> |
||
169 | <div style="padding:1em 2em; border:1px solid #999; background:#fcfcfc;"> |
||
170 | <h3>' . __( 'Class Details', 'event_espresso' ) . '</h3>'; |
||
171 | $a = new \ReflectionClass( $class ); |
||
172 | $output .= ' |
||
173 | <pre>' . $a . '</pre> |
||
174 | </div> |
||
175 | </div>'; |
||
176 | } |
||
177 | $output .= ' |
||
178 | </div> |
||
179 | </div> |
||
180 | <br />'; |
||
181 | } |
||
182 | // remove last linebreak |
||
183 | $output = substr( $output, 0, count( $output ) - 7 ); |
||
184 | if ( ! WP_DEBUG ) { |
||
185 | $output .= ' |
||
186 | </p>'; |
||
187 | } |
||
188 | $output .= ' |
||
189 | </div>'; |
||
190 | $output .= $this->printScripts( true ); |
||
191 | if ( defined( 'DOING_AJAX' ) ) { |
||
192 | echo json_encode( array( 'error' => $output ) ); |
||
193 | exit(); |
||
194 | } |
||
195 | echo $output; |
||
196 | die(); |
||
197 | } |
||
198 | |||
398 | // Location: /core/exceptions/ExceptionStackTraceDisplay.php |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.