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 |
||
50 | abstract class Application extends Module |
||
51 | { |
||
52 | /** |
||
53 | * @event Event an event raised before the application starts to handle a request. |
||
54 | */ |
||
55 | const EVENT_BEFORE_REQUEST = 'beforeRequest'; |
||
56 | /** |
||
57 | * @event Event an event raised after the application successfully handles a request (before the response is sent out). |
||
58 | */ |
||
59 | const EVENT_AFTER_REQUEST = 'afterRequest'; |
||
60 | /** |
||
61 | * Application state used by [[state]]: application just started. |
||
62 | */ |
||
63 | const STATE_BEGIN = 0; |
||
64 | /** |
||
65 | * Application state used by [[state]]: application is initializing. |
||
66 | */ |
||
67 | const STATE_INIT = 1; |
||
68 | /** |
||
69 | * Application state used by [[state]]: application is triggering [[EVENT_BEFORE_REQUEST]]. |
||
70 | */ |
||
71 | const STATE_BEFORE_REQUEST = 2; |
||
72 | /** |
||
73 | * Application state used by [[state]]: application is handling the request. |
||
74 | */ |
||
75 | const STATE_HANDLING_REQUEST = 3; |
||
76 | /** |
||
77 | * Application state used by [[state]]: application is triggering [[EVENT_AFTER_REQUEST]].. |
||
78 | */ |
||
79 | const STATE_AFTER_REQUEST = 4; |
||
80 | /** |
||
81 | * Application state used by [[state]]: application is about to send response. |
||
82 | */ |
||
83 | const STATE_SENDING_RESPONSE = 5; |
||
84 | /** |
||
85 | * Application state used by [[state]]: application has ended. |
||
86 | */ |
||
87 | const STATE_END = 6; |
||
88 | |||
89 | /** |
||
90 | * @var string the namespace that controller classes are located in. |
||
91 | * This namespace will be used to load controller classes by prepending it to the controller class name. |
||
92 | * The default namespace is `app\controllers`. |
||
93 | * |
||
94 | * Please refer to the [guide about class autoloading](guide:concept-autoloading.md) for more details. |
||
95 | */ |
||
96 | public $controllerNamespace = 'app\\controllers'; |
||
97 | /** |
||
98 | * @var string the application name. |
||
99 | */ |
||
100 | public $name = 'My Application'; |
||
101 | /** |
||
102 | * @var string the charset currently used for the application. |
||
103 | */ |
||
104 | public $charset = 'UTF-8'; |
||
105 | /** |
||
106 | * @var string the language that is meant to be used for end users. It is recommended that you |
||
107 | * use [IETF language tags](http://en.wikipedia.org/wiki/IETF_language_tag). For example, `en` stands |
||
108 | * for English, while `en-US` stands for English (United States). |
||
109 | * @see sourceLanguage |
||
110 | */ |
||
111 | public $language = 'en-US'; |
||
112 | /** |
||
113 | * @var string the language that the application is written in. This mainly refers to |
||
114 | * the language that the messages and view files are written in. |
||
115 | * @see language |
||
116 | */ |
||
117 | public $sourceLanguage = 'en-US'; |
||
118 | /** |
||
119 | * @var Controller the currently active controller instance |
||
120 | */ |
||
121 | public $controller; |
||
122 | /** |
||
123 | * @var string|bool the layout that should be applied for views in this application. Defaults to 'main'. |
||
124 | * If this is false, layout will be disabled. |
||
125 | */ |
||
126 | public $layout = 'main'; |
||
127 | /** |
||
128 | * @var string the requested route |
||
129 | */ |
||
130 | public $requestedRoute; |
||
131 | /** |
||
132 | * @var Action the requested Action. If null, it means the request cannot be resolved into an action. |
||
133 | */ |
||
134 | public $requestedAction; |
||
135 | /** |
||
136 | * @var array the parameters supplied to the requested action. |
||
137 | */ |
||
138 | public $requestedParams; |
||
139 | /** |
||
140 | * @var array list of installed Yii extensions. Each array element represents a single extension |
||
141 | * with the following structure: |
||
142 | * |
||
143 | * ```php |
||
144 | * [ |
||
145 | * 'name' => 'extension name', |
||
146 | * 'version' => 'version number', |
||
147 | * 'bootstrap' => 'BootstrapClassName', // optional, may also be a configuration array |
||
148 | * 'alias' => [ |
||
149 | * '@alias1' => 'to/path1', |
||
150 | * '@alias2' => 'to/path2', |
||
151 | * ], |
||
152 | * ] |
||
153 | * ``` |
||
154 | * |
||
155 | * The "bootstrap" class listed above will be instantiated during the application |
||
156 | * [[bootstrap()|bootstrapping process]]. If the class implements [[BootstrapInterface]], |
||
157 | * its [[BootstrapInterface::bootstrap()|bootstrap()]] method will be also be called. |
||
158 | * |
||
159 | * If not set explicitly in the application config, this property will be populated with the contents of |
||
160 | * `@vendor/yiisoft/extensions.php`. |
||
161 | */ |
||
162 | public $extensions; |
||
163 | /** |
||
164 | * @var array list of components that should be run during the application [[bootstrap()|bootstrapping process]]. |
||
165 | * |
||
166 | * Each component may be specified in one of the following formats: |
||
167 | * |
||
168 | * - an application component ID as specified via [[components]]. |
||
169 | * - a module ID as specified via [[modules]]. |
||
170 | * - a class name. |
||
171 | * - a configuration array. |
||
172 | * - a Closure |
||
173 | * |
||
174 | * During the bootstrapping process, each component will be instantiated. If the component class |
||
175 | * implements [[BootstrapInterface]], its [[BootstrapInterface::bootstrap()|bootstrap()]] method |
||
176 | * will be also be called. |
||
177 | */ |
||
178 | public $bootstrap = []; |
||
179 | /** |
||
180 | * @var int the current application state during a request handling life cycle. |
||
181 | * This property is managed by the application. Do not modify this property. |
||
182 | */ |
||
183 | public $state; |
||
184 | /** |
||
185 | * @var array list of loaded modules indexed by their class names. |
||
186 | */ |
||
187 | public $loadedModules = []; |
||
188 | |||
189 | |||
190 | /** |
||
191 | * Constructor. |
||
192 | * @param array $config name-value pairs that will be used to initialize the object properties. |
||
193 | * Note that the configuration must contain both [[id]] and [[basePath]]. |
||
194 | * @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing. |
||
195 | */ |
||
196 | 3184 | public function __construct($config = []) |
|
209 | |||
210 | /** |
||
211 | * Pre-initializes the application. |
||
212 | * This method is called at the beginning of the application constructor. |
||
213 | * It initializes several important application properties. |
||
214 | * If you override this method, please make sure you call the parent implementation. |
||
215 | * @param array $config the application configuration |
||
216 | * @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing. |
||
217 | */ |
||
218 | 3184 | public function preInit(&$config) |
|
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | 3184 | public function init() |
|
285 | |||
286 | /** |
||
287 | * Initializes extensions and executes bootstrap components. |
||
288 | * This method is called by [[init()]] after the application has been fully configured. |
||
289 | * If you override this method, make sure you also call the parent implementation. |
||
290 | */ |
||
291 | 3184 | protected function bootstrap() |
|
343 | |||
344 | /** |
||
345 | * Registers the errorHandler component as a PHP error handler. |
||
346 | * @param array $config application config |
||
347 | */ |
||
348 | 3184 | protected function registerErrorHandler(&$config) |
|
360 | |||
361 | /** |
||
362 | * Returns an ID that uniquely identifies this module among all modules within the current application. |
||
363 | * Since this is an application instance, it will always return an empty string. |
||
364 | * @return string the unique ID of the module. |
||
365 | */ |
||
366 | 1 | public function getUniqueId() |
|
367 | { |
||
368 | 1 | return ''; |
|
369 | } |
||
370 | |||
371 | /** |
||
372 | * Sets the root directory of the application and the @app alias. |
||
373 | * This method can only be invoked at the beginning of the constructor. |
||
374 | * @param string $path the root directory of the application. |
||
375 | * @property string the root directory of the application. |
||
376 | * @throws InvalidArgumentException if the directory does not exist. |
||
377 | */ |
||
378 | 3184 | public function setBasePath($path) |
|
383 | |||
384 | /** |
||
385 | * Runs the application. |
||
386 | * This is the main entrance of an application. |
||
387 | * @return int the exit status (0 means normal, non-zero values mean abnormal) |
||
388 | */ |
||
389 | public function run() |
||
412 | |||
413 | /** |
||
414 | * Handles the specified request. |
||
415 | * |
||
416 | * This method should return an instance of [[Response]] or its child class |
||
417 | * which represents the handling result of the request. |
||
418 | * |
||
419 | * @param Request $request the request to be handled |
||
420 | * @return Response the resulting response |
||
421 | */ |
||
422 | abstract public function handleRequest($request); |
||
423 | |||
424 | private $_runtimePath; |
||
425 | |||
426 | /** |
||
427 | * Returns the directory that stores runtime files. |
||
428 | * @return string the directory that stores runtime files. |
||
429 | * Defaults to the "runtime" subdirectory under [[basePath]]. |
||
430 | */ |
||
431 | 3184 | public function getRuntimePath() |
|
439 | |||
440 | /** |
||
441 | * Sets the directory that stores runtime files. |
||
442 | * @param string $path the directory that stores runtime files. |
||
443 | */ |
||
444 | 3184 | public function setRuntimePath($path) |
|
449 | |||
450 | private $_vendorPath; |
||
451 | |||
452 | /** |
||
453 | * Returns the directory that stores vendor files. |
||
454 | * @return string the directory that stores vendor files. |
||
455 | * Defaults to "vendor" directory under [[basePath]]. |
||
456 | */ |
||
457 | 10 | public function getVendorPath() |
|
465 | |||
466 | /** |
||
467 | * Sets the directory that stores vendor files. |
||
468 | * @param string $path the directory that stores vendor files. |
||
469 | */ |
||
470 | 3184 | public function setVendorPath($path) |
|
477 | |||
478 | /** |
||
479 | * Returns the time zone used by this application. |
||
480 | * This is a simple wrapper of PHP function date_default_timezone_get(). |
||
481 | * If time zone is not configured in php.ini or application config, |
||
482 | * it will be set to UTC by default. |
||
483 | * @return string the time zone used by this application. |
||
484 | * @see http://php.net/manual/en/function.date-default-timezone-get.php |
||
485 | */ |
||
486 | 310 | public function getTimeZone() |
|
490 | |||
491 | /** |
||
492 | * Sets the time zone used by this application. |
||
493 | * This is a simple wrapper of PHP function date_default_timezone_set(). |
||
494 | * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones. |
||
495 | * @param string $value the time zone used by this application. |
||
496 | * @see http://php.net/manual/en/function.date-default-timezone-set.php |
||
497 | */ |
||
498 | 376 | public function setTimeZone($value) |
|
502 | |||
503 | /** |
||
504 | * Returns the database connection component. |
||
505 | * @return \yii\db\Connection the database connection. |
||
506 | */ |
||
507 | 85 | public function getDb() |
|
511 | |||
512 | /** |
||
513 | * Sets up or configure the logger instance. |
||
514 | * @param \psr\log\LoggerInterface|\Closure|array|null $logger the logger object or its DI compatible configuration. |
||
515 | * @since 2.1.0 |
||
516 | */ |
||
517 | 6 | public function setLogger($logger) |
|
521 | |||
522 | /** |
||
523 | * Returns the logger instance. |
||
524 | * @return \psr\log\LoggerInterface the logger instance. |
||
525 | * @since 2.1.0 |
||
526 | */ |
||
527 | 5 | public function getLogger() |
|
528 | { |
||
529 | 5 | return Yii::getLogger(); |
|
530 | } |
||
531 | |||
532 | /** |
||
533 | * Sets up or configure the profiler instance. |
||
534 | * @param \yii\profile\ProfilerInterface|\Closure|array|null $profiler the profiler object or its DI compatible configuration. |
||
535 | * @since 2.1.0 |
||
536 | */ |
||
537 | public function setProfiler($profiler) |
||
541 | |||
542 | /** |
||
543 | * Returns the profiler instance. |
||
544 | * @return \yii\profile\ProfilerInterface profiler instance. |
||
545 | * @since 2.1.0 |
||
546 | */ |
||
547 | public function getProfiler() |
||
551 | |||
552 | /** |
||
553 | * Returns the error handler component. |
||
554 | * @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component. |
||
555 | */ |
||
556 | public function getErrorHandler() |
||
560 | |||
561 | /** |
||
562 | * Returns the cache component. |
||
563 | * @return \yii\caching\CacheInterface the cache application component. Null if the component is not enabled. |
||
564 | */ |
||
565 | public function getCache() |
||
569 | |||
570 | /** |
||
571 | * Returns the formatter component. |
||
572 | * @return \yii\i18n\Formatter the formatter application component. |
||
573 | */ |
||
574 | 31 | public function getFormatter() |
|
578 | |||
579 | /** |
||
580 | * Returns the request component. |
||
581 | * @return \yii\web\Request|\yii\console\Request the request component. |
||
582 | */ |
||
583 | public function getRequest() |
||
587 | |||
588 | /** |
||
589 | * Returns the response component. |
||
590 | * @return \yii\web\Response|\yii\console\Response the response component. |
||
591 | */ |
||
592 | public function getResponse() |
||
596 | |||
597 | /** |
||
598 | * Returns the view object. |
||
599 | * @return View|\yii\web\View the view application component that is used to render various view files. |
||
600 | */ |
||
601 | 46 | public function getView() |
|
605 | |||
606 | /** |
||
607 | * Returns the URL manager for this application. |
||
608 | * @return \yii\web\UrlManager the URL manager for this application. |
||
609 | */ |
||
610 | 36 | public function getUrlManager() |
|
614 | |||
615 | /** |
||
616 | * Returns the internationalization (i18n) component. |
||
617 | * @return \yii\i18n\I18N the internationalization application component. |
||
618 | */ |
||
619 | 538 | public function getI18n() |
|
623 | |||
624 | /** |
||
625 | * Returns the mailer component. |
||
626 | * @return \yii\mail\MailerInterface the mailer application component. |
||
627 | */ |
||
628 | public function getMailer() |
||
632 | |||
633 | /** |
||
634 | * Returns the auth manager for this application. |
||
635 | * @return \yii\rbac\ManagerInterface the auth manager application component. |
||
636 | * Null is returned if auth manager is not configured. |
||
637 | */ |
||
638 | public function getAuthManager() |
||
642 | |||
643 | /** |
||
644 | * Returns the asset manager. |
||
645 | * @return \yii\web\AssetManager the asset manager application component. |
||
646 | */ |
||
647 | 7 | public function getAssetManager() |
|
651 | |||
652 | /** |
||
653 | * Returns the security component. |
||
654 | * @return \yii\base\Security the security application component. |
||
655 | */ |
||
656 | 56 | public function getSecurity() |
|
660 | |||
661 | /** |
||
662 | * Returns the configuration of core application components. |
||
663 | * @see set() |
||
664 | */ |
||
665 | 3184 | public function coreComponents() |
|
677 | |||
678 | /** |
||
679 | * Terminates the application. |
||
680 | * This method replaces the `exit()` function by ensuring the application life cycle is completed |
||
681 | * before terminating the application. |
||
682 | * @param int $status the exit status (value 0 means normal exit while other values mean abnormal exit). |
||
683 | * @param Response $response the response to be sent. If not set, the default application [[response]] component will be used. |
||
684 | * @throws ExitException if the application is in testing mode |
||
685 | */ |
||
686 | 3 | public function end($status = 0, $response = null) |
|
705 | |||
706 | /** |
||
707 | * Configures [[Yii::$container]] with the $config. |
||
708 | * |
||
709 | * @param array $config values given in terms of name-value pairs |
||
710 | * @since 2.0.11 |
||
711 | */ |
||
712 | 1 | public function setContainer($config) |
|
716 | } |
||
717 |
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..