Total Complexity | 43 |
Total Lines | 286 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CBController 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 CBController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class CBController extends Controller |
||
25 | { |
||
26 | use Hooks; |
||
27 | use Deleter, CbFormLoader, CbIndexLoader, CbLayoutLoader, IndexAjax, ImportData, ExportData; |
||
28 | |||
29 | public $data_inputan; |
||
30 | |||
31 | public $module_name; |
||
32 | |||
33 | public $table; |
||
34 | |||
35 | public $title_field; |
||
36 | |||
37 | public $primary_key = 'id'; |
||
38 | |||
39 | public $arr = []; |
||
40 | |||
41 | public $col = []; |
||
42 | |||
43 | public $form = []; |
||
44 | |||
45 | public $data = []; |
||
46 | |||
47 | public $addaction = []; |
||
48 | |||
49 | //public $global_privilege = false; |
||
50 | |||
51 | public $button_delete = true; |
||
52 | |||
53 | public $button_action_style = 'button_icon'; |
||
54 | |||
55 | public $return_url = null; |
||
56 | |||
57 | public $parent_field = null; |
||
58 | |||
59 | public $parent_id = null; |
||
60 | |||
61 | public $hide_form = []; |
||
62 | |||
63 | public $index_return = false; //for export |
||
64 | |||
65 | public function cbView($template, $data) |
||
66 | { |
||
67 | $this->cbLayoutLoader(); |
||
68 | view()->share($this->data); |
||
69 | return view($template, $data); |
||
70 | } |
||
71 | |||
72 | public function cbLoader() |
||
73 | { |
||
74 | $this->cbInit(); |
||
75 | |||
76 | $this->cbLayoutLoader(); |
||
77 | $this->cbFormLoader(); |
||
78 | $this->cbIndexLoader(); |
||
79 | $this->checkHideForm(); |
||
80 | $this->genericLoader(); |
||
81 | |||
82 | view()->share($this->data); |
||
83 | } |
||
84 | |||
85 | private function checkHideForm() |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | public function getIndex() |
||
98 | { |
||
99 | $index = app(Index::class); |
||
100 | $this->cbIndexLoader(); |
||
101 | $this->genericLoader(); |
||
102 | $data = $index->index($this); |
||
103 | |||
104 | return $this->cbView('crudbooster::default.index', $data); |
||
105 | } |
||
106 | |||
107 | public function getUpdateSingle() |
||
108 | { |
||
109 | $table = request('table'); |
||
110 | $column = request('column'); |
||
111 | $value = request('value'); |
||
112 | $id = request('id'); |
||
113 | DB::table($table)->where(CB::pk($table), $id)->update([$column => $value]); |
||
114 | |||
115 | backWithMsg(cbTrans('alert_update_data_success')); |
||
116 | } |
||
117 | |||
118 | public function getAdd() |
||
123 | } |
||
124 | |||
125 | public function postAddSave() |
||
126 | { |
||
127 | $this->genericLoader(); |
||
128 | |||
129 | app(FormValidator::class)->validate(null, $this->form, $this->table); |
||
130 | $this->inputAssignment(); |
||
131 | |||
132 | $this->setTimeStamps('created_at'); |
||
133 | |||
134 | $this->hookBeforeAdd($this->arr); |
||
135 | |||
136 | $this->arr[$this->primary_key] = $id = $this->table()->insertGetId($this->arr); |
||
137 | app(RelationHandler::class)->save($this->table, $id, $this->data_inputan); |
||
138 | |||
139 | $this->hookAfterAdd($this->arr[$this->primary_key]); |
||
140 | |||
141 | $this->insertLog('log_add', $this->arr[$this->title_field]); |
||
142 | |||
143 | $this->sendResponseForSave('alert_add_data_success'); |
||
144 | } |
||
145 | |||
146 | public function inputAssignment($id = null) |
||
147 | { |
||
148 | $hide_form = (request('hide_form')) ? unserialize(request('hide_form')) : []; |
||
149 | |||
150 | foreach ($this->form as $ro) { |
||
151 | $name = $ro['name']; |
||
152 | $type = $ro['type'] ?: 'text'; |
||
153 | $inputdata = request($name); |
||
154 | |||
155 | if (! $name || $ro['exception']) { |
||
156 | continue; |
||
157 | } |
||
158 | |||
159 | if (count($hide_form) && in_array($name, $hide_form)) { |
||
160 | continue; |
||
161 | } |
||
162 | |||
163 | $hookPath = \CB::componentsPath($type).DIRECTORY_SEPARATOR.'hookInputAssignment.php'; |
||
164 | if (file_exists($hookPath)) { |
||
165 | require_once($hookPath); |
||
166 | } |
||
167 | unset($hookPath); |
||
168 | |||
169 | if (Request::hasFile($name)) { |
||
170 | continue; |
||
171 | } |
||
172 | |||
173 | if ($inputdata == '' && CB::isColumnNULL($this->table, $name)) { |
||
174 | continue; |
||
175 | } |
||
176 | |||
177 | $this->arr[$name] = ''; |
||
178 | |||
179 | if ($inputdata != '') { |
||
180 | $this->arr[$name] = $inputdata; |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param null $tableName |
||
187 | * @return mixed |
||
188 | */ |
||
189 | public function table($tableName = null) |
||
190 | { |
||
191 | $table = $tableName ?: $this->table; |
||
192 | return \DB::table($table); |
||
193 | } |
||
194 | |||
195 | public function getEdit($id) |
||
196 | { |
||
197 | $row = $this->findRow($id)->first(); |
||
198 | |||
199 | $page_title = cbTrans("edit_data_page_title", ['module' => CB::getCurrentModule()->name, 'name' => $row->{$this->title_field}]); |
||
200 | $command = 'edit'; |
||
201 | session()->put('current_row_id', $id); |
||
202 | |||
203 | return $this->cbForm(compact('id', 'row', 'page_title', 'command')); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param $id |
||
208 | * @return mixed |
||
209 | */ |
||
210 | public function findRow($id) |
||
211 | { |
||
212 | return $this->table()->where($this->primary_key, $id); |
||
213 | } |
||
214 | |||
215 | public function postEditSave($id) |
||
216 | { |
||
217 | $this->genericLoader(); |
||
218 | |||
219 | app(FormValidator::class)->validate($id, $this->form, $this->table); |
||
220 | $this->inputAssignment($id); |
||
221 | |||
222 | $this->setTimeStamps('updated_at'); |
||
223 | |||
224 | $this->hookBeforeEdit($this->arr, $id); |
||
225 | $this->findRow($id)->update($this->arr); |
||
226 | app(RelationHandler::class)->save($this->table, $id, $this->data_inputan); |
||
227 | |||
228 | $this->hookAfterEdit($id); |
||
229 | |||
230 | $this->insertLog('log_update', $this->arr[$this->title_field]); |
||
231 | |||
232 | $this->sendResponseForSave('alert_update_data_success'); |
||
233 | } |
||
234 | |||
235 | public function getDetail($id) |
||
236 | { |
||
237 | $row = $this->findRow($id)->first(); |
||
238 | |||
239 | |||
240 | $page_title = cbTrans('detail_data_page_title', ['module' => CB::getCurrentModule()->name, 'name' => $row->{$this->title_field}]); |
||
241 | $command = 'detail'; |
||
242 | |||
243 | session()->put('current_row_id', $id); |
||
244 | |||
245 | return $this->cbForm(compact('row', 'page_title', 'command', 'id')); |
||
246 | } |
||
247 | |||
248 | public function actionButtonSelected($id_selected, $button_name) |
||
249 | { |
||
250 | } |
||
251 | |||
252 | private function sendResponseForSave($msg) |
||
265 | } |
||
266 | |||
267 | private function insertLog($msg, $name) |
||
268 | { |
||
269 | CB::insertLog(trans('logging.'.$msg, ['module' => CB::getCurrentModule()->name, 'name' => $name])); |
||
270 | } |
||
271 | |||
272 | private function setTimeStamps($col) |
||
276 | } |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param $data |
||
281 | * @return mixed |
||
282 | */ |
||
283 | private function cbForm($data) |
||
284 | { |
||
285 | $this->genericLoader(); |
||
286 | $this->cbFormLoader(); |
||
287 | return $this->cbView('crudbooster::default.form', $data); |
||
288 | } |
||
289 | |||
290 | private function genericLoader() |
||
310 | } |
||
311 | } |
||
312 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths