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 ChromePhp 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 ChromePhp, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class ChromePhp { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | const VERSION = '4.1.0'; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | const HEADER_NAME = 'X-ChromeLogger-Data'; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | const BACKTRACE_LEVEL = 'backtrace_level'; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | const LOG = 'log'; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | const WARN = 'warn'; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | const ERROR = 'error'; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | const GROUP = 'group'; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | const INFO = 'info'; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | const GROUP_END = 'groupEnd'; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | const GROUP_COLLAPSED = 'groupCollapsed'; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | const TABLE = 'table'; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $_php_version; |
||
84 | |||
85 | /** |
||
86 | * @var int |
||
87 | */ |
||
88 | protected $_timestamp; |
||
89 | |||
90 | /** |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $_json = array( |
||
94 | 'version' => self::VERSION, |
||
95 | 'columns' => array( 'log', 'backtrace', 'type' ), |
||
96 | 'rows' => array() |
||
97 | ); |
||
98 | |||
99 | /** |
||
100 | * @var array |
||
101 | */ |
||
102 | protected $_backtraces = array(); |
||
103 | |||
104 | /** |
||
105 | * @var bool |
||
106 | */ |
||
107 | protected $_error_triggered = false; |
||
108 | |||
109 | /** |
||
110 | * @var array |
||
111 | */ |
||
112 | protected $_settings = array( |
||
113 | self::BACKTRACE_LEVEL => 1 |
||
114 | ); |
||
115 | |||
116 | /** |
||
117 | * @var ChromePhp |
||
118 | */ |
||
119 | protected static $_instance; |
||
120 | |||
121 | /** |
||
122 | * Prevent recursion when working with objects referring to each other |
||
123 | * |
||
124 | * @var array |
||
125 | */ |
||
126 | protected $_processed = array(); |
||
127 | |||
128 | /** |
||
129 | * constructor |
||
130 | */ |
||
131 | private function __construct() { |
||
136 | |||
137 | /** |
||
138 | * gets instance of this class |
||
139 | * |
||
140 | * @return ChromePhp |
||
141 | */ |
||
142 | public static function getInstance() { |
||
148 | |||
149 | /** |
||
150 | * logs a variable to the console |
||
151 | * |
||
152 | * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] |
||
|
|||
153 | * @return void |
||
154 | */ |
||
155 | public static function log() { |
||
159 | |||
160 | /** |
||
161 | * logs a warning to the console |
||
162 | * |
||
163 | * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] |
||
164 | * @return void |
||
165 | */ |
||
166 | public static function warn() { |
||
170 | |||
171 | /** |
||
172 | * logs an error to the console |
||
173 | * |
||
174 | * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] |
||
175 | * @return void |
||
176 | */ |
||
177 | public static function error() { |
||
181 | |||
182 | /** |
||
183 | * sends a group log |
||
184 | * |
||
185 | * @param string value |
||
186 | */ |
||
187 | public static function group() { |
||
191 | |||
192 | /** |
||
193 | * sends an info log |
||
194 | * |
||
195 | * @param mixed $data,... unlimited OPTIONAL number of additional logs [...] |
||
196 | * @return void |
||
197 | */ |
||
198 | public static function info() { |
||
202 | |||
203 | /** |
||
204 | * sends a collapsed group log |
||
205 | * |
||
206 | * @param string value |
||
207 | */ |
||
208 | public static function groupCollapsed() { |
||
212 | |||
213 | /** |
||
214 | * ends a group log |
||
215 | * |
||
216 | * @param string value |
||
217 | */ |
||
218 | public static function groupEnd() { |
||
222 | |||
223 | /** |
||
224 | * sends a table log |
||
225 | * |
||
226 | * @param string value |
||
227 | */ |
||
228 | public static function table() { |
||
232 | |||
233 | /** |
||
234 | * internal logging call |
||
235 | * |
||
236 | * @param string $type |
||
237 | * @return void |
||
238 | */ |
||
239 | protected static function _log( $type, array $args ) { |
||
264 | |||
265 | /** |
||
266 | * converts an object to a better format for logging |
||
267 | * |
||
268 | * @param Object |
||
269 | * @return array |
||
270 | */ |
||
271 | protected function _convert( $object ) { |
||
327 | |||
328 | /** |
||
329 | * takes a reflection property and returns a nicely formatted key of the property name |
||
330 | * |
||
331 | * @param ReflectionProperty |
||
332 | * @return string |
||
333 | */ |
||
334 | protected function _getPropertyKey( ReflectionProperty $property ) { |
||
348 | |||
349 | /** |
||
350 | * adds a value to the data array |
||
351 | * |
||
352 | * @var mixed |
||
353 | * @return void |
||
354 | */ |
||
355 | protected function _addRow( array $logs, $backtrace, $type ) { |
||
376 | |||
377 | protected function _writeHeader( $data ) { |
||
380 | |||
381 | /** |
||
382 | * encodes the data to be sent along with the request |
||
383 | * |
||
384 | * @param array $data |
||
385 | * @return string |
||
386 | */ |
||
387 | protected function _encode( $data ) { |
||
390 | |||
391 | /** |
||
392 | * adds a setting |
||
393 | * |
||
394 | * @param string key |
||
395 | * @param mixed value |
||
396 | * @return void |
||
397 | */ |
||
398 | public function addSetting( $key, $value ) { |
||
401 | |||
402 | /** |
||
403 | * add ability to set multiple settings in one call |
||
404 | * |
||
405 | * @param array $settings |
||
406 | * @return void |
||
407 | */ |
||
408 | public function addSettings( array $settings ) { |
||
413 | |||
414 | /** |
||
415 | * gets a setting |
||
416 | * |
||
417 | * @param string key |
||
418 | * @return mixed |
||
419 | */ |
||
420 | public function getSetting( $key ) { |
||
426 | } |
||
427 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.