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\Components; |
||
24 | class AbstractStackManager extends FilterIterator { |
||
25 | |||
26 | use FlapIntervalTrait; |
||
27 | |||
28 | 70 | public function accept() { |
|
29 | |||
30 | 70 | $provider = $this->getInnerIterator()->current(); |
|
31 | 70 | $provider = $provider[0]; |
|
32 | |||
33 | 70 | $status = $provider->getState(); |
|
34 | |||
35 | if ( |
||
36 | 70 | $status === $provider::CACHE_ERROR && |
|
37 | 70 | date_create('now')->diff($provider->getStateTime())->format('%s') > $this->getFlapInterval() |
|
38 | ) { |
||
39 | |||
40 | 2 | return $provider->test(); |
|
41 | |||
42 | } |
||
43 | |||
44 | 70 | return $status == $provider::CACHE_SUCCESS ? true : false; |
|
45 | |||
46 | } |
||
47 | |||
48 | 75 | public function genericAdd($provider, $weight) { |
|
57 | |||
58 | 2 | View Code Duplication | public function remove($id) { |
|
|||
59 | |||
60 | 2 | $pools = $this->getInnerIterator(); |
|
61 | |||
62 | 2 | if ( isset($pools[$id]) ) { |
|
63 | 2 | unset($pools[$id]); |
|
64 | 2 | return true; |
|
65 | } |
||
66 | |||
67 | throw new Exception("Provider $id not registered into the stack"); |
||
68 | |||
69 | } |
||
70 | |||
71 | 2 | View Code Duplication | public function get($id) { |
72 | |||
73 | 2 | $pools = $this->getInnerIterator(); |
|
74 | |||
75 | 2 | if ( isset($pools[$id]) ) return $pools[$id][0]; |
|
76 | |||
77 | throw new Exception("Provider $id not registered into the stack"); |
||
78 | |||
79 | } |
||
80 | |||
81 | 25 | public function getAll($enabled = false) { |
|
82 | |||
83 | 25 | $result = []; |
|
84 | |||
85 | 25 | if ( $enabled === true ) { |
|
86 | |||
87 | 15 | foreach($this as $id => $provider) $result[$id] = $provider[0]; |
|
88 | |||
89 | } else { |
||
90 | |||
91 | 10 | foreach($this->getInnerIterator() as $id => $provider) $result[$id] = $provider[0]; |
|
92 | |||
93 | } |
||
94 | |||
95 | 25 | return $result; |
|
96 | |||
97 | } |
||
98 | |||
99 | public function getCurrent() { |
||
105 | |||
106 | public function has($id) { |
||
113 | |||
114 | 4 | public function getRandomProvider() { |
|
115 | |||
116 | 4 | $stack = $this->getAll(true); |
|
117 | |||
118 | 4 | $rand = array_rand($stack); |
|
119 | |||
120 | 4 | return $rand === null ? null : $stack[$rand]; |
|
121 | |||
122 | } |
||
123 | |||
124 | 48 | public function getFirstProvider() { |
|
125 | |||
126 | 48 | $this->rewind(); |
|
127 | |||
128 | 48 | $current = $this->current(); |
|
129 | |||
130 | 48 | return $current === null ? null : $current[0]; |
|
131 | |||
132 | // $providers = $this->getAll(); |
||
133 | |||
134 | // return current($providers); |
||
135 | |||
136 | } |
||
137 | |||
138 | 11 | public function getLastProvider() { |
|
149 | |||
150 | public function getHeavyProvider() { |
||
151 | |||
152 | $providers = $this->getAll(true); |
||
153 | |||
154 | if ( count($providers) === 0 ) return null; |
||
155 | |||
165 | |||
166 | private function getWeights() { |
||
175 | |||
176 | } |
||
177 |
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.