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 | 59 | public function __construct(Client $client) |
|
18 | { |
||
19 | 59 | $this->client = $client; |
|
20 | |||
21 | 59 | $this->spaceSpace = $client->getSpace('_vspace'); |
|
22 | 59 | $this->indexSpace = $client->getSpace('_vindex'); |
|
23 | 59 | $spaces = $this->spaceSpace->select([])->getData(); |
|
24 | 59 | foreach ($spaces as $row) { |
|
|
|||
25 | 59 | list($id, $sys, $name) = $row; |
|
26 | 59 | $this->spaceId[$name] = $id; |
|
27 | 59 | } |
|
28 | 59 | } |
|
29 | |||
30 | 59 | View Code Duplication | public function getSpaceId($space) |
31 | { |
||
32 | 59 | if (!array_key_exists($space, $this->spaceId)) { |
|
33 | 59 | $response = $this->spaceSpace->select([$space], Index::SPACE_NAME); |
|
34 | 59 | $data = $response->getData(); |
|
35 | 59 | if (!empty($data)) { |
|
36 | 59 | $this->spaceId[$space] = $data[0][0]; |
|
37 | 59 | } |
|
38 | 59 | } |
|
39 | 59 | if (array_key_exists($space, $this->spaceId)) { |
|
40 | 59 | return $this->spaceId[$space]; |
|
41 | } |
||
42 | 59 | } |
|
43 | |||
44 | 7 | View Code Duplication | public function getSpaceName($spaceId) |
45 | { |
||
46 | 7 | if (!in_array($spaceId, $this->spaceId)) { |
|
47 | $response = $this->spaceSpace->select([$spaceId], 0); |
||
48 | $data = $response->getData(); |
||
49 | if (!empty($data)) { |
||
50 | $this->spaceId[$data[0][2]] = $spaceId; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | 7 | if (in_array($spaceId, $this->spaceId)) { |
|
55 | 7 | return array_search($spaceId, $this->spaceId); |
|
56 | } |
||
57 | } |
||
58 | |||
59 | 59 | public function hasSpace($space) |
|
63 | |||
64 | 59 | public function createSpace($space) |
|
68 | |||
69 | 2 | public function dropSpace($space) |
|
74 | |||
75 | 59 | public function hasIndex($space, $index) |
|
82 | |||
83 | 59 | public function listIndexes($space) |
|
84 | { |
||
85 | 59 | $result = []; |
|
86 | 59 | $response = $this->indexSpace->select([$this->getSpaceId($space)], Index::INDEX_NAME); |
|
87 | |||
88 | 59 | foreach ($response->getData() as $row) { |
|
89 | 59 | $result[$row[2]] = []; |
|
90 | 59 | foreach ($row[5] as $f) { |
|
91 | 59 | $result[$row[2]][] = $f[0]; |
|
92 | 59 | } |
|
93 | 59 | } |
|
94 | |||
95 | 59 | return $result; |
|
96 | } |
||
97 | |||
98 | 59 | public function createIndex($space, $index, array $arguments) |
|
99 | { |
||
100 | 59 | $config = []; |
|
101 | 59 | foreach ($arguments as $k => $v) { |
|
102 | 59 | if (is_array($v)) { |
|
103 | // convert to lua array |
||
104 | 59 | $v = str_replace(['[', ']'], ['{', '}'], json_encode($v)); |
|
105 | 59 | } |
|
106 | 59 | if (is_bool($v)) { |
|
107 | 59 | $v = $v ? 'true' : 'false'; |
|
108 | 59 | } |
|
109 | 59 | $config[] = $k.' = '.$v; |
|
110 | 59 | } |
|
111 | 59 | $config = '{'.implode(', ', $config).'}'; |
|
112 | 59 | $this->client->evaluate("box.space.$space:create_index('$index', $config)"); |
|
113 | |||
114 | 59 | $schema = $this->client->getSpace(Space::VINDEX); |
|
115 | 59 | $response = $schema->select([$this->getSpaceId($space), $index], Index::INDEX_NAME); |
|
116 | |||
117 | 59 | 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.