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