Total Complexity | 43 |
Total Lines | 270 |
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 |
||
17 | class BetterDebugView extends DebugView |
||
18 | { |
||
19 | /** |
||
20 | * @config |
||
21 | * @var string |
||
22 | */ |
||
23 | private static $ide_placeholder = 'vscode://file/{file}:{line}:0'; |
||
24 | |||
25 | /** |
||
26 | * @param string $file |
||
27 | * @param string $line |
||
28 | * @return string |
||
29 | */ |
||
30 | public static function makeIdeLink($file, $line) |
||
31 | { |
||
32 | $shortname = basename($file); |
||
33 | |||
34 | // does not make sense in testing or live |
||
35 | if (!Director::isDev()) { |
||
36 | return "$shortname:$line"; |
||
37 | } |
||
38 | |||
39 | $placeholder = self::config()->ide_placeholder; |
||
40 | |||
41 | // each dev can define their own settings |
||
42 | $envPlaceholder = Environment::getEnv('IDE_PLACEHOLDER'); |
||
43 | if ($envPlaceholder) { |
||
44 | $placeholder = $envPlaceholder; |
||
45 | } |
||
46 | |||
47 | $ide_link = str_replace(['{file}', '{line}'], [$file, $line], $placeholder); |
||
48 | $link = "<a href=\"$ide_link\">$shortname:$line</a>"; |
||
49 | return $link; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Similar to renderVariable() but respects debug() method on object if available |
||
54 | * |
||
55 | * @param mixed $val |
||
56 | * @param array $caller |
||
57 | * @param bool $showHeader |
||
58 | * @param int $argumentIndex |
||
59 | * @return string |
||
60 | */ |
||
61 | public function debugVariable($val, $caller, $showHeader = true, $argumentIndex = 0) |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $file |
||
94 | * @param int $line |
||
95 | * @return array |
||
96 | */ |
||
97 | protected function extractArgumentsName($file, $line) |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get debug text for this object |
||
113 | * |
||
114 | * Use symfony dumper if it exists |
||
115 | * |
||
116 | * @param mixed $val |
||
117 | * @return string |
||
118 | */ |
||
119 | public function debugVariableText($val) |
||
120 | { |
||
121 | // Empty stuff is tricky |
||
122 | if (empty($val)) { |
||
123 | $valtype = gettype($val); |
||
124 | return "<em>(empty $valtype)</em>"; |
||
125 | } |
||
126 | |||
127 | if (Director::is_ajax()) { |
||
128 | // In ajax context, we can still use debug info |
||
129 | if (is_object($val)) { |
||
130 | if (ClassInfo::hasMethod($val, 'debug')) { |
||
131 | return $val->debug(); |
||
132 | } |
||
133 | return print_r($val, true); |
||
134 | } |
||
135 | } else { |
||
136 | // Otherwise, we'd rater a full and usable object dump |
||
137 | if (function_exists('dump') && (is_object($val) || is_array($val))) { |
||
138 | ob_start(); |
||
139 | dump($val); |
||
140 | return ob_get_clean(); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | // Check debug |
||
145 | if (is_object($val) && ClassInfo::hasMethod($val, 'debug')) { |
||
146 | return $val->debug(); |
||
147 | } |
||
148 | |||
149 | // Format as array |
||
150 | if (is_array($val)) { |
||
151 | return print_r($val, true); |
||
152 | } |
||
153 | |||
154 | // Format object |
||
155 | if (is_object($val)) { |
||
156 | return var_export($val, true); |
||
157 | } |
||
158 | |||
159 | // Format bool |
||
160 | if (is_bool($val)) { |
||
161 | return '(bool) ' . ($val ? 'true' : 'false'); |
||
162 | } |
||
163 | |||
164 | // Format text |
||
165 | $html = Convert::raw2xml($val); |
||
166 | return "<pre style=\"font-family: Courier new, serif\">{$html}</pre>\n"; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Formats the caller of a method |
||
171 | * |
||
172 | * Improve method by creating the ide link |
||
173 | * |
||
174 | * @param array $caller |
||
175 | * @return string |
||
176 | */ |
||
177 | protected function formatCaller($caller) |
||
178 | { |
||
179 | $return = self::makeIdeLink($caller['file'], $caller['line']); |
||
180 | if (!empty($caller['class']) && !empty($caller['function'])) { |
||
181 | $return .= " - {$caller['class']}::{$caller['function']}()"; |
||
182 | } |
||
183 | return $return; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Render an error. |
||
188 | * |
||
189 | * @param string $httpRequest the kind of request |
||
190 | * @param int $errno Codenumber of the error |
||
191 | * @param string $errstr The error message |
||
192 | * @param string $errfile The name of the soruce code file where the error occurred |
||
193 | * @param int $errline The line number on which the error occured |
||
194 | * @return string |
||
195 | */ |
||
196 | public function renderError($httpRequest, $errno, $errstr, $errfile, $errline) |
||
215 | } |
||
216 | |||
217 | public function writeException(Exception $exception) |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Render a call track |
||
244 | * |
||
245 | * @param array $trace The debug_backtrace() array |
||
246 | * @return string |
||
247 | */ |
||
248 | public function renderTrace($trace) |
||
249 | { |
||
250 | $output = '<div class="info">'; |
||
251 | $output .= '<h3>Trace</h3>'; |
||
252 | $output .= self::get_rendered_backtrace($trace); |
||
253 | $output .= '</div>'; |
||
254 | |||
255 | return $output; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Render a backtrace array into an appropriate plain-text or HTML string. |
||
260 | * |
||
261 | * @param array $bt The trace array, as returned by debug_backtrace() or Exception::getTrace() |
||
262 | * @return string The rendered backtrace |
||
263 | */ |
||
264 | public static function get_rendered_backtrace($bt) |
||
287 | } |
||
288 | } |
||
289 |
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