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 |
||
21 | { |
||
22 | protected $_path; |
||
23 | protected $_locker; |
||
24 | |||
25 | 1 | public function init() |
|
29 | |||
30 | protected function getLocker() |
||
38 | |||
39 | public function getNextID() |
||
50 | |||
51 | protected function readLastId() |
||
57 | |||
58 | protected function writeLastId($value) |
||
62 | |||
63 | protected function getLastIDPath() |
||
67 | |||
68 | public function writePackage(AssetPackage $package) |
||
92 | |||
93 | /** |
||
94 | * Reads the $package information from the storage. |
||
95 | * |
||
96 | * @param AssetPackage $package |
||
97 | * @return array|null array of two elements: |
||
98 | * 0 - string sha256 hash of the package |
||
99 | * 1 - array[] releases |
||
100 | * |
||
101 | * Returns null, when package does not exist. |
||
102 | */ |
||
103 | public function readPackage(AssetPackage $package) |
||
104 | { |
||
105 | $name = $package->getNormalName(); |
||
106 | $path = $this->buildHashedPath($name); |
||
107 | if (!file_exists($path)) { |
||
108 | return null; |
||
109 | } |
||
110 | $json = file_get_contents($path); |
||
111 | $updateTime = filemtime($path); |
||
112 | $hash = hash('sha256', $json); |
||
113 | $data = Json::decode($json); |
||
114 | $releases = isset($data['packages'][$name]) ? $data['packages'][$name] : []; |
||
115 | |||
116 | return compact('hash', 'releases', 'updateTime'); |
||
117 | } |
||
118 | |||
119 | public function buildPath() |
||
126 | |||
127 | public function buildHashedPath($name, $hash = 'latest') |
||
128 | { |
||
129 | return $this->buildPath('p', $name, $hash . '.json'); |
||
130 | } |
||
131 | |||
132 | protected function writeProviderLatest($name, $hash) |
||
133 | { |
||
134 | $latest_path = $this->buildHashedPath('provider-latest'); |
||
135 | if (file_exists($latest_path)) { |
||
136 | $data = Json::decode(file_get_contents($latest_path) ?: '[]'); |
||
137 | } |
||
138 | if (!isset($data) || !is_array($data)) { |
||
139 | $data = []; |
||
140 | } |
||
141 | if (!isset($data['providers'])) { |
||
142 | $data['providers'] = []; |
||
143 | } |
||
144 | $data['providers'][$name] = ['sha256' => $hash]; |
||
145 | $json = Json::encode($data); |
||
146 | $hash = hash('sha256', $json); |
||
147 | $path = $this->buildHashedPath('provider-latest', $hash); |
||
148 | View Code Duplication | if (!file_exists($path)) { |
|
149 | $this->getLocker()->lock(); |
||
150 | { |
||
151 | static::mkdir(dirname($path)); |
||
152 | file_put_contents($path, $json); |
||
153 | file_put_contents($latest_path, $json); |
||
154 | $this->writePackagesJson($hash); |
||
155 | } |
||
156 | $this->getLocker()->release(); |
||
157 | } |
||
158 | |||
159 | return $hash; |
||
160 | } |
||
161 | |||
162 | protected function writePackagesJson($hash) |
||
178 | |||
179 | public static function mkdir($dir) |
||
185 | |||
186 | public function readJson($path) |
||
190 | |||
191 | protected function readPackagesJson() |
||
197 | |||
198 | protected function readProvider($path) |
||
204 | |||
205 | public function listPackages() |
||
216 | } |
||
217 |
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.