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