| Total Complexity | 40 |
| Total Lines | 152 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Page 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 Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Page { |
||
| 13 | /** |
||
| 14 | * @var Template |
||
| 15 | */ |
||
| 16 | public $template; |
||
| 17 | /** |
||
| 18 | * @var App |
||
| 19 | */ |
||
| 20 | public $app; |
||
| 21 | /** |
||
| 22 | * @var Module |
||
| 23 | */ |
||
| 24 | public $module; |
||
| 25 | /** |
||
| 26 | * @var Head |
||
| 27 | */ |
||
| 28 | public $head; |
||
| 29 | /** |
||
| 30 | * @var Assets |
||
| 31 | */ |
||
| 32 | public $assets; |
||
| 33 | /** |
||
| 34 | * @var Content |
||
| 35 | */ |
||
| 36 | public $content; |
||
| 37 | public $name = ''; |
||
| 38 | public $path = ''; |
||
| 39 | public $title = ''; |
||
| 40 | |||
| 41 | |||
| 42 | function __construct($params, ?App $app = null) { |
||
| 43 | $this->app = $app ?? App::$cur; |
||
| 44 | $this->setParams($params); |
||
| 45 | $this->head = new Head($this); |
||
| 46 | $this->assets = new Assets($this); |
||
| 47 | if (empty($this->template->config['noInjects']) && !empty(\Inji::$config['assets']['js'])) { |
||
| 48 | foreach (\Inji::$config['assets']['js'] as $js) { |
||
| 49 | $this->customAsset('js', $js); |
||
|
|
|||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | public function setTitle($title, $add = true) { |
||
| 55 | if ($add && !empty($this->app->config['site']['name'])) { |
||
| 56 | if ($title) { |
||
| 57 | $this->title = $title . ' - ' . $this->app->config['site']['name']; |
||
| 58 | } else { |
||
| 59 | $this->title = $this->app->config['site']['name']; |
||
| 60 | } |
||
| 61 | } else { |
||
| 62 | $this->title = $title; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | function setParams($params) { |
||
| 67 | |||
| 68 | $this->setTemplate($params['template'] ?? 'default'); |
||
| 69 | |||
| 70 | $this->setPage($params['page'] ?? false); |
||
| 71 | |||
| 72 | $this->setPath($params['pagePath'] ?? false); |
||
| 73 | |||
| 74 | $this->setModule($params['module'] ?? false); |
||
| 75 | |||
| 76 | $this->setContent([ |
||
| 77 | 'name' => $params['content'] ?? false, |
||
| 78 | 'path' => $params['contentPath'] ?? false, |
||
| 79 | 'data' => $params['data'] ?? [], |
||
| 80 | ]); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function setContent($content = false) { |
||
| 84 | if ($content instanceof Content) { |
||
| 85 | $this->content = $content; |
||
| 86 | } |
||
| 87 | if (!$this->content) { |
||
| 88 | if ($content && is_string($content)) { |
||
| 89 | $this->content = new Content(['name' => $content], $this); |
||
| 90 | } elseif ($content && is_array($content)) { |
||
| 91 | $this->content = new Content($content, $this); |
||
| 92 | } elseif (Controller::$cur && Controller::$cur->run) { |
||
| 93 | $this->content = new Content(['name' => Controller::$cur->method], $this); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set module for content path finder |
||
| 100 | * |
||
| 101 | * @param \Inji\Module $module |
||
| 102 | */ |
||
| 103 | public function setModule($module = null) { |
||
| 104 | if (!$module && !$this->module) { |
||
| 105 | $this->module = \Inji\Module::$cur; |
||
| 106 | } else { |
||
| 107 | $this->module = $module; |
||
| 108 | } |
||
| 109 | if (is_string($this->module)) { |
||
| 110 | $this->module = $this->app->{$this->module}; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | public function setPath($path = '') { |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | public function setPage($pageName = '') { |
||
| 134 | } |
||
| 135 | |||
| 136 | public function setTemplate($template) { |
||
| 137 | if ($template instanceof Template) { |
||
| 138 | $this->template = $template; |
||
| 139 | } elseif ($template && is_string($template) && $template != 'current') { |
||
| 140 | $this->template = Template::get($template, $this->app, $this->app->view->templatesPath()); |
||
| 141 | } else { |
||
| 142 | $this->template = Template::get('default', $this->app, $this->app->view->templatesPath()); |
||
| 143 | } |
||
| 144 | return $this->template; |
||
| 145 | } |
||
| 146 | |||
| 147 | function possiblePaths($page) { |
||
| 148 | $paths = [ |
||
| 149 | 'template' => $this->template->path . '/' . $page . '.html' |
||
| 150 | ]; |
||
| 151 | foreach (\Inji\Module::getModulePaths('View') as $pathName => $path) { |
||
| 152 | $paths[$pathName] = $path . '/templatePages/' . $page . '.html'; |
||
| 153 | } |
||
| 154 | return $paths; |
||
| 155 | } |
||
| 156 | |||
| 157 | function send() { |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.