We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 60 |
Total Lines | 502 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Complex classes like CrudPanel 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 CrudPanel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class CrudPanel |
||
43 | { |
||
44 | // load all the default CrudPanel features |
||
45 | use Create, Read, Search, Update, Delete, Input, Errors, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, AutoFocus, Filters, Tabs, Views, Validation, HeadingsAndTitles, Operations, SaveActions, Settings, Relationships, HasViewNamespaces, MorphRelationships; |
||
46 | |||
47 | // allow developers to add their own closures to this object |
||
48 | use Macroable; |
||
49 | |||
50 | // -------------- |
||
51 | // CRUD variables |
||
52 | // -------------- |
||
53 | // These variables are passed to the CRUD views, inside the $crud variable. |
||
54 | // All variables are public, so they can be modified from your EntityCrudController. |
||
55 | // All functions and methods are also public, so they can be used in your EntityCrudController to modify these variables. |
||
56 | |||
57 | public $model = "\App\Models\Entity"; // what's the namespace for your entity's model |
||
58 | |||
59 | public $route; // what route have you defined for your entity? used for links. |
||
60 | |||
61 | public $entity_name = 'entry'; // what name will show up on the buttons, in singural (ex: Add entity) |
||
62 | |||
63 | public $entity_name_plural = 'entries'; // what name will show up on the buttons, in plural (ex: Delete 5 entities) |
||
64 | |||
65 | public $entry; |
||
66 | |||
67 | protected $request; |
||
68 | |||
69 | public $initialized = false; |
||
70 | |||
71 | // The following methods are used in CrudController or your EntityCrudController to manipulate the variables above. |
||
72 | |||
73 | public function __construct() |
||
74 | { |
||
75 | } |
||
76 | |||
77 | public function isInitialized() |
||
78 | { |
||
79 | return $this->initialized; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Set the request instance for this CRUD. |
||
84 | * |
||
85 | * @param Request $request |
||
86 | */ |
||
87 | public function setRequest($request = null): self |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get the request instance for this CRUD. |
||
96 | * |
||
97 | * @return Request |
||
98 | */ |
||
99 | public function getRequest() |
||
100 | { |
||
101 | return $this->request; |
||
102 | } |
||
103 | |||
104 | // ------------------------------------------------------ |
||
105 | // BASICS - model, route, entity_name, entity_name_plural |
||
106 | // ------------------------------------------------------ |
||
107 | |||
108 | /** |
||
109 | * This function binds the CRUD to its corresponding Model (which extends Eloquent). |
||
110 | * All Create-Read-Update-Delete operations are done using that Eloquent Collection. |
||
111 | * |
||
112 | * @param string $model_namespace Full model namespace. Ex: App\Models\Article |
||
113 | * |
||
114 | * @throws Exception in case the model does not exist |
||
115 | */ |
||
116 | public function setModel($model_namespace) |
||
117 | { |
||
118 | if (! class_exists($model_namespace)) { |
||
119 | throw new Exception('The model does not exist.', 500); |
||
120 | } |
||
121 | |||
122 | if (! method_exists($model_namespace, 'hasCrudTrait')) { |
||
123 | throw new Exception('Please use CrudTrait on the model.', 500); |
||
124 | } |
||
125 | |||
126 | $this->model = new $model_namespace(); |
||
127 | $this->query = clone $this->totalQuery = $this->model->select('*'); |
||
128 | $this->entry = null; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Get the corresponding Eloquent Model for the CrudController, as defined with the setModel() function. |
||
133 | * |
||
134 | * @return string|\Illuminate\Database\Eloquent\Model |
||
135 | */ |
||
136 | public function getModel() |
||
137 | { |
||
138 | return $this->model; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Get the database connection, as specified in the .env file or overwritten by the property on the model. |
||
143 | * |
||
144 | * @return \Illuminate\Database\Schema\Builder |
||
145 | */ |
||
146 | private function getSchema() |
||
147 | { |
||
148 | return $this->getModel()->getConnection()->getSchemaBuilder(); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Check if the database connection driver is using mongodb. |
||
153 | * |
||
154 | * DEPRECATION NOTICE: This method is no longer used and will be removed in future versions of Backpack |
||
155 | * |
||
156 | * @deprecated |
||
157 | * |
||
158 | * @codeCoverageIgnore |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | private function driverIsMongoDb() |
||
163 | { |
||
164 | return $this->getSchema()->getConnection()->getConfig()['driver'] === 'mongodb'; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Check if the database connection is any sql driver. |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | private function driverIsSql() |
||
173 | { |
||
174 | $driver = $this->getSchema()->getConnection()->getConfig('driver'); |
||
175 | |||
176 | return in_array($driver, $this->getSqlDriverList()); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Get SQL driver list. |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | public function getSqlDriverList() |
||
185 | { |
||
186 | return ['mysql', 'sqlsrv', 'sqlite', 'pgsql', 'mariadb']; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Set the route for this CRUD. |
||
191 | * Ex: admin/article. |
||
192 | * |
||
193 | * @param string $route Route name. |
||
194 | */ |
||
195 | public function setRoute($route) |
||
196 | { |
||
197 | $this->route = ltrim($route, '/'); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Set the route for this CRUD using the route name. |
||
202 | * Ex: admin.article. |
||
203 | * |
||
204 | * @param string $route Route name. |
||
205 | * @param array $parameters Parameters. |
||
206 | * |
||
207 | * @throws Exception |
||
208 | */ |
||
209 | public function setRouteName($route, $parameters = []) |
||
210 | { |
||
211 | $route = ltrim($route, '.'); |
||
212 | |||
213 | $complete_route = $route.'.index'; |
||
214 | |||
215 | if (! \Route::has($complete_route)) { |
||
216 | throw new Exception('There are no routes for this route name.', 404); |
||
217 | } |
||
218 | |||
219 | $this->route = route($complete_route, $parameters); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Get the current CrudController route. |
||
224 | * |
||
225 | * Can be defined in the CrudController with: |
||
226 | * - $this->crud->setRoute(config('backpack.base.route_prefix').'/article') |
||
227 | * - $this->crud->setRouteName(config('backpack.base.route_prefix').'.article') |
||
228 | * - $this->crud->route = config('backpack.base.route_prefix')."/article" |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | public function getRoute() |
||
233 | { |
||
234 | return $this->route; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Set the entity name in singular and plural. |
||
239 | * Used all over the CRUD interface (header, add button, reorder button, breadcrumbs). |
||
240 | * |
||
241 | * @param string $singular Entity name, in singular. Ex: article |
||
242 | * @param string $plural Entity name, in plural. Ex: articles |
||
243 | */ |
||
244 | public function setEntityNameStrings($singular, $plural) |
||
245 | { |
||
246 | $this->entity_name = $singular; |
||
247 | $this->entity_name_plural = $plural; |
||
248 | } |
||
249 | |||
250 | // ----------------------------------------------- |
||
251 | // ACTIONS - the current operation being processed |
||
252 | // ----------------------------------------------- |
||
253 | |||
254 | /** |
||
255 | * Get the action being performed by the controller, |
||
256 | * including middleware names, route name, method name, |
||
257 | * namespace, prefix, etc. |
||
258 | * |
||
259 | * @return string The EntityCrudController route action array. |
||
260 | */ |
||
261 | public function getAction() |
||
262 | { |
||
263 | return $this->getRequest()->route()->getAction(); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Get the full name of the controller method |
||
268 | * currently being called (including namespace). |
||
269 | * |
||
270 | * @return string The EntityCrudController full method name with namespace. |
||
271 | */ |
||
272 | public function getActionName() |
||
273 | { |
||
274 | return $this->getRequest()->route()->getActionName(); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Get the name of the controller method |
||
279 | * currently being called. |
||
280 | * |
||
281 | * @return string The EntityCrudController method name. |
||
282 | */ |
||
283 | public function getActionMethod() |
||
284 | { |
||
285 | return $this->getRequest()->route()->getActionMethod(); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Check if the controller method being called |
||
290 | * matches a given string. |
||
291 | * |
||
292 | * @param string $methodName Name of the method (ex: index, create, update) |
||
293 | * @return bool Whether the condition is met or not. |
||
294 | */ |
||
295 | public function actionIs($methodName) |
||
296 | { |
||
297 | return $methodName === $this->getActionMethod(); |
||
298 | } |
||
299 | |||
300 | // ---------------------------------- |
||
301 | // Miscellaneous functions or methods |
||
302 | // ---------------------------------- |
||
303 | |||
304 | /** |
||
305 | * Return the first element in an array that has the given 'type' attribute. |
||
306 | * |
||
307 | * @param string $type |
||
308 | * @param array $array |
||
309 | * @return array |
||
310 | */ |
||
311 | public function getFirstOfItsTypeInArray($type, $array) |
||
312 | { |
||
313 | return Arr::first($array, function ($item) use ($type) { |
||
314 | return $item['type'] == $type; |
||
315 | }); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED IN THIS FILE. |
||
320 | * |
||
321 | * TODO: |
||
322 | * - figure out if they are really needed |
||
323 | * - comments inside the function to explain how they work |
||
324 | * - write docblock for them |
||
325 | * - place in the correct section above (CREATE, READ, UPDATE, DELETE, ACCESS, MANIPULATION) |
||
326 | * |
||
327 | * @deprecated |
||
328 | * |
||
329 | * @codeCoverageIgnore |
||
330 | */ |
||
331 | public function sync($type, $fields, $attributes) |
||
332 | { |
||
333 | if (! empty($this->{$type})) { |
||
334 | $this->{$type} = array_map(function ($field) use ($fields, $attributes) { |
||
335 | if (in_array($field['name'], (array) $fields)) { |
||
336 | $field = array_merge($field, $attributes); |
||
337 | } |
||
338 | |||
339 | return $field; |
||
340 | }, $this->{$type}); |
||
341 | } |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Get the Eloquent Model name from the given relation definition string. |
||
346 | * |
||
347 | * @example For a given string 'company' and a relation between App/Models/User and App/Models/Company, defined by a |
||
348 | * company() method on the user model, the 'App/Models/Company' string will be returned. |
||
349 | * @example For a given string 'company.address' and a relation between App/Models/User, App/Models/Company and |
||
350 | * App/Models/Address defined by a company() method on the user model and an address() method on the |
||
351 | * company model, the 'App/Models/Address' string will be returned. |
||
352 | * |
||
353 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
354 | * @param int $length Optionally specify the number of relations to omit from the start of the relation string. If |
||
355 | * the provided length is negative, then that many relations will be omitted from the end of the relation |
||
356 | * string. |
||
357 | * @param Model $model Optionally specify a different model than the one in the crud object. |
||
358 | * @return string Relation model name. |
||
359 | */ |
||
360 | public function getRelationModel($relationString, $length = null, $model = null) |
||
361 | { |
||
362 | $relationArray = explode('.', $relationString); |
||
363 | |||
364 | if (! isset($length)) { |
||
365 | $length = count($relationArray); |
||
366 | } |
||
367 | |||
368 | if (! isset($model)) { |
||
369 | $model = $this->model; |
||
370 | } |
||
371 | |||
372 | $result = array_reduce(array_splice($relationArray, 0, $length), function ($obj, $method) { |
||
373 | try { |
||
374 | $result = $obj->$method(); |
||
375 | if (! $result instanceof Relation) { |
||
376 | throw new Exception('Not a relation'); |
||
377 | } |
||
378 | |||
379 | return $result->getRelated(); |
||
380 | } catch (Exception $e) { |
||
381 | return $obj; |
||
382 | } |
||
383 | }, $model); |
||
384 | |||
385 | return get_class($result); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Get the given attribute from a model or models resulting from the specified relation string (eg: the list of streets from |
||
390 | * the many addresses of the company of a given user). |
||
391 | * |
||
392 | * @param Model $model Model (eg: user). |
||
393 | * @param string $relationString Model relation. Can be a string representing the name of a relation method in the given |
||
394 | * Model or one from a different Model through multiple relations. A dot notation can be used to specify |
||
395 | * multiple relations (eg: user.company.address). |
||
396 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
397 | * @return array An array containing a list of attributes from the resulting model. |
||
398 | */ |
||
399 | public function getRelatedEntriesAttributes($model, $relationString, $attribute) |
||
400 | { |
||
401 | $endModels = $this->getRelatedEntries($model, $relationString); |
||
402 | $attributes = []; |
||
403 | foreach ($endModels as $model => $entries) { |
||
404 | /** @var Model $model_instance */ |
||
405 | $model_instance = new $model(); |
||
406 | $modelKey = $model_instance->getKeyName(); |
||
407 | |||
408 | if (is_array($entries)) { |
||
409 | //if attribute does not exist in main array we have more than one entry OR the attribute |
||
410 | //is an accessor that is not in $appends property of model. |
||
411 | if (! isset($entries[$attribute])) { |
||
412 | //we first check if we don't have the attribute because it's an accessor that is not in appends. |
||
413 | if ($model_instance->hasGetMutator($attribute) && isset($entries[$modelKey])) { |
||
414 | $entry_in_database = $model_instance->find($entries[$modelKey]); |
||
415 | $attributes[$entry_in_database->{$modelKey}] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry_in_database->{$attribute}); |
||
416 | } else { |
||
417 | //we have multiple entries |
||
418 | //for each entry we check if $attribute exists in array or try to check if it's an accessor. |
||
419 | foreach ($entries as $entry) { |
||
420 | if (isset($entry[$attribute])) { |
||
421 | $attributes[$entry[$modelKey]] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry[$attribute]); |
||
422 | } else { |
||
423 | if ($model_instance->hasGetMutator($attribute)) { |
||
424 | $entry_in_database = $model_instance->find($entry[$modelKey]); |
||
425 | $attributes[$entry_in_database->{$modelKey}] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry_in_database->{$attribute}); |
||
426 | } |
||
427 | } |
||
428 | } |
||
429 | } |
||
430 | } else { |
||
431 | //if we have the attribute we just return it, does not matter if it is direct attribute or an accessor added in $appends. |
||
432 | $attributes[$entries[$modelKey]] = $this->parseTranslatableAttributes($model_instance, $attribute, $entries[$attribute]); |
||
433 | } |
||
434 | } |
||
435 | } |
||
436 | |||
437 | return $attributes; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Parse translatable attributes from a model or models resulting from the specified relation string. |
||
442 | * |
||
443 | * @param Model $model Model (eg: user). |
||
444 | * @param string $attribute The attribute from the relation model (eg: the street attribute from the address model). |
||
445 | * @param string $value Attribute value translatable or not |
||
446 | * @return string A string containing the translated attributed based on app()->getLocale() |
||
447 | */ |
||
448 | public function parseTranslatableAttributes($model, $attribute, $value) |
||
449 | { |
||
450 | if (! method_exists($model, 'isTranslatableAttribute')) { |
||
451 | return $value; |
||
452 | } |
||
453 | |||
454 | if (! $model->isTranslatableAttribute($attribute)) { |
||
455 | return $value; |
||
456 | } |
||
457 | |||
458 | if (! is_array($value)) { |
||
459 | $decodedAttribute = json_decode($value, true) ?? ($value !== null ? [$value] : []); |
||
460 | } else { |
||
461 | $decodedAttribute = $value; |
||
462 | } |
||
463 | |||
464 | if (is_array($decodedAttribute) && ! empty($decodedAttribute)) { |
||
465 | if (isset($decodedAttribute[app()->getLocale()])) { |
||
466 | return $decodedAttribute[app()->getLocale()]; |
||
467 | } else { |
||
468 | return Arr::first($decodedAttribute); |
||
469 | } |
||
470 | } |
||
471 | |||
472 | return $value; |
||
473 | } |
||
474 | |||
475 | public function setLocaleOnModel(Model $model) |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Traverse the tree of relations for the given model, defined by the given relation string, and return the ending |
||
492 | * associated model instance or instances. |
||
493 | * |
||
494 | * @param Model $model The CRUD model. |
||
495 | * @param string $relationString Relation string. A dot notation can be used to chain multiple relations. |
||
496 | * @return array An array of the associated model instances defined by the relation string. |
||
497 | */ |
||
498 | private function getRelatedEntries($model, $relationString) |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Allow to add an attribute to multiple fields/columns/filters/buttons at same time. |
||
533 | * |
||
534 | * Using the fluent syntax allow the developer to add attributes to multiple fields at the same time. Eg: |
||
535 | * |
||
536 | * - CRUD::group(CRUD::field('price')->type('number'), CRUD::field('title')->type('text'))->tab('both_on_same_tab'); |
||
537 | * |
||
538 | * @param mixed fluent syntax objects. |
||
539 | * @return CrudObjectGroup |
||
540 | */ |
||
541 | public function group(...$objects) |
||
544 | } |
||
545 | } |
||
546 |
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