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:
| 1 | <?php |
||
| 10 | class Schema implements Contracts\Schema |
||
| 11 | { |
||
| 12 | protected $client; |
||
| 13 | protected $spaceSpace; |
||
| 14 | protected $indexSpace; |
||
| 15 | protected $spaceId = []; |
||
| 16 | |||
| 17 | 60 | public function __construct(Client $client) |
|
| 18 | { |
||
| 19 | 60 | $this->client = $client; |
|
| 20 | |||
| 21 | 60 | $this->spaceSpace = $client->getSpace('_vspace'); |
|
| 22 | 60 | $this->indexSpace = $client->getSpace('_vindex'); |
|
| 23 | 60 | $spaces = $this->spaceSpace->select([])->getData(); |
|
| 24 | 60 | foreach ($spaces as $row) { |
|
|
|
|||
| 25 | 60 | list($id, $sys, $name) = $row; |
|
| 26 | 60 | $this->spaceId[$name] = $id; |
|
| 27 | 60 | } |
|
| 28 | 60 | } |
|
| 29 | |||
| 30 | 60 | View Code Duplication | public function getSpaceId($space) |
| 31 | { |
||
| 32 | 60 | if (!array_key_exists($space, $this->spaceId)) { |
|
| 33 | 60 | $response = $this->spaceSpace->select([$space], Index::SPACE_NAME); |
|
| 34 | 60 | $data = $response->getData(); |
|
| 35 | 60 | if (!empty($data)) { |
|
| 36 | 60 | $this->spaceId[$space] = $data[0][0]; |
|
| 37 | 60 | } |
|
| 38 | 60 | } |
|
| 39 | 60 | if (array_key_exists($space, $this->spaceId)) { |
|
| 40 | 60 | return $this->spaceId[$space]; |
|
| 41 | } |
||
| 42 | 60 | } |
|
| 43 | |||
| 44 | 7 | View Code Duplication | public function getSpaceName($spaceId) |
| 45 | { |
||
| 46 | 7 | if (!in_array($spaceId, $this->spaceId)) { |
|
| 47 | 1 | $response = $this->spaceSpace->select([$spaceId], 0); |
|
| 48 | 1 | $data = $response->getData(); |
|
| 49 | 1 | if (!empty($data)) { |
|
| 50 | 1 | $this->spaceId[$data[0][2]] = $spaceId; |
|
| 51 | 1 | } |
|
| 52 | 1 | } |
|
| 53 | |||
| 54 | 7 | if (in_array($spaceId, $this->spaceId)) { |
|
| 55 | 7 | return array_search($spaceId, $this->spaceId); |
|
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | 60 | public function hasSpace($space) |
|
| 63 | |||
| 64 | 60 | public function createSpace($space) |
|
| 68 | |||
| 69 | 2 | public function dropSpace($space) |
|
| 74 | |||
| 75 | 60 | public function hasIndex($space, $index) |
|
| 82 | |||
| 83 | 60 | public function listIndexes($space) |
|
| 84 | { |
||
| 85 | 60 | $result = []; |
|
| 86 | 60 | $response = $this->indexSpace->select([$this->getSpaceId($space)], Index::INDEX_NAME); |
|
| 87 | |||
| 88 | 60 | foreach ($response->getData() as $row) { |
|
| 89 | 60 | $result[$row[2]] = []; |
|
| 90 | 60 | foreach ($row[5] as $f) { |
|
| 91 | 60 | $result[$row[2]][] = $f[0]; |
|
| 92 | 60 | } |
|
| 93 | 60 | } |
|
| 94 | |||
| 95 | 60 | return $result; |
|
| 96 | } |
||
| 97 | |||
| 98 | 60 | public function createIndex($space, $index, array $arguments) |
|
| 99 | { |
||
| 100 | 60 | $config = []; |
|
| 101 | 60 | foreach ($arguments as $k => $v) { |
|
| 102 | 60 | if (is_array($v)) { |
|
| 103 | // convert to lua array |
||
| 104 | 60 | $v = str_replace(['[', ']'], ['{', '}'], json_encode($v)); |
|
| 105 | 60 | } |
|
| 106 | 60 | if (is_bool($v)) { |
|
| 107 | 60 | $v = $v ? 'true' : 'false'; |
|
| 108 | 60 | } |
|
| 109 | 60 | $config[] = $k.' = '.$v; |
|
| 110 | 60 | } |
|
| 111 | 60 | $config = '{'.implode(', ', $config).'}'; |
|
| 112 | 60 | $this->client->evaluate("box.space.$space:create_index('$index', $config)"); |
|
| 113 | |||
| 114 | 60 | $schema = $this->client->getSpace(Space::VINDEX); |
|
| 115 | 60 | $response = $schema->select([$this->getSpaceId($space), $index], Index::INDEX_NAME); |
|
| 116 | |||
| 117 | 60 | return $response->getData()[0][1]; |
|
| 118 | } |
||
| 119 | |||
| 120 | 3 | public function dropIndex($spaceId, $index) |
|
| 128 | } |
||
| 129 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.