Complex classes like Application 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 Application, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | abstract class Application extends Module |
||
49 | { |
||
50 | /** |
||
51 | * @event Event an event raised before the application starts to handle a request. |
||
52 | */ |
||
53 | const EVENT_BEFORE_REQUEST = 'beforeRequest'; |
||
54 | /** |
||
55 | * @event Event an event raised after the application successfully handles a request (before the response is sent out). |
||
56 | */ |
||
57 | const EVENT_AFTER_REQUEST = 'afterRequest'; |
||
58 | /** |
||
59 | * Application state used by [[state]]: application just started. |
||
60 | */ |
||
61 | const STATE_BEGIN = 0; |
||
62 | /** |
||
63 | * Application state used by [[state]]: application is initializing. |
||
64 | */ |
||
65 | const STATE_INIT = 1; |
||
66 | /** |
||
67 | * Application state used by [[state]]: application is triggering [[EVENT_BEFORE_REQUEST]]. |
||
68 | */ |
||
69 | const STATE_BEFORE_REQUEST = 2; |
||
70 | /** |
||
71 | * Application state used by [[state]]: application is handling the request. |
||
72 | */ |
||
73 | const STATE_HANDLING_REQUEST = 3; |
||
74 | /** |
||
75 | * Application state used by [[state]]: application is triggering [[EVENT_AFTER_REQUEST]].. |
||
76 | */ |
||
77 | const STATE_AFTER_REQUEST = 4; |
||
78 | /** |
||
79 | * Application state used by [[state]]: application is about to send response. |
||
80 | */ |
||
81 | const STATE_SENDING_RESPONSE = 5; |
||
82 | /** |
||
83 | * Application state used by [[state]]: application has ended. |
||
84 | */ |
||
85 | const STATE_END = 6; |
||
86 | |||
87 | /** |
||
88 | * @var string the namespace that controller classes are located in. |
||
89 | * This namespace will be used to load controller classes by prepending it to the controller class name. |
||
90 | * The default namespace is `app\controllers`. |
||
91 | * |
||
92 | * Please refer to the [guide about class autoloading](guide:concept-autoloading.md) for more details. |
||
93 | */ |
||
94 | public $controllerNamespace = 'app\\controllers'; |
||
95 | /** |
||
96 | * @var string the application name. |
||
97 | */ |
||
98 | public $name = 'My Application'; |
||
99 | /** |
||
100 | * @var string the charset currently used for the application. |
||
101 | */ |
||
102 | public $charset = 'UTF-8'; |
||
103 | /** |
||
104 | * @var string the language that is meant to be used for end users. It is recommended that you |
||
105 | * use [IETF language tags](http://en.wikipedia.org/wiki/IETF_language_tag). For example, `en` stands |
||
106 | * for English, while `en-US` stands for English (United States). |
||
107 | * @see sourceLanguage |
||
108 | */ |
||
109 | public $language = 'en-US'; |
||
110 | /** |
||
111 | * @var string the language that the application is written in. This mainly refers to |
||
112 | * the language that the messages and view files are written in. |
||
113 | * @see language |
||
114 | */ |
||
115 | public $sourceLanguage = 'en-US'; |
||
116 | /** |
||
117 | * @var Controller the currently active controller instance |
||
118 | */ |
||
119 | public $controller; |
||
120 | /** |
||
121 | * @var string|bool the layout that should be applied for views in this application. Defaults to 'main'. |
||
122 | * If this is false, layout will be disabled. |
||
123 | */ |
||
124 | public $layout = 'main'; |
||
125 | /** |
||
126 | * @var string the requested route |
||
127 | */ |
||
128 | public $requestedRoute; |
||
129 | /** |
||
130 | * @var Action the requested Action. If null, it means the request cannot be resolved into an action. |
||
131 | */ |
||
132 | public $requestedAction; |
||
133 | /** |
||
134 | * @var array the parameters supplied to the requested action. |
||
135 | */ |
||
136 | public $requestedParams; |
||
137 | /** |
||
138 | * @var array list of installed Yii extensions. Each array element represents a single extension |
||
139 | * with the following structure: |
||
140 | * |
||
141 | * ```php |
||
142 | * [ |
||
143 | * 'name' => 'extension name', |
||
144 | * 'version' => 'version number', |
||
145 | * 'bootstrap' => 'BootstrapClassName', // optional, may also be a configuration array |
||
146 | * 'alias' => [ |
||
147 | * '@alias1' => 'to/path1', |
||
148 | * '@alias2' => 'to/path2', |
||
149 | * ], |
||
150 | * ] |
||
151 | * ``` |
||
152 | * |
||
153 | * The "bootstrap" class listed above will be instantiated during the application |
||
154 | * [[bootstrap()|bootstrapping process]]. If the class implements [[BootstrapInterface]], |
||
155 | * its [[BootstrapInterface::bootstrap()|bootstrap()]] method will be also be called. |
||
156 | * |
||
157 | * If not set explicitly in the application config, this property will be populated with the contents of |
||
158 | * `@vendor/yiisoft/extensions.php`. |
||
159 | */ |
||
160 | public $extensions; |
||
161 | /** |
||
162 | * @var array list of components that should be run during the application [[bootstrap()|bootstrapping process]]. |
||
163 | * |
||
164 | * Each component may be specified in one of the following formats: |
||
165 | * |
||
166 | * - an application component ID as specified via [[components]]. |
||
167 | * - a module ID as specified via [[modules]]. |
||
168 | * - a class name. |
||
169 | * - a configuration array. |
||
170 | * |
||
171 | * During the bootstrapping process, each component will be instantiated. If the component class |
||
172 | * implements [[BootstrapInterface]], its [[BootstrapInterface::bootstrap()|bootstrap()]] method |
||
173 | * will be also be called. |
||
174 | */ |
||
175 | public $bootstrap = []; |
||
176 | /** |
||
177 | * @var int the current application state during a request handling life cycle. |
||
178 | * This property is managed by the application. Do not modify this property. |
||
179 | */ |
||
180 | public $state; |
||
181 | /** |
||
182 | * @var array list of loaded modules indexed by their class names. |
||
183 | */ |
||
184 | public $loadedModules = []; |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Constructor. |
||
189 | * @param array $config name-value pairs that will be used to initialize the object properties. |
||
190 | * Note that the configuration must contain both [[id]] and [[basePath]]. |
||
191 | * @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing. |
||
192 | */ |
||
193 | 2292 | public function __construct($config = []) |
|
194 | { |
||
195 | 2292 | Yii::$app = $this; |
|
|
|||
196 | 2292 | static::setInstance($this); |
|
197 | |||
198 | 2292 | $this->state = self::STATE_BEGIN; |
|
199 | |||
200 | 2292 | $this->preInit($config); |
|
201 | |||
202 | 2292 | $this->registerErrorHandler($config); |
|
203 | |||
204 | 2292 | Component::__construct($config); |
|
205 | 2292 | } |
|
206 | |||
207 | /** |
||
208 | * Pre-initializes the application. |
||
209 | * This method is called at the beginning of the application constructor. |
||
210 | * It initializes several important application properties. |
||
211 | * If you override this method, please make sure you call the parent implementation. |
||
212 | * @param array $config the application configuration |
||
213 | * @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing. |
||
214 | */ |
||
215 | 2292 | public function preInit(&$config) |
|
216 | { |
||
217 | 2292 | if (!isset($config['id'])) { |
|
218 | throw new InvalidConfigException('The "id" configuration for the Application is required.'); |
||
219 | } |
||
220 | 2292 | if (isset($config['basePath'])) { |
|
221 | 2292 | $this->setBasePath($config['basePath']); |
|
222 | 2292 | unset($config['basePath']); |
|
223 | 2292 | } else { |
|
224 | throw new InvalidConfigException('The "basePath" configuration for the Application is required.'); |
||
225 | } |
||
226 | |||
227 | 2292 | if (isset($config['vendorPath'])) { |
|
228 | 2292 | $this->setVendorPath($config['vendorPath']); |
|
229 | 2292 | unset($config['vendorPath']); |
|
230 | 2292 | } else { |
|
231 | // set "@vendor" |
||
232 | $this->getVendorPath(); |
||
233 | } |
||
234 | 2292 | if (isset($config['runtimePath'])) { |
|
235 | $this->setRuntimePath($config['runtimePath']); |
||
236 | unset($config['runtimePath']); |
||
237 | } else { |
||
238 | // set "@runtime" |
||
239 | 2292 | $this->getRuntimePath(); |
|
240 | } |
||
241 | |||
242 | 2292 | if (isset($config['timeZone'])) { |
|
243 | 363 | $this->setTimeZone($config['timeZone']); |
|
244 | 363 | unset($config['timeZone']); |
|
245 | 2292 | } elseif (!ini_get('date.timezone')) { |
|
246 | $this->setTimeZone('UTC'); |
||
247 | } |
||
248 | |||
249 | 2292 | if (isset($config['container'])) { |
|
250 | 1 | $this->setContainer($config['container']); |
|
251 | |||
252 | 1 | unset($config['container']); |
|
253 | 1 | } |
|
254 | |||
255 | // merge core components with custom components |
||
256 | 2292 | foreach ($this->coreComponents() as $id => $component) { |
|
257 | 2292 | if (!isset($config['components'][$id])) { |
|
258 | 2292 | $config['components'][$id] = $component; |
|
259 | 2292 | } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) { |
|
260 | 162 | $config['components'][$id]['class'] = $component['class']; |
|
261 | 162 | } |
|
262 | 2292 | } |
|
263 | 2292 | } |
|
264 | |||
265 | /** |
||
266 | * @inheritdoc |
||
267 | */ |
||
268 | 2292 | public function init() |
|
269 | { |
||
270 | 2292 | $this->state = self::STATE_INIT; |
|
271 | 2292 | $this->bootstrap(); |
|
272 | 2292 | } |
|
273 | |||
274 | /** |
||
275 | * Initializes extensions and executes bootstrap components. |
||
276 | * This method is called by [[init()]] after the application has been fully configured. |
||
277 | * If you override this method, make sure you also call the parent implementation. |
||
278 | */ |
||
279 | 2292 | protected function bootstrap() |
|
280 | { |
||
281 | 2292 | if ($this->extensions === null) { |
|
282 | 2292 | $file = Yii::getAlias('@vendor/yiisoft/extensions.php'); |
|
283 | 2292 | $this->extensions = is_file($file) ? include($file) : []; |
|
284 | 2292 | } |
|
285 | 2292 | foreach ($this->extensions as $extension) { |
|
286 | if (!empty($extension['alias'])) { |
||
287 | foreach ($extension['alias'] as $name => $path) { |
||
288 | Yii::setAlias($name, $path); |
||
289 | } |
||
290 | } |
||
291 | if (isset($extension['bootstrap'])) { |
||
292 | $component = Yii::createObject($extension['bootstrap']); |
||
293 | if ($component instanceof BootstrapInterface) { |
||
294 | Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__); |
||
295 | $component->bootstrap($this); |
||
296 | } else { |
||
297 | Yii::trace('Bootstrap with ' . get_class($component), __METHOD__); |
||
298 | } |
||
299 | } |
||
300 | 2292 | } |
|
301 | |||
302 | 2292 | foreach ($this->bootstrap as $class) { |
|
303 | 1 | $component = null; |
|
304 | 1 | if (is_string($class)) { |
|
305 | 1 | if ($this->has($class)) { |
|
306 | 1 | $component = $this->get($class); |
|
307 | 1 | } elseif ($this->hasModule($class)) { |
|
308 | $component = $this->getModule($class); |
||
309 | } elseif (strpos($class, '\\') === false) { |
||
310 | throw new InvalidConfigException("Unknown bootstrapping component ID: $class"); |
||
311 | } |
||
312 | 1 | } |
|
313 | 1 | if (!isset($component)) { |
|
314 | $component = Yii::createObject($class); |
||
315 | } |
||
316 | |||
317 | 1 | if ($component instanceof BootstrapInterface) { |
|
318 | Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__); |
||
319 | $component->bootstrap($this); |
||
320 | } else { |
||
321 | 1 | Yii::trace('Bootstrap with ' . get_class($component), __METHOD__); |
|
322 | } |
||
323 | 2292 | } |
|
324 | 2292 | } |
|
325 | |||
326 | /** |
||
327 | * Registers the errorHandler component as a PHP error handler. |
||
328 | * @param array $config application config |
||
329 | */ |
||
330 | 2292 | protected function registerErrorHandler(&$config) |
|
331 | { |
||
332 | 2292 | if (YII_ENABLE_ERROR_HANDLER) { |
|
333 | if (!isset($config['components']['errorHandler']['class'])) { |
||
334 | echo "Error: no errorHandler component is configured.\n"; |
||
335 | exit(1); |
||
336 | } |
||
337 | $this->set('errorHandler', $config['components']['errorHandler']); |
||
338 | unset($config['components']['errorHandler']); |
||
339 | $this->getErrorHandler()->register(); |
||
340 | } |
||
341 | 2292 | } |
|
342 | |||
343 | /** |
||
344 | * Returns an ID that uniquely identifies this module among all modules within the current application. |
||
345 | * Since this is an application instance, it will always return an empty string. |
||
346 | * @return string the unique ID of the module. |
||
347 | */ |
||
348 | public function getUniqueId() |
||
349 | { |
||
350 | return ''; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Sets the root directory of the application and the @app alias. |
||
355 | * This method can only be invoked at the beginning of the constructor. |
||
356 | * @param string $path the root directory of the application. |
||
357 | * @property string the root directory of the application. |
||
358 | * @throws InvalidParamException if the directory does not exist. |
||
359 | */ |
||
360 | 2292 | public function setBasePath($path) |
|
361 | { |
||
362 | 2292 | parent::setBasePath($path); |
|
363 | 2292 | Yii::setAlias('@app', $this->getBasePath()); |
|
364 | 2292 | } |
|
365 | |||
366 | /** |
||
367 | * Runs the application. |
||
368 | * This is the main entrance of an application. |
||
369 | * @return int the exit status (0 means normal, non-zero values mean abnormal) |
||
370 | */ |
||
371 | public function run() |
||
372 | { |
||
373 | try { |
||
374 | $this->state = self::STATE_BEFORE_REQUEST; |
||
375 | $this->trigger(self::EVENT_BEFORE_REQUEST); |
||
376 | |||
377 | $this->state = self::STATE_HANDLING_REQUEST; |
||
378 | $response = $this->handleRequest($this->getRequest()); |
||
379 | |||
380 | $this->state = self::STATE_AFTER_REQUEST; |
||
381 | $this->trigger(self::EVENT_AFTER_REQUEST); |
||
382 | |||
383 | $this->state = self::STATE_SENDING_RESPONSE; |
||
384 | $response->send(); |
||
385 | |||
386 | $this->state = self::STATE_END; |
||
387 | |||
388 | return $response->exitStatus; |
||
389 | } catch (ExitException $e) { |
||
390 | $this->end($e->statusCode, isset($response) ? $response : null); |
||
391 | return $e->statusCode; |
||
392 | } |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Handles the specified request. |
||
397 | * |
||
398 | * This method should return an instance of [[Response]] or its child class |
||
399 | * which represents the handling result of the request. |
||
400 | * |
||
401 | * @param Request $request the request to be handled |
||
402 | * @return Response the resulting response |
||
403 | */ |
||
404 | abstract public function handleRequest($request); |
||
405 | |||
406 | private $_runtimePath; |
||
407 | |||
408 | /** |
||
409 | * Returns the directory that stores runtime files. |
||
410 | * @return string the directory that stores runtime files. |
||
411 | * Defaults to the "runtime" subdirectory under [[basePath]]. |
||
412 | */ |
||
413 | 2292 | public function getRuntimePath() |
|
421 | |||
422 | /** |
||
423 | * Sets the directory that stores runtime files. |
||
424 | * @param string $path the directory that stores runtime files. |
||
425 | */ |
||
426 | 2292 | public function setRuntimePath($path) |
|
431 | |||
432 | private $_vendorPath; |
||
433 | |||
434 | /** |
||
435 | * Returns the directory that stores vendor files. |
||
436 | * @return string the directory that stores vendor files. |
||
437 | * Defaults to "vendor" directory under [[basePath]]. |
||
438 | */ |
||
439 | public function getVendorPath() |
||
447 | |||
448 | /** |
||
449 | * Sets the directory that stores vendor files. |
||
450 | * @param string $path the directory that stores vendor files. |
||
451 | */ |
||
452 | 2292 | public function setVendorPath($path) |
|
459 | |||
460 | /** |
||
461 | * Returns the time zone used by this application. |
||
462 | * This is a simple wrapper of PHP function date_default_timezone_get(). |
||
463 | * If time zone is not configured in php.ini or application config, |
||
464 | * it will be set to UTC by default. |
||
465 | * @return string the time zone used by this application. |
||
466 | * @see http://php.net/manual/en/function.date-default-timezone-get.php |
||
467 | */ |
||
468 | 259 | public function getTimeZone() |
|
472 | |||
473 | /** |
||
474 | * Sets the time zone used by this application. |
||
475 | * This is a simple wrapper of PHP function date_default_timezone_set(). |
||
476 | * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones. |
||
477 | * @param string $value the time zone used by this application. |
||
478 | * @see http://php.net/manual/en/function.date-default-timezone-set.php |
||
479 | */ |
||
480 | 363 | public function setTimeZone($value) |
|
484 | |||
485 | /** |
||
486 | * Returns the database connection component. |
||
487 | * @return \yii\db\Connection the database connection. |
||
488 | */ |
||
489 | 34 | public function getDb() |
|
493 | |||
494 | /** |
||
495 | * Returns the log dispatcher component. |
||
496 | * @return \yii\log\Dispatcher the log dispatcher application component. |
||
497 | */ |
||
498 | public function getLog() |
||
502 | |||
503 | /** |
||
504 | * Returns the error handler component. |
||
505 | * @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component. |
||
506 | */ |
||
507 | public function getErrorHandler() |
||
511 | |||
512 | /** |
||
513 | * Returns the cache component. |
||
514 | * @return \yii\caching\Cache the cache application component. Null if the component is not enabled. |
||
515 | */ |
||
516 | public function getCache() |
||
520 | |||
521 | /** |
||
522 | * Returns the formatter component. |
||
523 | * @return \yii\i18n\Formatter the formatter application component. |
||
524 | */ |
||
525 | 15 | public function getFormatter() |
|
529 | |||
530 | /** |
||
531 | * Returns the request component. |
||
532 | * @return \yii\web\Request|\yii\console\Request the request component. |
||
533 | */ |
||
534 | public function getRequest() |
||
538 | |||
539 | /** |
||
540 | * Returns the response component. |
||
541 | * @return \yii\web\Response|\yii\console\Response the response component. |
||
542 | */ |
||
543 | public function getResponse() |
||
547 | |||
548 | /** |
||
549 | * Returns the view object. |
||
550 | * @return View|\yii\web\View the view application component that is used to render various view files. |
||
551 | */ |
||
552 | 24 | public function getView() |
|
556 | |||
557 | /** |
||
558 | * Returns the URL manager for this application. |
||
559 | * @return \yii\web\UrlManager the URL manager for this application. |
||
560 | */ |
||
561 | 27 | public function getUrlManager() |
|
565 | |||
566 | /** |
||
567 | * Returns the internationalization (i18n) component |
||
568 | * @return \yii\i18n\I18N the internationalization application component. |
||
569 | */ |
||
570 | 590 | public function getI18n() |
|
574 | |||
575 | /** |
||
576 | * Returns the mailer component. |
||
577 | * @return \yii\mail\MailerInterface the mailer application component. |
||
578 | */ |
||
579 | public function getMailer() |
||
583 | |||
584 | /** |
||
585 | * Returns the auth manager for this application. |
||
586 | * @return \yii\rbac\ManagerInterface the auth manager application component. |
||
587 | * Null is returned if auth manager is not configured. |
||
588 | */ |
||
589 | public function getAuthManager() |
||
593 | |||
594 | /** |
||
595 | * Returns the asset manager. |
||
596 | * @return \yii\web\AssetManager the asset manager application component. |
||
597 | */ |
||
598 | 3 | public function getAssetManager() |
|
602 | |||
603 | /** |
||
604 | * Returns the security component. |
||
605 | * @return \yii\base\Security the security application component. |
||
606 | */ |
||
607 | 29 | public function getSecurity() |
|
611 | |||
612 | /** |
||
613 | * Returns the configuration of core application components. |
||
614 | * @see set() |
||
615 | */ |
||
616 | 2292 | public function coreComponents() |
|
629 | |||
630 | /** |
||
631 | * Terminates the application. |
||
632 | * This method replaces the `exit()` function by ensuring the application life cycle is completed |
||
633 | * before terminating the application. |
||
634 | * @param int $status the exit status (value 0 means normal exit while other values mean abnormal exit). |
||
635 | * @param Response $response the response to be sent. If not set, the default application [[response]] component will be used. |
||
636 | * @throws ExitException if the application is in testing mode |
||
637 | */ |
||
638 | 3 | public function end($status = 0, $response = null) |
|
657 | |||
658 | /** |
||
659 | * Configures [[Yii::$container]] with the $config |
||
660 | * |
||
661 | * @param array $config values given in terms of name-value pairs |
||
662 | * @since 2.0.11 |
||
663 | */ |
||
664 | 1 | public function setContainer($config) |
|
668 | } |
||
669 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..