Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Parser |
||
| 14 | { |
||
| 15 | use \Karma\Logging\LoggerAware; |
||
| 16 | |||
| 17 | const |
||
| 18 | INCLUDES = 'includes', |
||
| 19 | VARIABLES = 'variables', |
||
| 20 | EXTERNALS = 'externals', |
||
| 21 | GROUPS = 'groups'; |
||
| 22 | |||
| 23 | private |
||
| 24 | $parsers, |
||
|
|
|||
| 25 | $currentParser, |
||
| 26 | $parsedFiles, |
||
| 27 | $fs, |
||
| 28 | $eol; |
||
| 29 | |||
| 30 | public function __construct(Filesystem $fs) |
||
| 31 | { |
||
| 32 | $this->logger = new NullLogger(); |
||
| 33 | |||
| 34 | $this->parsers = array( |
||
| 35 | self::VARIABLES => new VariableParser(), |
||
| 36 | ); |
||
| 37 | |||
| 38 | $this->parsedFiles = array(); |
||
| 39 | $this->fs = $fs; |
||
| 40 | $this->eol = "\n"; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function setEOL($eol) |
||
| 49 | |||
| 50 | public function enableIncludeSupport() |
||
| 51 | { |
||
| 52 | if(! isset($this->parsers[self::INCLUDES])) |
||
| 53 | { |
||
| 54 | $this->parsers[self::INCLUDES] = new IncludeParser(); |
||
| 55 | } |
||
| 56 | |||
| 57 | return $this; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function enableExternalSupport() |
||
| 61 | { |
||
| 62 | if(! isset($this->parsers[self::EXTERNALS])) |
||
| 63 | { |
||
| 64 | $this->parsers[self::EXTERNALS] = new ExternalParser(new Parser($this->fs)); |
||
| 65 | } |
||
| 66 | |||
| 67 | return $this; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function enableGroupSupport() |
||
| 79 | |||
| 80 | public function parse($masterFilePath) |
||
| 81 | { |
||
| 82 | try |
||
| 83 | { |
||
| 84 | $this->parseFromMasterFile($masterFilePath); |
||
| 85 | |||
| 86 | $variables = $this->getVariables(); |
||
| 87 | $this->printExternalFilesStatus(); |
||
| 88 | |||
| 89 | $this->postParse(); |
||
| 90 | |||
| 91 | return $variables; |
||
| 92 | } |
||
| 93 | catch(\RuntimeException $e) |
||
| 94 | { |
||
| 95 | $this->error($e->getMessage()); |
||
| 96 | throw $e; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | private function parseFromMasterFile($masterFilePath) |
||
| 121 | |||
| 122 | private function readFile($filePath) |
||
| 123 | { |
||
| 124 | $lines = $this->extractLines($filePath); |
||
| 125 | $this->changeCurrentFile($filePath); |
||
| 146 | |||
| 147 | private function extractLines($filePath) |
||
| 169 | |||
| 170 | private function trimLines(array $lines) |
||
| 174 | |||
| 175 | private function removeEmptyLines(array $lines) |
||
| 179 | |||
| 180 | private function changeCurrentFile($filePath) |
||
| 189 | |||
| 190 | private function extractSectionName($line) |
||
| 202 | |||
| 203 | private function switchSectionParser($sectionName) |
||
| 212 | |||
| 213 | private function getVariables() |
||
| 217 | |||
| 218 | public function getFileSystem() |
||
| 222 | |||
| 223 | public function getExternalVariables() |
||
| 234 | |||
| 235 | private function printExternalFilesStatus() |
||
| 250 | |||
| 251 | private function getExternalFilesStatus() |
||
| 262 | |||
| 263 | public function getGroups() |
||
| 274 | |||
| 275 | private function postParse() |
||
| 282 | } |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.