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 Kint_Renderer_Text 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 Kint_Renderer_Text, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Kint_Renderer_Text extends Kint_Renderer |
||
| 4 | { |
||
| 5 | public static $object_renderers = array( |
||
| 6 | 'blacklist' => 'Kint_Renderer_Text_Blacklist', |
||
| 7 | 'depth_limit' => 'Kint_Renderer_Text_DepthLimit', |
||
| 8 | 'nothing' => 'Kint_Renderer_Text_Nothing', |
||
| 9 | 'recursion' => 'Kint_Renderer_Text_Recursion', |
||
| 10 | 'trace' => 'Kint_Renderer_Text_Trace', |
||
| 11 | ); |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Parser plugins must be instanceof one of these or |
||
| 15 | * it will be removed for performance reasons. |
||
| 16 | */ |
||
| 17 | public static $parser_plugin_whitelist = array( |
||
| 18 | 'Kint_Parser_Blacklist', |
||
| 19 | 'Kint_Parser_Stream', |
||
| 20 | 'Kint_Parser_Trace', |
||
| 21 | ); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The maximum length of a string before it is truncated. |
||
| 25 | * |
||
| 26 | * Falsey to disable |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | public static $strlen_max = 0; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The default width of the terminal for headers. |
||
| 34 | * |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | public static $default_width = 80; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Indentation width. |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | public static $default_indent = 4; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Decorate the header and footer. |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | public static $decorations = true; |
||
| 52 | |||
| 53 | public $header_width = 80; |
||
| 54 | public $indent_width = 4; |
||
| 55 | |||
| 56 | protected $plugin_objs = array(); |
||
| 57 | protected $previous_caller; |
||
| 58 | protected $callee; |
||
| 59 | protected $show_minitrace = true; |
||
| 60 | |||
| 61 | public function __construct(array $params = array()) |
||
| 76 | |||
| 77 | public function render(Kint_Object $o) |
||
| 96 | |||
| 97 | public function boxText($text, $width) |
||
| 111 | |||
| 112 | public function renderTitle(Kint_Object $o) |
||
| 126 | |||
| 127 | public function renderHeader(Kint_Object $o) |
||
| 166 | |||
| 167 | public function renderChildren(Kint_Object $o) |
||
| 198 | |||
| 199 | public function colorValue($string) |
||
| 203 | |||
| 204 | public function colorType($string) |
||
| 208 | |||
| 209 | public function colorTitle($string) |
||
| 213 | |||
| 214 | public function postRender() |
||
| 232 | |||
| 233 | public function parserPlugins(array $plugins) |
||
| 248 | |||
| 249 | protected function calledFrom() |
||
| 277 | |||
| 278 | public function ideLink($file, $line) |
||
| 282 | |||
| 283 | View Code Duplication | protected function getPlugin(array $plugins, array $hints) |
|
| 295 | |||
| 296 | public function escape($string, $encoding = false) |
||
| 300 | } |
||
| 301 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.