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 |
||
| 40 | class TemplateLayout extends \OC_Template { |
||
| 41 | |||
| 42 | private static $versionHash = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \OCP\IConfig |
||
| 46 | */ |
||
| 47 | private $config; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $renderAs |
||
| 51 | * @param string $appId application id |
||
| 52 | */ |
||
| 53 | public function __construct( $renderAs, $appId = '' ) { |
||
| 54 | |||
| 55 | // yes - should be injected .... |
||
| 56 | $this->config = \OC::$server->getConfig(); |
||
| 57 | |||
| 58 | // Decide which page we show |
||
| 59 | if($renderAs == 'user') { |
||
| 60 | parent::__construct( 'core', 'layout.user' ); |
||
| 61 | if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) { |
||
| 62 | $this->assign('bodyid', 'body-settings'); |
||
| 63 | }else{ |
||
| 64 | $this->assign('bodyid', 'body-user'); |
||
| 65 | } |
||
| 66 | |||
| 67 | // Code integrity notification |
||
| 68 | $integrityChecker = \OC::$server->getIntegrityCodeChecker(); |
||
| 69 | if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) { |
||
| 70 | \OCP\Util::addScript('core', 'integritycheck-failed-notification'); |
||
| 71 | } |
||
| 72 | |||
| 73 | // Add navigation entry |
||
| 74 | $this->assign( 'application', ''); |
||
| 75 | $this->assign( 'appid', $appId ); |
||
| 76 | $navigation = \OC_App::getNavigation(); |
||
| 77 | $this->assign( 'navigation', $navigation); |
||
| 78 | $settingsNavigation = \OC_App::getSettingsNavigation(); |
||
| 79 | $this->assign( 'settingsnavigation', $settingsNavigation); |
||
| 80 | foreach($navigation as $entry) { |
||
| 81 | if ($entry['active']) { |
||
| 82 | $this->assign( 'application', $entry['name'] ); |
||
| 83 | break; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | foreach($settingsNavigation as $entry) { |
||
| 88 | if ($entry['active']) { |
||
| 89 | $this->assign( 'application', $entry['name'] ); |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | $userDisplayName = \OC_User::getDisplayName(); |
||
| 94 | $this->assign('user_displayname', $userDisplayName); |
||
| 95 | $this->assign('user_uid', \OC_User::getUser()); |
||
| 96 | $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true); |
||
| 97 | |||
| 98 | if (\OC_User::getUser() === false) { |
||
| 99 | $this->assign('userAvatarSet', false); |
||
| 100 | } else { |
||
| 101 | $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists()); |
||
| 102 | } |
||
| 103 | |||
| 104 | } else if ($renderAs == 'error') { |
||
| 105 | parent::__construct('core', 'layout.guest', '', false); |
||
| 106 | $this->assign('bodyid', 'body-login'); |
||
| 107 | } else if ($renderAs == 'guest') { |
||
| 108 | parent::__construct('core', 'layout.guest'); |
||
| 109 | $this->assign('bodyid', 'body-login'); |
||
| 110 | } else { |
||
| 111 | parent::__construct('core', 'layout.base'); |
||
| 112 | |||
| 113 | } |
||
| 114 | // Send the language to our layouts |
||
| 115 | $lang = \OC::$server->getL10NFactory()->findLanguage(); |
||
| 116 | if ($lang === 'sr@latin') { |
||
| 117 | $lang = 'sr'; |
||
| 118 | } |
||
| 119 | $this->assign('language', $lang); |
||
| 120 | |||
| 121 | if(\OC::$server->getSystemConfig()->getValue('installed', false)) { |
||
| 122 | if (empty(self::$versionHash)) { |
||
| 123 | $v = \OC_App::getAppVersions(); |
||
| 124 | $v['core'] = implode('.', \OCP\Util::getVersion()); |
||
| 125 | self::$versionHash = md5(implode(',', $v)); |
||
| 126 | } |
||
| 127 | } else { |
||
| 128 | self::$versionHash = md5('not installed'); |
||
| 129 | } |
||
| 130 | |||
| 131 | // Add the js files |
||
| 132 | $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts); |
||
| 133 | $this->assign('jsfiles', []); |
||
| 134 | if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') { |
||
| 135 | $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
||
| 136 | } |
||
| 137 | foreach($jsFiles as $info) { |
||
| 138 | $web = $info[1]; |
||
| 139 | $file = $info[2]; |
||
| 140 | $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
||
| 141 | } |
||
| 142 | |||
| 143 | // Add the css files |
||
| 144 | $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); |
||
| 145 | $this->assign('cssfiles', []); |
||
| 146 | $this->assign('printcssfiles', []); |
||
| 147 | foreach($cssFiles as $info) { |
||
| 148 | $web = $info[1]; |
||
| 149 | $file = $info[2]; |
||
| 150 | |||
| 151 | if (substr($file, -strlen('print.css')) === 'print.css') { |
||
| 152 | $this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
||
| 153 | } else { |
||
| 154 | $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param array $styles |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | View Code Duplication | static public function findStylesheetFiles($styles) { |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param array $scripts |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | View Code Duplication | static public function findJavascriptFiles($scripts) { |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Converts the absolute file path to a relative path from \OC::$SERVERROOT |
||
| 189 | * @param string $filePath Absolute path |
||
| 190 | * @return string Relative path |
||
| 191 | * @throws \Exception If $filePath is not under \OC::$SERVERROOT |
||
| 192 | */ |
||
| 193 | public static function convertToRelativePath($filePath) { |
||
| 201 | |||
| 202 | } |
||
| 203 |