| Total Complexity | 40 |
| Total Lines | 425 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DebugView often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DebugView, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class DebugView |
||
| 20 | { |
||
| 21 | use Configurable; |
||
| 22 | use Injectable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Column size to wrap long strings to |
||
| 26 | * |
||
| 27 | * @var int |
||
| 28 | * @config |
||
| 29 | */ |
||
| 30 | private static $columns = 100; |
||
| 31 | |||
| 32 | protected static $error_types = array( |
||
| 33 | 0 => array( |
||
| 34 | 'title' => 'Emergency', |
||
| 35 | 'class' => 'error' |
||
| 36 | ), |
||
| 37 | 1 => array( |
||
| 38 | 'title' => 'Alert', |
||
| 39 | 'class' => 'error' |
||
| 40 | ), |
||
| 41 | 3 => array( |
||
| 42 | 'title' => 'Error', |
||
| 43 | 'class' => 'error' |
||
| 44 | ), |
||
| 45 | 4 => array( |
||
| 46 | 'title' => 'Warning', |
||
| 47 | 'class' => 'warning' |
||
| 48 | ), |
||
| 49 | 5 => array( |
||
| 50 | 'title' => 'Notice', |
||
| 51 | 'class' => 'notice' |
||
| 52 | ), |
||
| 53 | 6 => array( |
||
| 54 | 'title' => 'Information', |
||
| 55 | 'class' => 'info' |
||
| 56 | ), |
||
| 57 | 7=> array( |
||
| 58 | 'title' => 'SilverStripe\\Dev\\Debug', |
||
| 59 | 'class' => 'debug' |
||
| 60 | ), |
||
| 61 | E_USER_ERROR => array( |
||
| 62 | 'title' => 'User Error', |
||
| 63 | 'class' => 'error' |
||
| 64 | ), |
||
| 65 | E_CORE_ERROR => array( |
||
| 66 | 'title' => 'Core Error', |
||
| 67 | 'class' => 'error' |
||
| 68 | ), |
||
| 69 | E_NOTICE => array( |
||
| 70 | 'title' => 'Notice', |
||
| 71 | 'class' => 'notice' |
||
| 72 | ), |
||
| 73 | E_USER_NOTICE => array( |
||
| 74 | 'title' => 'User Notice', |
||
| 75 | 'class' => 'notice' |
||
| 76 | ), |
||
| 77 | E_DEPRECATED => array( |
||
| 78 | 'title' => 'Deprecated', |
||
| 79 | 'class' => 'notice' |
||
| 80 | ), |
||
| 81 | E_USER_DEPRECATED => array( |
||
| 82 | 'title' => 'User Deprecated', |
||
| 83 | 'class' => 'notice' |
||
| 84 | ), |
||
| 85 | E_WARNING => array( |
||
| 86 | 'title' => 'Warning', |
||
| 87 | 'class' => 'warning' |
||
| 88 | ), |
||
| 89 | E_CORE_WARNING => array( |
||
| 90 | 'title' => 'Core Warning', |
||
| 91 | 'class' => 'warning' |
||
| 92 | ), |
||
| 93 | E_USER_WARNING => array( |
||
| 94 | 'title' => 'User Warning', |
||
| 95 | 'class' => 'warning' |
||
| 96 | ), |
||
| 97 | E_STRICT => array( |
||
| 98 | 'title' => 'Strict Notice', |
||
| 99 | 'class' => 'notice' |
||
| 100 | ), |
||
| 101 | E_RECOVERABLE_ERROR => array( |
||
| 102 | 'title' => 'Recoverable Error', |
||
| 103 | 'class' => 'warning' |
||
| 104 | ) |
||
| 105 | ); |
||
| 106 | |||
| 107 | protected static $unknown_error = array( |
||
| 108 | 'title' => 'Unknown Error', |
||
| 109 | 'class' => 'error' |
||
| 110 | ); |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Generate breadcrumb links to the URL path being displayed |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function Breadcrumbs() |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @deprecated 4.0.0:5.0.0 Use renderHeader() instead |
||
| 139 | */ |
||
| 140 | public function writeHeader() |
||
| 141 | { |
||
| 142 | Deprecation::notice('4.0', 'Use renderHeader() instead'); |
||
| 143 | echo $this->renderHeader(); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @deprecated 4.0.0:5.0.0 Use renderInfo() instead |
||
| 148 | */ |
||
| 149 | public function writeInfo($title, $subtitle, $description = false) |
||
| 150 | { |
||
| 151 | Deprecation::notice('4.0', 'Use renderInfo() instead'); |
||
| 152 | echo $this->renderInfo($title, $subtitle, $description); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @deprecated 4.0.0:5.0.0 Use renderFooter() instead |
||
| 157 | */ |
||
| 158 | public function writeFooter() |
||
| 159 | { |
||
| 160 | Deprecation::notice('4.0', 'Use renderFooter() instead'); |
||
| 161 | echo $this->renderFooter(); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @deprecated 4.0.0:5.0.0 Use renderError() instead |
||
| 166 | */ |
||
| 167 | public function writeError($httpRequest, $errno, $errstr, $errfile, $errline) |
||
| 168 | { |
||
| 169 | Deprecation::notice('4.0', 'Use renderError() instead'); |
||
| 170 | echo $this->renderError($httpRequest, $errno, $errstr, $errfile, $errline); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @deprecated 4.0.0:5.0.0 Use renderSourceFragment() instead |
||
| 175 | */ |
||
| 176 | public function writeSourceFragment($lines, $errline) |
||
| 177 | { |
||
| 178 | Deprecation::notice('4.0', 'Use renderSourceFragment() instead'); |
||
| 179 | echo $this->renderSourceFragment($lines, $errline); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @deprecated 4.0.0:5.0.0 Use renderTrace() instead |
||
| 184 | */ |
||
| 185 | public function writeTrace($trace) |
||
| 186 | { |
||
| 187 | Deprecation::notice('4.0', 'Use renderTrace() instead'); |
||
| 188 | echo $this->renderTrace($trace); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @deprecated 4.0.0:5.0.0 Use renderVariable() instead |
||
| 193 | */ |
||
| 194 | public function writeVariable($val, $caller) |
||
| 195 | { |
||
| 196 | Deprecation::notice('4.0', 'Use renderVariable() instead'); |
||
| 197 | echo $this->renderVariable($val, $caller); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Render HTML header for development views |
||
| 202 | * |
||
| 203 | * @param HTTPRequest $httpRequest |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function renderHeader($httpRequest = null) |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Render the information header for the view |
||
| 226 | * |
||
| 227 | * @param string $title The main title |
||
| 228 | * @param string $subtitle The subtitle |
||
| 229 | * @param string|bool $description The description to show |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function renderInfo($title, $subtitle, $description = false) |
||
| 233 | { |
||
| 234 | $output = '<div class="info header">'; |
||
| 235 | $output .= "<h1>" . Convert::raw2xml($title) . "</h1>"; |
||
| 236 | if ($subtitle) { |
||
| 237 | $output .= "<h3>" . Convert::raw2xml($subtitle) . "</h3>"; |
||
| 238 | } |
||
| 239 | if ($description) { |
||
| 240 | $output .= "<p>$description</p>"; |
||
| 241 | } else { |
||
| 242 | $output .= $this->Breadcrumbs(); |
||
| 243 | } |
||
| 244 | $output .= '</div>'; |
||
| 245 | |||
| 246 | return $output; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Render HTML footer for development views |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function renderFooter() |
||
| 254 | { |
||
| 255 | return "</body></html>"; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Render an error. |
||
| 260 | * |
||
| 261 | * @param string $httpRequest the kind of request |
||
| 262 | * @param int $errno Codenumber of the error |
||
| 263 | * @param string $errstr The error message |
||
| 264 | * @param string $errfile The name of the soruce code file where the error occurred |
||
| 265 | * @param int $errline The line number on which the error occured |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function renderError($httpRequest, $errno, $errstr, $errfile, $errline) |
||
| 269 | { |
||
| 270 | $errorType = isset(self::$error_types[$errno]) ? self::$error_types[$errno] : self::$unknown_error; |
||
| 271 | $httpRequestEnt = htmlentities($httpRequest, ENT_COMPAT, 'UTF-8'); |
||
| 272 | if (ini_get('html_errors')) { |
||
| 273 | $errstr = strip_tags($errstr); |
||
| 274 | } else { |
||
| 275 | $errstr = Convert::raw2xml($errstr); |
||
| 276 | } |
||
| 277 | $output = '<div class="header info ' . $errorType['class'] . '">'; |
||
| 278 | $output .= "<h1>[" . $errorType['title'] . '] ' . $errstr . "</h1>"; |
||
| 279 | $output .= "<h3>$httpRequestEnt</h3>"; |
||
| 280 | $output .= "<p>Line <strong>$errline</strong> in <strong>$errfile</strong></p>"; |
||
| 281 | $output .= '</div>'; |
||
| 282 | |||
| 283 | return $output; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Render a fragment of the a source file |
||
| 288 | * |
||
| 289 | * @param array $lines An array of file lines; the keys should be the original line numbers |
||
| 290 | * @param int $errline The line of the error |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function renderSourceFragment($lines, $errline) |
||
| 294 | { |
||
| 295 | $output = '<div class="info"><h3>Source</h3>'; |
||
| 296 | $output .= '<pre>'; |
||
| 297 | foreach ($lines as $offset => $line) { |
||
| 298 | $line = htmlentities($line, ENT_COMPAT, 'UTF-8'); |
||
| 299 | if ($offset == $errline) { |
||
| 300 | $output .= "<span>$offset</span> <span class=\"error\">$line</span>"; |
||
| 301 | } else { |
||
| 302 | $output .= "<span>$offset</span> $line"; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | $output .= '</pre></div>'; |
||
| 306 | |||
| 307 | return $output; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Render a call track |
||
| 312 | * |
||
| 313 | * @param array $trace The debug_backtrace() array |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function renderTrace($trace) |
||
| 317 | { |
||
| 318 | $output = '<div class="info">'; |
||
| 319 | $output .= '<h3>Trace</h3>'; |
||
| 320 | $output .= Backtrace::get_rendered_backtrace($trace); |
||
| 321 | $output .= '</div>'; |
||
| 322 | |||
| 323 | return $output; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Render an arbitrary paragraph. |
||
| 328 | * |
||
| 329 | * @param string $text The HTML-escaped text to render |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function renderParagraph($text) |
||
| 333 | { |
||
| 334 | return '<div class="info"><p>' . $text . '</p></div>'; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Formats the caller of a method |
||
| 339 | * |
||
| 340 | * @param array $caller |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | protected function formatCaller($caller) |
||
| 344 | { |
||
| 345 | $return = basename($caller['file']) . ":" . $caller['line']; |
||
| 346 | if (!empty($caller['class']) && !empty($caller['function'])) { |
||
| 347 | $return .= " - {$caller['class']}::{$caller['function']}()"; |
||
| 348 | } |
||
| 349 | return $return; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Outputs a variable in a user presentable way |
||
| 354 | * |
||
| 355 | * @param object $val |
||
| 356 | * @param array $caller Caller information |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function renderVariable($val, $caller) |
||
| 371 | } |
||
| 372 | |||
| 373 | public function renderMessage($message, $caller, $showHeader = true) |
||
| 374 | { |
||
| 375 | $header = ''; |
||
| 376 | if ($showHeader) { |
||
| 377 | $file = basename($caller['file']); |
||
| 378 | $line = $caller['line']; |
||
| 379 | $header .= "<b>Debug (line {$line} of {$file}):</b>\n"; |
||
| 380 | } |
||
| 381 | return "<p class=\"message warning\">\n" . $header . Convert::raw2xml($message) . "</p>\n"; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Similar to renderVariable() but respects debug() method on object if available |
||
| 386 | * |
||
| 387 | * @param mixed $val |
||
| 388 | * @param array $caller |
||
| 389 | * @param bool $showHeader |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function debugVariable($val, $caller, $showHeader = true) |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get debug text for this object |
||
| 409 | * |
||
| 410 | * @param mixed $val |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function debugVariableText($val) |
||
| 444 | } |
||
| 445 | } |
||
| 446 |