| Total Complexity | 41 | 
| Total Lines | 257 | 
| Duplicated Lines | 0 % | 
| Changes | 5 | ||
| Bugs | 2 | Features | 0 | 
Complex classes like BaseServiceProvider 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 BaseServiceProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 26 | abstract class BaseServiceProvider extends ServiceProvider implements Module  | 
            ||
| 27 | { | 
            ||
| 28 | use HasModuleConfig;  | 
            ||
| 29 | |||
| 30 | /** @var string */  | 
            ||
| 31 | protected $basePath;  | 
            ||
| 32 | |||
| 33 | /** @var string */  | 
            ||
| 34 | protected $namespaceRoot;  | 
            ||
| 35 | |||
| 36 | /** @var string */  | 
            ||
| 37 | protected $id;  | 
            ||
| 38 | |||
| 39 | /** @var array */  | 
            ||
| 40 | protected $models = [];  | 
            ||
| 41 | |||
| 42 | /** @var array */  | 
            ||
| 43 | protected $enums = [];  | 
            ||
| 44 | |||
| 45 | /** @var array */  | 
            ||
| 46 | protected $requests = [];  | 
            ||
| 47 | |||
| 48 | /** @var ConcordContract */  | 
            ||
| 49 | protected $concord;  | 
            ||
| 50 | |||
| 51 | /** @var Convention */  | 
            ||
| 52 | protected $convention;  | 
            ||
| 53 | |||
| 54 | /** @var Kind */  | 
            ||
| 55 | protected $kind;  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * ModuleServiceProvider class constructor  | 
            ||
| 59 | *  | 
            ||
| 60 | * @param \Illuminate\Contracts\Foundation\Application $app  | 
            ||
| 61 | */  | 
            ||
| 62 | public function __construct($app)  | 
            ||
| 63 |     { | 
            ||
| 64 | parent::__construct($app);  | 
            ||
| 65 | |||
| 66 | $this->concord = $app->make(ConcordContract::class); // retrieve the concord singleton  | 
            ||
| 67 | $this->convention = $this->concord->getConvention(); // storing to get rid of train wrecks  | 
            ||
| 68 | $this->kind = Kind::create(static::$_kind);  | 
            ||
| 69 | $this->basePath = dirname(dirname((new ReflectionClass(static::class))->getFileName()));  | 
            ||
| 70 | $this->namespaceRoot = str_replace(  | 
            ||
| 71 | sprintf(  | 
            ||
| 72 | '\\%s\\ModuleServiceProvider',  | 
            ||
| 73 |                 str_replace('/', '\\', $this->convention->providersFolder()) | 
            ||
| 74 | ),  | 
            ||
| 75 | '',  | 
            ||
| 76 | static::class  | 
            ||
| 77 | );  | 
            ||
| 78 | $this->id = $this->getModuleId();  | 
            ||
| 79 | }  | 
            ||
| 80 | |||
| 81 | public function register()  | 
            ||
| 82 |     { | 
            ||
| 83 | $this->loadConfiguration();  | 
            ||
| 84 | |||
| 85 |         if (true === $this->config('event_listeners')) { | 
            ||
| 86 | $this->registerEventServiceProvider();  | 
            ||
| 87 | }  | 
            ||
| 88 | }  | 
            ||
| 89 | |||
| 90 | /**  | 
            ||
| 91 | * @inheritdoc  | 
            ||
| 92 | */  | 
            ||
| 93 | public function boot()  | 
            ||
| 117 | }  | 
            ||
| 118 | |||
| 119 | public function getId(): string  | 
            ||
| 120 |     { | 
            ||
| 121 | return $this->id;  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | /**  | 
            ||
| 125 | * Returns the root folder on the filesystem containing the module  | 
            ||
| 126 | *  | 
            ||
| 127 | * @return string  | 
            ||
| 128 | */  | 
            ||
| 129 | public function getBasePath(): string  | 
            ||
| 132 | }  | 
            ||
| 133 | |||
| 134 | /**  | 
            ||
| 135 | * @inheritdoc  | 
            ||
| 136 | */  | 
            ||
| 137 | public function getKind(): Kind  | 
            ||
