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 |
||
9 | final class Manager |
||
10 | { |
||
11 | use CloneWithMethod; |
||
12 | |||
13 | /** |
||
14 | * @var Connector |
||
15 | */ |
||
16 | private $connector; |
||
17 | |||
18 | /** |
||
19 | * @var Builder |
||
20 | */ |
||
21 | private $builder; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $operations = []; |
||
27 | |||
28 | /** |
||
29 | * @param Connector $connector |
||
30 | * @param Builder $builder |
||
31 | */ |
||
32 | 1 | public function __construct(Connector $connector, Builder $builder) |
|
33 | { |
||
34 | 1 | $this->connector = $connector; |
|
35 | 1 | $this->builder = $builder; |
|
36 | 1 | } |
|
37 | |||
38 | /** |
||
39 | * @param string $method |
||
40 | * @param array $parameters |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | 1 | public function __call($method, array $parameters = []) |
|
45 | { |
||
46 | 1 | $operations = $this->operations; |
|
47 | 1 | $operations[] = [$method, $parameters]; |
|
48 | |||
49 | 1 | return $this->cloneWith("operations", $operations); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string $table |
||
54 | * |
||
55 | * @return static |
||
56 | */ |
||
57 | 1 | public function table($table) |
|
58 | { |
||
59 | 1 | return $this->cloneWith("builder", $this->builder->table($table)); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param string $columns |
||
64 | * |
||
65 | * @return static |
||
66 | */ |
||
67 | 1 | public function select($columns = "*") |
|
68 | { |
||
69 | 1 | return $this->cloneWith("builder", $this->builder->select($columns)); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return PromiseInterface |
||
74 | */ |
||
75 | 1 | public function first() |
|
76 | { |
||
77 | return Coroutine\create(function () { |
||
78 | 1 | $rows = (yield $this->limit(1)->get()); |
|
79 | 1 | yield reset($rows); |
|
80 | 1 | }); |
|
81 | 1 | } |
|
82 | |||
83 | /** |
||
84 | * @return PromiseInterface |
||
85 | */ |
||
86 | 1 | public function get() |
|
87 | { |
||
88 | return Coroutine\create(function () { |
||
89 | 1 | $builder = $this->applyOperationsTo($this->builder); |
|
90 | |||
91 | 1 | yield $this->connector->query( |
|
92 | 1 | $this->interpolate($builder->build()) |
|
93 | 1 | ); |
|
94 | 1 | }); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param array $build |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 1 | private function interpolate(array $build) |
|
103 | { |
||
104 | 1 | list($statement, $values) = $build; |
|
105 | |||
106 | 1 | $pattern = "/\\:([_0-9a-zA-Z]+)/"; |
|
107 | |||
108 | $replacement = function ($matches) use ($values) { |
||
109 | 1 | return $this->quote($values[$matches[1]]); |
|
110 | 1 | }; |
|
111 | |||
112 | 1 | return preg_replace_callback($pattern, $replacement, $statement); |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param mixed $value |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 1 | private function quote($value) |
|
121 | { |
||
122 | 1 | return "'" . $this->connector->escape($value) . "'"; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param array $data |
||
127 | * |
||
128 | * @return PromiseInterface |
||
129 | */ |
||
130 | 1 | View Code Duplication | public function insert(array $data) |
|
|||
131 | { |
||
132 | return Coroutine\create(function () use ($data) { |
||
133 | 1 | $builder = $this->builder->insert($data); |
|
134 | 1 | $builder = $this->applyOperationsTo($builder); |
|
135 | |||
136 | 1 | yield $this->connector->query( |
|
137 | 1 | $this->interpolate($builder->build()) |
|
138 | 1 | ); |
|
139 | 1 | }); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param Builder $builder |
||
144 | * |
||
145 | * @return Builder |
||
146 | */ |
||
147 | 1 | private function applyOperationsTo($builder) |
|
148 | { |
||
149 | 1 | foreach ($this->operations as $operation) { |
|
150 | 1 | list($method, $parameters) = $operation; |
|
151 | 1 | $builder = call_user_func_array([$builder, $method], $parameters); |
|
152 | 1 | } |
|
153 | |||
154 | 1 | return $builder; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param array $data |
||
159 | * |
||
160 | * @return PromiseInterface |
||
161 | */ |
||
162 | View Code Duplication | public function update(array $data) |
|
173 | |||
174 | /** |
||
175 | * @return PromiseInterface |
||
176 | */ |
||
177 | View Code Duplication | public function delete() |
|
188 | } |
||
189 |
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.