| Total Complexity | 43 |
| Total Lines | 274 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like BetterDebugView 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 BetterDebugView, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class BetterDebugView extends DebugView |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @config |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private static $ide_placeholder = 'vscode://file/{file}:{line}:0'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $file |
||
| 26 | * @param string|int $line |
||
| 27 | * @return string |
||
| 28 | */ |
||
| 29 | public static function makeIdeLink($file, $line) |
||
| 30 | { |
||
| 31 | $shortname = basename($file); |
||
| 32 | |||
| 33 | // does not make sense in testing or live |
||
| 34 | if (!Director::isDev()) { |
||
| 35 | return "$shortname:$line"; |
||
| 36 | } |
||
| 37 | |||
| 38 | $placeholder = self::config()->ide_placeholder; |
||
| 39 | |||
| 40 | // each dev can define their own settings |
||
| 41 | $envPlaceholder = Environment::getEnv('IDE_PLACEHOLDER'); |
||
| 42 | if ($envPlaceholder) { |
||
| 43 | $placeholder = $envPlaceholder; |
||
| 44 | } |
||
| 45 | |||
| 46 | $ide_link = str_replace(['{file}', '{line}'], [$file, $line], $placeholder); |
||
| 47 | $link = "<a href=\"$ide_link\">$shortname:$line</a>"; |
||
| 48 | return $link; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Similar to renderVariable() but respects debug() method on object if available |
||
| 53 | * |
||
| 54 | * @param mixed $val |
||
| 55 | * @param array<string,mixed> $caller |
||
| 56 | * @param bool $showHeader |
||
| 57 | * @param int|null $argumentIndex |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function debugVariable($val, $caller, $showHeader = true, $argumentIndex = 0) |
||
| 61 | { |
||
| 62 | // Get arguments name |
||
| 63 | $args = $this->extractArgumentsName($caller['file'], $caller['line']); |
||
| 64 | |||
| 65 | if ($showHeader) { |
||
| 66 | $callerFormatted = $this->formatCaller($caller); |
||
| 67 | $defaultArgumentName = is_int($argumentIndex) ? 'Debug' : $argumentIndex; |
||
| 68 | $argumentName = $args[$argumentIndex] ?? $defaultArgumentName; |
||
| 69 | |||
| 70 | // Sql trick |
||
| 71 | if (strpos(strtolower($argumentName), 'sql') !== false && is_string($val)) { |
||
| 72 | $text = DatabaseHelper::formatSQL($val); |
||
| 73 | } else { |
||
| 74 | $text = $this->debugVariableText($val); |
||
| 75 | } |
||
| 76 | |||
| 77 | $html = "<div style=\"background-color: white; text-align: left;\">\n<hr>\n" |
||
| 78 | . "<h3>$argumentName <span style=\"font-size: 65%\">($callerFormatted)</span>\n</h3>\n" |
||
| 79 | . $text |
||
| 80 | . "</div>"; |
||
| 81 | |||
| 82 | if (Director::is_ajax()) { |
||
| 83 | $html = strip_tags($html); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $html; |
||
| 87 | } |
||
| 88 | return $this->debugVariableText($val); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $file |
||
| 93 | * @param int $line |
||
| 94 | * @return array<mixed> |
||
| 95 | */ |
||
| 96 | protected function extractArgumentsName($file, $line) |
||
| 97 | { |
||
| 98 | // Arguments passed to the function are stored in matches |
||
| 99 | $src = file($file); |
||
| 100 | $src_line = $src[$line - 1]; |
||
| 101 | preg_match("/d\((.+)\)/", $src_line, $matches); |
||
| 102 | // Find all arguments, ignore variables within parenthesis |
||
| 103 | $arguments = array(); |
||
| 104 | if (!empty($matches[1])) { |
||
| 105 | $arguments = array_map('trim', preg_split("/(?![^(]*\)),/", $matches[1])); |
||
| 106 | } |
||
| 107 | return $arguments; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get debug text for this object |
||
| 112 | * |
||
| 113 | * Use symfony dumper if it exists |
||
| 114 | * |
||
| 115 | * @param mixed $val |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function debugVariableText($val) |
||
| 119 | { |
||
| 120 | // Empty stuff is tricky |
||
| 121 | if (empty($val)) { |
||
| 122 | $valtype = gettype($val); |
||
| 123 | return "<em>(empty $valtype)</em>"; |
||
| 124 | } |
||
| 125 | |||
| 126 | if (Director::is_ajax()) { |
||
| 127 | // In ajax context, we can still use debug info |
||
| 128 | if (is_object($val)) { |
||
| 129 | if (ClassInfo::hasMethod($val, 'debug')) { |
||
| 130 | return $val->debug(); |
||
| 131 | } |
||
| 132 | return print_r($val, true); |
||
| 133 | } |
||
| 134 | } else { |
||
| 135 | // Otherwise, we'd rater a full and usable object dump |
||
| 136 | if (function_exists('dump') && (is_object($val) || is_array($val))) { |
||
| 137 | ob_start(); |
||
| 138 | dump($val); |
||
| 139 | return ob_get_clean(); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | // Check debug |
||
| 144 | if (is_object($val) && ClassInfo::hasMethod($val, 'debug')) { |
||
| 145 | return $val->debug(); |
||
| 146 | } |
||
| 147 | |||
| 148 | // Format as array |
||
| 149 | if (is_array($val)) { |
||
| 150 | return print_r($val, true); |
||
| 151 | } |
||
| 152 | |||
| 153 | // Format object |
||
| 154 | if (is_object($val)) { |
||
| 155 | return var_export($val, true); |
||
| 156 | } |
||
| 157 | |||
| 158 | // Format bool |
||
| 159 | if (is_bool($val)) { |
||
| 160 | return '(bool) ' . ($val ? 'true' : 'false'); |
||
| 161 | } |
||
| 162 | |||
| 163 | // Format text |
||
| 164 | $html = Convert::raw2xml($val); |
||
| 165 | return "<pre style=\"font-family: Courier new, serif\">{$html}</pre>\n"; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Formats the caller of a method |
||
| 170 | * |
||
| 171 | * Improve method by creating the ide link |
||
| 172 | * |
||
| 173 | * @param array $caller |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | protected function formatCaller($caller) |
||
| 177 | { |
||
| 178 | $return = self::makeIdeLink($caller['file'], $caller['line']); |
||
| 179 | if (!empty($caller['class']) && !empty($caller['function'])) { |
||
| 180 | $return .= " - {$caller['class']}::{$caller['function']}()"; |
||
| 181 | } |
||
| 182 | return $return; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Render an error. |
||
| 187 | * |
||
| 188 | * @param string $httpRequest the kind of request |
||
| 189 | * @param int $errno Codenumber of the error |
||
| 190 | * @param string $errstr The error message |
||
| 191 | * @param string $errfile The name of the soruce code file where the error occurred |
||
| 192 | * @param int $errline The line number on which the error occured |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function renderError($httpRequest, $errno, $errstr, $errfile, $errline) |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param Exception $exception |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | public function writeException(Exception $exception) |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Render a call track |
||
| 247 | * |
||
| 248 | * @param array<mixed> $trace The debug_backtrace() array |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function renderTrace($trace) |
||
| 252 | { |
||
| 253 | $output = '<div class="info">'; |
||
| 254 | $output .= '<h3>Trace</h3>'; |
||
| 255 | $output .= self::get_rendered_backtrace($trace); |
||
| 256 | $output .= '</div>'; |
||
| 257 | |||
| 258 | return $output; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Render a backtrace array into an appropriate plain-text or HTML string. |
||
| 263 | * |
||
| 264 | * @param array<string,mixed> $bt The trace array, as returned by debug_backtrace() or Exception::getTrace() |
||
| 265 | * @return string The rendered backtrace |
||
| 266 | */ |
||
| 267 | public static function get_rendered_backtrace($bt) |
||
| 290 | } |
||
| 291 | } |
||
| 292 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths