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 |
||
23 | class CollectionCache |
||
24 | { |
||
25 | /** Prefix cache key */ |
||
26 | const BASE_KEY = 'CollectionCache'; |
||
27 | |||
28 | /** Prefix cache key */ |
||
29 | const BASE_UPDATE_KEY = 'CollectionUpdate'; |
||
30 | |||
31 | /** @var array */ |
||
32 | private $config = []; |
||
33 | |||
34 | /** @var CacheProvider */ |
||
35 | private $cache; |
||
36 | |||
37 | /** |
||
38 | * @var Logger |
||
39 | */ |
||
40 | private $logger; |
||
41 | |||
42 | /** |
||
43 | * CollectionCache constructor. |
||
44 | * @param CacheProvider $cache Cache provider |
||
45 | * @param Logger $logger Logger |
||
46 | * @param array $configuration Collections to be saved |
||
47 | */ |
||
48 | 10 | public function __construct( |
|
57 | |||
58 | /** |
||
59 | * Makes an id |
||
60 | * |
||
61 | * @param string $collection DB collection name |
||
62 | * @param string $id Object Identifier |
||
63 | * @return string |
||
64 | */ |
||
65 | 6 | private function buildCacheKey($collection, $id) |
|
69 | |||
70 | /** |
||
71 | * Time it should be in cache and if so should happen |
||
72 | * |
||
73 | * @param string $collection DB collection name |
||
74 | * @return int |
||
75 | */ |
||
76 | 6 | private function getCollectionCacheTime($collection) |
|
83 | |||
84 | /** |
||
85 | * Return un cached object. |
||
86 | * |
||
87 | * @param Repository $repository DB Repository |
||
88 | * @param string $id Queried is |
||
89 | * @return object|false if no cache found |
||
90 | */ |
||
91 | 2 | View Code Duplication | public function getByRepository(Repository $repository, $id) |
104 | |||
105 | /** |
||
106 | * @param Repository $repository DB Repository |
||
107 | * @param string $serialized Serialised Object document |
||
108 | * @param string $id Object document identifier |
||
109 | * @return bool |
||
110 | */ |
||
111 | 4 | View Code Duplication | public function setByRepository(Repository $repository, $serialized, $id) |
121 | |||
122 | /** |
||
123 | * Will sleep until previous operation has finished. |
||
124 | * Default sleep time 10 seconds. |
||
125 | * Loops by 1/4 second while there is a cache update lock |
||
126 | * |
||
127 | * @param Repository $repository Model repository |
||
128 | * @param string $id Object identifier |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | 2 | public function updateOperationCheck(Repository $repository, $id) |
|
145 | |||
146 | /** |
||
147 | * Will add update lock |
||
148 | * |
||
149 | * @param Repository $repository Model repository |
||
150 | * @param string $id Object identifier |
||
151 | * @param integer $maxTime Set timeout for lock |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | 2 | public function addUpdateLock(Repository $repository, $id, $maxTime = 10) |
|
164 | |||
165 | /** |
||
166 | * Will remove lock if there was one. |
||
167 | * |
||
168 | * @param Repository $repository Model repository |
||
169 | * @param string $id Object identifier |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | public function releaseUpdateLock(Repository $repository, $id) |
||
187 | |||
188 | /** |
||
189 | * Update cache if needed |
||
190 | * |
||
191 | * @param array $configuration configuration array |
||
192 | * @return void |
||
193 | */ |
||
194 | public function setConfiguration($configuration) |
||
198 | } |
||
199 |