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 TextRenderer 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 TextRenderer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class TextRenderer extends Renderer |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * TextRenderer plugins should be instances of Kint\Renderer\Text\Plugin. |
||
| 14 | */ |
||
| 15 | public static $plugins = array( |
||
| 16 | 'blacklist' => 'Kint\\Renderer\\Text\\BlacklistPlugin', |
||
| 17 | 'depth_limit' => 'Kint\\Renderer\\Text\\DepthLimitPlugin', |
||
| 18 | 'nothing' => 'Kint\\Renderer\\Text\\NothingPlugin', |
||
| 19 | 'recursion' => 'Kint\\Renderer\\Text\\RecursionPlugin', |
||
| 20 | 'trace' => 'Kint\\Renderer\\Text\\TracePlugin', |
||
| 21 | ); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Parser plugins must be instanceof one of these or |
||
| 25 | * it will be removed for performance reasons. |
||
| 26 | */ |
||
| 27 | public static $parser_plugin_whitelist = array( |
||
| 28 | 'Kint\\Parser\\BlacklistPlugin', |
||
| 29 | 'Kint\\Parser\\StreamPlugin', |
||
| 30 | 'Kint\\Parser\\TracePlugin', |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The maximum length of a string before it is truncated. |
||
| 35 | * |
||
| 36 | * Falsey to disable |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | public static $strlen_max = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The default width of the terminal for headers. |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | public static $default_width = 80; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Indentation width. |
||
| 51 | * |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | public static $default_indent = 4; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Decorate the header and footer. |
||
| 58 | * |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | public static $decorations = true; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Sort mode for object properties. |
||
| 65 | * |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | public static $sort = self::SORT_NONE; |
||
| 69 | |||
| 70 | public $header_width = 80; |
||
| 71 | public $indent_width = 4; |
||
| 72 | |||
| 73 | protected $plugin_objs = array(); |
||
| 74 | protected $previous_caller; |
||
| 75 | protected $callee; |
||
| 76 | protected $show_minitrace = true; |
||
| 77 | |||
| 78 | public function __construct(array $params = array()) |
||
| 93 | |||
| 94 | public function render(BasicObject $o) |
||
| 113 | |||
| 114 | public function boxText($text, $width) |
||
| 128 | |||
| 129 | public function renderTitle(BasicObject $o) |
||
| 143 | |||
| 144 | public function renderHeader(BasicObject $o) |
||
| 183 | |||
| 184 | public function renderChildren(BasicObject $o) |
||
| 221 | |||
| 222 | public function colorValue($string) |
||
| 226 | |||
| 227 | public function colorType($string) |
||
| 231 | |||
| 232 | public function colorTitle($string) |
||
| 236 | |||
| 237 | public function postRender() |
||
| 255 | |||
| 256 | public function parserPlugins(array $plugins) |
||
| 271 | |||
| 272 | protected function calledFrom() |
||
| 303 | |||
| 304 | public function ideLink($file, $line) |
||
| 308 | |||
| 309 | View Code Duplication | protected function getPlugin(array $plugins, array $hints) |
|
| 321 | |||
| 322 | public function escape($string, $encoding = false) |
||
| 326 | } |
||
| 327 |
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.