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; |
||
35 | class Manager extends AbstractProvider implements CacheItemPoolManagerInterface { |
||
36 | |||
37 | use NamespaceTrait, GenericManagerTrait { |
||
38 | GenericManagerTrait::setNamespace insteadof NamespaceTrait; |
||
39 | } |
||
40 | use BasicCacheItemPoolTrait; |
||
41 | |||
42 | const DEFAULT_PICK_MODE = 1; |
||
43 | |||
44 | protected $pick_mode; |
||
45 | |||
46 | protected $stack; |
||
47 | |||
48 | protected $align_cache; |
||
49 | |||
50 | protected $vacuum; |
||
51 | |||
52 | protected $selected; |
||
53 | |||
54 | 45 | View Code Duplication | public function __construct( |
77 | |||
78 | 44 | public function addProvider(EnhancedCacheItemPoolInterface $provider, $weight = 0) { |
|
83 | |||
84 | 41 | public function getItem($key) { |
|
89 | |||
90 | 21 | public function hasItem($key) { |
|
95 | |||
96 | 4 | View Code Duplication | public function deleteItem($key) { |
97 | |||
98 | 4 | if ( iterator_count($this->stack) == 0 ) return $this->vacuum->deleteItem($key); |
|
99 | |||
100 | 3 | if ( $this->align_cache === false && $this->pick_mode < 5 ) { |
|
101 | return $this->selectProvider()->deleteItem($key); |
||
102 | } |
||
103 | |||
104 | 3 | $result = []; |
|
105 | |||
106 | 3 | foreach ( $this->stack as $provider ) { |
|
107 | |||
108 | 3 | $result[] = $provider[0]->deleteItem($key); |
|
109 | |||
110 | 3 | } |
|
111 | |||
112 | 3 | return !in_array(false, $result); |
|
113 | |||
114 | } |
||
115 | |||
116 | 36 | public function save(CacheItemInterface $item) { |
|
117 | |||
118 | 36 | if ( iterator_count($this->stack) == 0 ) return $this->vacuum->save($item); |
|
119 | |||
120 | 35 | if ( $this->align_cache === false && $this->pick_mode < 5 ) { |
|
121 | 1 | return $this->selectProvider()->save($item); |
|
122 | } |
||
123 | |||
124 | 34 | $result = []; |
|
125 | |||
126 | 34 | foreach ( $this->stack as $provider ) { |
|
127 | |||
128 | 34 | $pro = $provider[0]; |
|
129 | |||
130 | 34 | $provider_result = $pro->save($item); |
|
131 | |||
132 | 34 | $this->logger->debug("Saving item ".$item->getKey()." into provider ". |
|
133 | 34 | $pro->getId().": ".($provider_result ? "OK" : "NOK - ".$pro->getStateMessage())); |
|
134 | |||
135 | 34 | $result[] = $provider_result; |
|
136 | |||
137 | 34 | } |
|
138 | |||
139 | 34 | return !in_array(false, $result); |
|
140 | |||
141 | } |
||
142 | |||
143 | 1 | View Code Duplication | public static function createFromConfiguration(Configuration $configuration, LoggerInterface $logger) { |
144 | |||
145 | 1 | list($enable, $manager_configuration, $providers) = ConfigurationParser::parse($configuration, $logger); |
|
146 | |||
147 | $manager = new Manager(...$manager_configuration); |
||
148 | |||
149 | if ( $enable ) { |
||
150 | foreach ( $providers as $name => $provider ) { |
||
151 | $instance = $provider->instance; |
||
152 | $weight = $provider->weight; |
||
153 | $id = $instance->getId(); |
||
154 | $logger->debug("Adding provider $name ($id) to cache manager (w $weight)"); |
||
155 | $manager->addProvider($instance, $weight); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | return $manager; |
||
160 | |||
161 | } |
||
162 | |||
163 | 42 | protected function selectFrom($mode, $key) { |
|
164 | |||
165 | 42 | View Code Duplication | if ( $this->pick_mode < 5 ) { |
166 | |||
167 | 36 | $result = $this->fromSingleProvider($mode, $key); |
|
168 | |||
169 | 42 | } else if ( $this->pick_mode == 5 ) { |
|
170 | |||
171 | 1 | $result = $this->fromAllProviders($mode, $key); |
|
172 | |||
173 | 1 | } else { |
|
174 | |||
175 | 5 | $result = $this->traverse($mode, $key); |
|
176 | |||
177 | } |
||
178 | |||
179 | 42 | return $result; |
|
180 | |||
181 | } |
||
182 | |||
183 | 36 | protected function fromSingleProvider($mode, $key) { |
|
192 | |||
193 | 1 | protected function fromAllProviders($mode, $key) { |
|
194 | |||
195 | 1 | $result = []; |
|
196 | |||
228 | |||
229 | 5 | protected function traverse($mode, $key) { |
|
280 | |||
281 | } |
||
282 |
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.