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 |
||
22 | class CollectionCache |
||
23 | { |
||
24 | /** Prefix cache key */ |
||
25 | const BASE_KEY = 'CollectionCache'; |
||
26 | |||
27 | /** Prefix cache key */ |
||
28 | const BASE_UPDATE_KEY = 'CollectionUpdate'; |
||
29 | |||
30 | /** @var array */ |
||
31 | private $config = []; |
||
32 | |||
33 | /** @var CacheProvider */ |
||
34 | private $cache; |
||
35 | |||
36 | /** |
||
37 | * CollectionCache constructor. |
||
38 | * @param CacheProvider $cache Cache provider |
||
39 | * @param array $configuration Collections to be saved |
||
40 | */ |
||
41 | 10 | public function __construct( |
|
48 | |||
49 | /** |
||
50 | * Makes an id |
||
51 | * |
||
52 | * @param string $collection DB collection name |
||
53 | * @param string $id Object Identifier |
||
54 | * @return string |
||
55 | */ |
||
56 | 6 | private function buildCacheKey($collection, $id) |
|
60 | |||
61 | /** |
||
62 | * Time it should be in cache and if so should happen |
||
63 | * |
||
64 | * @param string $collection DB collection name |
||
65 | * @return int |
||
66 | */ |
||
67 | 6 | private function getCollectionCacheTime($collection) |
|
74 | |||
75 | /** |
||
76 | * Return un cached object. |
||
77 | * |
||
78 | * @param Repository $repository DB Repository |
||
79 | * @param string $id Queried is |
||
80 | * @return object|false if no cache found |
||
81 | */ |
||
82 | 2 | View Code Duplication | public function getByRepository(Repository $repository, $id) |
95 | |||
96 | /** |
||
97 | * @param Repository $repository DB Repository |
||
98 | * @param string $serialized Serialised Object document |
||
99 | * @param string $id Object document identifier |
||
100 | * @return bool |
||
101 | */ |
||
102 | 4 | View Code Duplication | public function setByRepository(Repository $repository, $serialized, $id) |
112 | |||
113 | /** |
||
114 | * Will sleep until previous operation has finished. |
||
115 | * Default sleep time 10 seconds. |
||
116 | * Loops by 1/4 second while there is a cache update lock |
||
117 | * |
||
118 | * @param Repository $repository Model repository |
||
119 | * @param string $id Object identifier |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | 2 | View Code Duplication | public function updateOperationCheck(Repository $repository, $id) |
124 | { |
||
125 | 2 | $collection = $repository->getClassMetadata()->collection; |
|
126 | 2 | $key = self::BASE_UPDATE_KEY.'-'.$this->buildCacheKey($collection, $id); |
|
127 | |||
128 | 2 | while ($this->cache->fetch($key)) { |
|
129 | 2 | usleep(250000); |
|
130 | 1 | } |
|
131 | 2 | } |
|
132 | |||
133 | /** |
||
134 | * Will add update lock |
||
135 | * |
||
136 | * @param Repository $repository Model repository |
||
137 | * @param string $id Object identifier |
||
138 | * @param integer $maxTime Set timeout for lock |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | 2 | View Code Duplication | public function addUpdateLock(Repository $repository, $id, $maxTime = 10) |
143 | { |
||
144 | 2 | $collection = $repository->getClassMetadata()->collection; |
|
145 | 2 | $key = self::BASE_UPDATE_KEY.'-'.$this->buildCacheKey($collection, $id); |
|
146 | |||
147 | 2 | return $this->cache->save($key, true, $maxTime); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Will remove lock if there was one. |
||
152 | * |
||
153 | * @param Repository $repository Model repository |
||
154 | * @param string $id Object identifier |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | public function releaseUpdateLock(Repository $repository, $id) |
||
159 | { |
||
160 | $collection = $repository->getClassMetadata()->collection; |
||
161 | $baseKey = $this->buildCacheKey($collection, $id); |
||
162 | $key = self::BASE_UPDATE_KEY.'-'.$baseKey; |
||
163 | |||
164 | $this->cache->delete($key); |
||
165 | |||
166 | if ($this->getCollectionCacheTime($collection)) { |
||
167 | $this->cache->delete($baseKey); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Update cache if needed |
||
173 | * |
||
174 | * @param array $configuration configuration array |
||
175 | * @return void |
||
176 | */ |
||
177 | public function setConfiguration($configuration) |
||
181 | } |
||
182 |
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.