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 ClassMetadata 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 ClassMetadata, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ClassMetadata |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ClassMetadata |
||
| 31 | */ |
||
| 32 | private $parent; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var TraitMetadata[] |
||
| 36 | */ |
||
| 37 | private $traits; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | private $final; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $abstract; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var MethodMetadata[] |
||
| 51 | */ |
||
| 52 | private $methods; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Class_ |
||
| 56 | */ |
||
| 57 | private $ast; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * ClassMetadata constructor. |
||
| 61 | * |
||
| 62 | * @param string $name |
||
| 63 | * @param ClassMetadata|null $parent |
||
| 64 | * @param bool $final |
||
| 65 | * @param bool $abstract |
||
| 66 | * @param MethodMetadata[] $methods |
||
| 67 | * @param Class_ $ast |
||
| 68 | */ |
||
| 69 | public function __construct($name, ClassMetadata $parent = null, array $traits = [], $final = false, $abstract = false, array $methods = [], Class_ $ast = null) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function getName() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getShortName() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function isAutoloadable() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Check if class inherits some other class. |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function hasParent() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return ClassMetadata|null |
||
| 121 | */ |
||
| 122 | public function getParent() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function hasTraits() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return TraitMetadata[] |
||
| 137 | */ |
||
| 138 | public function getTraits() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | public function isFinal() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function isAbstract() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return MethodMetadata[] |
||
| 161 | */ |
||
| 162 | public function getMethods() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Check if class has method, with optional inheritance tree and trait traverse. |
||
| 169 | * |
||
| 170 | * @param string $name |
||
| 171 | * @param bool $traverse |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function hasMethod($name, $traverse = true) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Check if class has public method, with optional inheritance tree and trait traverse. |
||
| 207 | * |
||
| 208 | * @param string $name |
||
| 209 | * @param bool $traverse |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function hasPublicMethod($name, $traverse = true) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Get public method for class, with optional inheritance tree and trait traverse. |
||
| 244 | * |
||
| 245 | * @param string $name |
||
| 246 | * @param bool $traverse |
||
| 247 | * |
||
| 248 | * @return MethodMetadata |
||
| 249 | * |
||
| 250 | * @throws \RunOpenCode\AbstractBuilder\Exception\RuntimeException |
||
| 251 | */ |
||
| 252 | public function getPublicMethod($name, $traverse = true) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return Class_ |
||
| 288 | */ |
||
| 289 | public function getAst() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function __toString() |
||
| 301 | } |
||
| 302 |