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 | 26 | public function __construct(Contracts\Manager $manager, $name, array $properties, array $types, array $indexes) |
|
| 31 | |||
| 32 | 26 | public function getSpace() |
|
| 40 | |||
| 41 | 26 | public function getSpaceId() |
|
| 45 | |||
| 46 | 26 | public function getManager() |
|
| 50 | |||
| 51 | 26 | public function getName() |
|
| 55 | |||
| 56 | 26 | public function addIndex($properties, array $arguments = null) |
|
| 57 | { |
||
| 58 | 26 | $properties = (array) $properties; |
|
| 59 | 26 | foreach ($properties as $property) { |
|
| 60 | 26 | if (!$this->hasProperty($property)) { |
|
| 61 | 26 | throw new LogicException("Unknown property $property for ".$this->name); |
|
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | 26 | $schema = $this->manager->getSchema(); |
|
| 66 | |||
| 67 | 26 | $indexName = implode('_', $properties); |
|
| 68 | |||
| 69 | 26 | if ($schema->hasIndex($this->getName(), $indexName)) { |
|
| 70 | 1 | throw new LogicException("Index $indexName already exists!"); |
|
| 71 | } |
||
| 72 | |||
| 73 | 26 | if (!$arguments) { |
|
| 74 | 26 | $arguments = []; |
|
| 75 | } |
||
| 76 | |||
| 77 | 26 | if (!array_key_exists('parts', $arguments) || !count($arguments['parts'])) { |
|
| 78 | 26 | $arguments['parts'] = []; |
|
| 79 | 26 | foreach ($properties as $property) { |
|
| 80 | 26 | $arguments['parts'][] = array_search($property, $this->properties) + 1; |
|
| 81 | 26 | $arguments['parts'][] = $this->convention->getTarantoolType($this->types[$property]); |
|
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | 26 | $num = $schema->createIndex($this->getName(), $indexName, $arguments); |
|
| 86 | 26 | $this->indexes[$num] = $properties; |
|
| 87 | |||
| 88 | 26 | return $this; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param $property name |
||
| 93 | * |
||
| 94 | * @return Type |
||
| 95 | */ |
||
| 96 | 26 | public function addProperty($name, $type = null) |
|
| 116 | |||
| 117 | 26 | public function hasProperty($name) |
|
| 121 | |||
| 122 | 26 | public function getProperties() |
|
| 126 | |||
| 127 | 1 | public function getPropertyType($property) |
|
| 131 | |||
| 132 | 4 | public function setPropertyType($property, $type) |
|
| 146 | |||
| 147 | 3 | public function reference(Contracts\Type $foreign, $property = null) |
|
| 159 | |||
| 160 | 1 | public function isReference($property) |
|
| 164 | |||
| 165 | 3 | public function getReferenceProperty(Contracts\Type $type) |
|
| 182 | |||
| 183 | 1 | public function getReferences() |
|
| 195 | |||
| 196 | 26 | public function getRequiredProperties() |
|
| 211 | |||
| 212 | 1 | public function getIndex($num) |
|
| 216 | |||
| 217 | 26 | public function findIndex($query) |
|
| 242 | |||
| 243 | 26 | public function getIndexTuple($index, $params) |
|
| 255 | |||
| 256 | 26 | View Code Duplication | public function getTuple($input) |
| 267 | |||
| 268 | 26 | View Code Duplication | public function fromTuple($input) |
| 279 | |||
| 280 | 26 | public function encodeProperty($name, $value) |
|
| 284 | |||
| 285 | 26 | public function decodeProperty($name, $value) |
|
| 289 | } |
||
| 290 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: