| Total Complexity | 45 |
| Total Lines | 209 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 3 | Features | 0 |
Complex classes like UccelloModule 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 UccelloModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | trait UccelloModule |
||
| 16 | { |
||
| 17 | use RelatedlistTrait; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The "booting" method of the trait. |
||
| 21 | * |
||
| 22 | * @return void |
||
| 23 | */ |
||
| 24 | protected static function bootUccelloModule() |
||
| 25 | { |
||
| 26 | if (static::isFilteredByUser()) { |
||
| 27 | static::addGlobalScope(new Scopes\AssignedUser); |
||
| 28 | } |
||
| 29 | |||
| 30 | // Create uuid after save |
||
| 31 | static::created(function ($model) { |
||
| 32 | $module = Module::where('model_class', get_class($model))->first(); |
||
| 33 | if ($module) { |
||
| 34 | Entity::create([ |
||
| 35 | 'id' => (string) Str::uuid(), |
||
| 36 | 'module_id' => $module->id, |
||
| 37 | 'record_id' => $model->getKey(), |
||
| 38 | 'creator_id' => auth()->id(), |
||
| 39 | ]); |
||
| 40 | } |
||
| 41 | }); |
||
| 42 | |||
| 43 | // Delete uuid after forced delete |
||
| 44 | static::deleted(function ($model) { |
||
| 45 | if (!empty($model->uuid) && (!method_exists($model, 'isForceDeleting') || $model->isForceDeleting() === true)) { |
||
| 46 | $entity = Entity::find($model->uuid); |
||
| 47 | if ($entity) { |
||
| 48 | $entity->delete(); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | }); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function initializeUccelloModule() |
||
| 55 | { |
||
| 56 | $this->appends = array_merge($this->appends, ['recordLabel','uuid']); |
||
| 57 | } |
||
| 58 | |||
| 59 | protected static function isFilteredByUser() |
||
| 60 | { |
||
| 61 | $isFilteredByUser = false; |
||
| 62 | |||
| 63 | $user = Auth::user(); |
||
| 64 | |||
| 65 | if ($user && !$user->is_admin) { |
||
| 66 | $module = static::getModuleFromClass(static::class); |
||
| 67 | |||
| 68 | if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) { |
||
| 69 | $isFilteredByUser = true; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | return $isFilteredByUser; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function getTableAttribute() |
||
| 77 | { |
||
| 78 | return $this->table; |
||
| 79 | } |
||
| 80 | |||
| 81 | public function getModuleAttribute() |
||
| 82 | { |
||
| 83 | return static::getModuleFromClass(get_class($this)); |
||
| 84 | } |
||
| 85 | |||
| 86 | protected static function getModuleFromClass($className) |
||
| 87 | { |
||
| 88 | $modules = Cache::rememberForever('modules_by_model_class', function () { |
||
| 89 | $modulesGroupedByModelClass = collect(); |
||
| 90 | Module::all()->map(function ($item) use ($modulesGroupedByModelClass) { |
||
| 91 | $modulesGroupedByModelClass[$item->model_class] = $item; |
||
| 92 | return $modulesGroupedByModelClass; |
||
| 93 | }); |
||
| 94 | return $modulesGroupedByModelClass; |
||
| 95 | }); |
||
| 96 | return $modules[(string) $className] ?? null; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function getUuidAttribute() |
||
| 100 | { |
||
| 101 | $uuid = null; |
||
| 102 | |||
| 103 | $module = $this->module; |
||
| 104 | |||
| 105 | if ($module) { |
||
| 106 | $entity = Entity::where('module_id', $module->getKey()) |
||
| 107 | ->where('record_id', $this->getKey()) |
||
| 108 | ->first(); |
||
| 109 | |||
| 110 | if ($entity) { |
||
| 111 | $uuid = $entity->getKey(); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return $uuid; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns Assigned User |
||
| 120 | * |
||
| 121 | * @return string|null |
||
| 122 | */ |
||
| 123 | public function getAssignedUserAttribute(): ?string |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns user who created the entity, if defined. |
||
| 130 | * |
||
| 131 | * @return \Uccello\Core\Models\User|null |
||
| 132 | */ |
||
| 133 | public function getCreatorAttribute() |
||
| 134 | { |
||
| 135 | $creator = null; |
||
| 136 | |||
| 137 | $module = $this->module; |
||
| 138 | |||
| 139 | if ($module) { |
||
| 140 | $entity = Entity::where('module_id', $module->getKey()) |
||
| 141 | ->where('record_id', $this->getKey()) |
||
| 142 | ->first(); |
||
| 143 | |||
| 144 | if ($entity) { |
||
| 145 | $creator = $entity->creator; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return $creator; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function scopeInDomain($query, ?Domain $domain, $withDescendants = false) |
||
| 153 | { |
||
| 154 | if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) { |
||
| 155 | // Activate descendant view if the user is allowed |
||
| 156 | if (Auth::user() && Auth::user()->canSeeDescendantsRecords($domain) && $withDescendants) { |
||
| 157 | $domainsIds = $domain->findDescendants()->pluck('id'); |
||
| 158 | $query = $query->whereIn('domain_id', $domainsIds); |
||
| 159 | } else { |
||
| 160 | $query = $query->where('domain_id', $domain->id); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return $query; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Add filter conditions to a query. |
||
| 169 | * @param QueryBuilder $query |
||
| 170 | * @param Filter|int|array $filter |
||
| 171 | * @return QueryBuilder|null |
||
| 172 | */ |
||
| 173 | public function scopeFilterBy($query, $filter) |
||
| 224 | } |
||
| 225 | } |
||
| 226 |
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