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 NodeGenerator 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 NodeGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | final class NodeGenerator |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var PhpGenerator |
||
| 24 | */ |
||
| 25 | private $phpGenerator; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $useQueryBuilderFactory; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param PhpGenerator $phpGenerator |
||
| 34 | * @param bool $useQueryBuilderFactory |
||
| 35 | */ |
||
| 36 | 15 | public function __construct(PhpGenerator $phpGenerator, bool $useQueryBuilderFactory = false) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @param $query |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | 15 | public function generateByJson($query): string |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @return Expr |
||
| 71 | */ |
||
| 72 | 13 | View Code Duplication | private function createObjectNode(): Expr |
| 80 | |||
| 81 | /** |
||
| 82 | * @return Expr |
||
| 83 | */ |
||
| 84 | 4 | View Code Duplication | private function createArrayNode(): Expr |
| 92 | |||
| 93 | /** |
||
| 94 | * @param string|float|int|bool|null $value |
||
| 95 | * |
||
| 96 | * @return Expr |
||
| 97 | */ |
||
| 98 | 13 | private function createScalarNode($value): Expr |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param string|float|int|bool|null $value |
||
| 109 | * |
||
| 110 | * @return Expr |
||
| 111 | */ |
||
| 112 | 12 | View Code Duplication | private function createScalarNodeDefault($value): Expr |
| 126 | |||
| 127 | /** |
||
| 128 | * @param string|float|int|bool|null $value |
||
| 129 | * |
||
| 130 | * @return Expr |
||
| 131 | */ |
||
| 132 | 1 | View Code Duplication | private function createScalarNodeQueryBuilderFactory($value): Expr |
| 146 | |||
| 147 | /** |
||
| 148 | * @param array $data |
||
| 149 | * |
||
| 150 | * @return Expr |
||
| 151 | */ |
||
| 152 | 4 | private function appendChildrenToArrayNode(array $data) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param \stdClass $data |
||
| 179 | * |
||
| 180 | * @return Expr |
||
| 181 | */ |
||
| 182 | 13 | private function appendChildrenToObjectNode(\stdClass $data) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $code |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | 14 | private function structureCode(string $code): string |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $code |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | 14 | private function getLinesByCode(string $code): array |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $line |
||
| 244 | * @param string $lastStructuredLine |
||
| 245 | * @param int $position |
||
| 246 | * @param array $structuredLines |
||
| 247 | */ |
||
| 248 | 14 | private function structuredLine(string $line, string $lastStructuredLine, int &$position, array &$structuredLines) |
|
| 280 | } |
||
| 281 |
If you suppress an error, we recommend checking for the error condition explicitly: