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 manager 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 manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class manager |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | private $types; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $schemadirs; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $namespace; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $merged_types = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $child_classes = []; |
||
| 44 | |||
| 45 | 14 | public function __construct(array $schemadirs, $namespace) |
|
| 50 | |||
| 51 | 16 | public function get_types() |
|
| 56 | |||
| 57 | 11 | public function get_inherited_mapping() |
|
| 62 | |||
| 63 | 14 | public function get_child_classes($typename) |
|
| 71 | |||
| 72 | 15 | public function resolve_targetclass(property $property) |
|
| 73 | { |
||
| 74 | 15 | $this->initialize(); |
|
| 75 | |||
| 76 | 15 | $fqcn = $property->link['target']; |
|
| 77 | 15 | if (!empty($this->namespace)) { |
|
| 78 | 15 | $fqcn = $this->namespace . '\\' . $fqcn; |
|
| 79 | 15 | } |
|
| 80 | |||
| 81 | 15 | if ( array_key_exists($fqcn, $this->types) |
|
| 82 | 15 | || $property->link['target'] === $property->get_parent()->name) { |
|
| 83 | 15 | $target_class = $property->link['target']; |
|
| 84 | 15 | } else { |
|
| 85 | if (!array_key_exists($property->link['target'], $this->merged_types)) { |
||
| 86 | throw new \Exception('Link to unknown class ' . $property->link['target']); |
||
| 87 | } |
||
| 88 | $target_class = $this->merged_types[$property->link['target']]; |
||
| 89 | } |
||
| 90 | 15 | return $target_class; |
|
| 91 | } |
||
| 92 | |||
| 93 | 19 | private function initialize() |
|
| 137 | |||
| 138 | 14 | private function register_child_class(type $type) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * This sort of provides a workaround for situations where two tables use the same name |
||
| 148 | */ |
||
| 149 | 5 | private function create_merged_types(array $types) |
|
| 196 | |||
| 197 | 5 | private function get_type_by_shortname($classname) |
|
| 198 | { |
||
| 199 | 5 | $fqcn = $classname; |
|
| 200 | 5 | if (!empty($this->namespace)) { |
|
| 201 | 5 | $fqcn = $this->namespace . '\\' . $classname; |
|
| 202 | 5 | } |
|
| 203 | 5 | if (array_key_exists($fqcn, $this->types)) { |
|
| 204 | return $this->types[$fqcn]; |
||
| 205 | 5 | } elseif (array_key_exists($classname, $this->merged_types)) { |
|
| 206 | 5 | $fqcn = $this->merged_types[$classname]; |
|
| 207 | 5 | if (!empty($this->namespace)) { |
|
| 208 | 5 | $fqcn = $this->namespace . '\\' . $fqcn; |
|
| 209 | 5 | } |
|
| 210 | 5 | return $this->types[$fqcn]; |
|
| 211 | } |
||
| 212 | return null; |
||
| 213 | } |
||
| 214 | |||
| 215 | 14 | private function add_type(type $type) |
|
| 239 | } |
||
| 240 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.