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