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 ChainRenderer 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 ChainRenderer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ChainRenderer implements CanSetTemplateRendererInterface |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var ChainableRendererInterface|null |
||
25 | */ |
||
26 | private $templateRenderer; |
||
27 | /** |
||
28 | * @var ChainableRendererInterface[] |
||
29 | */ |
||
30 | private $packageRenderers = []; |
||
31 | /** |
||
32 | * @var ChainableRendererInterface[] |
||
33 | */ |
||
34 | private $customRenderers = []; |
||
35 | |||
36 | private $cacheService; |
||
37 | |||
38 | private $initDone = false; |
||
39 | /** |
||
40 | * @var string[] |
||
41 | */ |
||
42 | private $customRendererInstanceNames; |
||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $templateRendererInstanceName; |
||
47 | /** |
||
48 | * @var string[] |
||
49 | */ |
||
50 | private $packageRendererInstanceNames; |
||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $uniqueName; |
||
55 | /** |
||
56 | * @var ContainerInterface |
||
57 | */ |
||
58 | private $container; |
||
59 | |||
60 | /** |
||
61 | * |
||
62 | * @param string[] $customRendererInstanceNames An array of names of custom renderers (container identifiers) |
||
63 | * @param string[] $packageRendererInstanceNames An array of names of package renderers (container identifiers) |
||
64 | * @param CacheInterface $cacheService This service is used to speed up the mapping between the object and the template. |
||
65 | * @param string $uniqueName The unique name for this instance (used for caching purpose) |
||
66 | */ |
||
67 | public function __construct(ContainerInterface $container, array $customRendererInstanceNames, array $packageRendererInstanceNames, CacheInterface $cacheService, string $uniqueName) |
||
75 | |||
76 | /** |
||
77 | * (non-PHPdoc) |
||
78 | * @see \Mouf\Html\Renderer\RendererInterface::render() |
||
79 | */ |
||
80 | public function render($object, string $context = null): void |
||
88 | |||
89 | /** |
||
90 | * @param object $object |
||
91 | * @param string|null $context |
||
92 | * @return ChainableRendererInterface |
||
93 | */ |
||
94 | private function getRenderer($object, string $context = null): ChainableRendererInterface |
||
156 | |||
157 | /** |
||
158 | * Returns a string explaining the steps done to find the renderer. |
||
159 | * |
||
160 | * @param object $object |
||
161 | * @param string $context |
||
162 | * @return string |
||
163 | */ |
||
164 | private function getRendererDebugMessage($object, string $context = null): string |
||
204 | |||
205 | /** |
||
206 | * Initializes the renderers list (from cache if available) |
||
207 | */ |
||
208 | private function initRenderersList(): void { |
||
221 | |||
222 | /** |
||
223 | * Sets the renderer associated to the template. |
||
224 | * There should be only one if these renderers. |
||
225 | * It is the role of the template to subscribe to this renderer. |
||
226 | * |
||
227 | * @param string $templateRendererInstanceName The name of the template renderer in the container |
||
228 | */ |
||
229 | public function setTemplateRendererInstanceName(string $templateRendererInstanceName): void |
||
233 | } |
||
234 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.