Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FbpParser 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 FbpParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | final class FbpParser implements FbpDefinitionsInterface |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $source; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $settings; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $schema; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | private $linecount; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | private $linecountOverall; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $definition; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * FbpParser constructor. |
||
| 59 | * |
||
| 60 | * @param string $source optional for initializing |
||
| 61 | 7 | * @param array $settings optional settings for parser |
|
| 62 | */ |
||
| 63 | 7 | public function __construct(string $source = '', array $settings = []) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $source |
||
| 85 | * @return DefinitionInterface |
||
| 86 | 7 | * @throws ParserException |
|
| 87 | */ |
||
| 88 | 7 | public function run(string $source = '') : DefinitionInterface |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $line |
||
| 126 | * @return array |
||
| 127 | 6 | * @throws ParserDefinitionException |
|
| 128 | */ |
||
| 129 | 6 | private function examineSubset(string $line) : array |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Check if array has a specific key and is not empty. |
||
| 217 | * |
||
| 218 | * @param array $check |
||
| 219 | * @param string $value |
||
| 220 | 6 | * @return bool |
|
| 221 | */ |
||
| 222 | 6 | private function hasValue(array $check, string $value) : bool |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param array $definition |
||
| 233 | * @param string $label |
||
| 234 | 6 | * @return array |
|
| 235 | */ |
||
| 236 | private function addPort(array $definition, string $label) : array |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $line |
||
| 246 | * @return array |
||
| 247 | 6 | * @throws ParserDefinitionException |
|
| 248 | */ |
||
| 249 | 6 | private function examineDefinition(string $line) : array |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Add entry to processes. |
||
| 275 | * |
||
| 276 | 6 | * @param array $process |
|
| 277 | */ |
||
| 278 | 6 | private function examineProcess(array $process) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Add name to definition |
||
| 297 | * |
||
| 298 | 1 | * @param string $line |
|
| 299 | */ |
||
| 300 | 1 | private function addName(string $line) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Check if line is empty or has comment. |
||
| 307 | * In case of comments, add name to definition. |
||
| 308 | * |
||
| 309 | * @param string $line |
||
| 310 | 6 | * @return bool |
|
| 311 | */ |
||
| 312 | 6 | private function doSkip(string $line) : bool |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param array $subset |
||
| 334 | 6 | * @param string $line |
|
| 335 | */ |
||
| 336 | 6 | private function validate(array $subset, string $line) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $line |
||
| 351 | * @param string $port |
||
| 352 | 1 | * @throws ParserException |
|
| 353 | */ |
||
| 354 | 1 | private function validationError(string $line, string $port) |
|
| 360 | } |
||
| 361 |