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 |
||
| 36 | class JSCombiner { |
||
| 37 | |||
| 38 | /** @var IAppData */ |
||
| 39 | protected $appData; |
||
| 40 | |||
| 41 | /** @var IURLGenerator */ |
||
| 42 | protected $urlGenerator; |
||
| 43 | |||
| 44 | /** @var ICache */ |
||
| 45 | protected $depsCache; |
||
| 46 | |||
| 47 | /** @var SystemConfig */ |
||
| 48 | protected $config; |
||
| 49 | |||
| 50 | /** @var ILogger */ |
||
| 51 | protected $logger; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param IAppData $appData |
||
| 55 | * @param IURLGenerator $urlGenerator |
||
| 56 | * @param ICache $depsCache |
||
| 57 | * @param SystemConfig $config |
||
| 58 | * @param ILogger $logger |
||
| 59 | */ |
||
| 60 | public function __construct(IAppData $appData, |
||
| 61 | IURLGenerator $urlGenerator, |
||
| 62 | ICache $depsCache, |
||
| 63 | SystemConfig $config, |
||
| 64 | ILogger $logger) { |
||
| 65 | $this->appData = $appData; |
||
| 66 | $this->urlGenerator = $urlGenerator; |
||
| 67 | $this->depsCache = $depsCache; |
||
| 68 | $this->config = $config; |
||
| 69 | $this->logger = $logger; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $root |
||
| 74 | * @param string $file |
||
| 75 | * @param string $app |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function process($root, $file, $app) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $fileName |
||
| 103 | * @param ISimpleFolder $folder |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | protected function isCached($fileName, ISimpleFolder $folder) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string $path |
||
| 140 | * @param string $fileName |
||
| 141 | * @param ISimpleFolder $folder |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | protected function cache($path, $fileName, ISimpleFolder $folder) { |
||
| 145 | $deps = []; |
||
| 146 | $fullPath = $path . '/' . $fileName; |
||
| 147 | $data = json_decode(file_get_contents($fullPath)); |
||
| 148 | $deps[$fullPath] = filemtime($fullPath); |
||
| 149 | |||
| 150 | $res = ''; |
||
| 151 | foreach ($data as $file) { |
||
| 152 | $filePath = $path . '/' . $file; |
||
| 153 | |||
| 154 | if (is_file($filePath)) { |
||
| 155 | $res .= file_get_contents($filePath); |
||
| 156 | $res .= PHP_EOL . PHP_EOL; |
||
| 157 | $deps[$filePath] = filemtime($filePath); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | $fileName = str_replace('.json', '.js', $fileName); |
||
| 162 | try { |
||
| 163 | $cachedfile = $folder->getFile($fileName); |
||
| 164 | } catch(NotFoundException $e) { |
||
| 165 | $cachedfile = $folder->newFile($fileName); |
||
| 166 | } |
||
| 167 | |||
| 168 | $depFileName = $fileName . '.deps'; |
||
| 169 | try { |
||
| 170 | $depFile = $folder->getFile($depFileName); |
||
| 171 | } catch (NotFoundException $e) { |
||
| 172 | $depFile = $folder->newFile($depFileName); |
||
| 173 | } |
||
| 174 | |||
| 175 | try { |
||
| 176 | $gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz |
||
| 177 | } catch (NotFoundException $e) { |
||
| 178 | $gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz |
||
| 179 | } |
||
| 180 | |||
| 181 | try { |
||
| 182 | $cachedfile->putContent($res); |
||
| 183 | $deps = json_encode($deps); |
||
| 184 | $depFile->putContent($deps); |
||
| 185 | $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
||
| 186 | $gzipFile->putContent(gzencode($res, 9)); |
||
| 187 | $this->logger->debug('JSCombiner: successfully cached: ' . $fileName); |
||
| 188 | return true; |
||
| 189 | } catch (NotPermittedException $e) { |
||
| 190 | $this->logger->error('JSCombiner: unable to cache: ' . $fileName); |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $appName |
||
| 197 | * @param string $fileName |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | View Code Duplication | public function getCachedJS($appName, $fileName) { |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $root |
||
| 210 | * @param string $file |
||
| 211 | * @return string[] |
||
| 212 | */ |
||
| 213 | public function getContent($root, $file) { |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Clear cache with combined javascript files |
||
| 235 | * |
||
| 236 | * @throws NotFoundException |
||
| 237 | */ |
||
| 238 | public function resetCache() { |
||
| 247 | } |
||
| 248 |