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() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param RequestInterface $request A PSR-7 compatible Request instance. |
||
| 139 | * @param ResponseInterface $response A PSR-7 compatible Response instance. |
||
| 140 | * @return ResponseInterface |
||
| 141 | */ |
||
| 142 | public function run(RequestInterface $request, ResponseInterface $response) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Give feedback about what's going on. |
||
| 171 | * @return self Chainable. |
||
| 172 | */ |
||
| 173 | protected function displayInformations() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Complete filepath to the CSV location. |
||
| 200 | * @return string Filepath to the csv. |
||
| 201 | */ |
||
| 202 | protected function filePath() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Available locales (languages) |
||
| 216 | * @return array Locales. |
||
| 217 | */ |
||
| 218 | protected function locales() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return string Current locale. |
||
| 225 | */ |
||
| 226 | protected function locale() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function output() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Domain which is the csv file name prefix |
||
| 246 | * @return string domain. |
||
| 247 | */ |
||
| 248 | public function domain() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Regex to match in files. |
||
| 255 | * @param string $type File type (mustache|php) |
||
| 256 | * @return string Regex string. |
||
| 257 | */ |
||
| 258 | public function regEx($type) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Loop through all paths to get translations. |
||
| 281 | * Also merge with already existing translations. |
||
| 282 | * Translations associated with the locale. |
||
| 283 | * [ |
||
| 284 | * 'fr' => [ |
||
| 285 | * 'string' => 'translation', |
||
| 286 | * 'string' => 'translation', |
||
| 287 | * 'string' => 'translation', |
||
| 288 | * 'string' => 'translation' |
||
| 289 | * ], |
||
| 290 | * 'en' => [ |
||
| 291 | * 'string' => 'translation', |
||
| 292 | * 'string' => 'translation', |
||
| 293 | * 'string' => 'translation', |
||
| 294 | * 'string' => 'translation' |
||
| 295 | * ] |
||
| 296 | * ] |
||
| 297 | * @return array Translations. |
||
| 298 | */ |
||
| 299 | public function getTranslations() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get all translations in given path for the given file extension (mustache | php). |
||
| 324 | * @param string $path The path. |
||
| 325 | * @param string $fileType The file extension|type. |
||
| 326 | * @return array Translations. |
||
| 327 | */ |
||
| 328 | public function getTranslationsFromPath($path, $fileType) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $pattern The pattern to search. |
||
| 380 | * @param integer $flags The glob flags. |
||
| 381 | * @return array |
||
| 382 | * @see http://in.php.net/manual/en/function.glob.php#106595 |
||
| 383 | */ |
||
| 384 | public function globRecursive($pattern, $flags = 0) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Custom path |
||
| 401 | * @return [type] [description] |
||
| 402 | */ |
||
| 403 | public function path() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function paths() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function fileTypes() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param array $translations The translations to save in CSV. |
||
| 442 | * @return TranslateScript Chainable |
||
| 443 | */ |
||
| 444 | public function toCSV(array $translations) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public function enclosure() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | public function separator() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @return integer |
||
| 502 | */ |
||
| 503 | public function maxRecursiveLevel() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @return string Php function |
||
| 513 | */ |
||
| 514 | private function phpFunction() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return string Mustache tag |
||
| 525 | */ |
||
| 526 | private function mustacheTag() |
||
| 533 | } |
||
| 534 |
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.