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 Space 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 Space, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Space |
||
| 8 | { |
||
| 9 | private $mapper; |
||
| 10 | |||
| 11 | private $id; |
||
| 12 | private $name; |
||
| 13 | private $format; |
||
| 14 | private $indexes; |
||
| 15 | |||
| 16 | private $formatNamesHash = []; |
||
| 17 | private $formatTypesHash = []; |
||
| 18 | |||
| 19 | private $repository; |
||
| 20 | |||
| 21 | public function __construct(Mapper $mapper, $id, $name, $meta = null) |
||
| 33 | |||
| 34 | public function addProperties($config) |
||
| 41 | |||
| 42 | public function addProperty($name, $type) |
||
| 58 | |||
| 59 | public function removeProperty($name) |
||
| 73 | |||
| 74 | public function removeIndex($name) |
||
| 82 | |||
| 83 | public function addIndex($config) |
||
| 87 | |||
| 88 | public function createIndex($config) |
||
| 89 | { |
||
| 90 | if (!is_array($config)) { |
||
| 91 | $config = ['fields' => $config]; |
||
| 92 | } |
||
| 93 | |||
| 94 | |||
| 95 | if (!array_key_exists('fields', $config)) { |
||
| 96 | if (array_values($config) != $config) { |
||
| 97 | throw new Exception("Invalid index configuration"); |
||
| 98 | } |
||
| 99 | $config = [ |
||
| 100 | 'fields' => $config |
||
| 101 | ]; |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!is_array($config['fields'])) { |
||
| 105 | $config['fields'] = [$config['fields']]; |
||
| 106 | } |
||
| 107 | |||
| 108 | $options = [ |
||
| 109 | 'parts' => [] |
||
| 110 | ]; |
||
| 111 | |||
| 112 | foreach ($config as $k => $v) { |
||
| 113 | if ($k != 'name' && $k != 'fields') { |
||
| 114 | $options[$k] = $v; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | View Code Duplication | foreach ($config['fields'] as $property) { |
|
|
|
|||
| 119 | if (!$this->getPropertyType($property)) { |
||
| 120 | throw new Exception("Unknown property $property", 1); |
||
| 121 | } |
||
| 122 | $options['parts'][] = $this->getPropertyIndex($property)+1; |
||
| 123 | $options['parts'][] = $this->getPropertyType($property); |
||
| 124 | } |
||
| 125 | |||
| 126 | $name = array_key_exists('name', $config) ? $config['name'] : implode('_', $config['fields']); |
||
| 127 | |||
| 128 | $this->mapper->getClient()->evaluate("box.space[$this->id]:create_index('$name', ...)", [$options]); |
||
| 129 | $this->indexes = []; |
||
| 130 | |||
| 131 | $this->mapper->getSchema()->getSpace('_index')->getRepository()->flushCache(); |
||
| 132 | |||
| 133 | return $this; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function isSpecial() |
||
| 140 | |||
| 141 | public function getId() |
||
| 145 | |||
| 146 | public function getTupleMap() |
||
| 154 | |||
| 155 | public function getFormat() |
||
| 172 | |||
| 173 | public function getMapper() |
||
| 177 | |||
| 178 | public function getName() |
||
| 182 | |||
| 183 | private function parseFormat() |
||
| 193 | |||
| 194 | public function hasProperty($name) |
||
| 199 | |||
| 200 | public function getMeta() |
||
| 212 | |||
| 213 | public function getPropertyType($name) |
||
| 220 | |||
| 221 | public function getPropertyIndex($name) |
||
| 228 | |||
| 229 | public function getIndexes() |
||
| 259 | |||
| 260 | public function castIndex($params, $suppressException = false) |
||
| 310 | |||
| 311 | public function getIndexValues($indexId, $params) |
||
| 335 | |||
| 336 | public function getPrimaryIndex() |
||
| 344 | |||
| 345 | public function getTupleKey($tuple) |
||
| 353 | |||
| 354 | public function getInstanceKey($instance) |
||
| 368 | |||
| 369 | public function getRepository() |
||
| 383 | |||
| 384 | public function repositoryExists() |
||
| 388 | } |
||
| 389 |
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.