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 | 'microtime' => 'Kint\\Renderer\\Text\\MicrotimePlugin', |
||
| 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\\MicrotimePlugin', |
||
| 30 | 'Kint\\Parser\\StreamPlugin', |
||
| 31 | 'Kint\\Parser\\TracePlugin', |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The maximum length of a string before it is truncated. |
||
| 36 | * |
||
| 37 | * Falsey to disable |
||
| 38 | * |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | public static $strlen_max = 0; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The default width of the terminal for headers. |
||
| 45 | * |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | public static $default_width = 80; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Indentation width. |
||
| 52 | * |
||
| 53 | * @var int |
||
| 54 | */ |
||
| 55 | public static $default_indent = 4; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Decorate the header and footer. |
||
| 59 | * |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | public static $decorations = true; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Sort mode for object properties. |
||
| 66 | * |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | public static $sort = self::SORT_NONE; |
||
| 70 | |||
| 71 | public $header_width = 80; |
||
| 72 | public $indent_width = 4; |
||
| 73 | |||
| 74 | protected $plugin_objs = array(); |
||
| 75 | |||
| 76 | public function __construct() |
||
| 81 | |||
| 82 | public function render(BasicObject $o) |
||
| 101 | |||
| 102 | public function renderNothing() |
||
| 112 | |||
| 113 | public function boxText($text, $width) |
||
| 127 | |||
| 128 | public function renderTitle(BasicObject $o) |
||
| 142 | |||
| 143 | public function renderHeader(BasicObject $o) |
||
| 182 | |||
| 183 | public function renderChildren(BasicObject $o) |
||
| 220 | |||
| 221 | public function colorValue($string) |
||
| 225 | |||
| 226 | public function colorType($string) |
||
| 230 | |||
| 231 | public function colorTitle($string) |
||
| 235 | |||
| 236 | public function postRender() |
||
| 254 | |||
| 255 | public function filterParserPlugins(array $plugins) |
||
| 270 | |||
| 271 | protected function calledFrom() |
||
| 302 | |||
| 303 | public function ideLink($file, $line) |
||
| 307 | |||
| 308 | View Code Duplication | protected function getPlugin(array $plugins, array $hints) |
|
| 320 | |||
| 321 | public function escape($string, $encoding = false) |
||
| 325 | } |
||
| 326 |
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.