| Total Complexity | 54 |
| Total Lines | 341 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Capsule 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 Capsule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Capsule |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | public $name; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $path; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | public $enabled; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | public $packageCapsule = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | public $loaded = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string|null |
||
| 44 | */ |
||
| 45 | private $singular; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $namespace; |
||
| 51 | |||
| 52 | public function __construct( |
||
| 53 | string $name, |
||
| 54 | string $namespace, |
||
| 55 | string $path, |
||
| 56 | string $singular = null, |
||
| 57 | bool $enabled = true, |
||
| 58 | bool $packageCapsule = false |
||
| 59 | ) { |
||
| 60 | $this->name = $name; |
||
| 61 | $this->path = $path; |
||
| 62 | $this->enabled = $enabled; |
||
| 63 | $this->namespace = $namespace; |
||
| 64 | $this->singular = $singular; |
||
| 65 | $this->packageCapsule = $packageCapsule; |
||
| 66 | |||
| 67 | $this->boot(); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function boot(): void |
||
| 71 | { |
||
| 72 | $this->autoloadConfigFiles(); |
||
| 73 | $this->registerServiceProvider(); |
||
| 74 | $this->registerViews(); |
||
| 75 | $this->loadMigrations(); |
||
| 76 | |||
| 77 | if ($this->packageCapsule) { |
||
| 78 | $this->registerConfig(); |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->registerRoutes(); |
||
| 82 | $this->loadTranslations(); |
||
| 83 | |||
| 84 | $this->loaded = true; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function registerServiceProvider(): void |
||
| 88 | { |
||
| 89 | $serviceProviderName = $this->name . 'CapsuleServiceProvider'; |
||
| 90 | |||
| 91 | if (File::exists($this->path . '/' . $serviceProviderName . '.php')) { |
||
| 92 | App::register($this->namespace . '\\' . $serviceProviderName); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | public function autoloadConfigFiles(): void |
||
| 97 | { |
||
| 98 | $files = $this->getConfig()['autoload']['files'] ?? null; |
||
| 99 | |||
| 100 | if (blank($files)) { |
||
| 101 | return; |
||
| 102 | } |
||
| 103 | |||
| 104 | collect($files)->each(function ($file) { |
||
| 105 | if (file_exists($file)) { |
||
| 106 | require_once $file; |
||
| 107 | } |
||
| 108 | }); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function registerViews(): void |
||
| 112 | { |
||
| 113 | View::addLocation(Str::replaceLast('/' . $this->name, '', $this->path)); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function loadMigrations(): void |
||
| 117 | { |
||
| 118 | $callback = function (Migrator $migrator) { |
||
| 119 | $migrator->path($this->getMigrationsPath()); |
||
| 120 | }; |
||
| 121 | |||
| 122 | App()->afterResolving('migrator', $callback); |
||
| 123 | |||
| 124 | if (app()->resolved('migrator')) { |
||
| 125 | $callback(App::make('migrator')); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | public function registerRoutes(): void { |
||
| 130 | TwillRoutes::registerCapsuleRoutes(App::get('router'), $this); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function loadTranslations(): void { |
||
| 134 | $callback = function (Translator $translator) { |
||
| 135 | $translator->addNamespace($this->getLanguagesPath(), 'twill:capsules:' . $this->getModule()); |
||
|
|
|||
| 136 | }; |
||
| 137 | |||
| 138 | App()->afterResolving('translator', $callback); |
||
| 139 | |||
| 140 | if (app()->resolved('translator')) { |
||
| 141 | $callback(App::make('translator')); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | public function getBasePath(string $path): string |
||
| 146 | { |
||
| 147 | $exploded = explode('/', $path); |
||
| 148 | return implode('/', array_pop($exploded)); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function getModule(): string |
||
| 152 | { |
||
| 153 | return Str::camel($this->name); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getDisplayName(): string |
||
| 157 | { |
||
| 158 | return Str::studly($this->name); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function getPlural(): string |
||
| 162 | { |
||
| 163 | return $this->name; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getSingular(): string |
||
| 167 | { |
||
| 168 | return $this->singular ?? Str::singular($this->name); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function getBaseNamespace(): string |
||
| 172 | { |
||
| 173 | $explodedNamespace = explode('\\', $this->namespace); |
||
| 174 | return implode('\\', array_pop($explodedNamespace)); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function getDatabaseNamespace(): string |
||
| 178 | { |
||
| 179 | return $this->namespace . '\\Database'; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function getDatabasePsr4Path(): string |
||
| 183 | { |
||
| 184 | return $this->path . '/Database'; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function getSeedsNamespace(): string |
||
| 188 | { |
||
| 189 | return $this->namespace . '\\Database\\Seeds'; |
||
| 190 | } |
||
| 191 | |||
| 192 | public function getSeedsPsr4Path(): string |
||
| 193 | { |
||
| 194 | return $this->getDatabasePsr4Path() . '/Seeds'; |
||
| 195 | } |
||
| 196 | |||
| 197 | public function getMigrationsPath(): string |
||
| 198 | { |
||
| 199 | return $this->getDatabasePsr4Path() . '/migrations'; |
||
| 200 | } |
||
| 201 | |||
| 202 | public function getResourcesPath(): string |
||
| 203 | { |
||
| 204 | return $this->getPsr4Path() . '/resources'; |
||
| 205 | } |
||
| 206 | |||
| 207 | public function getLanguagesPath(): string |
||
| 208 | { |
||
| 209 | return $this->getResourcesPath() . '/lang'; |
||
| 210 | } |
||
| 211 | |||
| 212 | public function getViewsPath(): string |
||
| 213 | { |
||
| 214 | return $this->getResourcesPath() . '/views'; |
||
| 215 | } |
||
| 216 | |||
| 217 | public function getModelNamespace(): string |
||
| 218 | { |
||
| 219 | // @todo: config('twill.capsules.namespaces.models'); |
||
| 220 | return $this->namespace . '\\Models'; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function getModelsDir(): string |
||
| 224 | { |
||
| 225 | return $this->getPsr4Path() . '/Models'; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function getRepositoriesNamespace(): string |
||
| 229 | { |
||
| 230 | // @todo: config('twill.capsules.namespaces.repositories'); |
||
| 231 | return $this->namespace . '\\Repositories'; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function getRepositoriesDir(): string |
||
| 235 | { |
||
| 236 | return $this->getPsr4Path() . '/Repositories'; |
||
| 237 | } |
||
| 238 | |||
| 239 | public function getControllersNamespace(): string |
||
| 240 | { |
||
| 241 | // @todo: config('twill.capsules.namespaces.controllers'); |
||
| 242 | return $this->namespace . '\\Http\\Controllers'; |
||
| 243 | } |
||
| 244 | |||
| 245 | public function getControllersDir(): string |
||
| 246 | { |
||
| 247 | return $this->getPsr4Path() . '/Http/Controllers'; |
||
| 248 | } |
||
| 249 | |||
| 250 | public function getRequestsNamespace(): string |
||
| 251 | { |
||
| 252 | // @todo: config('twill.capsules.namespaces.requests'); |
||
| 253 | return $this->namespace . '\\Http\\Requests'; |
||
| 254 | } |
||
| 255 | |||
| 256 | public function getRequestsDir(): string |
||
| 257 | { |
||
| 258 | return $this->getPsr4Path() . '/Http/Requests'; |
||
| 259 | } |
||
| 260 | |||
| 261 | public function getPsr4Path(): string |
||
| 262 | { |
||
| 263 | // @todo: config('twill.capsules.namespaces.subdir'); |
||
| 264 | return $this->path; |
||
| 265 | } |
||
| 266 | |||
| 267 | public function getViewPrefix(): string |
||
| 268 | { |
||
| 269 | return "{$this->getModule()}.resources.views.admin"; |
||
| 270 | } |
||
| 271 | |||
| 272 | public function getRoutesFile(): string |
||
| 273 | { |
||
| 274 | return $this->getPsr4Path() . '/routes/admin.php'; |
||
| 275 | } |
||
| 276 | |||
| 277 | public function routesFileExists(): bool |
||
| 278 | { |
||
| 279 | return file_exists($this->getRoutesFile()); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function getModel(): string |
||
| 283 | { |
||
| 284 | return $this->getModelNamespace() . '\\' . $this->getSingular(); |
||
| 285 | } |
||
| 286 | |||
| 287 | public function getTranslationModel(): string |
||
| 288 | { |
||
| 289 | return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Translation'; |
||
| 290 | } |
||
| 291 | |||
| 292 | public function getSlugModel(): string |
||
| 293 | { |
||
| 294 | return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Slug'; |
||
| 295 | } |
||
| 296 | |||
| 297 | public function getRevisionModel(): string |
||
| 298 | { |
||
| 299 | return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Revision'; |
||
| 300 | } |
||
| 301 | |||
| 302 | public function getRepositoryClass(): string |
||
| 303 | { |
||
| 304 | return $this->getRepositoriesNamespace() . '\\' . $this->getSingular() . 'Repository'; |
||
| 305 | } |
||
| 306 | |||
| 307 | public function getControllerClass(): string |
||
| 308 | { |
||
| 309 | return $this->getControllersNamespace() . '\\' . $this->getSingular() . 'Controller'; |
||
| 310 | } |
||
| 311 | |||
| 312 | public function getFormRequestClass(): string |
||
| 315 | } |
||
| 316 | |||
| 317 | public function getConfigFile(): string |
||
| 318 | { |
||
| 319 | return $this->path . '/config.php'; |
||
| 320 | } |
||
| 321 | |||
| 322 | public function getConfig(): array |
||
| 328 | } |
||
| 329 | |||
| 330 | public function registerConfig(): void |
||
| 331 | { |
||
| 332 | $config = Config::get('twill-navigation', []); |
||
| 333 | |||
| 334 | if ($this->isSingleton()) { |
||
| 335 | $config[lcfirst($this->getSingular())] = [ |
||
| 336 | 'title' => $this->name, |
||
| 337 | 'singleton' => true, |
||
| 338 | ]; |
||
| 339 | } |
||
| 340 | else { |
||
| 341 | $config[$this->name] = [ |
||
| 342 | 'title' => $this->name, |
||
| 343 | 'module' => true, |
||
| 344 | ]; |
||
| 345 | } |
||
| 346 | |||
| 347 | Config::set('twill-navigation', $config); |
||
| 348 | } |
||
| 349 | |||
| 350 | public function isSingleton(): bool { |
||
| 351 | return is_subclass_of($this->getControllerClass(), SingletonModuleController::class); |
||
| 352 | } |
||
| 353 | |||
| 354 | public function getType(): string { |
||
| 355 | return ''; |
||
| 356 | } |
||
| 357 | } |
||
| 358 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.