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 CFileBasedContent 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 CFileBasedContent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class CFileBasedContent |
||
9 | { |
||
10 | use \Anax\TConfigure, |
||
11 | \Anax\DI\TInjectionAware; |
||
12 | |||
13 | |||
14 | |||
15 | /** |
||
16 | * Properties. |
||
17 | */ |
||
18 | private $index = null; |
||
19 | private $meta = null; |
||
20 | private $ignoreCache = false; |
||
21 | |||
22 | /** |
||
23 | * File name pattern, all files must match this pattern and the first |
||
24 | * numbered part is optional, the second part becomes the route. |
||
25 | */ |
||
26 | private $filenamePattern = "#^(\d*)_*([^\.]+)\.md$#"; |
||
27 | |||
28 | /** |
||
29 | * Internal routes that is marked as internal content routes and not |
||
30 | * exposed as public routes. |
||
31 | */ |
||
32 | private $internalRouteDirPattern = [ |
||
33 | "#block/#", |
||
34 | ]; |
||
35 | |||
36 | private $internalRouteFilePattern = [ |
||
37 | "#^block[_-]{1}#", |
||
38 | "#^_#", |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Routes that should be used in toc. |
||
43 | */ |
||
44 | private $allowedInTocPattern = "([\d]+_(\w)+)"; |
||
45 | |||
46 | |||
47 | |||
48 | /** |
||
49 | * Create a breadcrumb, append slash / to all dirs. |
||
50 | * |
||
51 | * @param string $route current route. |
||
52 | * |
||
53 | * @return array with values for the breadcrumb. |
||
54 | */ |
||
55 | public function createBreadcrumb($route) |
||
56 | { |
||
57 | $breadcrumbs = []; |
||
58 | |||
59 | while ($route !== "./" && $route !== "/") { |
||
60 | $routeIndex = $this->mapRoute2IndexKey($route); |
||
61 | $item["url"] = $route; |
||
|
|||
62 | $item["text"] = $this->getBreadcrumbTitle($this->index[$routeIndex]["file"]); |
||
63 | $breadcrumbs[] = $item; |
||
64 | $route = dirname($route) . "/"; |
||
65 | } |
||
66 | |||
67 | krsort($breadcrumbs); |
||
68 | return $breadcrumbs; |
||
69 | } |
||
70 | |||
71 | |||
72 | |||
73 | /** |
||
74 | * Get time when the content was last updated. |
||
75 | * |
||
76 | * @return string with the time. |
||
77 | */ |
||
78 | /*public function PublishTime() { |
||
79 | if(!empty($this['published'])) { |
||
80 | return $this['published']; |
||
81 | } else if(isset($this['updated'])) { |
||
82 | return $this['updated']; |
||
83 | } else { |
||
84 | return $this['created']; |
||
85 | } |
||
86 | } |
||
87 | */ |
||
88 | /** |
||
89 | * Get the action for latest updated of the content. |
||
90 | * |
||
91 | * @return string with the time. |
||
92 | */ |
||
93 | /*public function PublishAction() { |
||
94 | if(!empty($this['published'])) { |
||
95 | //return t('Published'); |
||
96 | return t('Last updated'); |
||
97 | } else if(isset($this['updated'])) { |
||
98 | return t('Updated'); |
||
99 | } else { |
||
100 | return t('Created'); |
||
101 | } |
||
102 | } |
||
103 | */ |
||
104 | |||
105 | |||
106 | |||
107 | /** |
||
108 | * Set default values from configuration. |
||
109 | * |
||
110 | * @return this. |
||
111 | */ |
||
112 | public function setDefaultsFromConfiguration() |
||
113 | { |
||
114 | $this->ignoreCache = isset($this->config["ignoreCache"]) |
||
115 | ? $this->config["ignoreCache"] |
||
116 | : $this->ignoreCache; |
||
117 | |||
118 | return $this; |
||
119 | } |
||
120 | |||
121 | |||
122 | |||
123 | /** |
||
124 | * Should the cache be used or ignored. |
||
125 | * |
||
126 | * @param boolean $use true to use the cache or false to ignore the cache |
||
127 | * |
||
128 | * @return this. |
||
129 | */ |
||
130 | public function useCache($use) |
||
131 | { |
||
132 | $this->ignoreCache = !$use; |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | |||
138 | |||
139 | /** |
||
140 | * Get the index as an array. |
||
141 | * |
||
142 | * @return array as index. |
||
143 | */ |
||
144 | public function getIndex() |
||
145 | { |
||
146 | return $this->loadIndex(); |
||
147 | } |
||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * Create the index of all content into an array. |
||
153 | * |
||
154 | * @return array as index. |
||
155 | */ |
||
156 | View Code Duplication | private function loadIndex() |
|
157 | { |
||
158 | if ($this->index) { |
||
159 | return $this->index; |
||
160 | } |
||
161 | |||
162 | $key = $this->di->cache->createKey(__CLASS__, "index"); |
||
163 | $this->index = $this->di->cache->get($key); |
||
164 | |||
165 | if (!$this->index || $this->ignoreCache) { |
||
166 | $this->index = $this->createIndex(); |
||
167 | $this->di->cache->put($key, $this->index); |
||
168 | } |
||
169 | |||
170 | return $this->index; |
||
171 | } |
||
172 | |||
173 | |||
174 | |||
175 | /** |
||
176 | * Check if a filename is to be marked as an internal route.. |
||
177 | * |
||
178 | * @param string $filepath as the basepath (routepart) to the file. |
||
179 | * |
||
180 | * @return boolean true if the route content is internal, else false |
||
181 | */ |
||
182 | private function isInternalRoute($filepath) |
||
183 | { |
||
184 | foreach ($this->internalRouteDirPattern as $pattern) { |
||
185 | if (preg_match($pattern, $filepath)) { |
||
186 | return true; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | $filename = basename($filepath); |
||
191 | foreach ($this->internalRouteFilePattern as $pattern) { |
||
192 | if (preg_match($pattern, $filename)) { |
||
193 | return true; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | return false; |
||
198 | } |
||
199 | |||
200 | |||
201 | |||
202 | /** |
||
203 | * Check if filepath should be used as part of toc. |
||
204 | * |
||
205 | * @param string $filepath as the basepath (routepart) to the file. |
||
206 | * |
||
207 | * @return boolean true if the route content shoul dbe in toc, else false |
||
208 | */ |
||
209 | private function allowInToc($filepath) |
||
210 | { |
||
211 | return (boolean) preg_match($this->allowedInTocPattern, $filepath); |
||
212 | } |
||
213 | |||
214 | |||
215 | |||
216 | /** |
||
217 | * Generate an index from the directory structure. |
||
218 | * |
||
219 | * @return array as index for all content files. |
||
220 | */ |
||
221 | private function createIndex() |
||
222 | { |
||
223 | $basepath = $this->config["basepath"]; |
||
224 | $pattern = $this->config["pattern"]; |
||
225 | $path = "$basepath/$pattern"; |
||
226 | |||
227 | $index = []; |
||
228 | foreach (glob_recursive($path) as $file) { |
||
229 | $filepath = substr($file, strlen($basepath) + 1); |
||
230 | |||
231 | // Find content files |
||
232 | $matches = []; |
||
233 | preg_match($this->filenamePattern, basename($filepath), $matches); |
||
234 | $dirpart = dirname($filepath) . "/"; |
||
235 | if ($dirpart === "./") { |
||
236 | $dirpart = null; |
||
237 | } |
||
238 | $key = $dirpart . $matches[2]; |
||
239 | |||
240 | // Create level depending on the file id |
||
241 | $id = $matches[1]; |
||
242 | $level = 2; |
||
243 | View Code Duplication | if ($id % 100 === 0) { |
|
244 | $level = 0; |
||
245 | } elseif ($id % 10 === 0) { |
||
246 | $level = 1; |
||
247 | } |
||
248 | |||
249 | $index[$key] = [ |
||
250 | "file" => $filepath, |
||
251 | "section" => $matches[1], |
||
252 | "level" => $level, |
||
253 | "internal" => $this->isInternalRoute($filepath), |
||
254 | "tocable" => $this->allowInToc($filepath), |
||
255 | ]; |
||
256 | } |
||
257 | |||
258 | return $index; |
||
259 | } |
||
260 | |||
261 | |||
262 | |||
263 | /** |
||
264 | * Create the index of all meta content into an array. |
||
265 | * |
||
266 | * @return array as index. |
||
267 | */ |
||
268 | View Code Duplication | private function loadMetaIndex() |
|
269 | { |
||
270 | if ($this->meta) { |
||
271 | return $this->meta; |
||
272 | } |
||
273 | |||
274 | $key = $this->di->cache->createKey(__CLASS__, "meta"); |
||
275 | $this->meta = $this->di->cache->get($key); |
||
276 | |||
277 | if (!$this->meta || $this->ignoreCache) { |
||
278 | $this->meta = $this->createMetaIndex(); |
||
279 | $this->di->cache->put($key, $this->meta); |
||
280 | } |
||
281 | |||
282 | return $this->meta; |
||
283 | } |
||
284 | |||
285 | |||
286 | |||
287 | /** |
||
288 | * Generate an index for meta files. |
||
289 | * |
||
290 | * @return array as table of content. |
||
291 | */ |
||
292 | private function createMetaIndex() |
||
293 | { |
||
294 | $basepath = $this->config["basepath"]; |
||
295 | $filter = $this->config["metafilter"]; |
||
296 | $meta = $this->config["meta"]; |
||
297 | $path = "$basepath/$meta"; |
||
298 | |||
299 | $meta = []; |
||
300 | foreach (glob_recursive($path) as $file) { |
||
301 | $filepath = substr($file, strlen($basepath) + 1); |
||
302 | |||
303 | $src = file_get_contents($file); |
||
304 | $filtered = $this->di->get("textFilter")->parse($src, $filter); |
||
305 | |||
306 | $key = dirname($filepath); |
||
307 | $meta[$key] = $filtered->frontmatter; |
||
308 | |||
309 | // Add Toc to the data array |
||
310 | $meta[$key]["__toc__"] = $this->createBaseRouteToc(dirname($filepath)); |
||
311 | } |
||
312 | |||
313 | return $meta; |
||
314 | } |
||
315 | |||
316 | |||
317 | |||
318 | /** |
||
319 | * Get a reference to meta data for specific route. |
||
320 | * |
||
321 | * @param string $route current route used to access page. |
||
322 | * |
||
323 | * @return array as table of content. |
||
324 | */ |
||
325 | private function getMetaForRoute($route) |
||
326 | { |
||
327 | $base = dirname($route); |
||
328 | return isset($this->meta[$base]) |
||
329 | ? $this->meta[$base] |
||
330 | : []; |
||
331 | } |
||
332 | |||
333 | |||
334 | |||
335 | /** |
||
336 | * Get the frontmatter of a document. |
||
337 | * |
||
338 | * @param string $file to get frontmatter from. |
||
339 | * |
||
340 | * @return array as frontmatter. |
||
341 | */ |
||
342 | private function getFrontmatter($file) |
||
343 | { |
||
344 | $basepath = $this->config["basepath"]; |
||
345 | $filter = $this->config["textfilter"]; |
||
346 | |||
347 | $path = $basepath . "/" . $file; |
||
348 | $src = file_get_contents($path); |
||
349 | $filtered = $this->di->textFilter->parse($src, $filter); |
||
350 | return $filtered->frontmatter; |
||
351 | } |
||
352 | |||
353 | |||
354 | |||
355 | /** |
||
356 | * Get the title of a document to use for breadcrumb. |
||
357 | * |
||
358 | * @param string $file to get title from. |
||
359 | * |
||
360 | * @return string as the breadcrumb title. |
||
361 | */ |
||
362 | private function getBreadcrumbTitle($file) |
||
363 | { |
||
364 | $frontmatter = $this->getFrontmatter($file); |
||
365 | |||
366 | $title = $frontmatter["title"]; |
||
367 | if (isset($frontmatter["titleBreadcrumb"])) { |
||
368 | $title = $frontmatter["titleBreadcrumb"]; |
||
369 | } |
||
370 | |||
371 | return $title; |
||
372 | } |
||
373 | |||
374 | |||
375 | |||
376 | /** |
||
377 | * Create a table of content for routes at particular level. |
||
378 | * |
||
379 | * @param string $route base route to use. |
||
380 | * |
||
381 | * @return array as the toc. |
||
382 | */ |
||
383 | private function createBaseRouteToc($route) |
||
384 | { |
||
385 | $toc = []; |
||
386 | $len = strlen($route); |
||
387 | |||
388 | foreach ($this->index as $key => $value) { |
||
389 | if (substr($key, 0, $len) === $route) { |
||
390 | if ($value["internal"] === false |
||
391 | && $value["tocable"] === true) { |
||
392 | $toc[$key] = $value; |
||
393 | |||
394 | $frontm = $this->getFrontmatter($value["file"]); |
||
395 | $toc[$key]["title"] = $frontm["title"]; |
||
396 | $toc[$key]["sectionHeader"] = isset($frontm["sectionHeader"]) ? $frontm["sectionHeader"] : null; |
||
397 | $toc[$key]["linkable"] = isset($frontm["linkable"]) ? $frontm["linkable"] : null; |
||
398 | } |
||
399 | } |
||
400 | }; |
||
401 | |||
402 | return $toc; |
||
403 | } |
||
404 | |||
405 | |||
406 | |||
407 | /** |
||
408 | * Map the route to the correct key in the index. |
||
409 | * |
||
410 | * @param string $route current route used to access page. |
||
411 | * |
||
412 | * @return string as key or false if no match. |
||
413 | */ |
||
414 | private function mapRoute2IndexKey($route) |
||
415 | { |
||
416 | $route = rtrim($route, "/"); |
||
417 | |||
418 | if (key_exists($route, $this->index)) { |
||
419 | return $route; |
||
420 | } elseif (empty($route) && key_exists("index", $this->index)) { |
||
421 | return "index"; |
||
422 | } elseif (key_exists($route . "/index", $this->index)) { |
||
423 | return "$route/index"; |
||
424 | } |
||
425 | |||
426 | return false; |
||
427 | } |
||
428 | |||
429 | |||
430 | |||
431 | /** |
||
432 | * Map the route to the correct entry in the index. |
||
433 | * |
||
434 | * @param string $route current route used to access page. |
||
435 | * |
||
436 | * @return array as the matched route. |
||
437 | */ |
||
438 | private function mapRoute2Index($route) |
||
439 | { |
||
440 | $routeIndex = $this->mapRoute2IndexKey($route); |
||
441 | |||
442 | if ($routeIndex) { |
||
443 | return [$routeIndex, $this->index[$routeIndex]]; |
||
444 | } |
||
445 | |||
446 | throw new \Anax\Exception\NotFoundException(t("The route '!ROUTE' does not exists in the index.", ["!ROUTE" => $route])); |
||
447 | } |
||
448 | |||
449 | |||
450 | |||
451 | /** |
||
452 | * Get view by mergin information from meta and frontmatter. |
||
453 | * |
||
454 | * @param string $route current route used to access page. |
||
455 | * @param array $frontmatter for the content. |
||
456 | * @param string $key for the view to retrive. |
||
457 | * @param string $distinct how to merge the array. |
||
458 | * |
||
459 | * @return array with data to add as view. |
||
460 | */ |
||
461 | private function getView($route, $frontmatter, $key, $distinct = true) |
||
482 | |||
483 | |||
484 | |||
485 | /** |
||
486 | * Get details on extra views. |
||
487 | * |
||
488 | * @param string $route current route used to access page. |
||
489 | * @param array $frontmatter for the content. |
||
490 | * |
||
491 | * @return array with page data to send to view. |
||
492 | */ |
||
493 | private function getViews($route, $frontmatter) |
||
494 | { |
||
495 | // Arrange data into views |
||
496 | $views = $this->getView($route, $frontmatter, "views", false); |
||
497 | |||
498 | // Set defaults |
||
519 | |||
520 | |||
521 | |||
522 | /** |
||
523 | * Load extra info into views based of meta information provided in each |
||
524 | * view. |
||
525 | * |
||
526 | * @param string $view with current settings. |
||
527 | * @param string $route to load view from. |
||
528 | * @param string $baseurl to prepend relative urls. |
||
529 | * |
||
530 | * @return array with view details. |
||
531 | */ |
||
532 | private function getAdditionalViewDataForRoute($view, $route, $baseurl) |
||
554 | |||
555 | |||
556 | |||
557 | /** |
||
558 | * Load extra info into views based of meta information provided in each |
||
559 | * view. |
||
560 | * |
||
561 | * @param string $view with current settings. |
||
562 | * @param string $route to load view from. |
||
563 | * @param string $baseurl to prepend relative urls. |
||
564 | * |
||
565 | * @return array with view data details. |
||
566 | */ |
||
567 | private function getViewDataForRoute($route, $baseurl) |
||
580 | |||
581 | |||
582 | |||
583 | /** |
||
584 | * Order and limit toc items. |
||
585 | * |
||
586 | * @param string &$toc array with current toc. |
||
587 | * @param string &$meta on how to order and limit toc. |
||
588 | * |
||
589 | * @return void. |
||
590 | */ |
||
591 | private function orderAndlimitToc(&$toc, &$meta) |
||
620 | |||
621 | |||
622 | |||
623 | /** |
||
624 | * Find next and previous links of current content. |
||
625 | * |
||
626 | * @param string $routeIndex target route to find next and previous for. |
||
627 | * |
||
628 | * @return array with next and previous if found. |
||
629 | */ |
||
630 | private function findNextAndPrevious($routeIndex) |
||
674 | |||
675 | |||
676 | |||
677 | /** |
||
678 | * Load extra info into views based of meta information provided in each |
||
679 | * view. |
||
680 | * |
||
681 | * @param array &$views with all views. |
||
682 | * @param string $route current route |
||
683 | * @param string $routeIndex route with appended /index |
||
684 | * |
||
685 | * @throws NotFoundException when mapping can not be done. |
||
686 | * |
||
687 | * @return void. |
||
688 | */ |
||
689 | private function loadAdditionalContent(&$views, $route, $routeIndex) |
||
744 | |||
745 | |||
746 | |||
747 | /** |
||
748 | * Get basurl from view, if it is defined. |
||
749 | * |
||
750 | * @param array $views data for all views. |
||
751 | * @param string $current for current view if any. |
||
752 | * |
||
753 | * @return string | null as baseurl. |
||
754 | */ |
||
755 | private function getBaseurl($views, $current = null) |
||
769 | |||
770 | |||
771 | |||
772 | /** |
||
773 | * Parse text, find and update all a href to use baseurl. |
||
774 | * |
||
775 | * @param object &$filtered with text and excerpt to process. |
||
776 | * @param string $baseurl add as baseurl for all relative urls. |
||
777 | * |
||
778 | * @return void. |
||
779 | */ |
||
780 | private function addBaseurl2AnchorUrls(&$filtered, $baseurl) |
||
793 | |||
794 | |||
795 | |||
796 | /** |
||
797 | * Load content file and frontmatter, this is the first time we process |
||
798 | * the content. |
||
799 | * |
||
800 | * @param string $key array with all views. |
||
801 | * @param string $content array with all views. |
||
802 | * |
||
803 | * @throws NotFoundException when mapping can not be done. |
||
804 | * |
||
805 | * @return void. |
||
806 | */ |
||
807 | private function loadFileContent($key, $content) |
||
829 | |||
830 | |||
831 | |||
832 | /** |
||
833 | * Look up the route in the index and use that to retrieve the filtered |
||
834 | * content. |
||
835 | * |
||
836 | * @param string $route to look up. |
||
837 | * |
||
838 | * @return array with content and filtered version. |
||
839 | */ |
||
840 | public function mapRoute2Content($route) |
||
848 | |||
849 | |||
850 | |||
851 | /** |
||
852 | * Look up the route in the index and use that to retrieve the filtered |
||
853 | * content. |
||
854 | * |
||
855 | * @param array $content to process. |
||
856 | * @param object &$filtered to use for settings. |
||
857 | * |
||
858 | * @return array with content and filtered version. |
||
859 | */ |
||
860 | public function processContentPhaseTwo($content, &$filtered) |
||
899 | |||
900 | |||
901 | |||
902 | /** |
||
903 | * Map url to content if such mapping can be done, exclude internal routes. |
||
904 | * |
||
905 | * @param string $route optional route to look up. |
||
906 | * |
||
907 | * @return object with content and filtered version. |
||
908 | */ |
||
909 | public function contentForRoute($route = null) |
||
919 | |||
920 | |||
921 | |||
922 | /** |
||
923 | * Map url to content, even internal content, if such mapping can be done. |
||
924 | * |
||
925 | * @param string $route route to look up. |
||
926 | * |
||
927 | * @return object with content and filtered version. |
||
928 | */ |
||
929 | public function createContentForInternalRoute($route) |
||
956 | |||
957 | |||
958 | |||
959 | /** |
||
960 | * Map url to content, even internal content, if such mapping can be done. |
||
961 | * |
||
962 | * @param string $route optional route to look up. |
||
963 | * |
||
964 | * @return object with content and filtered version. |
||
965 | */ |
||
966 | public function contentForInternalRoute($route = null) |
||
985 | } |
||
986 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.