| Total Complexity | 40 |
| Total Lines | 244 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Template 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 Template, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Template extends \Inji\InjiObject { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * App for template |
||
| 20 | * |
||
| 21 | * @var \App |
||
| 22 | */ |
||
| 23 | public $app = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Template name |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $name = 'default'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Template path |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | public $path = ''; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Template config path |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $configPath = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Template config |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | public $config = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Current template page for rendering |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $page = 'index'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Current template page path for rendering |
||
| 62 | * |
||
| 63 | * @var string|boolean |
||
| 64 | */ |
||
| 65 | public $pagePath = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Template module for content path finder |
||
| 69 | * |
||
| 70 | * @var \Module |
||
| 71 | */ |
||
| 72 | public $module = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Current content file for rendering |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | public $content = ''; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Current content file path for rendering |
||
| 83 | * |
||
| 84 | * @var string|boolean |
||
| 85 | */ |
||
| 86 | public $contentPath = ''; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Setup template object |
||
| 90 | * |
||
| 91 | * @param array $params |
||
| 92 | */ |
||
| 93 | public function __construct($params = []) { |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Load template config |
||
| 106 | * |
||
| 107 | * @param string $configPath |
||
| 108 | */ |
||
| 109 | public function loadConfig($configPath = '') { |
||
| 110 | if (!$configPath) { |
||
| 111 | $configPath = $this->path . '/config.php'; |
||
| 112 | } |
||
| 113 | $this->configPath = $configPath; |
||
| 114 | $this->config = \Inji\Config::custom($this->configPath); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set params for template |
||
| 119 | * |
||
| 120 | * @param array $params |
||
| 121 | */ |
||
| 122 | public function setParams($params) { |
||
| 123 | foreach ($params as $param => $value) { |
||
| 124 | $this->$param = $value; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set page and page path for template |
||
| 130 | * |
||
| 131 | * @param string $page |
||
| 132 | */ |
||
| 133 | public function setPage($page = '') { |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get posible paths for template page by name |
||
| 146 | * |
||
| 147 | * @param string $page |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function getPagePaths($page = '') { |
||
| 151 | if (!$page) { |
||
| 152 | $page = $this->page; |
||
| 153 | } |
||
| 154 | return [ |
||
| 155 | 'template' => $this->path . '/' . $page . '.html', |
||
| 156 | 'defaultPage' => \Inji\Module::getModulePath('View') . '/templatePages/' . $page . '.html' |
||
| 157 | ]; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set module for content path finder |
||
| 162 | * |
||
| 163 | * @param \Inji\Module $module |
||
| 164 | */ |
||
| 165 | public function setModule($module = null) { |
||
| 166 | if (!$module && !$this->module) { |
||
| 167 | $this->module = \Inji\Module::$cur; |
||
|
|
|||
| 168 | } else { |
||
| 169 | $this->module = $module; |
||
| 170 | } |
||
| 171 | if (is_string($this->module)) { |
||
| 172 | $this->module = $this->app->{$this->module}; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Set content file for rendering |
||
| 178 | * |
||
| 179 | * @param string $content |
||
| 180 | */ |
||
| 181 | public function setContent($content = '') { |
||
| 182 | if ($content) { |
||
| 183 | $this->content = $content; |
||
| 184 | } |
||
| 185 | if (\Inji\Controller::$cur && \Inji\Controller::$cur->run) { |
||
| 186 | if (!$this->content) { |
||
| 187 | $this->content = \Inji\Controller::$cur->method; |
||
| 188 | } |
||
| 189 | if ((!$this->contentPath || $content) && \Inji\Module::$cur) { |
||
| 190 | $this->contentPath = \Inji\Module::$cur->path . '/' . \Inji\Module::$cur->app->type . "Controllers/content/{$this->content}.php"; |
||
| 191 | } |
||
| 192 | $this->contentPath = \Inji\Tools::pathsResolve($this->getContentPaths(), $this->contentPath); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Return posible path for content file by name |
||
| 198 | * |
||
| 199 | * @param string $content |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getContentPaths($content = '') { |
||
| 203 | if (!$content) { |
||
| 204 | $content = $this->content; |
||
| 205 | } |
||
| 206 | $paths = []; |
||
| 207 | if ($this->module) { |
||
| 208 | if (\Inji\Controller::$cur) { |
||
| 209 | $paths['templateModuleController'] = $this->path . "/modules/{$this->module->name}/" . \Inji\Controller::$cur->name . "/{$content}.php"; |
||
| 210 | } |
||
| 211 | $paths['templateModule'] = $this->path . "/modules/{$this->module->name}/{$content}.php"; |
||
| 212 | } |
||
| 213 | if (\Inji\Module::$cur) { |
||
| 214 | if (\Inji\Controller::$cur) { |
||
| 215 | $paths['templateCurModuleController'] = $this->path . "/modules/" . \Inji\Module::$cur->name . "/" . \Inji\Controller::$cur->name . "/{$content}.php"; |
||
| 216 | } |
||
| 217 | $paths['templateCurModule'] = $this->path . "/modules/" . \Inji\Module::$cur->name . "/{$content}.php"; |
||
| 218 | } |
||
| 219 | if (\Inji\Controller::$cur) { |
||
| 220 | $modulePaths = \Inji\Module::getModulePaths(\Inji\Controller::$cur->module->name); |
||
| 221 | foreach ($modulePaths as $key => $modulePath) { |
||
| 222 | $paths['module_' . $key . '_appType'] = $modulePath . '/views/' . \Inji\Controller::$cur->module->app->type . '/' . \Inji\Controller::$cur->name . "/{$content}.php"; |
||
| 223 | $paths['module_' . $key . '_appType_controllerName'] = $modulePath . '/views/' . \Inji\Controller::$cur->module->app->type . "/{$content}.php"; |
||
| 224 | $paths['module_' . $key] = $modulePath . '/views/' . "/{$content}.php"; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | if ($this->module) { |
||
| 229 | if (\Inji\Controller::$cur) { |
||
| 230 | $paths['customModuleTemplateControllerContentController'] = $this->path . "/modules/" . $this->module->name . "/" . \Inji\Controller::$cur->name . "/{$content}.php"; |
||
| 231 | } |
||
| 232 | $paths['customModuleTemplateControllerContent'] = $this->path . "/modules/" . $this->module->name . "/{$content}.php"; |
||
| 233 | } |
||
| 234 | if ($this->module && \Inji\Controller::$cur) { |
||
| 235 | $paths['customModuleControllerContentController'] = $this->module->path . '/' . \Inji\Controller::$cur->module->app->type . "Controllers/content/" . \Inji\Controller::$cur->name . "/{$content}.php"; |
||
| 236 | $paths['customModuleControllerContent'] = $this->module->path . '/' . \Inji\Controller::$cur->module->app->type . "Controllers/content/{$content}.php"; |
||
| 237 | } |
||
| 238 | return $paths; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Retrn object of template by template name |
||
| 243 | * |
||
| 244 | * @param string $templateName |
||
| 245 | * @param \Inji\App $app |
||
| 246 | * @return \Inji\View\Template |
||
| 247 | */ |
||
| 248 | public static function get($templateName, $app = null, $templatesPath = '') { |
||
| 260 | ]); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | } |
||
| 265 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..