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 Type 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 Type, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Type implements Contracts\Type |
||
| 9 | { |
||
| 10 | private $convention; |
||
| 11 | private $properties = []; |
||
| 12 | private $indexes = []; |
||
| 13 | private $types = []; |
||
| 14 | |||
| 15 | private $manager; |
||
| 16 | private $space; |
||
| 17 | private $spaceId; |
||
| 18 | private $name; |
||
| 19 | |||
| 20 | 39 | public function __construct(Contracts\Manager $manager, $name, array $properties, array $types, array $indexes) |
|
| 31 | |||
| 32 | 39 | public function getSpace() |
|
| 40 | |||
| 41 | 39 | public function getSpaceId() |
|
| 45 | |||
| 46 | 39 | public function getManager() |
|
| 50 | |||
| 51 | 39 | public function getName() |
|
| 55 | |||
| 56 | 39 | public function addIndex($properties, array $arguments = null) |
|
| 57 | { |
||
| 58 | 39 | $properties = (array) $properties; |
|
| 59 | 39 | foreach ($properties as $property) { |
|
| 60 | 39 | if (!$this->hasProperty($property)) { |
|
| 61 | 39 | throw new LogicException("Unknown property $property for ".$this->name); |
|
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | 39 | $schema = $this->manager->getSchema(); |
|
| 66 | |||
| 67 | 39 | $indexName = implode('_', $properties); |
|
| 68 | |||
| 69 | 39 | if ($schema->hasIndex($this->getName(), $indexName)) { |
|
| 70 | 1 | throw new LogicException("Index $indexName already exists!"); |
|
| 71 | } |
||
| 72 | |||
| 73 | 39 | if (!$arguments) { |
|
| 74 | 39 | $arguments = []; |
|
| 75 | } |
||
| 76 | |||
| 77 | 39 | if (!array_key_exists('parts', $arguments) || !count($arguments['parts'])) { |
|
| 78 | 39 | $arguments['parts'] = []; |
|
| 79 | 39 | foreach ($properties as $property) { |
|
| 80 | 39 | $arguments['parts'][] = array_search($property, $this->properties) + 1; |
|
| 81 | 39 | $arguments['parts'][] = $this->convention->getTarantoolType($this->types[$property]); |
|
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | 39 | $num = $schema->createIndex($this->getName(), $indexName, $arguments); |
|
| 86 | 39 | $this->indexes[$num] = $properties; |
|
| 87 | |||
| 88 | 39 | return $this; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param $property name |
||
| 93 | * |
||
| 94 | * @return Type |
||
| 95 | */ |
||
| 96 | 39 | public function addProperty($name, $type = null) |
|
| 97 | { |
||
| 98 | 39 | if ($this->hasProperty($name)) { |
|
| 99 | 1 | throw new LogicException("Duplicate property $name"); |
|
| 100 | } |
||
| 101 | 39 | if (!$type) { |
|
| 102 | 39 | $type = $this->manager->getMeta()->getConvention()->getType($name); |
|
| 103 | } |
||
| 104 | 39 | $this->types[$name] = $type; |
|
| 105 | 39 | $property = $this->manager->create('property', [ |
|
| 106 | 39 | 'space' => $this->spaceId, |
|
| 107 | 39 | 'index' => count($this->properties), |
|
| 108 | 39 | 'name' => $name, |
|
| 109 | 39 | 'type' => $this->types[$name], |
|
| 110 | ]); |
||
| 111 | |||
| 112 | 39 | $this->properties[$property->index] = $name; |
|
|
|
|||
| 113 | |||
| 114 | 39 | return $this; |
|
| 115 | } |
||
| 116 | |||
| 117 | 39 | public function hasProperty($name) |
|
| 121 | |||
| 122 | 39 | public function getProperties() |
|
| 126 | |||
| 127 | 2 | public function getPropertyType($property) |
|
| 131 | |||
| 132 | 7 | public function setPropertyType($property, $type) |
|
| 154 | |||
| 155 | 4 | public function removeProperty($name) |
|
| 185 | |||
| 186 | 5 | public function reference(Contracts\Type $foreign, $property = null) |
|
| 198 | |||
| 199 | 39 | public function isReference($property) |
|
| 203 | |||
| 204 | 4 | public function getReferenceProperty(Contracts\Type $type) |
|
| 221 | |||
| 222 | 1 | public function getReferences() |
|
| 234 | |||
| 235 | 39 | public function getRequiredProperties() |
|
| 250 | |||
| 251 | 1 | public function getIndex($num) |
|
| 255 | |||
| 256 | public function dropIndex($num) |
||
| 259 | |||
| 260 | 2 | public function getIndexes() |
|
| 264 | |||
| 265 | 39 | public function findIndex($query) |
|
| 290 | |||
| 291 | 39 | public function getIndexTuple($index, $params) |
|
| 302 | |||
| 303 | 39 | public function getCompleteTuple($input) |
|
| 332 | |||
| 333 | 39 | View Code Duplication | public function getTuple($input) |
| 344 | |||
| 345 | 39 | View Code Duplication | public function fromTuple($input) |
| 356 | |||
| 357 | 39 | public function encodeProperty($name, $value) |
|
| 361 | |||
| 362 | 39 | public function decodeProperty($name, $value) |
|
| 366 | } |
||
| 367 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: