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 Compiler 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 Compiler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class Compiler |
||
44 | { |
||
45 | use LoggerAwareTrait; |
||
46 | use ContainerAwareTrait; |
||
47 | |||
48 | /** @var string|false */ |
||
49 | private $redirectTemplate; |
||
50 | |||
51 | /** @var BasePageView[][] */ |
||
52 | private $importDependencies; |
||
53 | |||
54 | /** |
||
55 | * Any time a PageView extends another template, that relationship is stored in this array. This is necessary so |
||
56 | * when watching a website, we can rebuild the necessary PageViews when these base templates change. |
||
57 | * |
||
58 | * ``` |
||
59 | * array['_layouts/base.html.twig'] = &PageView; |
||
60 | * ``` |
||
61 | * |
||
62 | * @var TemplateInterface[] |
||
63 | */ |
||
64 | private $templateDependencies; |
||
65 | |||
66 | /** |
||
67 | * All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
||
68 | * |
||
69 | * ``` |
||
70 | * array['_pages/index.html.twig'] = &PageView; |
||
71 | * ``` |
||
72 | * |
||
73 | * @var BasePageView[] |
||
74 | */ |
||
75 | private $pageViewsFlattened; |
||
76 | |||
77 | /** @var string[] */ |
||
78 | private $templateMapping; |
||
79 | |||
80 | /** @var BasePageView[][] */ |
||
81 | private $pageViews; |
||
|
|||
82 | |||
83 | /** @var Folder */ |
||
84 | private $folder; |
||
85 | |||
86 | /** @var string */ |
||
87 | private $theme; |
||
88 | |||
89 | /** @var TemplateBridgeInterface */ |
||
90 | private $templateBridge; |
||
91 | /** |
||
92 | * @var PageManager |
||
93 | */ |
||
94 | private $pageManager; |
||
95 | /** |
||
96 | * @var EventDispatcherInterface |
||
97 | */ |
||
98 | private $eventDispatcher; |
||
99 | |||
100 | public function __construct(TemplateBridgeInterface $templateBridge, PageManager $pageManager, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
||
110 | |||
111 | /** |
||
112 | * @param string|false $template |
||
113 | */ |
||
114 | public function setRedirectTemplate($template) |
||
118 | |||
119 | /** |
||
120 | * @param Folder $folder |
||
121 | */ |
||
122 | public function setTargetFolder(Folder $folder) |
||
126 | |||
127 | /** |
||
128 | * @param string $themeName |
||
129 | */ |
||
130 | public function setThemeName($themeName) |
||
134 | |||
135 | /// |
||
136 | // Twig parent templates |
||
137 | /// |
||
138 | |||
139 | public function isImportDependency($filePath) |
||
143 | |||
144 | /** |
||
145 | * Check whether a given file path is used as a parent template by a PageView |
||
146 | * |
||
147 | * @param string $filePath |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function isParentTemplate($filePath) |
||
155 | |||
156 | /** |
||
157 | * Rebuild all of the PageViews that used a given template as a parent |
||
158 | * |
||
159 | * @param string $filePath The file path to the parent Twig template |
||
160 | */ |
||
161 | public function refreshParent($filePath) |
||
168 | |||
169 | public function getTemplateMappings() |
||
173 | |||
174 | /// |
||
175 | // IO Functionality |
||
176 | /// |
||
177 | |||
178 | /** |
||
179 | * Compile all of the PageViews registered with the compiler. |
||
180 | * |
||
181 | * @since 0.1.0 |
||
182 | */ |
||
183 | public function compileAll() |
||
190 | |||
191 | public function compileImportDependencies($filePath) |
||
198 | |||
199 | public function compileSome(array $filter = array()) |
||
213 | |||
214 | /** |
||
215 | * Compile an individual PageView from a given path. |
||
216 | * |
||
217 | * @param string $filePath |
||
218 | * |
||
219 | * @throws FileNotFoundException When the given file path isn't tracked by the Compiler. |
||
220 | */ |
||
221 | public function compilePageViewFromPath($filePath) |
||
230 | |||
231 | /** |
||
232 | * Compile an individual PageView item. |
||
233 | * |
||
234 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
235 | * respective target file. |
||
236 | * |
||
237 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
238 | * |
||
239 | * @since 0.1.1 |
||
240 | */ |
||
241 | public function compilePageView(BasePageView &$pageView) |
||
280 | |||
281 | /** |
||
282 | * Write the compiled output for a static PageView. |
||
283 | * |
||
284 | * @since 0.1.1 |
||
285 | * |
||
286 | * @throws TemplateErrorInterface |
||
287 | */ |
||
288 | private function compileStaticPageView(StaticPageView &$pageView) |
||
298 | |||
299 | /** |
||
300 | * Write the compiled output for a dynamic PageView. |
||
301 | * |
||
302 | * @param DynamicPageView $pageView |
||
303 | * |
||
304 | * @since 0.1.1 |
||
305 | * |
||
306 | * @throws TemplateErrorInterface |
||
307 | */ |
||
308 | private function compileDynamicPageViews(DynamicPageView &$pageView) |
||
333 | |||
334 | /** |
||
335 | * Write the compiled output for a repeater PageView. |
||
336 | * |
||
337 | * @param RepeaterPageView $pageView |
||
338 | |||
339 | * @since 0.1.1 |
||
340 | * |
||
341 | * @throws TemplateErrorInterface |
||
342 | */ |
||
343 | private function compileRepeaterPageViews(RepeaterPageView &$pageView) |
||
360 | |||
361 | /** |
||
362 | * @deprecated |
||
363 | * |
||
364 | * @todo This function needs to be rewritten or removed. Something |
||
365 | * |
||
366 | * @param ContentItem $contentItem |
||
367 | */ |
||
368 | public function compileContentItem(ContentItem &$contentItem) |
||
382 | |||
383 | /// |
||
384 | // Redirect handling |
||
385 | /// |
||
386 | |||
387 | /** |
||
388 | * Write redirects for standard redirects. |
||
389 | * |
||
390 | * @throws TemplateErrorInterface |
||
391 | * |
||
392 | * @since 0.1.1 |
||
393 | */ |
||
394 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
||
412 | |||
413 | /** |
||
414 | * Write redirects for expanded redirects. |
||
415 | * |
||
416 | * @param RepeaterPageView $pageView |
||
417 | * |
||
418 | * @since 0.1.1 |
||
419 | */ |
||
420 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
||
446 | |||
447 | /// |
||
448 | // Twig Functionality |
||
449 | /// |
||
450 | |||
451 | /** |
||
452 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
453 | * |
||
454 | * @param TemplateInterface $template |
||
455 | * @param RepeaterPageView $pageView |
||
456 | * @param ExpandedValue $expandedValue |
||
457 | * |
||
458 | * @since 0.1.1 |
||
459 | * |
||
460 | * @return string |
||
461 | */ |
||
462 | private function renderRepeaterPageView(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
||
474 | |||
475 | /** |
||
476 | * Get the compiled HTML for a specific ContentItem. |
||
477 | * |
||
478 | * @since 0.1.1 |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | private function renderDynamicPageView(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
||
489 | |||
490 | /** |
||
491 | * Get the compiled HTML for a static PageView. |
||
492 | * |
||
493 | * @since 0.1.1 |
||
494 | * |
||
495 | * @throws TemplateErrorInterface |
||
496 | * |
||
497 | * @return string |
||
498 | */ |
||
499 | private function renderStaticPageView(StaticPageView &$pageView) |
||
507 | |||
508 | /** |
||
509 | * Create a Twig template that just needs an array to render. |
||
510 | * |
||
511 | * @since 0.1.1 |
||
512 | * |
||
513 | * @throws TemplateErrorInterface |
||
514 | * |
||
515 | * @return TemplateInterface |
||
516 | */ |
||
517 | private function createTwigTemplate(BasePageView &$pageView) |
||
563 | } |
||
564 |
This check marks private properties in classes that are never used. Those properties can be removed.