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 |
||
20 | class Storage extends Component implements StorageInterface |
||
21 | { |
||
22 | protected $_path; |
||
23 | protected $_locker; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | 1 | public function init() |
|
29 | { |
||
30 | 1 | $this->_path = Yii::getAlias('@storage', false); |
|
31 | 1 | } |
|
32 | |||
33 | protected function getLocker() |
||
34 | { |
||
35 | if ($this->_locker === null) { |
||
36 | $this->_locker = Locker::getInstance($this->buildPath('lock')); // TODO: get rid of singleton |
||
37 | } |
||
38 | |||
39 | return $this->_locker; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function getNextId() |
||
46 | { |
||
47 | $this->getLocker()->lock(); |
||
48 | { |
||
49 | $nextID = $this->readLastId() + 1; |
||
50 | $this->writeLastId($nextID); |
||
51 | } |
||
52 | $this->getLocker()->release(); |
||
53 | |||
54 | return $nextID; |
||
55 | } |
||
56 | |||
57 | protected function readLastId() |
||
63 | |||
64 | protected function writeLastId($value) |
||
65 | { |
||
66 | if (file_put_contents($this->getLastIdPath(), $value) === false) { |
||
67 | throw new AssetFileStorageException('Filed to write lastId to the storage'); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | protected function getLastIdPath() |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function writePackage(AssetPackage $package) |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function readPackage(AssetPackage $package) |
||
133 | |||
134 | protected function buildPath() |
||
141 | |||
142 | protected function buildHashedPath($name, $hash = 'latest') |
||
146 | |||
147 | protected function writeProviderLatest($name, $hash) |
||
187 | |||
188 | protected function writePackagesJson($hash) |
||
189 | { |
||
190 | $data = [ |
||
191 | 'providers-url' => '/p/%package%/%hash%.json', |
||
192 | 'provider-includes' => [ |
||
193 | 'p/provider-latest/%hash%.json' => [ |
||
194 | 'sha256' => $hash, |
||
195 | ], |
||
196 | ], |
||
197 | ]; |
||
198 | $this->getLocker()->lock(); |
||
199 | $filename = $this->buildPath('packages.json'); |
||
200 | try { |
||
201 | if (file_put_contents($filename, Json::encode($data)) === false) { |
||
202 | throw new AssetFileStorageException('Failed to write main packages.json'); |
||
203 | } |
||
204 | touch($filename); |
||
205 | } finally { |
||
206 | $this->getLocker()->release(); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Creates directory $dir and sets chmod 777. |
||
212 | * @param string $dir |
||
213 | * @return bool whether the directory was created successfully |
||
214 | */ |
||
215 | protected function mkdir($dir) |
||
223 | |||
224 | public function readJson($path) |
||
228 | |||
229 | protected function readPackagesJson() |
||
235 | |||
236 | protected function readProvider($path) |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function listPackages() |
||
257 | } |
||
258 |
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.