Complex classes like Translation 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 Translation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Translation implements CreatableFromArray |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $id = ''; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $type = ''; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | private $translated = false; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $flagged = false; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $status = ''; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $translation = ''; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | private $revision = 0; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | private $comments = 0; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $modified = ''; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private $author = []; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private $flagger = []; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private $locale = []; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | private $plurals = []; |
||
76 | |||
77 | private function __construct() |
||
80 | |||
81 | /** |
||
82 | * @param array $data |
||
83 | * |
||
84 | * @return Translation |
||
85 | */ |
||
86 | public static function createFromArray(array $data) |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getId(): string |
||
140 | |||
141 | /** |
||
142 | * @param string $id |
||
143 | */ |
||
144 | private function setId($id) |
||
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getType(): string |
||
156 | |||
157 | /** |
||
158 | * @param string $type |
||
159 | */ |
||
160 | private function setType($type) |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getTranslated(): bool |
||
172 | |||
173 | /** |
||
174 | * @param bool $translated |
||
175 | */ |
||
176 | private function setTranslated($translated) |
||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getFlagged(): bool |
||
188 | |||
189 | /** |
||
190 | * @param bool $flagged |
||
191 | */ |
||
192 | private function setFlagged($flagged) |
||
196 | |||
197 | /** |
||
198 | * @return string |
||
199 | */ |
||
200 | public function getStatus(): string |
||
204 | |||
205 | /** |
||
206 | * @param string $status |
||
207 | */ |
||
208 | private function setStatus($status) |
||
212 | |||
213 | /** |
||
214 | * @return string |
||
215 | */ |
||
216 | public function getTranslation(): string |
||
220 | |||
221 | /** |
||
222 | * @param string $translation |
||
223 | */ |
||
224 | private function setTranslation($translation) |
||
228 | |||
229 | /** |
||
230 | * @return string |
||
231 | */ |
||
232 | public function getRevision(): int |
||
236 | |||
237 | /** |
||
238 | * @param int $revision |
||
239 | */ |
||
240 | private function setRevision(int $revision) |
||
244 | |||
245 | /** |
||
246 | * @return int |
||
247 | */ |
||
248 | public function getComments(): int |
||
252 | |||
253 | /** |
||
254 | * @param int $comments |
||
255 | */ |
||
256 | private function setComments(int $comments) |
||
260 | |||
261 | /** |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getModified(): string |
||
268 | |||
269 | /** |
||
270 | * @param string $modified |
||
271 | */ |
||
272 | private function setModified($modified) |
||
276 | |||
277 | /** |
||
278 | * @return array |
||
279 | */ |
||
280 | public function getAuthor(): array |
||
284 | |||
285 | /** |
||
286 | * @param array $author |
||
287 | */ |
||
288 | private function setAuthor($author) |
||
292 | |||
293 | /** |
||
294 | * @return array |
||
295 | */ |
||
296 | public function getFlagger(): array |
||
300 | |||
301 | /** |
||
302 | * @param array $flagger |
||
303 | */ |
||
304 | private function setFlagger($flagger) |
||
308 | |||
309 | /** |
||
310 | * @return array |
||
311 | */ |
||
312 | public function getLocale(): array |
||
316 | |||
317 | /** |
||
318 | * @param array $locale |
||
319 | */ |
||
320 | private function setLocale($locale) |
||
324 | |||
325 | /** |
||
326 | * @return array |
||
327 | */ |
||
328 | public function getPlurals(): array |
||
332 | |||
333 | /** |
||
334 | * @param array $plurals |
||
335 | */ |
||
336 | private function setPlurals($plurals) |
||
340 | } |
||
341 |