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 |
||
| 47 | class TemplateLayout extends \OC_Template { |
||
| 48 | |||
| 49 | private static $versionHash = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \OCP\IConfig |
||
| 53 | */ |
||
| 54 | private $config; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $renderAs |
||
| 58 | * @param string $appId application id |
||
| 59 | */ |
||
| 60 | public function __construct( $renderAs, $appId = '' ) { |
||
| 61 | |||
| 62 | // yes - should be injected .... |
||
| 63 | $this->config = \OC::$server->getConfig(); |
||
| 64 | |||
| 65 | // Decide which page we show |
||
| 66 | if($renderAs == 'user') { |
||
| 67 | parent::__construct( 'core', 'layout.user' ); |
||
| 68 | if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) { |
||
| 69 | $this->assign('bodyid', 'body-settings'); |
||
| 70 | }else{ |
||
| 71 | $this->assign('bodyid', 'body-user'); |
||
| 72 | } |
||
| 73 | |||
| 74 | // Code integrity notification |
||
| 75 | $integrityChecker = \OC::$server->getIntegrityCodeChecker(); |
||
| 76 | if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) { |
||
| 77 | \OCP\Util::addScript('core', 'integritycheck-failed-notification'); |
||
| 78 | } |
||
| 79 | |||
| 80 | // Add navigation entry |
||
| 81 | $this->assign( 'application', ''); |
||
| 82 | $this->assign( 'appid', $appId ); |
||
| 83 | $navigation = \OC_App::getNavigation(); |
||
| 84 | $this->assign( 'navigation', $navigation); |
||
| 85 | $settingsNavigation = \OC_App::getSettingsNavigation(); |
||
| 86 | $this->assign( 'settingsnavigation', $settingsNavigation); |
||
| 87 | foreach($navigation as $entry) { |
||
| 88 | if ($entry['active']) { |
||
| 89 | $this->assign( 'application', $entry['name'] ); |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | foreach($settingsNavigation as $entry) { |
||
| 95 | if ($entry['active']) { |
||
| 96 | $this->assign( 'application', $entry['name'] ); |
||
| 97 | break; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | $userDisplayName = \OC_User::getDisplayName(); |
||
| 101 | $appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0; |
||
| 102 | if ($appsMgmtActive) { |
||
| 103 | $l = \OC::$server->getL10N('lib'); |
||
| 104 | $this->assign('application', $l->t('Apps')); |
||
| 105 | } |
||
| 106 | $this->assign('user_displayname', $userDisplayName); |
||
| 107 | $this->assign('user_uid', \OC_User::getUser()); |
||
| 108 | $this->assign('appsmanagement_active', $appsMgmtActive); |
||
| 109 | $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true); |
||
| 110 | |||
| 111 | if (\OC_User::getUser() === false) { |
||
| 112 | $this->assign('userAvatarSet', false); |
||
| 113 | } else { |
||
| 114 | $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists()); |
||
| 115 | } |
||
| 116 | |||
| 117 | } else if ($renderAs == 'error') { |
||
| 118 | parent::__construct('core', 'layout.guest', '', false); |
||
| 119 | $this->assign('bodyid', 'body-login'); |
||
| 120 | } else if ($renderAs == 'guest') { |
||
| 121 | parent::__construct('core', 'layout.guest'); |
||
| 122 | $this->assign('bodyid', 'body-login'); |
||
| 123 | } else { |
||
| 124 | parent::__construct('core', 'layout.base'); |
||
| 125 | |||
| 126 | } |
||
| 127 | // Send the language to our layouts |
||
| 128 | $this->assign('language', \OC_L10N::findLanguage()); |
||
| 129 | |||
| 130 | if(\OC::$server->getSystemConfig()->getValue('installed', false)) { |
||
| 131 | if (empty(self::$versionHash)) { |
||
| 132 | $v = \OC_App::getAppVersions(); |
||
| 133 | $v['core'] = implode('.', \OCP\Util::getVersion()); |
||
| 134 | self::$versionHash = md5(implode(',', $v)); |
||
| 135 | } |
||
| 136 | } else { |
||
| 137 | self::$versionHash = md5('not installed'); |
||
| 138 | } |
||
| 139 | |||
| 140 | $useAssetPipeline = self::isAssetPipelineEnabled(); |
||
| 141 | if ($useAssetPipeline) { |
||
| 142 | $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
||
| 143 | $this->generateAssets(); |
||
| 144 | } else { |
||
| 145 | // Add the js files |
||
| 146 | $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts); |
||
| 147 | $this->assign('jsfiles', array()); |
||
| 148 | if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') { |
||
| 149 | $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
||
| 150 | } |
||
| 151 | foreach($jsFiles as $info) { |
||
| 152 | $web = $info[1]; |
||
| 153 | $file = $info[2]; |
||
| 154 | $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
||
| 155 | } |
||
| 156 | |||
| 157 | // Add the css files |
||
| 158 | $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); |
||
| 159 | $this->assign('cssfiles', array()); |
||
| 160 | $this->assign('printcssfiles', []); |
||
| 161 | foreach($cssFiles as $info) { |
||
| 162 | $web = $info[1]; |
||
| 163 | $file = $info[2]; |
||
| 164 | |||
| 165 | if (substr($file, -strlen('print.css')) === 'print.css') { |
||
| 166 | $this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
||
| 167 | } else { |
||
| 168 | $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param array $styles |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | View Code Duplication | static public function findStylesheetFiles($styles) { |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param array $scripts |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | View Code Duplication | static public function findJavascriptFiles($scripts) { |
|
| 207 | |||
| 208 | public function generateAssets() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * generates a single css asset file from an array of css files if at least one of them has changed |
||
| 255 | * otherwise it just returns the path to the old asset file |
||
| 256 | * @param $files |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | private function generateCssAsset($files) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Converts the absolute file path to a relative path from \OC::$SERVERROOT |
||
| 295 | * @param string $filePath Absolute path |
||
| 296 | * @return string Relative path |
||
| 297 | * @throws \Exception If $filePath is not under \OC::$SERVERROOT |
||
| 298 | */ |
||
| 299 | public static function convertToRelativePath($filePath) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param array $files |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | |||
| 313 | private static function hashFileNames($files) { |
||
| 327 | } |
||
| 328 |