| Total Complexity | 41 |
| Total Lines | 227 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Module extends SplDirectoryInfo |
||
| 28 | { |
||
| 29 | private $type = 'MODULE'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Module Namespace |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $namespace; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Module Segments |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $segments; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Module Parent Segments |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $parentSegments; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Module Properties |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $properties = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Module Config |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $config = []; |
||
| 65 | |||
| 66 | public function __construct($dir) |
||
| 67 | { |
||
| 68 | parent::__construct($dir); |
||
| 69 | |||
| 70 | $this->namespace = prepare_namespace(str_replace(PATH_ROOT, '', $dir), false); |
||
|
|
|||
| 71 | } |
||
| 72 | |||
| 73 | public function getSegments($returnArray = true) |
||
| 74 | { |
||
| 75 | if ($returnArray) { |
||
| 76 | return explode('/', $this->segments); |
||
| 77 | } |
||
| 78 | |||
| 79 | return $this->segments; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function setSegments($segments) |
||
| 83 | { |
||
| 84 | $this->segments = is_array($segments) ? implode('/', $segments) : $segments; |
||
| 85 | |||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function getParentSegments($returnArray = true) |
||
| 96 | } |
||
| 97 | |||
| 98 | public function setParentSegments($parentSegments) |
||
| 99 | { |
||
| 100 | $this->parentSegments = is_array($parentSegments) ? implode('/', $parentSegments) : $parentSegments; |
||
| 101 | |||
| 102 | return $this; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getParameter() |
||
| 108 | } |
||
| 109 | |||
| 110 | public function getCode() |
||
| 111 | { |
||
| 112 | return strtoupper(substr(md5($this->getDirName()), 2, 7)); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function getChecksum() |
||
| 116 | { |
||
| 117 | return md5($this->getMTime()); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function getProperties() |
||
| 121 | { |
||
| 122 | return new SplArrayObject($this->properties); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function setProperties(array $properties) |
||
| 126 | { |
||
| 127 | $this->properties = $properties; |
||
| 128 | |||
| 129 | return $this; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function getConfig() |
||
| 133 | { |
||
| 134 | return new SplArrayObject($this->config); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function setConfig(array $config) |
||
| 138 | { |
||
| 139 | $this->config = $config; |
||
| 140 | |||
| 141 | return $this; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getNamespace() |
||
| 147 | } |
||
| 148 | |||
| 149 | public function setNamespace($namespace) |
||
| 150 | { |
||
| 151 | $this->namespace = trim($namespace, '\\') . '\\'; |
||
| 152 | |||
| 153 | return $this; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getDefaultControllerClassName() |
||
| 157 | { |
||
| 158 | return $this->getNamespace() . 'Controllers\\' . $this->getDirName(); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function getThemes() |
||
| 162 | { |
||
| 163 | $directory = new SplDirectoryInfo($this->getResourcesDir() . 'themes' . DIRECTORY_SEPARATOR); |
||
| 164 | |||
| 165 | $themes = []; |
||
| 166 | foreach ($directory->getTree() as $themeName => $themeTree) { |
||
| 167 | if (($theme = $this->getTheme($themeName)) instanceof Theme) { |
||
| 168 | $themes[ $themeName ] = $theme; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | return $themes; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function getPublicDir() |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getResourcesDir() |
||
| 181 | { |
||
| 182 | return PATH_RESOURCES . strtolower(str_replace(PATH_APP, '', $this->getRealPath())); |
||
| 183 | } |
||
| 184 | |||
| 185 | public function getTheme($theme, $failover = true) |
||
| 186 | { |
||
| 187 | $theme = dash($theme); |
||
| 188 | |||
| 189 | if ($failover === false) { |
||
| 190 | if (is_dir($themePath = $this->getResourcesDir() . 'themes' . DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR)) { |
||
| 191 | $themeObject = new Theme($themePath); |
||
| 192 | |||
| 193 | if ($themeObject->isValid()) { |
||
| 194 | return $themeObject; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } else { |
||
| 198 | foreach (modules() as $module) { |
||
| 199 | if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
||
| 200 | continue; |
||
| 201 | } elseif ($themeObject = $module->getTheme($theme, false)) { |
||
| 202 | return $themeObject; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | return false; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function getType() |
||
| 213 | } |
||
| 214 | |||
| 215 | public function setType($type) |
||
| 216 | { |
||
| 217 | $this->type = strtoupper($type); |
||
| 218 | |||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function getDir($dirName, $psrDir = false) |
||
| 223 | { |
||
| 224 | $dirName = $psrDir === true ? prepare_class_name($dirName) : $dirName; |
||
| 225 | $dirName = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $dirName); |
||
| 226 | |||
| 227 | if (is_dir($dirPath = $this->getRealPath() . $dirName)) { |
||
| 228 | return $dirPath . DIRECTORY_SEPARATOR; |
||
| 229 | } |
||
| 230 | |||
| 231 | return false; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function hasTheme($theme) |
||
| 235 | { |
||
| 236 | if (is_dir($this->getThemesPath() . $theme)) { |
||
| 237 | return true; |
||
| 238 | } |
||
| 239 | |||
| 240 | return false; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getThemesPath() |
||
| 244 | { |
||
| 245 | return PATH_RESOURCES . DIRECTORY_SEPARATOR . $this->getParameter() . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR; |
||
| 246 | } |
||
| 247 | |||
| 248 | public function loadModel() |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |