Complex classes like DebugBar 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DebugBar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | use DebugBar\DebugBar as BaseDebugBar; |
||
7 | use DebugBar\DataCollector\MemoryCollector; |
||
8 | use DebugBar\DataCollector\PDO\PDOCollector; |
||
9 | use DebugBar\DataCollector\PDO\TraceablePDO; |
||
10 | use DebugBar\DataCollector\PhpInfoCollector; |
||
11 | use DebugBar\Storage\FileStorage; |
||
12 | use Exception; |
||
13 | use LeKoala\DebugBar\Collector\DatabaseCollector; |
||
14 | use LeKoala\DebugBar\Collector\SilverStripeCollector; |
||
15 | use LeKoala\DebugBar\Collector\TimeDataCollector; |
||
16 | use LeKoala\DebugBar\Extension\ControllerExtension; |
||
17 | use LeKoala\DebugBar\LogWriter; |
||
18 | use LeKoala\DebugBar\Proxy\DatabaseProxy; |
||
19 | use Psr\Log\LoggerInterface; |
||
20 | use ReflectionObject; |
||
21 | use SilverStripe\Admin\LeftAndMain; |
||
22 | use SilverStripe\Control\Controller as BaseController; |
||
23 | use SilverStripe\Control\Director; |
||
24 | use SilverStripe\Core\Config\Configurable; |
||
25 | use SilverStripe\Core\Injector\Injectable; |
||
26 | use SilverStripe\Core\Injector\Injector; |
||
27 | use SilverStripe\ORM\Connect\PDOConnector; |
||
28 | use SilverStripe\ORM\DB; |
||
29 | use SilverStripe\View\Requirements; |
||
30 | |||
31 | /** |
||
32 | * A simple helper |
||
33 | */ |
||
34 | class DebugBar |
||
35 | { |
||
36 | use Configurable; |
||
37 | use Injectable; |
||
38 | |||
39 | /** |
||
40 | * @var DebugBar\DebugBar |
||
41 | */ |
||
42 | protected static $debugbar = null; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | public static $bufferingEnabled = false; |
||
48 | |||
49 | /** |
||
50 | * @var DebugBar\JavascriptRenderer |
||
51 | */ |
||
52 | protected static $renderer = null; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected static $showQueries = false; |
||
58 | |||
59 | /** |
||
60 | * Get the Debug Bar instance |
||
61 | * @return \DebugBar\StandardDebugBar |
||
62 | * @throws Exception |
||
63 | * @global array $databaseConfig |
||
64 | */ |
||
65 | public static function getDebugBar() |
||
66 | { |
||
67 | if (self::$debugbar !== null) { |
||
68 | return self::$debugbar; |
||
69 | } |
||
70 | |||
71 | if (!Director::isDev() || self::isDisabled() || self::vendorNotInstalled() || |
||
72 | self::notLocalIp() || Director::is_cli() || self::isDevUrl() || |
||
73 | (self::isAdminUrl() && !self::config()->enabled_in_admin) |
||
74 | ) { |
||
75 | self::$debugbar = false; // no need to check again |
||
|
|||
76 | return; |
||
77 | } |
||
78 | |||
79 | self::initDebugBar(); |
||
80 | |||
81 | if (!self::$debugbar) { |
||
82 | throw new Exception("Failed to initialize the DebugBar"); |
||
83 | } |
||
84 | |||
85 | return self::$debugbar; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Init the debugbar instance |
||
90 | * |
||
91 | * @global array $databaseConfig |
||
92 | * @return DebugBar\StandardDebugBar |
||
93 | */ |
||
94 | public static function initDebugBar() |
||
95 | { |
||
96 | // Prevent multiple inits |
||
97 | if (self::$debugbar) { |
||
98 | return self::$debugbar; |
||
99 | } |
||
100 | |||
101 | // Add the controller extension programmaticaly because it might not be added properly through yml |
||
102 | Controller::add_extension(ControllerExtension::class); |
||
103 | |||
104 | self::$debugbar = $debugbar = new BaseDebugBar; |
||
105 | |||
106 | if (isset($_REQUEST['showqueries'])) { |
||
107 | self::setShowQueries(true); |
||
108 | echo "The queries above have been run before we started DebugBar"; |
||
109 | echo '<hr>'; |
||
110 | unset($_REQUEST['showqueries']); |
||
111 | } |
||
112 | |||
113 | $debugbar->addCollector(new PhpInfoCollector); |
||
114 | $debugbar->addCollector(new TimeDataCollector); |
||
115 | $debugbar->addCollector(new MemoryCollector); |
||
116 | |||
117 | if (!DB::get_conn()) { |
||
118 | global $databaseConfig; |
||
119 | if ($databaseConfig) { |
||
120 | DB::connect($databaseConfig); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | $connector = DB::get_connector(); |
||
125 | if (!self::config()->force_proxy && $connector instanceof PDOConnector) { |
||
126 | // Use a little bit of magic to replace the pdo instance |
||
127 | $refObject = new ReflectionObject($connector); |
||
128 | $refProperty = $refObject->getProperty('pdoConnection'); |
||
129 | $refProperty->setAccessible(true); |
||
130 | $traceablePdo = new TraceablePDO($refProperty->getValue($connector)); |
||
131 | $refProperty->setValue($connector, $traceablePdo); |
||
132 | |||
133 | $debugbar->addCollector(new PDOCollector($traceablePdo)); |
||
134 | } else { |
||
135 | DB::set_conn($db = new DatabaseProxy(DB::get_conn())); |
||
136 | $db->setShowQueries(self::getShowQueries()); |
||
137 | $debugbar->addCollector(new DatabaseCollector); |
||
138 | } |
||
139 | |||
140 | // Add message collector last so other collectors can send messages to the console using it |
||
141 | $logger = Injector::inst()->get(LoggerInterface::class); |
||
142 | $debugbar->addCollector(new MonologCollector($logger)); |
||
143 | |||
144 | // Add some SilverStripe specific infos |
||
145 | $debugbar->addCollector(new SilverStripeCollector); |
||
146 | |||
147 | if (self::config()->enable_storage) { |
||
148 | $debugbar->setStorage(new FileStorage(TEMP_FOLDER . '/debugbar')); |
||
149 | } |
||
150 | |||
151 | // Since we buffer everything, why not enable all dev options ? |
||
152 | if (self::config()->auto_debug) { |
||
153 | $_REQUEST['debug'] = true; |
||
154 | $_REQUEST['debug_request'] = true; |
||
155 | } |
||
156 | |||
157 | if (isset($_REQUEST['debug']) || isset($_REQUEST['debug_request'])) { |
||
158 | self::$bufferingEnabled = true; |
||
159 | ob_start(); // We buffer everything until we have called an action |
||
160 | } |
||
161 | |||
162 | return $debugbar; |
||
163 | } |
||
164 | |||
165 | public static function clearDebugBar() |
||
169 | |||
170 | public static function getShowQueries() |
||
174 | |||
175 | public static function setShowQueries($showQueries) |
||
179 | |||
180 | public static function includeRequirements() |
||
235 | |||
236 | public static function renderDebugBar() |
||
255 | |||
256 | /** |
||
257 | * Determine why DebugBar is disabled |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | public static function whyDisabled() |
||
286 | |||
287 | public static function vendorNotInstalled() |
||
291 | |||
292 | public static function notLocalIp() |
||
302 | |||
303 | public static function isDisabled() |
||
304 | { |
||
305 | if ((defined('DEBUGBAR_DISABLE') && DEBUGBAR_DISABLE) || static::config()->disabled) { |
||
306 | return true; |
||
307 | } |
||
308 | return false; |
||
309 | } |
||
310 | |||
311 | public static function isDevUrl() |
||
315 | |||
316 | public static function isAdminUrl() |
||
320 | |||
321 | public static function isAdminController() |
||
328 | |||
329 | /** |
||
330 | * Avoid triggering data collection for open handler |
||
331 | * |
||
332 | * @return boolean |
||
333 | */ |
||
334 | public static function isDebugBarRequest() |
||
341 | |||
342 | /** |
||
343 | * Get request url |
||
344 | * |
||
370 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..