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