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 |
||
37 | class Compiler extends BaseManager |
||
38 | { |
||
39 | /** @var string|false */ |
||
40 | private $redirectTemplate; |
||
41 | |||
42 | /** @var PageView[][] */ |
||
43 | private $importDependencies; |
||
44 | |||
45 | /** @var Twig_Template[] */ |
||
46 | private $templateDependencies; |
||
47 | |||
48 | /** @var PageView[] */ |
||
49 | private $pageViewsFlattened; |
||
50 | |||
51 | /** @var string[] */ |
||
52 | private $templateMapping; |
||
53 | |||
54 | /** @var PageView[][] */ |
||
55 | private $pageViews; |
||
56 | |||
57 | /** @var Folder */ |
||
58 | private $folder; |
||
59 | |||
60 | /** @var string */ |
||
61 | private $theme; |
||
62 | |||
63 | /** @var Twig_Environment */ |
||
64 | private $twig; |
||
65 | |||
66 | 14 | public function __construct() |
|
73 | |||
74 | /** |
||
75 | * @param string|false $template |
||
76 | */ |
||
77 | public function setRedirectTemplate($template) |
||
81 | |||
82 | /** |
||
83 | * @param Folder $folder |
||
84 | */ |
||
85 | 14 | public function setTargetFolder(Folder $folder) |
|
89 | |||
90 | /** |
||
91 | * @param PageView[][] $pageViews |
||
92 | * @param PageView[] $pageViewsFlattened |
||
93 | */ |
||
94 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
99 | |||
100 | /** |
||
101 | * @param string $themeName |
||
102 | */ |
||
103 | public function setThemeName($themeName) |
||
107 | |||
108 | /// |
||
109 | // Twig parent templates |
||
110 | /// |
||
111 | |||
112 | public function isImportDependency($filePath) |
||
116 | |||
117 | /** |
||
118 | * Check whether a given file path is used as a parent template by a PageView |
||
119 | * |
||
120 | * @param string $filePath |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function isParentTemplate($filePath) |
||
128 | |||
129 | /** |
||
130 | * Rebuild all of the PageViews that used a given template as a parent |
||
131 | * |
||
132 | * @param string $filePath The file path to the parent Twig template |
||
133 | */ |
||
134 | public function refreshParent($filePath) |
||
141 | |||
142 | public function getTemplateMappings() |
||
146 | |||
147 | /// |
||
148 | // IO Functionality |
||
149 | /// |
||
150 | |||
151 | /** |
||
152 | * Compile all of the PageViews registered with the compiler. |
||
153 | * |
||
154 | * @since 0.1.0 |
||
155 | */ |
||
156 | 14 | public function compileAll() |
|
163 | |||
164 | public function compileImportDependencies($filePath) |
||
171 | |||
172 | public function compileSome(array $filter = array()) |
||
186 | |||
187 | /** |
||
188 | * Compile an individual PageView item. |
||
189 | * |
||
190 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
191 | * respective target file. |
||
192 | * |
||
193 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
194 | * |
||
195 | * @since 0.1.1 |
||
196 | */ |
||
197 | 14 | public function compilePageView(PageView &$pageView) |
|
236 | |||
237 | /** |
||
238 | * Write the compiled output for a static PageView. |
||
239 | * |
||
240 | * @param PageView $pageView |
||
241 | * |
||
242 | * @since 0.1.1 |
||
243 | */ |
||
244 | 10 | private function compileStaticPageView(PageView &$pageView) |
|
252 | |||
253 | /** |
||
254 | * Write the compiled output for a dynamic PageView. |
||
255 | * |
||
256 | * @param DynamicPageView $pageView |
||
257 | * |
||
258 | * @since 0.1.1 |
||
259 | */ |
||
260 | 2 | private function compileDynamicPageViews(DynamicPageView &$pageView) |
|
283 | |||
284 | /** |
||
285 | * Write the compiled output for a repeater PageView. |
||
286 | * |
||
287 | * @param RepeaterPageView $pageView |
||
288 | * |
||
289 | * @since 0.1.1 |
||
290 | */ |
||
291 | 2 | private function compileRepeaterPageViews(RepeaterPageView &$pageView) |
|
308 | |||
309 | /** |
||
310 | * @deprecated |
||
311 | * |
||
312 | * @todo This function needs to be rewritten or removed. Something |
||
313 | * |
||
314 | * @param ContentItem $contentItem |
||
315 | */ |
||
316 | public function compileContentItem(ContentItem &$contentItem) |
||
330 | |||
331 | /// |
||
332 | // Redirect handling |
||
333 | /// |
||
334 | |||
335 | /** |
||
336 | * Write redirects for standard redirects. |
||
337 | * |
||
338 | * @param PageView $pageView |
||
339 | * |
||
340 | * @since 0.1.1 |
||
341 | */ |
||
342 | 12 | private function compileStandardRedirects(PageView &$pageView) |
|
357 | |||
358 | /** |
||
359 | * Write redirects for expanded redirects. |
||
360 | * |
||
361 | * @param RepeaterPageView $pageView |
||
362 | * |
||
363 | * @since 0.1.1 |
||
364 | */ |
||
365 | 2 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
387 | |||
388 | /// |
||
389 | // Twig Functionality |
||
390 | /// |
||
391 | |||
392 | /** |
||
393 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
394 | * |
||
395 | * @param Twig_Template $template |
||
396 | * @param PageView $pageView |
||
397 | * @param ExpandedValue $expandedValue |
||
398 | * |
||
399 | * @since 0.1.1 |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | 2 | private function renderRepeaterPageView(Twig_Template &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
415 | |||
416 | /** |
||
417 | * Get the compiled HTML for a specific ContentItem. |
||
418 | * |
||
419 | * @param Twig_Template $template |
||
420 | * @param TwigDocument $twigItem |
||
421 | * |
||
422 | * @return string |
||
423 | * @since 0.1.1 |
||
424 | * |
||
425 | */ |
||
426 | 2 | private function renderDynamicPageView(Twig_Template &$template, TwigDocument &$twigItem) |
|
433 | |||
434 | /** |
||
435 | * Get the compiled HTML for a static PageView. |
||
436 | * |
||
437 | * @param PageView $pageView |
||
438 | * |
||
439 | * @since 0.1.1 |
||
440 | * |
||
441 | * @throws \Exception |
||
442 | * @throws \Throwable |
||
443 | * @throws Twig_Error_Syntax |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | 10 | private function renderStaticPageView(PageView &$pageView) |
|
455 | |||
456 | /** |
||
457 | * Create a Twig template that just needs an array to render. |
||
458 | * |
||
459 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
460 | * |
||
461 | * @since 0.1.1 |
||
462 | * |
||
463 | * @throws \Exception |
||
464 | * @throws \Throwable |
||
465 | * @throws Twig_Error_Syntax |
||
466 | * |
||
467 | * @return Twig_Template |
||
468 | */ |
||
469 | 14 | private function createTwigTemplate(PageView &$pageView) |
|
514 | } |
||
515 |