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