Complex classes like CatalogueManager 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 CatalogueManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class CatalogueManager |
||
23 | { |
||
24 | /** |
||
25 | * @var MessageCatalogue[] |
||
26 | */ |
||
27 | private $catalogues; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $projectRoot; |
||
33 | |||
34 | /** |
||
35 | * @param string $projectRoot |
||
36 | */ |
||
37 | public function __construct($projectRoot) |
||
41 | |||
42 | /** |
||
43 | * @param MessageCatalogue[] $catalogues |
||
44 | */ |
||
45 | public function load(array $catalogues) |
||
52 | |||
53 | /** |
||
54 | * @return array |
||
55 | */ |
||
56 | public function getDomains() |
||
63 | |||
64 | /** |
||
65 | * @param string $locale |
||
66 | * @param string $domain |
||
67 | * |
||
68 | * @return CatalogueMessage[] |
||
69 | */ |
||
70 | public function getMessages($locale, $domain) |
||
83 | |||
84 | /** |
||
85 | * @param array $config { |
||
86 | * |
||
87 | * @var string $domain |
||
88 | * @var string $locale |
||
89 | * @var bool $isNew |
||
90 | * @var bool $isObsolete |
||
91 | * } |
||
92 | * |
||
93 | * @return CatalogueMessage[] |
||
94 | */ |
||
95 | public function findMessages(array $config = []) |
||
142 | |||
143 | /** |
||
144 | * @param string $domain |
||
145 | * @param string $key |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | public function getTranslations($domain, $key) |
||
160 | |||
161 | /** |
||
162 | * @param string $domain |
||
163 | * @param string $key |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | public function getSourceLocations($domain, $key) |
||
180 | |||
181 | /** |
||
182 | * @param string $domain |
||
183 | * @param string $key |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function isNew($domain, $key) |
||
193 | |||
194 | /** |
||
195 | * @param string $domain |
||
196 | * @param string $key |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function isObsolete($domain, $key) |
||
206 | |||
207 | /** |
||
208 | * @param $domain |
||
209 | * @param $key |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | private function getNotes($domain, $key, MessageCatalogue $catalogue = null) |
||
227 | |||
228 | /** |
||
229 | * @param array $notes |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | private function hasNoteObsolete(array $notes) |
||
243 | |||
244 | /** |
||
245 | * @param array $notes |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | private function hasNoteNew(array $notes) |
||
259 | } |
||
260 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: