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