Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like RichRenderer 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 RichRenderer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class RichRenderer extends Renderer |
||
12 | { |
||
13 | /** |
||
14 | * RichRenderer object plugins should implement Kint\Renderer\Rich\ObjectPluginInterface. |
||
15 | */ |
||
16 | public static $object_plugins = array( |
||
17 | 'blacklist' => 'Kint\\Renderer\\Rich\\BlacklistPlugin', |
||
18 | 'callable' => 'Kint\\Renderer\\Rich\\CallablePlugin', |
||
19 | 'closure' => 'Kint\\Renderer\\Rich\\ClosurePlugin', |
||
20 | 'color' => 'Kint\\Renderer\\Rich\\ColorPlugin', |
||
21 | 'depth_limit' => 'Kint\\Renderer\\Rich\\DepthLimitPlugin', |
||
22 | 'nothing' => 'Kint\\Renderer\\Rich\\NothingPlugin', |
||
23 | 'recursion' => 'Kint\\Renderer\\Rich\\RecursionPlugin', |
||
24 | 'simplexml_element' => 'Kint\\Renderer\\Rich\\SimpleXMLElementPlugin', |
||
25 | 'trace_frame' => 'Kint\\Renderer\\Rich\\TraceFramePlugin', |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * RichRenderer tab plugins should implement Kint\Renderer\Rich\TabPluginInterface. |
||
30 | */ |
||
31 | public static $tab_plugins = array( |
||
32 | 'binary' => 'Kint\\Renderer\\Rich\\BinaryPlugin', |
||
33 | 'color' => 'Kint\\Renderer\\Rich\\ColorPlugin', |
||
34 | 'docstring' => 'Kint\\Renderer\\Rich\\DocstringPlugin', |
||
35 | 'microtime' => 'Kint\\Renderer\\Rich\\MicrotimePlugin', |
||
36 | 'source' => 'Kint\\Renderer\\Rich\\SourcePlugin', |
||
37 | 'table' => 'Kint\\Renderer\\Rich\\TablePlugin', |
||
38 | 'timestamp' => 'Kint\\Renderer\\Rich\\TimestampPlugin', |
||
39 | ); |
||
40 | |||
41 | public static $pre_render_sources = array( |
||
42 | 'script' => array( |
||
43 | array('Kint\\Renderer\\RichRenderer', 'renderJs'), |
||
44 | array('Kint\\Renderer\\Rich\\MicrotimePlugin', 'renderJs'), |
||
45 | ), |
||
46 | 'style' => array( |
||
47 | array('Kint\\Renderer\\RichRenderer', 'renderCss'), |
||
48 | ), |
||
49 | 'raw' => array( |
||
50 | array('Kint\\Renderer\\RichRenderer', 'renderFolder'), |
||
51 | ), |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Whether or not to render access paths. |
||
56 | * |
||
57 | * Access paths can become incredibly heavy with very deep and wide |
||
58 | * structures. Given mostly public variables it will typically make |
||
59 | * up one quarter of the output HTML size. |
||
60 | * |
||
61 | * If this is an unacceptably large amount and your browser is groaning |
||
62 | * under the weight of the access paths - your first order of buisiness |
||
63 | * should be to get a new browser. Failing that, use this to turn them off. |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | public static $access_paths = true; |
||
68 | |||
69 | /** |
||
70 | * The maximum length of a string before it is truncated. |
||
71 | * |
||
72 | * Falsey to disable |
||
73 | * |
||
74 | * @var int |
||
75 | */ |
||
76 | public static $strlen_max = 80; |
||
77 | |||
78 | /** |
||
79 | * Path to the CSS file to load by default. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | public static $theme = 'original.css'; |
||
84 | |||
85 | /** |
||
86 | * Assume types and sizes don't need to be escaped. |
||
87 | * |
||
88 | * Turn this off if you use anything but ascii in your class names, |
||
89 | * but it'll cause a slowdown of around 10% |
||
90 | * |
||
91 | * @var bool |
||
92 | */ |
||
93 | public static $escape_types = false; |
||
94 | |||
95 | /** |
||
96 | * Move all dumps to a folder at the bottom of the body. |
||
97 | * |
||
98 | * @var bool |
||
99 | */ |
||
100 | public static $folder = true; |
||
101 | |||
102 | /** |
||
103 | * Sort mode for object properties. |
||
104 | * |
||
105 | * @var int |
||
106 | */ |
||
107 | public static $sort = self::SORT_NONE; |
||
108 | |||
109 | protected static $been_run = false; |
||
110 | |||
111 | protected $plugin_objs = array(); |
||
112 | protected $mod_return = false; |
||
113 | protected $callee; |
||
114 | protected $mini_trace; |
||
115 | protected $previous_caller; |
||
116 | protected $file_link_format = false; |
||
117 | protected $show_minitrace = true; |
||
118 | protected $auto_expand = false; |
||
119 | |||
120 | public function __construct(array $params = array()) |
||
151 | |||
152 | public function render(BasicObject $o) |
||
165 | |||
166 | public function renderHeaderWrapper(BasicObject $o, $has_children, $contents) |
||
198 | |||
199 | public function renderHeader(BasicObject $o) |
||
245 | |||
246 | public function renderChildren(BasicObject $o) |
||
291 | |||
292 | protected function renderTab(BasicObject $o, Representation $rep) |
||
340 | |||
341 | protected static function renderJs() |
||
345 | |||
346 | View Code Duplication | protected static function renderCss() |
|
354 | |||
355 | protected static function renderFolder() |
||
363 | |||
364 | View Code Duplication | public function preRender() |
|
398 | |||
399 | public function postRender() |
||
461 | |||
462 | View Code Duplication | public function escape($string, $encoding = false) |
|
483 | |||
484 | View Code Duplication | protected function getPlugin(array $plugins, array $hints) |
|
496 | |||
497 | View Code Duplication | protected function ideLink($file, $line) |
|
509 | } |
||
510 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.