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 |
||
35 | class Compiler extends BaseManager |
||
36 | { |
||
37 | /** @var string|false */ |
||
38 | private $redirectTemplate; |
||
39 | |||
40 | /** @var Twig_Template[] */ |
||
41 | private $templateDependencies; |
||
42 | |||
43 | /** @var PageView[] */ |
||
44 | private $pageViewsFlattened; |
||
45 | |||
46 | /** @var string[] */ |
||
47 | private $templateMapping; |
||
48 | |||
49 | /** @var PageView[][] */ |
||
50 | private $pageViews; |
||
51 | |||
52 | /** @var Folder */ |
||
53 | private $folder; |
||
54 | |||
55 | /** @var string */ |
||
56 | private $theme; |
||
57 | |||
58 | /** @var Twig_Environment */ |
||
59 | private $twig; |
||
60 | |||
61 | 14 | public function __construct() |
|
68 | |||
69 | /** |
||
70 | * @param string|false $template |
||
71 | */ |
||
72 | public function setRedirectTemplate($template) |
||
73 | { |
||
74 | $this->redirectTemplate = $template; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param Folder $folder |
||
79 | */ |
||
80 | 14 | public function setTargetFolder(Folder $folder) |
|
84 | |||
85 | /** |
||
86 | * @param PageView[][] $pageViews |
||
87 | * @param PageView[] $pageViewsFlattened |
||
88 | */ |
||
89 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
94 | |||
95 | /** |
||
96 | * @param string $themeName |
||
97 | */ |
||
98 | public function setThemeName($themeName) |
||
102 | |||
103 | /// |
||
104 | // Twig parent templates |
||
105 | /// |
||
106 | |||
107 | /** |
||
108 | * Check whether a given file path is used as a parent template by a PageView |
||
109 | * |
||
110 | * @param string $filePath |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function isParentTemplate($filePath) |
||
118 | |||
119 | /** |
||
120 | * Rebuild all of the PageViews that used a given template as a parent |
||
121 | * |
||
122 | * @param string $filePath The file path to the parent Twig template |
||
123 | */ |
||
124 | public function refreshParent($filePath) |
||
131 | |||
132 | public function getTemplateMappings() |
||
136 | |||
137 | /// |
||
138 | // IO Functionality |
||
139 | /// |
||
140 | |||
141 | /** |
||
142 | * Compile all of the PageViews registered with the compiler. |
||
143 | * |
||
144 | * @since 0.1.0 |
||
145 | */ |
||
146 | 14 | public function compileAll() |
|
153 | |||
154 | public function compileSome($filter = array()) |
||
155 | { |
||
156 | /** @var PageView $pageView */ |
||
157 | foreach ($this->pageViewsFlattened as &$pageView) |
||
158 | { |
||
159 | $ns = $filter['namespace']; |
||
160 | |||
161 | if ($pageView->hasTwigDependency($ns, $filter['dependency']) || |
||
162 | $pageView->hasTwigDependency($ns, null) |
||
163 | ) { |
||
164 | $this->compilePageView($pageView); |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Compile an individual PageView item. |
||
171 | * |
||
172 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
173 | * respective target file. |
||
174 | * |
||
175 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
176 | * |
||
177 | * @since 0.1.1 |
||
178 | */ |
||
179 | 14 | public function compilePageView(&$pageView) |
|
180 | { |
||
181 | 14 | $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); |
|
182 | 14 | $this->output->debug('Compiling {type} PageView: {pageview}', array( |
|
183 | 14 | 'pageview' => $pageView->getRelativeFilePath(), |
|
184 | 14 | 'type' => $pageView->getType() |
|
185 | )); |
||
186 | |||
187 | try |
||
188 | { |
||
189 | 14 | switch ($pageView->getType()) |
|
190 | { |
||
191 | 14 | case PageView::STATIC_TYPE: |
|
192 | 10 | $this->compileStaticPageView($pageView); |
|
193 | 10 | $this->compileStandardRedirects($pageView); |
|
194 | 10 | break; |
|
195 | |||
196 | 4 | case PageView::DYNAMIC_TYPE: |
|
197 | 2 | $this->compileDynamicPageViews($pageView); |
|
1 ignored issue
–
show
|
|||
198 | 2 | $this->compileStandardRedirects($pageView); |
|
199 | 2 | break; |
|
200 | |||
201 | 2 | case PageView::REPEATER_TYPE: |
|
202 | 2 | $this->compileRepeaterPageViews($pageView); |
|
1 ignored issue
–
show
|
|||
203 | 2 | $this->compileExpandedRedirects($pageView); |
|
204 | 14 | break; |
|
205 | } |
||
206 | } |
||
207 | catch (Twig_Error_Runtime $e) |
||
208 | { |
||
209 | throw new FileAwareException( |
||
210 | $e->getRawMessage(), |
||
211 | $e->getCode(), |
||
212 | $e, |
||
213 | $pageView->getRelativeFilePath(), |
||
214 | $e->getTemplateLine() + $pageView->getLineOffset() |
||
215 | ); |
||
216 | } |
||
217 | 14 | } |
|
218 | |||
219 | /** |
||
220 | * Write the compiled output for a static PageView. |
||
221 | * |
||
222 | * @param PageView $pageView |
||
223 | * |
||
224 | * @since 0.1.1 |
||
225 | */ |
||
226 | 10 | private function compileStaticPageView(&$pageView) |
|
234 | |||
235 | /** |
||
236 | * Write the compiled output for a dynamic PageView. |
||
237 | * |
||
238 | * @param DynamicPageView $pageView |
||
239 | * |
||
240 | * @since 0.1.1 |
||
241 | */ |
||
242 | 2 | private function compileDynamicPageViews(&$pageView) |
|
243 | { |
||
244 | 2 | $contentItems = $pageView->getRepeatableItems(); |
|
245 | 2 | $template = $this->createTwigTemplate($pageView); |
|
246 | |||
247 | 2 | foreach ($contentItems as &$contentItem) |
|
248 | { |
||
249 | 2 | if ($contentItem->isDraft() && !Service::getParameter(BuildableCommand::USE_DRAFTS)) |
|
250 | { |
||
251 | 1 | $this->output->debug('{file}: marked as a draft', array( |
|
252 | 1 | 'file' => $contentItem->getRelativeFilePath() |
|
253 | )); |
||
254 | |||
255 | 1 | continue; |
|
256 | } |
||
257 | |||
258 | 2 | $targetFile = $contentItem->getTargetFile(); |
|
259 | 2 | $output = $this->renderDynamicPageView($template, $contentItem); |
|
260 | |||
261 | 2 | $this->output->notice('Writing file: {file}', array('file' => $targetFile)); |
|
262 | 2 | $this->folder->writeFile($targetFile, $output); |
|
263 | } |
||
264 | 2 | } |
|
265 | |||
266 | /** |
||
267 | * Write the compiled output for a repeater PageView. |
||
268 | * |
||
269 | * @param RepeaterPageView $pageView |
||
270 | * |
||
271 | * @since 0.1.1 |
||
272 | */ |
||
273 | 2 | private function compileRepeaterPageViews(&$pageView) |
|
274 | { |
||
275 | 2 | $pageView->rewindPermalink(); |
|
276 | |||
277 | 2 | $template = $this->createTwigTemplate($pageView); |
|
278 | 2 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
279 | |||
280 | 2 | foreach ($permalinks as $permalink) |
|
281 | { |
||
282 | 2 | $pageView->bumpPermalink(); |
|
283 | 2 | $targetFile = $pageView->getTargetFile(); |
|
284 | 2 | $output = $this->renderRepeaterPageView($template, $pageView, $permalink); |
|
285 | |||
286 | 2 | $this->output->notice('Writing repeater file: {file}', array('file' => $targetFile)); |
|
287 | 2 | $this->folder->writeFile($targetFile, $output); |
|
288 | } |
||
289 | 2 | } |
|
290 | |||
291 | /** |
||
292 | * @deprecated |
||
293 | * |
||
294 | * @todo This function needs to be rewritten or removed. Something |
||
295 | * |
||
296 | * @param ContentItem $contentItem |
||
297 | */ |
||
298 | public function compileContentItem(&$contentItem) |
||
312 | |||
313 | /// |
||
314 | // Redirect handling |
||
315 | /// |
||
316 | |||
317 | /** |
||
318 | * Write redirects for standard redirects. |
||
319 | * |
||
320 | * @param PageView $pageView |
||
321 | * |
||
322 | * @since 0.1.1 |
||
323 | */ |
||
324 | 12 | private function compileStandardRedirects(&$pageView) |
|
339 | |||
340 | /** |
||
341 | * Write redirects for expanded redirects. |
||
342 | * |
||
343 | * @param RepeaterPageView $pageView |
||
344 | * |
||
345 | * @since 0.1.1 |
||
346 | */ |
||
347 | 2 | private function compileExpandedRedirects(&$pageView) |
|
369 | |||
370 | /// |
||
371 | // Twig Functionality |
||
372 | /// |
||
373 | |||
374 | /** |
||
375 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
376 | * |
||
377 | * @param Twig_Template $template |
||
378 | * @param PageView $pageView |
||
379 | * @param ExpandedValue $expandedValue |
||
380 | * |
||
381 | * @since 0.1.1 |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
397 | |||
398 | /** |
||
399 | * Get the compiled HTML for a specific ContentItem. |
||
400 | * |
||
401 | * @param Twig_Template $template |
||
402 | * @param ContentItem $contentItem |
||
403 | * |
||
404 | * @since 0.1.1 |
||
405 | * |
||
406 | * @return string |
||
407 | */ |
||
408 | 2 | private function renderDynamicPageView(&$template, &$contentItem) |
|
415 | |||
416 | /** |
||
417 | * Get the compiled HTML for a static PageView. |
||
418 | * |
||
419 | * @param PageView $pageView |
||
420 | * |
||
421 | * @since 0.1.1 |
||
422 | * |
||
423 | * @throws \Exception |
||
424 | * @throws \Throwable |
||
425 | * @throws Twig_Error_Syntax |
||
426 | * |
||
427 | * @return string |
||
428 | */ |
||
429 | 10 | private function renderStaticPageView(&$pageView) |
|
437 | |||
438 | /** |
||
439 | * Create a Twig template that just needs an array to render. |
||
440 | * |
||
441 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
442 | * |
||
443 | * @since 0.1.1 |
||
444 | * |
||
445 | * @throws \Exception |
||
446 | * @throws \Throwable |
||
447 | * @throws Twig_Error_Syntax |
||
448 | * |
||
449 | * @return Twig_Template |
||
450 | */ |
||
451 | 14 | private function createTwigTemplate(&$pageView) |
|
486 | } |
||
487 |