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 | 'recursion' => 'Kint\\Renderer\\Rich\\RecursionPlugin', |
||
23 | 'simplexml_element' => 'Kint\\Renderer\\Rich\\SimpleXMLElementPlugin', |
||
24 | 'trace_frame' => 'Kint\\Renderer\\Rich\\TraceFramePlugin', |
||
25 | ); |
||
26 | |||
27 | /** |
||
28 | * RichRenderer tab plugins should implement Kint\Renderer\Rich\TabPluginInterface. |
||
29 | */ |
||
30 | public static $tab_plugins = array( |
||
31 | 'binary' => 'Kint\\Renderer\\Rich\\BinaryPlugin', |
||
32 | 'color' => 'Kint\\Renderer\\Rich\\ColorPlugin', |
||
33 | 'docstring' => 'Kint\\Renderer\\Rich\\DocstringPlugin', |
||
34 | 'microtime' => 'Kint\\Renderer\\Rich\\MicrotimePlugin', |
||
35 | 'source' => 'Kint\\Renderer\\Rich\\SourcePlugin', |
||
36 | 'table' => 'Kint\\Renderer\\Rich\\TablePlugin', |
||
37 | 'timestamp' => 'Kint\\Renderer\\Rich\\TimestampPlugin', |
||
38 | ); |
||
39 | |||
40 | public static $pre_render_sources = array( |
||
41 | 'script' => array( |
||
42 | array('Kint\\Renderer\\RichRenderer', 'renderJs'), |
||
43 | array('Kint\\Renderer\\Rich\\MicrotimePlugin', 'renderJs'), |
||
44 | ), |
||
45 | 'style' => array( |
||
46 | array('Kint\\Renderer\\RichRenderer', 'renderCss'), |
||
47 | ), |
||
48 | 'raw' => array( |
||
49 | array('Kint\\Renderer\\RichRenderer', 'renderFolder'), |
||
50 | ), |
||
51 | ); |
||
52 | |||
53 | /** |
||
54 | * Whether or not to render access paths. |
||
55 | * |
||
56 | * Access paths can become incredibly heavy with very deep and wide |
||
57 | * structures. Given mostly public variables it will typically make |
||
58 | * up one quarter of the output HTML size. |
||
59 | * |
||
60 | * If this is an unacceptably large amount and your browser is groaning |
||
61 | * under the weight of the access paths - your first order of buisiness |
||
62 | * should be to get a new browser. Failing that, use this to turn them off. |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | public static $access_paths = true; |
||
67 | |||
68 | /** |
||
69 | * The maximum length of a string before it is truncated. |
||
70 | * |
||
71 | * Falsey to disable |
||
72 | * |
||
73 | * @var int |
||
74 | */ |
||
75 | public static $strlen_max = 80; |
||
76 | |||
77 | /** |
||
78 | * Path to the CSS file to load by default. |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | public static $theme = 'original.css'; |
||
83 | |||
84 | /** |
||
85 | * Assume types and sizes don't need to be escaped. |
||
86 | * |
||
87 | * Turn this off if you use anything but ascii in your class names, |
||
88 | * but it'll cause a slowdown of around 10% |
||
89 | * |
||
90 | * @var bool |
||
91 | */ |
||
92 | public static $escape_types = false; |
||
93 | |||
94 | /** |
||
95 | * Move all dumps to a folder at the bottom of the body. |
||
96 | * |
||
97 | * @var bool |
||
98 | */ |
||
99 | public static $folder = true; |
||
100 | |||
101 | /** |
||
102 | * Sort mode for object properties. |
||
103 | * |
||
104 | * @var int |
||
105 | */ |
||
106 | public static $sort = self::SORT_NONE; |
||
107 | |||
108 | protected static $been_run = false; |
||
109 | |||
110 | protected $plugin_objs = array(); |
||
111 | |||
112 | public function render(BasicObject $o) |
||
125 | |||
126 | public function renderNothing() |
||
130 | |||
131 | public function renderHeaderWrapper(BasicObject $o, $has_children, $contents) |
||
163 | |||
164 | public function renderHeader(BasicObject $o) |
||
210 | |||
211 | public function renderChildren(BasicObject $o) |
||
256 | |||
257 | protected function renderTab(BasicObject $o, Representation $rep) |
||
307 | |||
308 | protected static function renderJs() |
||
312 | |||
313 | View Code Duplication | protected static function renderCss() |
|
321 | |||
322 | protected static function renderFolder() |
||
330 | |||
331 | View Code Duplication | public function preRender() |
|
365 | |||
366 | public function postRender() |
||
432 | |||
433 | View Code Duplication | public function escape($string, $encoding = false) |
|
454 | |||
455 | View Code Duplication | protected function getPlugin(array $plugins, array $hints) |
|
467 | |||
468 | View Code Duplication | public function ideLink($file, $line) |
|
485 | } |
||
486 |
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
.