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 PageManager 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 PageManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class PageManager extends TrackingManager |
||
33 | { |
||
34 | /** |
||
35 | * The relative (to the stakx project) file path to the redirect template |
||
36 | * |
||
37 | * @var string|bool |
||
38 | */ |
||
39 | private $redirectTemplate; |
||
40 | |||
41 | /** |
||
42 | * @var PageView[] |
||
43 | */ |
||
44 | private $twigExtendsDeps; |
||
45 | |||
46 | /** |
||
47 | * @var ContentItem[][] |
||
48 | */ |
||
49 | private $collections; |
||
50 | |||
51 | /** |
||
52 | * @var Folder |
||
53 | */ |
||
54 | private $targetDir; |
||
55 | |||
56 | /** |
||
57 | * @var PageView[] |
||
58 | */ |
||
59 | private $flatPages; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | private $twigOpts; |
||
65 | |||
66 | /** |
||
67 | * @var \Twig_Environment |
||
68 | */ |
||
69 | private $twig; |
||
70 | |||
71 | /** |
||
72 | * PageManager constructor |
||
73 | */ |
||
74 | 2 | public function __construct() |
|
83 | |||
84 | /** |
||
85 | * Give this manager the collections we'll be using for dynamic PageViews |
||
86 | * |
||
87 | * @param ContentItem[][] $collections |
||
88 | */ |
||
89 | 2 | public function setCollections (&$collections) |
|
93 | |||
94 | /** |
||
95 | * Set the template used for redirects |
||
96 | * |
||
97 | * @param false|string $filePath The path to the redirect template |
||
98 | */ |
||
99 | public function setRedirectTemplate ($filePath) |
||
103 | |||
104 | /** |
||
105 | * The location where the compiled website will be written to |
||
106 | * |
||
107 | * @param Folder $folder The relative target directory as specified from the configuration file |
||
108 | */ |
||
109 | 2 | public function setTargetFolder (&$folder) |
|
113 | |||
114 | 2 | public function configureTwig ($configuration, $options) |
|
121 | |||
122 | public function getStaticPages () |
||
126 | |||
127 | 1 | View Code Duplication | public function getJailedStaticPages () |
144 | |||
145 | /** |
||
146 | * Go through all of the PageView directories and create a respective PageView for each and classify them as a |
||
147 | * dynamic or static PageView. |
||
148 | * |
||
149 | * @param $pageViewFolders |
||
150 | */ |
||
151 | 2 | public function parsePageViews ($pageViewFolders) |
|
175 | |||
176 | /** |
||
177 | * Compile dynamic and static PageViews |
||
178 | */ |
||
179 | 2 | public function compileAll () |
|
186 | |||
187 | public function compileSome ($filter = array()) |
||
198 | |||
199 | /** |
||
200 | * @param ContentItem $contentItem |
||
201 | */ |
||
202 | public function compileContentItem (&$contentItem) |
||
223 | |||
224 | /** |
||
225 | * Add a new ContentItem to the respective parent PageView of the ContentItem |
||
226 | * |
||
227 | * @param ContentItem $contentItem |
||
228 | */ |
||
229 | public function updatePageView ($contentItem) |
||
242 | |||
243 | /** |
||
244 | * Update an existing Twig variable that's injected globally |
||
245 | * |
||
246 | * @param string $variable |
||
247 | * @param string $value |
||
248 | */ |
||
249 | public function updateTwigVariable ($variable, $value) |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | 2 | public function isTracked($filePath) |
|
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | 2 | public function refreshItem($filePath) |
|
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | 2 | protected function handleTrackableItem($filePath, $options = array()) |
|
309 | |||
310 | /** |
||
311 | * @param DynamicPageView $pageView |
||
312 | */ |
||
313 | 2 | private function handleTrackableDynamicPageView ($pageView) |
|
329 | |||
330 | /** |
||
331 | * @param PageView $pageView |
||
332 | */ |
||
333 | 2 | private function handleTrackableStaticPageView ($pageView) |
|
339 | |||
340 | /** |
||
341 | * Create a Twig environment |
||
342 | */ |
||
343 | 2 | private function createTwigManager () |
|
353 | |||
354 | /** |
||
355 | * Compile a given PageView |
||
356 | * |
||
357 | * @param string $filePath The file path to the PageView to compile |
||
358 | * |
||
359 | * @throws \Exception |
||
360 | */ |
||
361 | 2 | private function compileFromFilePath ($filePath) |
|
381 | |||
382 | /** |
||
383 | * @param DynamicPageView|RepeaterPageView|PageView $pageView |
||
384 | */ |
||
385 | 2 | private function compilePageView ($pageView) |
|
405 | |||
406 | /** |
||
407 | * @param RepeaterPageView $pageView |
||
408 | */ |
||
409 | 2 | private function compileRepeaterPageView (&$pageView) |
|
430 | |||
431 | /** |
||
432 | * @param PageView $pageView |
||
433 | */ |
||
434 | 2 | private function compileDynamicPageView (&$pageView) |
|
457 | |||
458 | /** |
||
459 | * @param PageView $pageView |
||
460 | */ |
||
461 | 2 | private function compileStaticPageView (&$pageView) |
|
473 | |||
474 | /** |
||
475 | * @param DynamicPageView|PageView $pageView |
||
476 | */ |
||
477 | 2 | private function compileNormalRedirects (&$pageView) |
|
490 | |||
491 | /** |
||
492 | * @param RepeaterPageView $pageView |
||
493 | */ |
||
494 | 2 | private function compileExpandedRedirects (&$pageView) |
|
517 | |||
518 | /** |
||
519 | * @param PageView $pageView |
||
520 | * |
||
521 | * @return Twig_Template |
||
522 | * @throws Twig_Error_Syntax |
||
523 | */ |
||
524 | 2 | private function createTemplate (&$pageView) |
|
542 | |||
543 | /** |
||
544 | * Find the parent Twig templates of the given template and keep a list of it |
||
545 | * |
||
546 | * @param Twig_Template $template The template created from the PageView's content |
||
547 | * @param PageView $pageView The PageView that has this content. Used to keep a reference of PageViews |
||
548 | */ |
||
549 | 2 | private function trackParentTwigTemplate ($template, &$pageView) |
|
564 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.