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