| Total Complexity | 48 |
| Total Lines | 288 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DisplayTabbed 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 DisplayTabbed, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class DisplayTabbed implements DisplayInterface, FormInterface |
||
| 21 | { |
||
| 22 | use FormElements, VisibleCondition, \SleepingOwl\Admin\Traits\Renderable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $view = 'display.tabbed'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * DisplayTabbed constructor. |
||
| 31 | * |
||
| 32 | * @param Closure|TabInterface[] $tabs |
||
| 33 | */ |
||
| 34 | public function __construct($tabs = null) |
||
| 35 | { |
||
| 36 | $this->elements = new DisplayTabsCollection(); |
||
| 37 | |||
| 38 | if (is_array($tabs) || is_callable($tabs)) { |
||
| 39 | $this->setTabs($tabs); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Initialize tabbed interface. |
||
| 45 | */ |
||
| 46 | public function initialize() |
||
| 47 | { |
||
| 48 | $this->initializeElements(); |
||
| 49 | |||
| 50 | $tabs = $this->getTabs(); |
||
| 51 | |||
| 52 | foreach ($tabs as $tab) { |
||
| 53 | if (method_exists($tab->getContent(), 'getElements')) { |
||
| 54 | $elements = $tab->getContent()->getElements(); |
||
| 55 | foreach ($elements as $element) { |
||
| 56 | if ($element instanceof self) { |
||
| 57 | foreach ($element->getTabs() as $subTab) { |
||
| 58 | if ($subTab->getName() == session('sleeping_owl_tab_id')) { |
||
| 59 | $tab->setActive(true); |
||
| 60 | $subTab->setActive(true); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($tab->getName() == session('sleeping_owl_tab_id')) { |
||
| 68 | $tab->setActive(true); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | $activeTabs = $this->getTabs()->filter(function (TabInterface $tab) { |
||
| 73 | return $tab->isActive(); |
||
| 74 | })->count(); |
||
| 75 | |||
| 76 | if ($activeTabs === 0 && $firstTab = $this->getTabs()->first()) { |
||
| 77 | $firstTab->setActive(true); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return Model $model|null |
||
| 83 | */ |
||
| 84 | public function getModel() |
||
| 85 | { |
||
| 86 | foreach ($this->getTabs() as $tab) { |
||
| 87 | if ($tab->getContent() instanceof FormInterface) { |
||
| 88 | return $tab->getContent()->getModel(); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $class |
||
| 95 | * |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | public function setModelClass($class) |
||
| 99 | { |
||
| 100 | $this->getTabs()->each(function (TabInterface $tab) use ($class) { |
||
| 101 | if ($tab instanceof DisplayInterface) { |
||
| 102 | $tab->setModelClass($class); |
||
| 103 | } |
||
| 104 | }); |
||
| 105 | |||
| 106 | return $this; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return TabInterface[]|\SleepingOwl\Admin\Form\FormElementsCollection |
||
| 111 | */ |
||
| 112 | public function getTabs() |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param Closure|TabInterface[] $tabs |
||
| 119 | * |
||
| 120 | * @return $this |
||
| 121 | */ |
||
| 122 | public function setTabs($tabs) |
||
| 123 | { |
||
| 124 | if (is_callable($tabs)) { |
||
| 125 | $tabs = call_user_func($tabs, $this); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $this->setElements($tabs); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param array $elements |
||
| 133 | * |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function setElements(array $elements) |
||
| 137 | { |
||
| 138 | foreach ($elements as $label => $tab) { |
||
| 139 | if ($tab instanceof TabInterface) { |
||
| 140 | $this->addElement($tab); |
||
| 141 | } else { |
||
| 142 | $this->appendTab($tab, $label); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param Renderable $display |
||
| 151 | * @param string $label |
||
| 152 | * @param bool|false $active |
||
| 153 | * |
||
| 154 | * @return DisplayTab |
||
| 155 | */ |
||
| 156 | public function appendTab(Renderable $display, $label, $active = false) |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param TabInterface $element |
||
| 167 | * |
||
| 168 | * @return $this |
||
| 169 | */ |
||
| 170 | public function addElement(TabInterface $element) |
||
| 171 | { |
||
| 172 | $this->elements->push($element); |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $action |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function setAction($action) |
||
| 183 | { |
||
| 184 | $this->getTabs()->each(function (TabInterface $tab) use ($action) { |
||
| 185 | if ($tab instanceof FormInterface) { |
||
| 186 | $tab->setAction($action); |
||
| 187 | } |
||
| 188 | }); |
||
| 189 | |||
| 190 | return $this; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param int $id |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function setId($id) |
||
| 199 | { |
||
| 200 | $model_class = get_class($this->getModel()); |
||
| 201 | $this->getTabs()->each(function (TabInterface $tab) use ($id, $model_class) { |
||
| 202 | if ($tab instanceof FormInterface) { |
||
| 203 | if (! $tab->getExternalForm()) { |
||
| 204 | $tab_content = $tab->getContent(); |
||
| 205 | if ($tab_content instanceof FormInterface) { |
||
| 206 | $tab_model = $tab->getModel(); |
||
| 207 | $set_id = $model_class == get_class($tab_model); |
||
| 208 | $tab_model_section = \AdminSection::getModel($tab_model); |
||
| 209 | if (is_object($tab_model_section) && $tab_model_section instanceof SectionModelConfiguration) { |
||
| 210 | $set_id = $set_id && $tab->getContent()->getAction() == $tab_model_section->getUpdateUrl($id); |
||
| 211 | } |
||
| 212 | if ($set_id) { |
||
| 213 | $tab->setId($id); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | }); |
||
| 219 | |||
| 220 | return $this; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param \Illuminate\Http\Request $request |
||
| 225 | * @param ModelConfigurationInterface $model |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function validateForm(Request $request, ModelConfigurationInterface $model = null) |
||
| 230 | { |
||
| 231 | $this->getTabs()->each(function ($tab) use ($request, $model) { |
||
| 232 | $tabId = $request->get('sleeping_owl_tab_id'); |
||
| 233 | |||
| 234 | if ($tab instanceof FormInterface && $tab->getName() == $tabId) { |
||
| 235 | $tab->validateForm($request, $model); |
||
| 236 | } |
||
| 237 | }); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param \Illuminate\Http\Request $request |
||
| 242 | * @param ModelConfigurationInterface $model |
||
| 243 | * |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | public function saveForm(Request $request, ModelConfigurationInterface $model = null) |
||
| 247 | { |
||
| 248 | $this->getTabs()->each(function (TabInterface $tab) use ($request, $model) { |
||
| 249 | $tabId = $request->get('sleeping_owl_tab_id'); |
||
| 250 | |||
| 251 | if ($tab instanceof FormInterface && $tab->getName() == $tabId) { |
||
| 252 | $tab->saveForm($request, $model); |
||
| 253 | } |
||
| 254 | }); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return null |
||
| 259 | */ |
||
| 260 | public function getValue() |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public function isReadonly() |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function isVisibled() |
||
| 276 | { |
||
| 277 | return true; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function isValueSkipped() |
||
| 284 | { |
||
| 285 | return false; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | public function toArray() |
||
| 295 | ]; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Using in trait FormElements;. |
||
| 300 | * |
||
| 301 | * @param $object |
||
| 302 | * |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | protected function getElementContainer($object) |
||
| 310 |
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.