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 |
||
40 | class Compiler |
||
41 | { |
||
42 | /** @var string|false */ |
||
43 | private $redirectTemplate; |
||
44 | |||
45 | /** @var BasePageView[][] */ |
||
46 | private $importDependencies; |
||
47 | |||
48 | /** |
||
49 | * Any time a PageView extends another template, that relationship is stored in this array. This is necessary so |
||
50 | * when watching a website, we can rebuild the necessary PageViews when these base templates change. |
||
51 | * |
||
52 | * ``` |
||
53 | * array['_layouts/base.html.twig'] = &PageView; |
||
54 | * ``` |
||
55 | * |
||
56 | * @var TemplateInterface[] |
||
57 | */ |
||
58 | private $templateDependencies; |
||
59 | |||
60 | /** |
||
61 | * All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
||
62 | * |
||
63 | * ``` |
||
64 | * array['_pages/index.html.twig'] = &PageView; |
||
65 | * ``` |
||
66 | * |
||
67 | * @var BasePageView[] |
||
68 | */ |
||
69 | private $pageViewsFlattened; |
||
70 | |||
71 | /** @var string[] */ |
||
72 | private $templateMapping; |
||
73 | |||
74 | /** @var Folder */ |
||
75 | private $folder; |
||
76 | |||
77 | /** @var string */ |
||
78 | private $theme; |
||
79 | |||
80 | private $templateBridge; |
||
81 | private $pageManager; |
||
82 | private $eventDispatcher; |
||
83 | private $configuration; |
||
84 | |||
85 | 12 | public function __construct(TemplateBridgeInterface $templateBridge, Configuration $configuration, PageManager $pageManager, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
|
97 | |||
98 | /** |
||
99 | * @param Folder $folder |
||
100 | */ |
||
101 | 12 | public function setTargetFolder(Folder $folder) |
|
105 | |||
106 | /** |
||
107 | * @param string $themeName |
||
108 | */ |
||
109 | public function setThemeName($themeName) |
||
113 | |||
114 | /// |
||
115 | // Twig parent templates |
||
116 | /// |
||
117 | |||
118 | public function isImportDependency($filePath) |
||
122 | |||
123 | /** |
||
124 | * Check whether a given file path is used as a parent template by a PageView. |
||
125 | * |
||
126 | * @param string $filePath |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function isParentTemplate($filePath) |
||
134 | |||
135 | /** |
||
136 | * Rebuild all of the PageViews that used a given template as a parent. |
||
137 | * |
||
138 | * @param string $filePath The file path to the parent Twig template |
||
139 | */ |
||
140 | public function refreshParent($filePath) |
||
147 | |||
148 | public function getTemplateMappings() |
||
152 | |||
153 | /// |
||
154 | // IO Functionality |
||
155 | /// |
||
156 | |||
157 | /** |
||
158 | * Compile all of the PageViews registered with the compiler. |
||
159 | * |
||
160 | * @since 0.1.0 |
||
161 | */ |
||
162 | 12 | public function compileAll() |
|
169 | |||
170 | public function compileImportDependencies($filePath) |
||
177 | |||
178 | public function compileSome(array $filter = []) |
||
192 | |||
193 | /** |
||
194 | * Compile an individual PageView from a given path. |
||
195 | * |
||
196 | * @param string $filePath |
||
197 | * |
||
198 | * @throws FileNotFoundException when the given file path isn't tracked by the Compiler |
||
199 | */ |
||
200 | public function compilePageViewFromPath($filePath) |
||
209 | |||
210 | /** |
||
211 | * Compile an individual PageView item. |
||
212 | * |
||
213 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
214 | * respective target file. |
||
215 | * |
||
216 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
217 | * |
||
218 | * @since 0.1.1 |
||
219 | */ |
||
220 | 12 | public function compilePageView(BasePageView &$pageView) |
|
259 | |||
260 | /** |
||
261 | * Write the compiled output for a static PageView. |
||
262 | * |
||
263 | * @since 0.1.1 |
||
264 | * |
||
265 | * @throws TemplateErrorInterface |
||
266 | */ |
||
267 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
277 | |||
278 | /** |
||
279 | * Write the compiled output for a dynamic PageView. |
||
280 | * |
||
281 | * @param DynamicPageView $pageView |
||
282 | * |
||
283 | * @since 0.1.1 |
||
284 | * |
||
285 | * @throws TemplateErrorInterface |
||
286 | */ |
||
287 | private function compileDynamicPageViews(DynamicPageView &$pageView) |
||
312 | |||
313 | /** |
||
314 | * Write the compiled output for a repeater PageView. |
||
315 | * |
||
316 | * @param RepeaterPageView $pageView |
||
317 | * |
||
318 | * @since 0.1.1 |
||
319 | * |
||
320 | * @throws TemplateErrorInterface |
||
321 | */ |
||
322 | 3 | private function compileRepeaterPageViews(RepeaterPageView &$pageView) |
|
339 | |||
340 | /** |
||
341 | * @deprecated |
||
342 | * |
||
343 | * @todo This function needs to be rewritten or removed. Something |
||
344 | * |
||
345 | * @param ContentItem $contentItem |
||
346 | */ |
||
347 | public function compileContentItem(ContentItem &$contentItem) |
||
361 | |||
362 | /// |
||
363 | // Redirect handling |
||
364 | /// |
||
365 | |||
366 | /** |
||
367 | * Write redirects for standard redirects. |
||
368 | * |
||
369 | * @throws TemplateErrorInterface |
||
370 | * |
||
371 | * @since 0.1.1 |
||
372 | */ |
||
373 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
391 | |||
392 | /** |
||
393 | * Write redirects for expanded redirects. |
||
394 | * |
||
395 | * @param RepeaterPageView $pageView |
||
396 | * |
||
397 | * @since 0.1.1 |
||
398 | */ |
||
399 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
425 | |||
426 | /// |
||
427 | // Twig Functionality |
||
428 | /// |
||
429 | |||
430 | /** |
||
431 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
432 | * |
||
433 | * @param TemplateInterface $template |
||
434 | * @param RepeaterPageView $pageView |
||
435 | * @param ExpandedValue $expandedValue |
||
436 | * |
||
437 | * @since 0.1.1 |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | 3 | private function renderRepeaterPageView(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
454 | |||
455 | /** |
||
456 | * Get the compiled HTML for a specific ContentItem. |
||
457 | * |
||
458 | * @since 0.1.1 |
||
459 | * |
||
460 | * @return string |
||
461 | */ |
||
462 | private function renderDynamicPageView(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
||
470 | |||
471 | /** |
||
472 | * Get the compiled HTML for a static PageView. |
||
473 | * |
||
474 | * @since 0.1.1 |
||
475 | * |
||
476 | * @throws TemplateErrorInterface |
||
477 | * |
||
478 | * @return string |
||
479 | */ |
||
480 | 10 | private function renderStaticPageView(StaticPageView &$pageView) |
|
489 | |||
490 | /** |
||
491 | * Create a Twig template that just needs an array to render. |
||
492 | * |
||
493 | * @since 0.1.1 |
||
494 | * |
||
495 | * @throws TemplateErrorInterface |
||
496 | * |
||
497 | * @return TemplateInterface |
||
498 | */ |
||
499 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
545 | } |
||
546 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: