| Total Complexity | 42 |
| Total Lines | 267 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | abstract class Module |
||
| 12 | { |
||
| 13 | use Macroable; |
||
| 14 | |||
| 15 | /** @var \Rawilk\LaravelModules\Contracts\Activator */ |
||
| 16 | private $activator; |
||
| 17 | |||
| 18 | /** @var \Illuminate\Contracts\Foundation\Application */ |
||
| 19 | protected $app; |
||
| 20 | |||
| 21 | /** @var \Illuminate\Cache\CacheManager */ |
||
| 22 | private $cache; |
||
| 23 | |||
| 24 | /** @var \Illuminate\Filesystem\Filesystem */ |
||
| 25 | private $files; |
||
| 26 | |||
| 27 | /** @var array */ |
||
| 28 | protected $moduleJson = []; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | protected $name; |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | protected $path; |
||
| 35 | |||
| 36 | /** @var \Illuminate\Translation\Translator */ |
||
| 37 | private $translator; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param \Illuminate\Contracts\Container\Container $app |
||
| 41 | * @param string $name |
||
| 42 | * @param string|null $path |
||
| 43 | */ |
||
| 44 | public function __construct(Container $app, string $name, ?string $path) |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get the path to the cached *_module.php file. |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | abstract public function getCachedServicesPath(): string; |
||
| 61 | |||
| 62 | abstract public function registerAliases(): void; |
||
| 63 | |||
| 64 | abstract public function registerProviders(): void; |
||
| 65 | |||
| 66 | public function boot(): void |
||
| 67 | { |
||
| 68 | if (config('modules.register.translations', true)) { |
||
| 69 | $this->registerTranslation(); |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($this->isLoadFilesOnBoot()) { |
||
| 73 | $this->registerFiles(); |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->fireEvent('boot'); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function delete(): bool |
||
| 80 | { |
||
| 81 | $this->activator->delete($this); |
||
| 82 | |||
| 83 | return $this->json()->getFilesystem()->deleteDirectory($this->getPath()); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function disable(): void |
||
| 87 | { |
||
| 88 | $this->fireEvent('disabling'); |
||
| 89 | |||
| 90 | $this->activator->disable($this); |
||
| 91 | $this->flushCache(); |
||
| 92 | |||
| 93 | $this->fireEvent('disabled'); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function enable(): void |
||
| 104 | } |
||
| 105 | |||
| 106 | public function get(string $key, $default = null) |
||
| 107 | { |
||
| 108 | return $this->json()->get($key, $default); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function getAlias(): string |
||
| 112 | { |
||
| 113 | return $this->get('alias'); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function getAssets(): array |
||
| 117 | { |
||
| 118 | try { |
||
| 119 | return $this->json('assets.json')->toArray(); |
||
| 120 | } catch (\Exception $e) { |
||
| 121 | return []; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | public function getAssetAttr(string $key, $default = null) |
||
| 126 | { |
||
| 127 | try { |
||
| 128 | return $this->json('assets.json')->get($key, $default); |
||
| 129 | } catch (\Exception $e) { |
||
| 130 | return null; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getComposerAttr(string $key, $default = null) |
||
| 135 | { |
||
| 136 | return $this->json('composer.json')->get($key, $default); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getDescription(): string |
||
| 140 | { |
||
| 141 | return $this->get('description'); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getExtraPath(string $path): string |
||
| 145 | { |
||
| 146 | return $this->getPath() . '/' . $path; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getName(): string |
||
| 150 | { |
||
| 151 | return $this->name; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function getLowerName(): string |
||
| 155 | { |
||
| 156 | return strtolower($this->name); |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getPriority(): string |
||
| 160 | { |
||
| 161 | return $this->get('priority'); |
||
| 162 | } |
||
| 163 | |||
| 164 | public function getRequires(): array |
||
| 165 | { |
||
| 166 | return $this->get('requires'); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function getPath(): string |
||
| 170 | { |
||
| 171 | return $this->path; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function getSnakeName(): string |
||
| 175 | { |
||
| 176 | return Str::snake($this->name); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function getStudlyName(): string |
||
| 180 | { |
||
| 181 | return Str::studly($this->name); |
||
| 182 | } |
||
| 183 | |||
| 184 | public function isDisabled(): bool |
||
| 187 | } |
||
| 188 | |||
| 189 | public function isEnabled(): bool |
||
| 190 | { |
||
| 191 | return $this->activator->hasStatus($this, true); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function isStatus(bool $status): bool |
||
| 195 | { |
||
| 196 | return $this->activator->hasStatus($this, $status); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function json(?string $file = null): Json |
||
| 200 | { |
||
| 201 | if ($file === null) { |
||
| 202 | $file = 'module.json'; |
||
| 203 | } |
||
| 204 | |||
| 205 | return Arr::get($this->moduleJson, $file, function () use ($file) { |
||
| 206 | return $this->moduleJson[$file] = new Json($this->getPath() . '/' . $file, $this->files); |
||
| 207 | }); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function register(): void |
||
| 211 | { |
||
| 212 | $this->registerAliases(); |
||
| 213 | |||
| 214 | $this->registerProviders(); |
||
| 215 | |||
| 216 | if (! $this->isLoadFilesOnBoot()) { |
||
| 217 | $this->registerFiles(); |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->fireEvent('register'); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function setActive(bool $active): bool |
||
| 224 | { |
||
| 225 | return $this->activator->setActive($this, $active); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function setPath(string $path): self |
||
| 229 | { |
||
| 230 | $this->path = $path; |
||
| 231 | |||
| 232 | return $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | protected function fireEvent(string $event): void |
||
| 236 | { |
||
| 237 | $this->app['events']->dispatch(sprintf('modules.%s.' . $event, $this->getLowerName()), [$this]); |
||
| 238 | } |
||
| 239 | |||
| 240 | protected function isLoadFilesOnBoot(): bool |
||
| 241 | { |
||
| 242 | return config('modules.register.files', 'register') === 'boot'; |
||
| 243 | } |
||
| 244 | |||
| 245 | protected function registerFiles(): void |
||
| 246 | { |
||
| 247 | foreach ($this->get('files', []) as $file) { |
||
| 248 | include $this->path . '/' . $file; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | protected function registerTranslation(): void |
||
| 253 | { |
||
| 254 | $lowerName = $this->getLowerName(); |
||
| 255 | |||
| 256 | $langPath = $this->getPath() . '/resources/lang'; |
||
| 257 | |||
| 258 | if (is_dir($langPath)) { |
||
| 259 | $this->loadTranslationsFrom($langPath, $lowerName); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | private function flushCache(): void |
||
| 264 | { |
||
| 265 | if (config('modules.cache.enabled')) { |
||
| 266 | $this->cache->store()->flush(); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | private function loadTranslationsFrom(string $path, string $namespace): void |
||
| 273 | } |
||
| 274 | |||
| 275 | public function __toString() |
||
| 276 | { |
||
| 277 | return $this->getStudlyName(); |
||
| 278 | } |
||
| 279 | } |
||
| 280 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.