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 namespace Comodojo\Cache\Traits; |
||
19 | trait GenericManagerTrait { |
||
20 | |||
21 | 79 | public function genericAddProvider($provider, $weight = 0) { |
|
28 | |||
29 | 2 | public function removeProvider($id) { |
|
34 | |||
35 | 2 | public function getProvider($id) { |
|
40 | |||
41 | 2 | public function getProviders($enabled = false) { |
|
46 | |||
47 | 29 | public function getSelectedProvider() { |
|
52 | |||
53 | 11 | View Code Duplication | public function clear() { |
54 | |||
55 | 11 | $result = []; |
|
56 | |||
57 | 11 | foreach ( $this->stack->getAll() as $provider ) { |
|
58 | |||
59 | 9 | $result[] = $provider->clear(); |
|
60 | |||
61 | } |
||
62 | |||
63 | 11 | return !in_array(false, $result); |
|
64 | |||
65 | } |
||
66 | |||
67 | 6 | public function setNamespace($namespace = null) { |
|
78 | |||
79 | 4 | View Code Duplication | public function clearNamespace() { |
80 | |||
81 | 4 | $result = []; |
|
82 | |||
83 | 4 | foreach ( $this->stack->getAll() as $provider ) { |
|
84 | 2 | $result[] = $provider->clearNamespace(); |
|
85 | } |
||
86 | |||
87 | 4 | return !in_array(false, $result); |
|
88 | |||
89 | } |
||
90 | |||
91 | 2 | public function getStats() { |
|
102 | |||
103 | 66 | protected function selectProvider() { |
|
127 | |||
128 | } |
||
129 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: