Complex classes like Extractor 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 Extractor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Extractor |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var resource |
||
| 41 | */ |
||
| 42 | public $logHandler; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | public $inputFiles = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | public $extractors = [ |
||
| 53 | 'php' => ['PHP'], |
||
| 54 | 'tpl' => ['PHP', 'Template'], |
||
| 55 | ]; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | public $data = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $extractorStore = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Log setup. |
||
| 69 | * |
||
| 70 | * @param string|bool $logToFile Bool or path of custom log file |
||
| 71 | */ |
||
| 72 | public function __construct($logToFile = false) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Close the log handler if needed. |
||
| 84 | */ |
||
| 85 | public function __destruct() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Writes messages into log or dumps them on screen. |
||
| 94 | * |
||
| 95 | * @param string $message |
||
| 96 | * |
||
| 97 | * @return string Html Log Message if logHandler resource is false. |
||
|
|
|||
| 98 | */ |
||
| 99 | public function log($message) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Exception factory. |
||
| 110 | * |
||
| 111 | * @param string $message |
||
| 112 | * |
||
| 113 | * @throws \Koch\Exception\Exception |
||
| 114 | */ |
||
| 115 | protected function throwException($message) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Scans given files or directories (recursively) for input files. |
||
| 128 | * |
||
| 129 | * @param string $resource File or directory |
||
| 130 | */ |
||
| 131 | protected function scan($resource) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Extracts gettext keys from multiple input files using multiple extraction adapters. |
||
| 188 | * |
||
| 189 | * @param array $inputFiles Array, defining a set of files. |
||
| 190 | * |
||
| 191 | * @return array All gettext keys of all input files. |
||
| 192 | */ |
||
| 193 | protected function extract($inputFiles) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Factory Method - Gets an instance of a Koch_Gettext_Extractor. |
||
| 238 | * |
||
| 239 | * @param string $extractor |
||
| 240 | * |
||
| 241 | * @return object Extractor Object implementing Koch\Localization\Gettext\Extractor_Interface |
||
| 242 | */ |
||
| 243 | public function getExtractor($extractor) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Assigns an extractor to an extension. |
||
| 278 | * |
||
| 279 | * @param string $extension |
||
| 280 | * @param string $extractor |
||
| 281 | * |
||
| 282 | * @return null|Extractor |
||
| 283 | */ |
||
| 284 | public function setExtractor($extension, $extractor) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Removes all extractor settings. |
||
| 298 | * |
||
| 299 | * @return Extractor |
||
| 300 | */ |
||
| 301 | public function removeAllExtractors() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Saves extracted data into gettext file. |
||
| 310 | * |
||
| 311 | * @param string $file |
||
| 312 | * @param array $data |
||
| 313 | * |
||
| 314 | * @return Extractor |
||
| 315 | */ |
||
| 316 | public function save($file, $data = null) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns a fileheader for a gettext portable object file. |
||
| 343 | * |
||
| 344 | * @param bool $return_as_string True, returns a string (default) and false returns an array. |
||
| 345 | * |
||
| 346 | * @return mixed Array or String. Returns string by default. |
||
| 347 | */ |
||
| 348 | public static function getPOFileHeader($return_as_string = true) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Formats fetched data to gettext portable object syntax. |
||
| 377 | * |
||
| 378 | * @param array $data |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | protected function formatData($data) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Escapes a string without breaking gettext syntax. |
||
| 419 | * |
||
| 420 | * @param string $string |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public static function addSlashes($string) |
||
| 428 | } |
||
| 429 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.