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 |
||
24 | class CouchbaseConnection extends Connection |
||
25 | { |
||
26 | /** @var string */ |
||
27 | protected $bucket; |
||
28 | |||
29 | /** @var \CouchbaseCluster */ |
||
30 | protected $connection; |
||
31 | |||
32 | /** @var */ |
||
33 | protected $managerUser; |
||
34 | |||
35 | /** @var */ |
||
36 | protected $managerPassword; |
||
37 | |||
38 | /** @var int */ |
||
39 | protected $fetchMode = 0; |
||
40 | |||
41 | /** @var array */ |
||
42 | protected $enableN1qlServers = []; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $bucketPassword = ''; |
||
46 | |||
47 | /** |
||
48 | * @param array $config |
||
49 | */ |
||
50 | 20 | public function __construct(array $config) |
|
59 | |||
60 | /** |
||
61 | * @param $password |
||
62 | * |
||
63 | * @return $this |
||
64 | */ |
||
65 | 5 | public function setBucketPassword($password) |
|
70 | |||
71 | /** |
||
72 | * @param $name |
||
73 | * |
||
74 | * @return \CouchbaseBucket |
||
75 | */ |
||
76 | 5 | public function openBucket($name) |
|
80 | |||
81 | /** |
||
82 | * @return Processor |
||
83 | */ |
||
84 | 20 | protected function getDefaultPostProcessor() |
|
88 | |||
89 | /** |
||
90 | * @return Grammar |
||
91 | */ |
||
92 | 20 | protected function getDefaultQueryGrammar() |
|
96 | |||
97 | /** |
||
98 | * @param array $config |
||
99 | */ |
||
100 | 20 | protected function getManagedConfigure(array $config) |
|
113 | |||
114 | /** |
||
115 | * @param $dsn |
||
116 | * |
||
117 | * @return \CouchbaseCluster |
||
118 | */ |
||
119 | 20 | protected function createConnection($dsn) |
|
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function getDriverName() |
||
131 | |||
132 | /** |
||
133 | * @return \CouchbaseCluster |
||
134 | */ |
||
135 | 11 | public function getCouchbase() |
|
139 | |||
140 | /** |
||
141 | * @param string $table |
||
142 | * |
||
143 | * @return \Ytake\LaravelCouchbase\Database\QueryBuilder |
||
144 | */ |
||
145 | 5 | public function table($table) |
|
151 | |||
152 | /** |
||
153 | * @param string $bucket |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function bucket($bucket) |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | 3 | public function select($query, $bindings = [], $useReadPdo = true) |
|
181 | |||
182 | /** |
||
183 | * @param string $query |
||
184 | * @param array $bindings |
||
185 | * |
||
186 | * @return int|mixed |
||
187 | */ |
||
188 | 4 | public function insert($query, $bindings = []) |
|
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | 5 | View Code Duplication | public function affectingStatement($query, $bindings = []) |
211 | |||
212 | /** |
||
213 | * @param $query |
||
214 | * @param array $bindings |
||
215 | * |
||
216 | * @return mixed |
||
217 | */ |
||
218 | View Code Duplication | public function positionalStatement($query, array $bindings = []) |
|
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | 1 | public function transaction(Closure $callback) |
|
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | 1 | public function beginTransaction() |
|
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | 1 | public function commit() |
|
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | 1 | public function rollBack() |
|
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | 16 | public function setFetchMode($fetchMode) |
|
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | 5 | protected function reconnectIfMissingConnection() |
|
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function disconnect() |
||
291 | |||
292 | /** |
||
293 | * @param CouchbaseBucket $bucket |
||
294 | * |
||
295 | * @return CouchbaseBucket |
||
296 | */ |
||
297 | protected function enableN1ql(CouchbaseBucket $bucket) |
||
306 | |||
307 | /** |
||
308 | * N1QL upsert query. |
||
309 | * |
||
310 | * @param string $query |
||
311 | * @param array $bindings |
||
312 | * |
||
313 | * @return int |
||
314 | */ |
||
315 | 1 | public function upsert($query, $bindings = []) |
|
319 | |||
320 | /** |
||
321 | * Get a new query builder instance. |
||
322 | * |
||
323 | * @return \Illuminate\Database\Query\Builder |
||
324 | */ |
||
325 | 5 | public function query() |
|
331 | |||
332 | /** |
||
333 | * Run an update statement against the database. |
||
334 | * |
||
335 | * @param string $query |
||
336 | * @param array $bindings |
||
337 | * |
||
338 | * @return int|\stdClass |
||
339 | */ |
||
340 | 1 | public function update($query, $bindings = []) |
|
344 | |||
345 | /** |
||
346 | * Run a delete statement against the database. |
||
347 | * |
||
348 | * @param string $query |
||
349 | * @param array $bindings |
||
350 | * |
||
351 | * @return int|\stdClass |
||
352 | */ |
||
353 | 5 | public function delete($query, $bindings = []) |
|
357 | } |
||
358 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.