Complex classes like Taxonomy 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 Taxonomy, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Rocket\Taxonomy; |
||
15 | class Taxonomy implements TaxonomyInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var array Terms internal cache |
||
19 | */ |
||
20 | public $terms = []; |
||
21 | |||
22 | /** |
||
23 | * @var array List of vocabularies by ID |
||
24 | */ |
||
25 | protected $vocabularyById = []; |
||
26 | |||
27 | /** |
||
28 | * @var array List of vocabularies by Name |
||
29 | */ |
||
30 | protected $vocabularyByName = []; |
||
31 | |||
32 | /** |
||
33 | * @var CacheRepository The repository to cache Terms |
||
34 | */ |
||
35 | protected $cache; |
||
36 | |||
37 | /** |
||
38 | * @var TermRep The repository to load Terms |
||
39 | */ |
||
40 | protected $termRepository; |
||
41 | |||
42 | /** |
||
43 | * @var TermHieraRep The repository to handle terms hierarchies |
||
44 | */ |
||
45 | protected $termHierarchyRepository; |
||
46 | |||
47 | /** |
||
48 | * Initialize the Taxonomy system, Loads all existing vocabularies |
||
49 | * |
||
50 | * @param CacheRepository $cache The cache repository |
||
51 | * @param TermRep $termRepository The term retrieval repository |
||
52 | * @param TermHieraRep $termHierarchyRepository The term hierarchy repository |
||
53 | */ |
||
54 | public function __construct(CacheRepository $cache, TermRep $termRepository, TermHieraRep $termHierarchyRepository) |
||
70 | |||
71 | 192 | /** |
|
72 | 186 | * Is this vocabulary translatable ? |
|
73 | 192 | * |
|
74 | 192 | * @param int|string $vid |
|
75 | * @return bool |
||
76 | */ |
||
77 | public function isTranslatable($vid) |
||
85 | 3 | ||
86 | 3 | /** |
|
87 | * Get the internal language for the vocabulary |
||
88 | 180 | * |
|
89 | * This will return the language_id if the vocabulary is translated or 1 if it's not |
||
90 | * |
||
91 | * @param int|string $vocabulary_id |
||
92 | * @param int $language_id |
||
93 | * @return int|null |
||
94 | */ |
||
95 | public function getLanguage($vocabulary_id, $language_id = null) |
||
107 | 150 | ||
108 | /** |
||
109 | * Get a vocabulary by name or ID |
||
110 | 150 | * |
|
111 | * Taxonomy::vocabulary(1); |
||
112 | * returns 'tags' |
||
113 | * |
||
114 | * Taxonomy::vocabulary('tags'); |
||
115 | * returns 1 |
||
116 | * |
||
117 | * @param int|string $key |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function vocabulary($key) |
||
128 | 3 | ||
129 | /** |
||
130 | * Get all vocabularies with keys as id's |
||
131 | 186 | * |
|
132 | * @return array |
||
133 | */ |
||
134 | public function vocabularies() |
||
138 | |||
139 | 6 | /** |
|
140 | * Get a term with all translations |
||
141 | 6 | * |
|
142 | * @param int $term_id The term's ID |
||
143 | * @param bool $from_cache Should we take this term from cache or request a fresh one ? |
||
144 | * @return Term |
||
145 | */ |
||
146 | public function getTerm($term_id, $from_cache = true) |
||
157 | 39 | ||
158 | 39 | /** |
|
159 | * Remove a term from the cache |
||
160 | 39 | * |
|
161 | * @param int $term_id |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function uncacheTerm($term_id) |
||
172 | 3 | ||
173 | 3 | /** |
|
174 | * Get all paths for a term |
||
175 | 174 | * |
|
176 | * @param int $term_id |
||
177 | * @return array<array<int>> |
||
178 | */ |
||
179 | public function getAncestryPaths($term_id) |
||
183 | |||
184 | 12 | /** |
|
185 | * Get all paths for a term |
||
186 | 12 | * |
|
187 | * @param int $term_id |
||
188 | * @return array<array<int>> |
||
189 | */ |
||
190 | public function getDescentPaths($term_id) |
||
194 | |||
195 | 12 | /** |
|
196 | * Get the complete graph |
||
197 | 12 | * @param int $term_id |
|
198 | * @return array |
||
199 | */ |
||
200 | public function getAncestryGraph($term_id) |
||
204 | |||
205 | 3 | /** |
|
206 | * Get the complete graph |
||
207 | 3 | * @param int $term_id |
|
208 | * @return array |
||
209 | */ |
||
210 | public function getDescentGraph($term_id) |
||
214 | |||
215 | 3 | /** |
|
216 | * Add one parent to a term |
||
217 | 3 | * |
|
218 | * @param int $term_id |
||
219 | * @param int $parent_id |
||
220 | */ |
||
221 | public function addParent($term_id, $parent_id) |
||
227 | |||
228 | 111 | /** |
|
229 | * Add a list of parents to a term |
||
230 | 108 | * |
|
231 | 108 | * @param int $term_id |
|
232 | * @param array<integer> $parent_ids |
||
233 | */ |
||
234 | public function addParents($term_id, array $parent_ids) |
||
242 | |||
243 | 6 | /** |
|
244 | 6 | * Test if the term can have more parents |
|
245 | 6 | * |
|
246 | 6 | * @param int $term_id |
|
247 | * @param int $count |
||
248 | * @throws \RuntimeException |
||
249 | */ |
||
250 | protected function testCanAddParents($term_id, $count) |
||
268 | 111 | ||
269 | 111 | /** |
|
270 | 3 | * Remove the parents form this term |
|
271 | * |
||
272 | 111 | * @param int $term_id |
|
273 | * @return bool |
||
274 | */ |
||
275 | public function unsetParents($term_id) |
||
279 | |||
280 | 6 | /** |
|
281 | * Get all the terms of a vocabulary |
||
282 | 6 | * |
|
283 | * @param int $vocabulary_id |
||
284 | * @return array |
||
285 | */ |
||
286 | public function getTermsForVocabulary($vocabulary_id) |
||
305 | |||
306 | 3 | /** |
|
307 | * Search a specific term, if it doesn't exist, returns false |
||
308 | 3 | * |
|
309 | * @param string $term |
||
310 | * @param int $vocabulary_id |
||
311 | * @param int $language_id |
||
312 | * @param array $exclude |
||
313 | * @return int|null |
||
314 | */ |
||
315 | public function searchTerm($term, $vocabulary_id, $language_id = null, $exclude = []) |
||
336 | 3 | ||
337 | 3 | /** |
|
338 | * Returns the id of a term, if it doesn't exist, creates it. |
||
339 | 174 | * |
|
340 | * @param string $title |
||
341 | * @param int $vocabulary_id |
||
342 | * @param int $language_id |
||
343 | * @param int $type |
||
344 | * @return bool|int |
||
345 | */ |
||
346 | public function getTermId($title, $vocabulary_id, $language_id = null, $type = 0) |
||
381 | 174 | ||
382 | 174 | /** |
|
383 | * Adds one or more tags and returns an array of id's |
||
384 | 174 | * |
|
385 | * @param array $taxonomies |
||
386 | * @return array |
||
387 | */ |
||
388 | public function getTermIds($taxonomies) |
||
405 | } |
||
406 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: