| Total Complexity | 54 |
| Total Lines | 301 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TTheme often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TTheme, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class TTheme extends \Prado\TApplicationComponent implements ITheme |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * prefix for cache variable name used to store parsed themes |
||
| 48 | */ |
||
| 49 | const THEME_CACHE_PREFIX = 'prado:theme:'; |
||
| 50 | /** |
||
| 51 | * Extension name of skin files |
||
| 52 | */ |
||
| 53 | const SKIN_FILE_EXT = '.skin'; |
||
| 54 | /** |
||
| 55 | * @var string theme path |
||
| 56 | */ |
||
| 57 | private $_themePath; |
||
| 58 | /** |
||
| 59 | * @var string theme url |
||
| 60 | */ |
||
| 61 | private $_themeUrl; |
||
| 62 | /** |
||
| 63 | * @var array list of skins for the theme |
||
| 64 | */ |
||
| 65 | private $_skins; |
||
| 66 | /** |
||
| 67 | * @var string theme name |
||
| 68 | */ |
||
| 69 | private $_name = ''; |
||
| 70 | /** |
||
| 71 | * @var array list of css files |
||
| 72 | */ |
||
| 73 | private $_cssFiles = []; |
||
| 74 | /** |
||
| 75 | * @var array list of js files |
||
| 76 | */ |
||
| 77 | private $_jsFiles = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor. |
||
| 81 | * @param string theme path |
||
|
|
|||
| 82 | * @param string theme URL |
||
| 83 | * @throws TConfigurationException if theme path does not exist or any parsing error of the skin files |
||
| 84 | */ |
||
| 85 | public function __construct($themePath, $themeUrl) |
||
| 86 | { |
||
| 87 | $this->_themeUrl = $themeUrl; |
||
| 88 | $this->_themePath = realpath($themePath); |
||
| 89 | $this->_name = basename($themePath); |
||
| 90 | $cacheValid = false; |
||
| 91 | // TODO: the following needs to be cleaned up (Qiang) |
||
| 92 | if(($cache = $this->getApplication()->getCache()) !== null) |
||
| 93 | { |
||
| 94 | $array = $cache->get(self::THEME_CACHE_PREFIX . $themePath); |
||
| 95 | if(is_array($array)) |
||
| 96 | { |
||
| 97 | list($skins, $cssFiles, $jsFiles, $timestamp) = $array; |
||
| 98 | if($this->getApplication()->getMode() !== TApplicationMode::Performance) |
||
| 99 | { |
||
| 100 | if(($dir = opendir($themePath)) === false) |
||
| 101 | throw new TIOException('theme_path_inexistent', $themePath); |
||
| 102 | $cacheValid = true; |
||
| 103 | while(($file = readdir($dir)) !== false) |
||
| 104 | { |
||
| 105 | if($file === '.' || $file === '..') |
||
| 106 | continue; |
||
| 107 | elseif(basename($file, '.css') !== $file) |
||
| 108 | $this->_cssFiles[] = $themeUrl . '/' . $file; |
||
| 109 | elseif(basename($file, '.js') !== $file) |
||
| 110 | $this->_jsFiles[] = $themeUrl . '/' . $file; |
||
| 111 | elseif(basename($file, self::SKIN_FILE_EXT) !== $file && filemtime($themePath . DIRECTORY_SEPARATOR . $file) > $timestamp) |
||
| 112 | { |
||
| 113 | $cacheValid = false; |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | closedir($dir); |
||
| 118 | if($cacheValid) |
||
| 119 | $this->_skins = $skins; |
||
| 120 | } |
||
| 121 | else |
||
| 122 | { |
||
| 123 | $cacheValid = true; |
||
| 124 | $this->_cssFiles = $cssFiles; |
||
| 125 | $this->_jsFiles = $jsFiles; |
||
| 126 | $this->_skins = $skins; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | if(!$cacheValid) |
||
| 131 | { |
||
| 132 | $this->_cssFiles = []; |
||
| 133 | $this->_jsFiles = []; |
||
| 134 | $this->_skins = []; |
||
| 135 | if(($dir = opendir($themePath)) === false) |
||
| 136 | throw new TIOException('theme_path_inexistent', $themePath); |
||
| 137 | while(($file = readdir($dir)) !== false) |
||
| 138 | { |
||
| 139 | if($file === '.' || $file === '..') |
||
| 140 | continue; |
||
| 141 | elseif(basename($file, '.css') !== $file) |
||
| 142 | $this->_cssFiles[] = $themeUrl . '/' . $file; |
||
| 143 | elseif(basename($file, '.js') !== $file) |
||
| 144 | $this->_jsFiles[] = $themeUrl . '/' . $file; |
||
| 145 | elseif(basename($file, self::SKIN_FILE_EXT) !== $file) |
||
| 146 | { |
||
| 147 | $template = new TTemplate(file_get_contents($themePath . '/' . $file), $themePath, $themePath . '/' . $file); |
||
| 148 | foreach($template->getItems() as $skin) |
||
| 149 | { |
||
| 150 | if(!isset($skin[2])) // a text string, ignored |
||
| 151 | continue; |
||
| 152 | elseif($skin[0] !== -1) |
||
| 153 | throw new TConfigurationException('theme_control_nested', $skin[1], dirname($themePath)); |
||
| 154 | $type = $skin[1]; |
||
| 155 | $id = isset($skin[2]['skinid'])?$skin[2]['skinid']:0; |
||
| 156 | unset($skin[2]['skinid']); |
||
| 157 | if(isset($this->_skins[$type][$id])) |
||
| 158 | throw new TConfigurationException('theme_skinid_duplicated', $type, $id, dirname($themePath)); |
||
| 159 | /* |
||
| 160 | foreach($skin[2] as $name=>$value) |
||
| 161 | { |
||
| 162 | if(is_array($value) && ($value[0]===TTemplate::CONFIG_DATABIND || $value[0]===TTemplate::CONFIG_PARAMETER)) |
||
| 163 | throw new TConfigurationException('theme_databind_forbidden',dirname($themePath),$type,$id); |
||
| 164 | } |
||
| 165 | */ |
||
| 166 | $this->_skins[$type][$id] = $skin[2]; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | closedir($dir); |
||
| 171 | sort($this->_cssFiles); |
||
| 172 | sort($this->_jsFiles); |
||
| 173 | if($cache !== null) |
||
| 174 | $cache->set(self::THEME_CACHE_PREFIX . $themePath, [$this->_skins,$this->_cssFiles,$this->_jsFiles,time()]); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return string theme name |
||
| 180 | */ |
||
| 181 | public function getName() |
||
| 182 | { |
||
| 183 | return $this->_name; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param string theme name |
||
| 188 | */ |
||
| 189 | protected function setName($value) |
||
| 190 | { |
||
| 191 | $this->_name = $value; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return string the URL to the theme folder (without ending slash) |
||
| 196 | */ |
||
| 197 | public function getBaseUrl() |
||
| 198 | { |
||
| 199 | return $this->_themeUrl; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string the URL to the theme folder |
||
| 204 | */ |
||
| 205 | protected function setBaseUrl($value) |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return string the file path to the theme folder |
||
| 212 | */ |
||
| 213 | public function getBasePath() |
||
| 214 | { |
||
| 215 | return $this->_themePath; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string tthe file path to the theme folder |
||
| 220 | */ |
||
| 221 | protected function setBasePath($value) |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return array list of skins for the theme |
||
| 228 | */ |
||
| 229 | public function getSkins() |
||
| 230 | { |
||
| 231 | return $this->_skins; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param array list of skins for the theme |
||
| 236 | */ |
||
| 237 | protected function setSkins($value) |
||
| 238 | { |
||
| 239 | $this->_skins = $value; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Applies the theme to a particular control. |
||
| 244 | * The control's class name and SkinID value will be used to |
||
| 245 | * identify which skin to be applied. If the control's SkinID is empty, |
||
| 246 | * the default skin will be applied. |
||
| 247 | * @param TControl the control to be applied with a skin |
||
| 248 | * @return boolean if a skin is successfully applied |
||
| 249 | * @throws TConfigurationException if any error happened during the skin application |
||
| 250 | */ |
||
| 251 | public function applySkin($control) |
||
| 252 | { |
||
| 253 | $type = get_class($control); |
||
| 254 | if(($id = $control->getSkinID()) === '') |
||
| 255 | $id = 0; |
||
| 256 | if(isset($this->_skins[$type][$id])) |
||
| 257 | { |
||
| 258 | foreach($this->_skins[$type][$id] as $name => $value) |
||
| 259 | { |
||
| 260 | Prado::trace("Applying skin $name to $type", 'Prado\Web\UI\TThemeManager'); |
||
| 261 | if(is_array($value)) |
||
| 262 | { |
||
| 263 | switch($value[0]) |
||
| 264 | { |
||
| 265 | case TTemplate::CONFIG_EXPRESSION: |
||
| 266 | $value = $this->evaluateExpression($value[1]); |
||
| 267 | break; |
||
| 268 | case TTemplate::CONFIG_ASSET: |
||
| 269 | $value = $this->_themeUrl . '/' . ltrim($value[1], '/'); |
||
| 270 | break; |
||
| 271 | case TTemplate::CONFIG_DATABIND: |
||
| 272 | $control->bindProperty($name, $value[1]); |
||
| 273 | break; |
||
| 274 | case TTemplate::CONFIG_PARAMETER: |
||
| 275 | $control->setSubProperty($name, $this->getApplication()->getParameters()->itemAt($value[1])); |
||
| 276 | break; |
||
| 277 | case TTemplate::CONFIG_TEMPLATE: |
||
| 278 | $control->setSubProperty($name, $value[1]); |
||
| 279 | break; |
||
| 280 | case TTemplate::CONFIG_LOCALIZATION: |
||
| 281 | $control->setSubProperty($name, Prado::localize($value[1])); |
||
| 282 | break; |
||
| 283 | default: |
||
| 284 | throw new TConfigurationException('theme_tag_unexpected', $name, $value[0]); |
||
| 285 | break; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | if(!is_array($value)) |
||
| 289 | { |
||
| 290 | if(strpos($name, '.') === false) // is simple property or custom attribute |
||
| 291 | { |
||
| 292 | if($control->hasProperty($name)) |
||
| 293 | { |
||
| 294 | if($control->canSetProperty($name)) |
||
| 295 | { |
||
| 296 | $setter = 'set' . $name; |
||
| 297 | $control->$setter($value); |
||
| 298 | } |
||
| 299 | else |
||
| 300 | throw new TConfigurationException('theme_property_readonly', $type, $name); |
||
| 301 | } |
||
| 302 | else |
||
| 303 | throw new TConfigurationException('theme_property_undefined', $type, $name); |
||
| 304 | } |
||
| 305 | else // complex property |
||
| 306 | $control->setSubProperty($name, $value); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | return true; |
||
| 310 | } |
||
| 311 | else |
||
| 312 | return false; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return array list of CSS files (URL) in the theme |
||
| 317 | */ |
||
| 318 | public function getStyleSheetFiles() |
||
| 319 | { |
||
| 320 | return $this->_cssFiles; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param array list of CSS files (URL) in the theme |
||
| 325 | */ |
||
| 326 | protected function setStyleSheetFiles($value) |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return array list of Javascript files (URL) in the theme |
||
| 333 | */ |
||
| 334 | public function getJavaScriptFiles() |
||
| 335 | { |
||
| 336 | return $this->_jsFiles; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param array list of Javascript files (URL) in the theme |
||
| 341 | */ |
||
| 342 | protected function setJavaScriptFiles($value) |
||
| 345 | } |
||
| 346 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths