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() |
||
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) |
||
80 | { |
||
81 | $name = $package->getNormalName(); |
||
82 | $data = [ |
||
83 | 'packages' => [ |
||
84 | $name => $package->getReleases(), |
||
85 | ], |
||
86 | ]; |
||
87 | $json = Json::encode($data); |
||
88 | $hash = hash('sha256', $json); |
||
89 | $path = $this->buildHashedPath($name, $hash); |
||
90 | $latestPath = $this->buildHashedPath($name); |
||
91 | if (!file_exists($path)) { |
||
92 | $this->getLocker()->lock(); |
||
93 | try { |
||
94 | if ($this->mkdir(dirname($path)) === false) { |
||
95 | throw new AssetFileStorageException('Failed to create a directory for asset-package', $package); |
||
96 | } |
||
97 | if (file_put_contents($path, $json) === false) { |
||
98 | throw new AssetFileStorageException('Failed to write package', $package); |
||
99 | } |
||
100 | if (file_put_contents($latestPath, $json) === false) { |
||
101 | throw new AssetFileStorageException('Failed to write file "latest.json" for asset-packge', $package); |
||
102 | } |
||
103 | $this->writeProviderLatest($name, $hash); |
||
104 | } finally { |
||
105 | $this->getLocker()->release(); |
||
106 | } |
||
107 | } else { |
||
108 | touch($latestPath); |
||
109 | } |
||
110 | |||
111 | return $hash; |
||
112 | } |
||
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) |
||
148 | { |
||
149 | $latestPath = $this->buildHashedPath('provider-latest'); |
||
150 | if (file_exists($latestPath)) { |
||
151 | $data = Json::decode(file_get_contents($latestPath) ?: '[]'); |
||
152 | } |
||
153 | if (!isset($data) || !is_array($data)) { |
||
154 | $data = []; |
||
155 | } |
||
156 | if (!isset($data['providers'])) { |
||
157 | $data['providers'] = []; |
||
158 | } |
||
159 | $data['providers'][$name] = ['sha256' => $hash]; |
||
160 | $json = Json::encode($data); |
||
161 | $hash = hash('sha256', $json); |
||
162 | $path = $this->buildHashedPath('provider-latest', $hash); |
||
163 | |||
164 | if (!file_exists($path)) { |
||
165 | $this->getLocker()->lock(); |
||
166 | |||
167 | try { |
||
168 | if ($this->mkdir(dirname($path)) === false) { |
||
169 | throw new AssetFileStorageException('Failed to create a directory for provider-latest storage'); |
||
170 | } |
||
171 | View Code Duplication | if (file_put_contents($path, $json) === false) { |
|
|
|||
172 | throw new AssetFileStorageException('Failed to write package to provider-latest storage for package "' . $name . '"'); |
||
173 | } |
||
174 | View Code Duplication | if (file_put_contents($latestPath, $json) === false) { |
|
175 | throw new AssetFileStorageException('Failed to write file "latest.json" to provider-latest storage for package "' . $name . '"'); |
||
176 | } |
||
177 | $this->writePackagesJson($hash); |
||
178 | } finally { |
||
179 | $this->getLocker()->release(); |
||
180 | } |
||
181 | } else { |
||
182 | touch($latestPath); |
||
183 | } |
||
184 | |||
185 | return $hash; |
||
186 | } |
||
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.