| 138 |     { | 
            ||
| 139 | return $this->kind;  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | /**  | 
            ||
| 143 | * Returns the folder where the module/box configuration files are  | 
            ||
| 144 | *  | 
            ||
| 145 | * @return string  | 
            ||
| 146 | */  | 
            ||
| 147 | public function getConfigPath(): string  | 
            ||
| 148 |     { | 
            ||
| 149 | return $this->getBasePath() . '/' . $this->convention->configFolder();  | 
            ||
| 150 | }  | 
            ||
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * Returns the module's root (topmost) namespace  | 
            ||
| 154 | *  | 
            ||
| 155 | * @return string  | 
            ||
| 156 | */  | 
            ||
| 157 | public function getNamespaceRoot(): string  | 
            ||
| 158 |     { | 
            ||
| 159 | return $this->namespaceRoot;  | 
            ||
| 160 | }  | 
            ||
| 161 | |||
| 162 | /**  | 
            ||
| 163 | * Returns the short (abbreviated) name of the module  | 
            ||
| 164 | * E.g. Konekt\AppShell => app_shell  | 
            ||
| 165 | */  | 
            ||
| 166 | public function shortName()  | 
            ||
| 167 |     { | 
            ||
| 168 | $id = $this->getModuleId();  | 
            ||
| 169 | $p = strrpos($id, '.');  | 
            ||
| 170 | |||
| 171 | return $p ? substr($id, $p + 1) : $id;  | 
            ||
| 172 | }  | 
            ||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * Returns a standard module name based on the module provider's classname  | 
            ||
| 176 | *  | 
            ||
| 177 | * Eg.: '\Vendor\Module\Services\ModuleServiceProvider' -> 'vendor.module'  | 
            ||
| 178 | *  | 
            ||
| 179 | * @param string $classname  | 
            ||
| 180 | *  | 
            ||
| 181 | * @see concord_module_id  | 
            ||
| 182 | *  | 
            ||
| 183 | * @return string  | 
            ||
| 184 | */  | 
            ||
| 185 | protected function getModuleId($classname = null)  | 
            ||
| 186 |     { | 
            ||
| 187 | return concord_module_id($classname ?: static::class);  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 188 | }  | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Register the module's migrations  | 
            ||
| 192 | */  | 
            ||
| 193 | protected function registerMigrations()  | 
            ||
| 194 |     { | 
            ||
| 195 | $path = $this->getBasePath() . '/' . $this->convention->migrationsFolder();  | 
            ||
| 196 | |||
| 197 |         if ($this->app->runningInConsole() && is_dir($path)) { | 
            ||
| 198 | $this->loadMigrationsFrom($path);  | 
            ||
| 199 | }  | 
            ||
| 200 | }  | 
            ||
| 201 | |||
| 202 | /**  | 
            ||
| 203 | * Register models in a box/module  | 
            ||
| 204 | */  | 
            ||
| 205 | protected function registerModels()  | 
            ||
| 206 |     { | 
            ||
| 207 |         foreach ($this->models as $key => $model) { | 
            ||
| 208 | $contract = is_string($key) ? $key : $this->convention->contractForModel($model);  | 
            ||
| 209 |             $this->concord->registerModel($contract, $model, config('concord.register_route_models', true)); | 
            ||
| 210 | }  | 
            ||
| 211 | }  | 
            ||
| 212 | |||
| 213 | /**  | 
            ||
| 214 | * Register enums in a box/module  | 
            ||
| 215 | */  | 
            ||
| 216 | protected function registerEnums()  | 
            ||
| 217 |     { | 
            ||
| 218 |         foreach ($this->enums as $key => $enum) { | 
            ||
| 219 | $contract = is_string($key) ? $key : $this->convention->contractForEnum($enum);  | 
            ||
| 220 | $this->concord->registerEnum($contract, $enum);  | 
            ||
| 221 | }  | 
            ||
| 222 | }  | 
            ||
| 223 | |||
| 224 | /**  | 
            ||
| 225 | * Register request types in a box/module  | 
            ||
| 226 | */  | 
            ||
| 227 | protected function registerRequestTypes()  | 
            ||
| 228 |     { | 
            ||
| 229 |         foreach ($this->requests as $key => $requestType) { | 
            ||
| 230 | $contract = is_string($key) ? $key : $this->convention->contractForRequest($requestType);  | 
            ||
| 231 | $this->concord->registerRequest($contract, $requestType);  | 
            ||
| 232 | }  | 
            ||
| 233 | }  | 
            ||
| 234 | |||
| 235 | /**  | 
            ||
| 236 | * Register the views folder, in a separate namespace  | 
            ||
| 237 | */  | 
            ||
| 238 | protected function registerViews()  | 
            ||
| 245 | }  | 
            ||
| 246 | }  | 
            ||
| 247 | |||
| 248 | /**  | 
            ||
| 249 | * Registers the event service provider of the module/config (ie. event-listener bindings)  | 
            ||
| 250 | */  | 
            ||
| 251 | protected function registerEventServiceProvider()  | 
            ||
| 252 |     { | 
            ||
| 253 | $eventServiceProviderClass = sprintf(  | 
            ||
| 254 | '%s\\%s\\EventServiceProvider',  | 
            ||
| 255 | $this->namespaceRoot,  | 
            ||
| 256 |             str_replace('/', '\\', $this->convention->providersFolder()) | 
            ||
| 257 | );  | 
            ||
| 258 | |||
| 259 |         if (class_exists($eventServiceProviderClass)) { | 
            ||
| 260 | $this->app->register($eventServiceProviderClass);  | 
            ||
| 261 | }  | 
            ||
| 262 | }  | 
            ||
| 263 | |||
| 264 | protected function loadConfiguration()  | 
            ||
| 270 | }  | 
            ||
| 271 | }  | 
            ||
| 272 | |||
| 273 | protected function registerRoutes($routes): void  | 
            ||
| 283 | }  | 
            ||
| 284 | }  | 
            ||
| 285 | }  | 
            ||
| 286 | }  | 
            ||
| 287 |