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 MainRenderer 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 MainRenderer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class MainRenderer extends Renderer implements RendererInterface |
||
16 | { |
||
17 | protected $matcher; |
||
18 | protected $defaultOptions; |
||
19 | |||
20 | /** |
||
21 | * @param MatcherInterface $matcher |
||
22 | * @param array $defaultOptions |
||
23 | * @param string $charset |
||
24 | */ |
||
25 | public function __construct(MatcherInterface $matcher, array $defaultOptions = array(), $charset = null) |
||
50 | |||
51 | public function render(ItemInterface $item, array $options = array()) |
||
76 | |||
77 | protected function renderList(ItemInterface $item, array $attributes, array $options) |
||
95 | |||
96 | /** |
||
97 | * Renders all of the children of this menu. |
||
98 | * |
||
99 | * This calls ->renderItem() on each menu item, which instructs each |
||
100 | * menu item to render themselves as an <li> tag (with nested ul if it |
||
101 | * has children). |
||
102 | * This method updates the depth for the children. |
||
103 | * |
||
104 | * @param ItemInterface $item |
||
105 | * @param array $options The options to render the item. |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | protected function renderChildren(ItemInterface $item, array $options) |
||
127 | |||
128 | /** |
||
129 | * Called by the parent menu item to render this menu. |
||
130 | * |
||
131 | * This renders the li tag to fit into the parent ul as well as its |
||
132 | * own nested ul tag if this menu item has children |
||
133 | * |
||
134 | * @param ItemInterface $item |
||
135 | * @param array $options The options to render the item |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | protected function renderItem(ItemInterface $item, array $options) |
||
207 | |||
208 | /** |
||
209 | * Renders the link in a a tag with link attributes or |
||
210 | * the label in a span tag with label attributes |
||
211 | * |
||
212 | * Tests if item has a an uri and if not tests if it's |
||
213 | * the current item and if the text has to be rendered |
||
214 | * as a link or not. |
||
215 | * |
||
216 | * @param ItemInterface $item The item to render the link or label for |
||
217 | * @param array $options The options to render the item |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | protected function renderLink(ItemInterface $item, array $options = array()) |
||
231 | |||
232 | View Code Duplication | protected function renderLinkElement(ItemInterface $item, array $options) |
|
245 | |||
246 | View Code Duplication | protected function renderSpanElement(ItemInterface $item, array $options) |
|
259 | |||
260 | protected function renderLabel(ItemInterface $item, array $options) |
||
268 | |||
269 | /** |
||
270 | * If $this->renderCompressed is on, this will apply the necessary |
||
271 | * spacing and line-breaking so that the particular thing being rendered |
||
272 | * makes up its part in a fully-rendered and spaced menu. |
||
273 | * |
||
274 | * @param string $html The html to render in an (un)formatted way |
||
275 | * @param string $type The type [ul,link,li] of thing being rendered |
||
276 | * @param integer $level |
||
277 | * @param array $options |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | protected function format($html, $type, $level, array $options) |
||
300 | } |
||
301 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.