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 | * JSCombiner constructor. |
||
| 49 | * |
||
| 50 | * @param IAppData $appData |
||
| 51 | * @param IURLGenerator $urlGenerator |
||
| 52 | * @param ICache $depsCache |
||
| 53 | */ |
||
| 54 | public function __construct(IAppData $appData, |
||
| 55 | IURLGenerator $urlGenerator, |
||
| 56 | ICache $depsCache, |
||
| 57 | SystemConfig $config) { |
||
| 58 | $this->appData = $appData; |
||
| 59 | $this->urlGenerator = $urlGenerator; |
||
| 60 | $this->depsCache = $depsCache; |
||
| 61 | $this->config = $config; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $root |
||
| 66 | * @param string $file |
||
| 67 | * @param string $app |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | public function process($root, $file, $app) { |
||
| 71 | if ($this->config->getValue('debug')) { |
||
| 72 | return false; |
||
| 73 | } |
||
| 74 | |||
| 75 | $path = explode('/', $root . '/' . $file); |
||
| 76 | |||
| 77 | $fileName = array_pop($path); |
||
| 78 | $path = implode('/', $path); |
||
| 79 | |||
| 80 | try { |
||
| 81 | $folder = $this->appData->getFolder($app); |
||
| 82 | } catch(NotFoundException $e) { |
||
| 83 | // creating css appdata folder |
||
| 84 | $folder = $this->appData->newFolder($app); |
||
| 85 | } |
||
| 86 | |||
| 87 | if($this->isCached($fileName, $folder)) { |
||
| 88 | return true; |
||
| 89 | } |
||
| 90 | return $this->cache($path, $fileName, $folder); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $fileName |
||
| 95 | * @param ISimpleFolder $folder |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | protected function isCached($fileName, ISimpleFolder $folder) { |
||
| 99 | $fileName = str_replace('.json', '.js', $fileName) . '.deps'; |
||
| 100 | try { |
||
| 101 | $deps = $this->depsCache->get($folder->getName() . '-' . $fileName); |
||
| 102 | View Code Duplication | if ($deps === null) { |
|
| 103 | $depFile = $folder->getFile($fileName); |
||
| 104 | $deps = $depFile->getContent(); |
||
| 105 | $this->depsCache->set($folder->getName() . '-' . $fileName, $deps); |
||
| 106 | } |
||
| 107 | $deps = json_decode($deps, true); |
||
| 108 | |||
| 109 | View Code Duplication | foreach ($deps as $file=>$mtime) { |
|
| 110 | if (!file_exists($file) || filemtime($file) > $mtime) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return true; |
||
| 116 | } catch(NotFoundException $e) { |
||
| 117 | return false; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $path |
||
| 123 | * @param string $fileName |
||
| 124 | * @param ISimpleFolder $folder |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | protected function cache($path, $fileName, ISimpleFolder $folder) { |
||
| 128 | $deps = []; |
||
| 129 | $fullPath = $path . '/' . $fileName; |
||
| 130 | $data = json_decode(file_get_contents($fullPath)); |
||
| 131 | $deps[$fullPath] = filemtime($fullPath); |
||
| 132 | |||
| 133 | $res = ''; |
||
| 134 | foreach ($data as $file) { |
||
| 135 | $filePath = $path . '/' . $file; |
||
| 136 | |||
| 137 | if (is_file($filePath)) { |
||
| 138 | $res .= file_get_contents($filePath); |
||
| 139 | $res .= PHP_EOL . PHP_EOL; |
||
| 140 | $deps[$filePath] = filemtime($filePath); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | $fileName = str_replace('.json', '.js', $fileName); |
||
| 145 | try { |
||
| 146 | $cachedfile = $folder->getFile($fileName); |
||
| 147 | } catch(NotFoundException $e) { |
||
| 148 | $cachedfile = $folder->newFile($fileName); |
||
| 149 | } |
||
| 150 | |||
| 151 | $depFileName = $fileName . '.deps'; |
||
| 152 | try { |
||
| 153 | $depFile = $folder->getFile($depFileName); |
||
| 154 | } catch (NotFoundException $e) { |
||
| 155 | $depFile = $folder->newFile($depFileName); |
||
| 156 | } |
||
| 157 | |||
| 158 | try { |
||
| 159 | $cachedfile->putContent($res); |
||
| 160 | $depFile->putContent(json_encode($deps)); |
||
| 161 | return true; |
||
| 162 | } catch (NotPermittedException $e) { |
||
| 163 | return false; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $appName |
||
| 169 | * @param string $fileName |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | View Code Duplication | public function getCachedJS($appName, $fileName) { |
|
|
|
|||
| 173 | $tmpfileLoc = explode('/', $fileName); |
||
| 174 | $fileName = array_pop($tmpfileLoc); |
||
| 175 | $fileName = str_replace('.json', '.js', $fileName); |
||
| 176 | |||
| 177 | return substr($this->urlGenerator->linkToRoute('core.Js.getJs', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $root |
||
| 182 | * @param string $file |
||
| 183 | * @return string[] |
||
| 184 | */ |
||
| 185 | public function getContent($root, $file) { |
||
| 199 | } |
||
| 200 |
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.