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 |
||
33 | class JSCombiner { |
||
34 | |||
35 | /** @var IAppData */ |
||
36 | protected $appData; |
||
37 | |||
38 | /** @var IURLGenerator */ |
||
39 | protected $urlGenerator; |
||
40 | |||
41 | /** @var ICache */ |
||
42 | protected $depsCache; |
||
43 | |||
44 | /** @var SystemConfig */ |
||
45 | protected $config; |
||
46 | |||
47 | /** |
||
48 | * @param IAppData $appData |
||
49 | * @param IURLGenerator $urlGenerator |
||
50 | * @param ICache $depsCache |
||
51 | * @param SystemConfig $config |
||
52 | */ |
||
53 | public function __construct(IAppData $appData, |
||
62 | |||
63 | /** |
||
64 | * @param string $root |
||
65 | * @param string $file |
||
66 | * @param string $app |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function process($root, $file, $app) { |
||
91 | |||
92 | /** |
||
93 | * @param string $fileName |
||
94 | * @param ISimpleFolder $folder |
||
95 | * @return bool |
||
96 | */ |
||
97 | protected function isCached($fileName, ISimpleFolder $folder) { |
||
98 | $fileName = str_replace('.json', '.js', $fileName) . '.deps'; |
||
99 | try { |
||
100 | $deps = $this->depsCache->get($folder->getName() . '-' . $fileName); |
||
101 | if ($deps === null || $deps === '') { |
||
102 | $depFile = $folder->getFile($fileName); |
||
103 | $deps = $depFile->getContent(); |
||
104 | } |
||
105 | $deps = json_decode($deps, true); |
||
106 | |||
107 | View Code Duplication | foreach ($deps as $file=>$mtime) { |
|
108 | if (!file_exists($file) || filemtime($file) > $mtime) { |
||
109 | return false; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | return true; |
||
114 | } catch(NotFoundException $e) { |
||
115 | return false; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param string $path |
||
121 | * @param string $fileName |
||
122 | * @param ISimpleFolder $folder |
||
123 | * @return bool |
||
124 | */ |
||
125 | protected function cache($path, $fileName, ISimpleFolder $folder) { |
||
126 | $deps = []; |
||
127 | $fullPath = $path . '/' . $fileName; |
||
128 | $data = json_decode(file_get_contents($fullPath)); |
||
129 | $deps[$fullPath] = filemtime($fullPath); |
||
130 | |||
131 | $res = ''; |
||
132 | foreach ($data as $file) { |
||
133 | $filePath = $path . '/' . $file; |
||
134 | |||
135 | if (is_file($filePath)) { |
||
136 | $res .= file_get_contents($filePath); |
||
137 | $res .= PHP_EOL . PHP_EOL; |
||
138 | $deps[$filePath] = filemtime($filePath); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | $fileName = str_replace('.json', '.js', $fileName); |
||
143 | try { |
||
144 | $cachedfile = $folder->getFile($fileName); |
||
145 | } catch(NotFoundException $e) { |
||
146 | $cachedfile = $folder->newFile($fileName); |
||
147 | } |
||
148 | |||
149 | $depFileName = $fileName . '.deps'; |
||
150 | try { |
||
151 | $depFile = $folder->getFile($depFileName); |
||
152 | } catch (NotFoundException $e) { |
||
153 | $depFile = $folder->newFile($depFileName); |
||
154 | } |
||
155 | |||
156 | try { |
||
157 | $gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz |
||
158 | } catch (NotFoundException $e) { |
||
159 | $gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz |
||
160 | } |
||
161 | |||
162 | try { |
||
163 | $cachedfile->putContent($res); |
||
164 | $deps = json_encode($deps); |
||
165 | $depFile->putContent($deps); |
||
166 | $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
||
167 | $gzipFile->putContent(gzencode($res, 9)); |
||
168 | |||
169 | return true; |
||
170 | } catch (NotPermittedException $e) { |
||
171 | return false; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param string $appName |
||
177 | * @param string $fileName |
||
178 | * @return string |
||
179 | */ |
||
180 | View Code Duplication | public function getCachedJS($appName, $fileName) { |
|
187 | |||
188 | /** |
||
189 | * @param string $root |
||
190 | * @param string $file |
||
191 | * @return string[] |
||
192 | */ |
||
193 | public function getContent($root, $file) { |
||
211 | } |
||
212 |
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.