Complex classes like TranslationParserScript 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 TranslationParserScript, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class TranslationParserScript extends AdminScript |
||
19 | { |
||
20 | use TranslatorAwareTrait; |
||
21 | |||
22 | /** |
||
23 | * App configurations from dependencies |
||
24 | * @var AppConfig |
||
25 | */ |
||
26 | private $appConfig; |
||
27 | |||
28 | /** |
||
29 | * @var string $fileType |
||
30 | */ |
||
31 | protected $fileType; |
||
32 | |||
33 | /** |
||
34 | * Output file |
||
35 | * @var string $output |
||
36 | */ |
||
37 | protected $output; |
||
38 | |||
39 | /** |
||
40 | * @var array $paths |
||
41 | */ |
||
42 | protected $paths; |
||
43 | |||
44 | /** |
||
45 | * @var string $path |
||
46 | */ |
||
47 | protected $path; |
||
48 | |||
49 | /** |
||
50 | * Path where the CSV file will be |
||
51 | * Full path with base path. |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $filePath; |
||
55 | |||
56 | /** |
||
57 | * @var array $locales |
||
58 | */ |
||
59 | protected $locales; |
||
60 | |||
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | */ |
||
65 | public function setDependencies(Container $container) |
||
71 | |||
72 | /** |
||
73 | * Arguments that can be use in the script. |
||
74 | * If no path is provided, all views path are parsed. |
||
75 | * If you do precise a path, notice that you will loose |
||
76 | * all modified translations that comes from other paths. |
||
77 | * |
||
78 | * Valid arguments: |
||
79 | * - output : path-to-csv/ |
||
80 | * - domain : filename prefix |
||
81 | * - recursive : level of recursiveness (how deep the glob checks for the strings) |
||
82 | * - path : Path to get translation from a precise location (i.e: templates/emails/) |
||
83 | * - type : file type (either mustache or php) |
||
84 | * |
||
85 | * @todo Support php file type. |
||
86 | * @return array |
||
87 | */ |
||
88 | public function defaultArguments() |
||
126 | |||
127 | /** |
||
128 | * @param RequestInterface $request A PSR-7 compatible Request instance. |
||
129 | * @param ResponseInterface $response A PSR-7 compatible Response instance. |
||
130 | * @return ResponseInterface |
||
131 | */ |
||
132 | public function run(RequestInterface $request, ResponseInterface $response) |
||
158 | |||
159 | /** |
||
160 | * Give feedback about what's going on. |
||
161 | * @return self Chainable. |
||
162 | */ |
||
163 | protected function displayInformations() |
||
188 | |||
189 | /** |
||
190 | * Complete filepath to the CSV location. |
||
191 | * @return string Filepath to the csv. |
||
192 | */ |
||
193 | protected function filePath() |
||
204 | |||
205 | /** |
||
206 | * Available locales (languages) |
||
207 | * @return array Locales. |
||
208 | */ |
||
209 | protected function locales() |
||
213 | |||
214 | /** |
||
215 | * @return string Current locale. |
||
216 | */ |
||
217 | protected function locale() |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function output() |
||
234 | |||
235 | /** |
||
236 | * Domain which is the csv file name prefix |
||
237 | * @return string domain. |
||
238 | */ |
||
239 | public function domain() |
||
243 | |||
244 | /** |
||
245 | * Regex to match in files. |
||
246 | * @param string $type File type (mustache|php) |
||
247 | * @return string Regex string. |
||
248 | */ |
||
249 | public function regEx($type) |
||
267 | |||
268 | /** |
||
269 | * Loop through all paths to get translations. |
||
270 | * Also merge with already existing translations. |
||
271 | * Translations associated with the locale. |
||
272 | * [ |
||
273 | * 'fr' => [ |
||
274 | * 'string' => 'translation', |
||
275 | * 'string' => 'translation', |
||
276 | * 'string' => 'translation', |
||
277 | * 'string' => 'translation' |
||
278 | * ], |
||
279 | * 'en' => [ |
||
280 | * 'string' => 'translation', |
||
281 | * 'string' => 'translation', |
||
282 | * 'string' => 'translation', |
||
283 | * 'string' => 'translation' |
||
284 | * ] |
||
285 | * ] |
||
286 | * @return array Translations. |
||
287 | */ |
||
288 | public function getTranslations() |
||
307 | |||
308 | /** |
||
309 | * Get all translations in given path for the given file extension (mustache | php). |
||
310 | * @param string $path The path. |
||
311 | * @param string $fileType The file extension|type. |
||
312 | * @return array Translations. |
||
313 | */ |
||
314 | public function getTranslationsFromPath($path, $fileType) |
||
357 | |||
358 | /** |
||
359 | * @param string $pattern The pattern to search. |
||
360 | * @param integer $flags The glob flags. |
||
361 | * @return array |
||
362 | * @see http://in.php.net/manual/en/function.glob.php#106595 |
||
363 | */ |
||
364 | public function globRecursive($pattern, $flags = 0) |
||
378 | |||
379 | /** |
||
380 | * Custom path |
||
381 | * @return [type] [description] |
||
382 | */ |
||
383 | public function path() |
||
390 | |||
391 | /** |
||
392 | * @return string |
||
393 | */ |
||
394 | public function paths() |
||
401 | |||
402 | /** |
||
403 | * @return string |
||
404 | */ |
||
405 | public function fileTypes() |
||
415 | |||
416 | /** |
||
417 | * @param array $translations The translations to save in CSV. |
||
418 | * @return TranslateScript Chainable |
||
419 | */ |
||
420 | public function toCSV(array $translations) |
||
459 | |||
460 | /** |
||
461 | * @return string |
||
462 | */ |
||
463 | public function enclosure() |
||
467 | |||
468 | /** |
||
469 | * @return string |
||
470 | */ |
||
471 | public function separator() |
||
475 | |||
476 | /** |
||
477 | * @return integer |
||
478 | */ |
||
479 | public function maxRecursiveLevel() |
||
486 | } |
||
487 |
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.