Total Complexity | 40 |
Total Lines | 315 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 0 |
Complex classes like IndexManager 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.
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 IndexManager, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
7 | final class IndexManager extends AbstractIndexer |
||
8 | { |
||
9 | /** @var array<string, \stdClass> $catIndex */ |
||
10 | private array $catIndex; |
||
11 | |||
12 | /** @var array<string, \stdClass> $pageIndex */ |
||
13 | private array $pageIndex; |
||
14 | |||
15 | /** @var array<string, \stdClass> $postIndex */ |
||
16 | private array $postIndex; |
||
17 | |||
18 | /** @var array<mixed> $menuIndex */ |
||
19 | private array $menuIndex; |
||
20 | |||
21 | /** |
||
22 | * A tag is NOT represented by any file on the physical filessytem. |
||
23 | * @var array<string, \stdClass> $tagIndex |
||
24 | */ |
||
25 | private array $tagIndex; |
||
26 | |||
27 | /** |
||
28 | * The slug index holds references to files on the physical filesystem., |
||
29 | * and is a combination of the catIndex, postIndex and pageIndex |
||
30 | * @var array<string, \stdClass> $slugIndex |
||
31 | */ |
||
32 | private array $slugIndex; |
||
33 | |||
34 | /** @var array<string, \stdClass> $tag2postIndex */ |
||
35 | private array $tag2postIndex; |
||
36 | |||
37 | /** @var array<string, array> $cat2postIndex */ |
||
38 | private array $cat2postIndex; |
||
39 | |||
40 | /** |
||
41 | * IndexManager constructor. |
||
42 | * @param string $parentDir The parent directory for the content directory. |
||
43 | * @throws \InvalidArgumentException If the parent directory is invalid. |
||
44 | * @throws InternalException |
||
45 | */ |
||
46 | public function __construct(string $parentDir) { |
||
47 | parent::__construct($parentDir); |
||
48 | $this->initialize(); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Rebuild all the indexes. |
||
53 | */ |
||
54 | public function reindex(): void { |
||
55 | $this->catIndex = $this->buildCategoryIndex(); |
||
56 | $pageMenuIndex = $this->buildPageAndMenuIndexes(); |
||
57 | $this->pageIndex = $pageMenuIndex[0]; |
||
58 | $this->menuIndex = $pageMenuIndex[1]; |
||
59 | $this->postIndex = $this->buildPostIndex(); |
||
60 | $compositeIndex = $this->buildCompositeIndexes(); |
||
61 | $this->tagIndex = $compositeIndex[0]; |
||
62 | $this->tag2postIndex = $compositeIndex[1]; |
||
63 | $this->cat2postIndex = $compositeIndex[2]; |
||
64 | $this->slugIndex = \array_merge($this->postIndex, $this->catIndex, $this->pageIndex, $this->tagIndex); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Returns an object representing an element in the index. |
||
69 | * @param string $key |
||
70 | * @throws \InvalidArgumentException If the given $key does not exist in the index. |
||
71 | * @return \stdClass |
||
72 | */ |
||
73 | public function getElementFromSlugIndex(string $key): \stdClass { |
||
74 | if (!isset($this->slugIndex[$key])) { |
||
75 | throw new InvalidArgumentException("Key does not exist in the slugIndex! Use 'elementExists()' before calling this method."); |
||
76 | } |
||
77 | return $this->slugIndex[$key]; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Check if an key exists in the <b>slug index</b>. |
||
82 | * @param string $key |
||
83 | * @return bool <code>true</code> if exists, otherwise <code>false</code> |
||
84 | */ |
||
85 | public function elementExists(string $key): bool { |
||
86 | return isset($this->slugIndex[$key]); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Return the posts index. |
||
91 | * @return array<string, \stdClass> |
||
92 | */ |
||
93 | public function getPostsIndex(): array { |
||
94 | return $this->postIndex; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Return the category index. |
||
99 | * @return array<string, \stdClass> |
||
100 | */ |
||
101 | public function getCategoriesIndex(): array { |
||
102 | return $this->catIndex; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Return the tag index. |
||
107 | * @return array<string, \stdClass> |
||
108 | */ |
||
109 | public function getTagIndex(): array { |
||
110 | return $this->tagIndex; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Return the menus index. |
||
115 | * @return array<mixed> |
||
116 | */ |
||
117 | public function getMenusIndex(): array { |
||
118 | return $this->menuIndex; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Return the tag index. |
||
123 | * @return array<string, \stdClass> |
||
124 | */ |
||
125 | public function getPageIndex(): array { |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Return the posts for category index. |
||
131 | * @return array<string, array> |
||
132 | */ |
||
133 | public function getPostsForCategoryIndex(): array { |
||
134 | return $this->cat2postIndex; |
||
135 | } |
||
136 | |||
137 | private function initialize(): void { |
||
138 | if ((\is_dir($this->parentDir.DS.'cache'.DS.'indexes')) === false) { |
||
139 | $dir = $this->parentDir.DS.'cache'.DS.'indexes'; |
||
140 | if (\mkdir($dir, MODE, true) === false) { |
||
141 | throw new InternalException("Unable to create cache/indexes directory [$dir]"); // @codeCoverageIgnore |
||
142 | } |
||
143 | $this->reindex(); |
||
144 | } else { |
||
145 | $this->catIndex = $this->loadIndex($this->catInxFile); |
||
146 | $this->pageIndex = $this->loadIndex($this->pageInxFile); |
||
147 | $this->postIndex = $this->loadIndex($this->postInxFile); |
||
148 | $this->tagIndex = $this->loadIndex($this->tagInxFile); |
||
149 | $this->cat2postIndex = $this->loadIndex($this->cat2postInxFile); |
||
150 | $this->tag2postIndex = $this->loadIndex($this->tag2postInxFile); |
||
151 | $this->menuIndex = $this->loadIndex($this->menuInxFile); |
||
152 | $this->slugIndex = \array_merge($this->postIndex, $this->catIndex, $this->pageIndex, $this->tagIndex); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return array<string, \stdClass> |
||
158 | */ |
||
159 | private function buildCategoryIndex(): array { |
||
160 | $index = []; |
||
161 | foreach ($this->parseDirectory($this->commonDir.DS.CATEGORY_SECTION.DS.'*'.CONTENT_FILE_EXT) as $filepath) { |
||
162 | $root = \substr($filepath, \strlen($this->commonDir) + 1); |
||
163 | $key = \str_replace(DS, FWD_SLASH, \substr($root, 0, \strlen($root) - CONTENT_FILE_EXT_LEN)); |
||
164 | if (CATEGORY_SECTION.FWD_SLASH.'index' === $key) { |
||
165 | continue; // 'index.json' file is the landing page |
||
166 | } |
||
167 | $index[$key] = $this->createElement($key, $filepath, CATEGORY_SECTION); |
||
168 | } |
||
169 | $this->writeIndex($this->catInxFile, $index); |
||
170 | return $index; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Builds two indexes: menu and post indexes. |
||
175 | * @return array<mixed> |
||
176 | */ |
||
177 | private function buildPageAndMenuIndexes(): array { |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Builds the post index. |
||
221 | * @return array<string, \stdClass> |
||
222 | */ |
||
223 | private function buildPostIndex(): array { |
||
224 | $index = []; |
||
225 | foreach ($this->parseDirectory($this->userDataDir.DS.'*'.DS.POST_SECTION.DS.'*'.DS.'*'.DS.'*'.CONTENT_FILE_EXT) as $filepath) { |
||
226 | $key = \pathinfo($filepath, PATHINFO_FILENAME); |
||
227 | $element = $this->createElement(/** @scrutinizer ignore-type */ $key, $filepath, POST_SECTION); |
||
228 | $index[(string)$element->key] = $element; |
||
229 | } |
||
230 | $this->writeIndex($this->postInxFile, $index); |
||
231 | return $index; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Reads the given file and creates an array of menus in which this |
||
236 | * resource is listed. |
||
237 | * @return array<mixed> |
||
238 | */ |
||
239 | private function buildMenus(string $key, string $filepath): array { |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Does a <code>usort</code> on the <code>weight</code> property. |
||
263 | * @param array<mixed> $index the unsorted array |
||
264 | * @return array<mixed> the sorted array |
||
265 | */ |
||
266 | private function orderMenuEntries(array $index): array { |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Builds three indexes: 'category 2 posts', 'tag 2 posts' and tag index. |
||
281 | * @throws InternalException |
||
282 | * @return array<mixed> |
||
283 | */ |
||
284 | private function buildCompositeIndexes(): array { |
||
285 | $tagInx = []; |
||
286 | $tag2PostsIndex = []; |
||
287 | $cat2PostIndex = []; |
||
288 | foreach ($this->postIndex as $post) { |
||
289 | if (!isset($post->key, $post->tags, $post->category)) { |
||
290 | throw new InternalException("Invalid format of index element: "./** @scrutinizer ignore-type */print_r($post, true)); // @codeCoverageIgnore |
||
291 | } |
||
292 | foreach ($post->tags as $tag) { |
||
293 | $key = TAG_SECTION.FWD_SLASH.(string)$tag; |
||
294 | $tagInx[$key] = $this->createElementClass($key, EMPTY_VALUE, TAG_SECTION); |
||
295 | $tag2PostsIndex[$key][] = $post->key; |
||
296 | } |
||
297 | $cat2PostIndex[$post->category][] = $post->key; |
||
298 | } |
||
299 | $this->writeIndex($this->tagInxFile, $tagInx); |
||
300 | $this->writeIndex($this->tag2postInxFile, $tag2PostsIndex); |
||
301 | $this->writeIndex($this->cat2postInxFile, $cat2PostIndex); |
||
302 | return [$tagInx, $tag2PostsIndex, $cat2PostIndex]; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Merge the given menu array into the master menu index returning the new |
||
307 | * master menu index. |
||
308 | * @param array<mixed> $initial |
||
309 | * @param array<mixed> $toMerge The menu array to be merged. |
||
310 | * @return array<mixed> |
||
311 | */ |
||
312 | private function mergeToMenuIndex(array $initial, array $toMerge): array { |
||
322 | } |
||
323 | } |
||
324 |