Complex classes like Container 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Container extends BaseContainer implements LaravelApplication |
||
18 | { |
||
19 | /** |
||
20 | * A custom callback used to configure Monolog. |
||
21 | * |
||
22 | * @var callable|null |
||
23 | */ |
||
24 | protected $monologConfigurator; |
||
25 | |||
26 | /** |
||
27 | * The application namespace. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $namespace; |
||
32 | |||
33 | /** |
||
34 | * The custom environment path defined by the developer. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $environmentPath; |
||
39 | |||
40 | /** |
||
41 | * The environment file to load during bootstrapping. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $environmentFile = '.env'; |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 2 | public function version() |
|
54 | |||
55 | /** |
||
56 | * Get the base path of the Laravel installation. |
||
57 | * |
||
58 | * @param string $path |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | 57 | public function basePath($path = '') |
|
66 | |||
67 | /** |
||
68 | * Get the path to the application configuration files. |
||
69 | * |
||
70 | * @param string $path |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 57 | public function configPath($path = '') |
|
78 | |||
79 | /** |
||
80 | * Get the path to the database directory. |
||
81 | * |
||
82 | * @param string $path |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 57 | public function databasePath($path = '') |
|
90 | |||
91 | /** |
||
92 | * Get the path to the language files. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 1 | public function langPath() |
|
100 | |||
101 | /** |
||
102 | * Get the path to the resources directory. |
||
103 | * |
||
104 | * @param string $path |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 3 | public function resourcePath($path = '') |
|
112 | |||
113 | /** |
||
114 | * Get the path to the storage directory. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 57 | public function storagePath() |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 57 | public function environment() |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 1 | public function runningInConsole() |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 57 | public function getNamespace() |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 1 | public function isDownForMaintenance() |
|
165 | |||
166 | /** |
||
167 | * Get the path to the environment file directory. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 2 | public function environmentPath() |
|
175 | |||
176 | /** |
||
177 | * Set the directory for the environment file. |
||
178 | * |
||
179 | * @param string $path |
||
180 | * @return $this |
||
181 | */ |
||
182 | 1 | public function useEnvironmentPath($path) |
|
188 | |||
189 | /** |
||
190 | * Set the environment file to be loaded during bootstrapping. |
||
191 | * |
||
192 | * @param string $file |
||
193 | * @return $this |
||
194 | */ |
||
195 | 1 | public function loadEnvironmentFrom($file) |
|
201 | |||
202 | /** |
||
203 | * Get the environment file the application is using. |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 2 | public function environmentFile() |
|
211 | |||
212 | /** |
||
213 | * Get the fully qualified path to the environment file. |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | 1 | public function environmentFilePath() |
|
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | 1 | public function configurationIsCached() |
|
229 | |||
230 | /** |
||
231 | * Throw an Console Exception with the given data unless the given condition is true. |
||
232 | * |
||
233 | * @param int $code |
||
234 | * @param string $message |
||
235 | * @param array $headers |
||
236 | * @return void |
||
237 | * |
||
238 | * @throws \Symfony\Component\Console\Exception\CommandNotFoundException |
||
239 | * @throws \LaravelZero\Framework\Contracts\Exceptions\ConsoleException |
||
240 | */ |
||
241 | 1 | public function abort($code, $message = '', array $headers = []) |
|
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | 1 | public function registerConfiguredProviders() |
|
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | 1 | public function register($provider, $options = [], $force = false) |
|
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | 1 | public function registerDeferredProvider($provider, $service = null) |
|
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | 1 | public function boot() |
|
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | 1 | public function booting($callback) |
|
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | 1 | public function booted($callback) |
|
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | 1 | public function getCachedServicesPath() |
|
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function getCachedPackagesPath() |
||
313 | |||
314 | /** |
||
315 | * Define a callback to be used to configure Monolog. |
||
316 | * |
||
317 | * @param callable $callback |
||
318 | * |
||
319 | * @return $this |
||
320 | */ |
||
321 | 1 | public function configureMonologUsing(callable $callback) |
|
327 | |||
328 | /** |
||
329 | * Determine if the application has a custom Monolog configurator. |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | 1 | public function hasMonologConfigurator() |
|
337 | |||
338 | /** |
||
339 | * Get the custom Monolog configurator for the application. |
||
340 | * |
||
341 | * @return callable |
||
342 | */ |
||
343 | 1 | public function getMonologConfigurator() |
|
347 | } |
||
348 |
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 mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.