Complex classes like BaseYii 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 BaseYii, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class BaseYii |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @var array class map used by the Yii autoloading mechanism. |
||
| 64 | * The array keys are the class names (without leading backslashes), and the array values |
||
| 65 | * are the corresponding class file paths (or [path aliases](guide:concept-aliases)). This property mainly affects |
||
| 66 | * how [[autoload()]] works. |
||
| 67 | * @see autoload() |
||
| 68 | */ |
||
| 69 | public static $classMap = []; |
||
| 70 | /** |
||
| 71 | * @var \yii\console\Application|\yii\web\Application the application instance |
||
| 72 | */ |
||
| 73 | public static $app; |
||
| 74 | /** |
||
| 75 | * @var array registered path aliases |
||
| 76 | * @see getAlias() |
||
| 77 | * @see setAlias() |
||
| 78 | */ |
||
| 79 | public static $aliases = ['@yii' => __DIR__]; |
||
| 80 | /** |
||
| 81 | * @var Container the dependency injection (DI) container used by [[createObject()]]. |
||
| 82 | * You may use [[Container::set()]] to set up the needed dependencies of classes and |
||
| 83 | * their initial property values. |
||
| 84 | * @see createObject() |
||
| 85 | * @see Container |
||
| 86 | */ |
||
| 87 | public static $container; |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns a string representing the current version of the Yii framework. |
||
| 92 | * @return string the version of Yii framework |
||
| 93 | */ |
||
| 94 | 60 | public static function getVersion() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Translates a path alias into an actual path. |
||
| 101 | * |
||
| 102 | * The translation is done according to the following procedure: |
||
| 103 | * |
||
| 104 | * 1. If the given alias does not start with '@', it is returned back without change; |
||
| 105 | * 2. Otherwise, look for the longest registered alias that matches the beginning part |
||
| 106 | * of the given alias. If it exists, replace the matching part of the given alias with |
||
| 107 | * the corresponding registered path. |
||
| 108 | * 3. Throw an exception or return false, depending on the `$throwException` parameter. |
||
| 109 | * |
||
| 110 | * For example, by default '@yii' is registered as the alias to the Yii framework directory, |
||
| 111 | * say '/path/to/yii'. The alias '@yii/web' would then be translated into '/path/to/yii/web'. |
||
| 112 | * |
||
| 113 | * If you have registered two aliases '@foo' and '@foo/bar'. Then translating '@foo/bar/config' |
||
| 114 | * would replace the part '@foo/bar' (instead of '@foo') with the corresponding registered path. |
||
| 115 | * This is because the longest alias takes precedence. |
||
| 116 | * |
||
| 117 | * However, if the alias to be translated is '@foo/barbar/config', then '@foo' will be replaced |
||
| 118 | * instead of '@foo/bar', because '/' serves as the boundary character. |
||
| 119 | * |
||
| 120 | * Note, this method does not check if the returned path exists or not. |
||
| 121 | * |
||
| 122 | * See the [guide article on aliases](guide:concept-aliases) for more information. |
||
| 123 | * |
||
| 124 | * @param string $alias the alias to be translated. |
||
| 125 | * @param bool $throwException whether to throw an exception if the given alias is invalid. |
||
| 126 | * If this is false and an invalid alias is given, false will be returned by this method. |
||
| 127 | * @return string|bool the path corresponding to the alias, false if the root alias is not previously registered. |
||
| 128 | * @throws InvalidParamException if the alias is invalid while $throwException is true. |
||
| 129 | * @see setAlias() |
||
| 130 | */ |
||
| 131 | 3074 | public static function getAlias($alias, $throwException = true) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Returns the root alias part of a given alias. |
||
| 162 | * A root alias is an alias that has been registered via [[setAlias()]] previously. |
||
| 163 | * If a given alias matches multiple root aliases, the longest one will be returned. |
||
| 164 | * @param string $alias the alias |
||
| 165 | * @return string|bool the root alias, or false if no root alias is found |
||
| 166 | */ |
||
| 167 | public static function getRootAlias($alias) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Registers a path alias. |
||
| 189 | * |
||
| 190 | * A path alias is a short name representing a long path (a file path, a URL, etc.) |
||
| 191 | * For example, we use '@yii' as the alias of the path to the Yii framework directory. |
||
| 192 | * |
||
| 193 | * A path alias must start with the character '@' so that it can be easily differentiated |
||
| 194 | * from non-alias paths. |
||
| 195 | * |
||
| 196 | * Note that this method does not check if the given path exists or not. All it does is |
||
| 197 | * to associate the alias with the path. |
||
| 198 | * |
||
| 199 | * Any trailing '/' and '\' characters in the given path will be trimmed. |
||
| 200 | * |
||
| 201 | * See the [guide article on aliases](guide:concept-aliases) for more information. |
||
| 202 | * |
||
| 203 | * @param string $alias the alias name (e.g. "@yii"). It must start with a '@' character. |
||
| 204 | * It may contain the forward slash '/' which serves as boundary character when performing |
||
| 205 | * alias translation by [[getAlias()]]. |
||
| 206 | * @param string $path the path corresponding to the alias. If this is null, the alias will |
||
| 207 | * be removed. Trailing '/' and '\' characters will be trimmed. This can be |
||
| 208 | * |
||
| 209 | * - a directory or a file path (e.g. `/tmp`, `/tmp/main.txt`) |
||
| 210 | * - a URL (e.g. `http://www.yiiframework.com`) |
||
| 211 | * - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the |
||
| 212 | * actual path first by calling [[getAlias()]]. |
||
| 213 | * |
||
| 214 | * @throws InvalidParamException if $path is an invalid alias. |
||
| 215 | * @see getAlias() |
||
| 216 | */ |
||
| 217 | 2848 | public static function setAlias($alias, $path) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Class autoload loader. |
||
| 256 | * |
||
| 257 | * This method is invoked automatically when PHP sees an unknown class. |
||
| 258 | * The method will attempt to include the class file according to the following procedure: |
||
| 259 | * |
||
| 260 | * 1. Search in [[classMap]]; |
||
| 261 | * 2. If the class is namespaced (e.g. `yii\base\Component`), it will attempt |
||
| 262 | * to include the file associated with the corresponding path alias |
||
| 263 | * (e.g. `@yii/base/Component.php`); |
||
| 264 | * |
||
| 265 | * This autoloader allows loading classes that follow the [PSR-4 standard](http://www.php-fig.org/psr/psr-4/) |
||
| 266 | * and have its top-level namespace or sub-namespaces defined as path aliases. |
||
| 267 | * |
||
| 268 | * Example: When aliases `@yii` and `@yii/bootstrap` are defined, classes in the `yii\bootstrap` namespace |
||
| 269 | * will be loaded using the `@yii/bootstrap` alias which points to the directory where bootstrap extension |
||
| 270 | * files are installed and all classes from other `yii` namespaces will be loaded from the yii framework directory. |
||
| 271 | * |
||
| 272 | * Also the [guide section on autoloading](guide:concept-autoloading). |
||
| 273 | * |
||
| 274 | * @param string $className the fully qualified class name without a leading backslash "\" |
||
| 275 | * @throws UnknownClassException if the class does not exist in the class file |
||
| 276 | */ |
||
| 277 | 204 | public static function autoload($className) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Creates a new object using the given configuration. |
||
| 302 | * |
||
| 303 | * You may view this method as an enhanced version of the `new` operator. |
||
| 304 | * The method supports creating an object based on a class name, a configuration array or |
||
| 305 | * an anonymous function. |
||
| 306 | * |
||
| 307 | * Below are some usage examples: |
||
| 308 | * |
||
| 309 | * ```php |
||
| 310 | * // create an object using a class name |
||
| 311 | * $object = Yii::createObject('yii\db\Connection'); |
||
| 312 | * |
||
| 313 | * // create an object using a configuration array |
||
| 314 | * $object = Yii::createObject([ |
||
| 315 | * 'class' => 'yii\db\Connection', |
||
| 316 | * 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', |
||
| 317 | * 'username' => 'root', |
||
| 318 | * 'password' => '', |
||
| 319 | * 'charset' => 'utf8', |
||
| 320 | * ]); |
||
| 321 | * |
||
| 322 | * // create an object with two constructor parameters |
||
| 323 | * $object = \Yii::createObject('MyClass', [$param1, $param2]); |
||
| 324 | * ``` |
||
| 325 | * |
||
| 326 | * Using [[\yii\di\Container|dependency injection container]], this method can also identify |
||
| 327 | * dependent objects, instantiate them and inject them into the newly created object. |
||
| 328 | * |
||
| 329 | * @param string|array|callable $type the object type. This can be specified in one of the following forms: |
||
| 330 | * |
||
| 331 | * - a string: representing the class name of the object to be created |
||
| 332 | * - a configuration array: the array must contain a `class` element which is treated as the object class, |
||
| 333 | * and the rest of the name-value pairs will be used to initialize the corresponding object properties |
||
| 334 | * - a PHP callable: either an anonymous function or an array representing a class method (`[$class or $object, $method]`). |
||
| 335 | * The callable should return a new instance of the object being created. |
||
| 336 | * |
||
| 337 | * @param array $params the constructor parameters |
||
| 338 | * @return object the created object |
||
| 339 | * @throws InvalidConfigException if the configuration is invalid. |
||
| 340 | * @see \yii\di\Container |
||
| 341 | */ |
||
| 342 | 2660 | public static function createObject($type, array $params = []) |
|
| 364 | |||
| 365 | private static $_logger; |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return Logger message logger |
||
| 369 | */ |
||
| 370 | 1617 | public static function getLogger() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Sets the logger object. |
||
| 381 | * @param Logger $logger the logger object. |
||
| 382 | */ |
||
| 383 | 12 | public static function setLogger($logger) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Logs a trace message. |
||
| 390 | * Trace messages are logged mainly for development purpose to see |
||
| 391 | * the execution work flow of some code. This method will only log |
||
| 392 | * a message when the application is in debug mode. |
||
| 393 | * @param string|array $message the message to be logged. This can be a simple string or a more |
||
| 394 | * complex data structure, such as array. |
||
| 395 | * @param string $category the category of the message. |
||
| 396 | */ |
||
| 397 | 1502 | public static function trace($message, $category = 'application') |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Logs an error message. |
||
| 406 | * An error message is typically logged when an unrecoverable error occurs |
||
| 407 | * during the execution of an application. |
||
| 408 | * @param string|array $message the message to be logged. This can be a simple string or a more |
||
| 409 | * complex data structure, such as array. |
||
| 410 | * @param string $category the category of the message. |
||
| 411 | */ |
||
| 412 | 5 | public static function error($message, $category = 'application') |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Logs a warning message. |
||
| 419 | * A warning message is typically logged when an error occurs while the execution |
||
| 420 | * can still continue. |
||
| 421 | * @param string|array $message the message to be logged. This can be a simple string or a more |
||
| 422 | * complex data structure, such as array. |
||
| 423 | * @param string $category the category of the message. |
||
| 424 | */ |
||
| 425 | 13 | public static function warning($message, $category = 'application') |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Logs an informative message. |
||
| 432 | * An informative message is typically logged by an application to keep record of |
||
| 433 | * something important (e.g. an administrator logs in). |
||
| 434 | * @param string|array $message the message to be logged. This can be a simple string or a more |
||
| 435 | * complex data structure, such as array. |
||
| 436 | * @param string $category the category of the message. |
||
| 437 | */ |
||
| 438 | 1421 | public static function info($message, $category = 'application') |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Marks the beginning of a code block for profiling. |
||
| 445 | * |
||
| 446 | * This has to be matched with a call to [[endProfile]] with the same category name. |
||
| 447 | * The begin- and end- calls must also be properly nested. For example, |
||
| 448 | * |
||
| 449 | * ```php |
||
| 450 | * \Yii::beginProfile('block1'); |
||
| 451 | * // some code to be profiled |
||
| 452 | * \Yii::beginProfile('block2'); |
||
| 453 | * // some other code to be profiled |
||
| 454 | * \Yii::endProfile('block2'); |
||
| 455 | * \Yii::endProfile('block1'); |
||
| 456 | * ``` |
||
| 457 | * @param string $token token for the code block |
||
| 458 | * @param string $category the category of this log message |
||
| 459 | * @see endProfile() |
||
| 460 | */ |
||
| 461 | 1374 | public static function beginProfile($token, $category = 'application') |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Marks the end of a code block for profiling. |
||
| 468 | * This has to be matched with a previous call to [[beginProfile]] with the same category name. |
||
| 469 | * @param string $token token for the code block |
||
| 470 | * @param string $category the category of this log message |
||
| 471 | * @see beginProfile() |
||
| 472 | */ |
||
| 473 | 1374 | public static function endProfile($token, $category = 'application') |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Returns an HTML hyperlink that can be displayed on your Web page showing "Powered by Yii Framework" information. |
||
| 480 | * @return string an HTML hyperlink that can be displayed on your Web page showing "Powered by Yii Framework" information |
||
| 481 | */ |
||
| 482 | 1 | public static function powered() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Translates a message to the specified language. |
||
| 492 | * |
||
| 493 | * This is a shortcut method of [[\yii\i18n\I18N::translate()]]. |
||
| 494 | * |
||
| 495 | * The translation will be conducted according to the message category and the target language will be used. |
||
| 496 | * |
||
| 497 | * You can add parameters to a translation message that will be substituted with the corresponding value after |
||
| 498 | * translation. The format for this is to use curly brackets around the parameter name as you can see in the following example: |
||
| 499 | * |
||
| 500 | * ```php |
||
| 501 | * $username = 'Alexander'; |
||
| 502 | * echo \Yii::t('app', 'Hello, {username}!', ['username' => $username]); |
||
| 503 | * ``` |
||
| 504 | * |
||
| 505 | * Further formatting of message parameters is supported using the [PHP intl extensions](http://www.php.net/manual/en/intro.intl.php) |
||
| 506 | * message formatter. See [[\yii\i18n\I18N::translate()]] for more details. |
||
| 507 | * |
||
| 508 | * @param string $category the message category. |
||
| 509 | * @param string $message the message to be translated. |
||
| 510 | * @param array $params the parameters that will be used to replace the corresponding placeholders in the message. |
||
| 511 | * @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current |
||
| 512 | * [[\yii\base\Application::language|application language]] will be used. |
||
| 513 | * @return string the translated message. |
||
| 514 | */ |
||
| 515 | 718 | public static function t($category, $message, $params = [], $language = null) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Configures an object with the initial property values. |
||
| 531 | * @param object $object the object to be configured |
||
| 532 | * @param array $properties the property initial values given in terms of name-value pairs. |
||
| 533 | * @return object the object itself |
||
| 534 | */ |
||
| 535 | 3307 | public static function configure($object, $properties) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Returns the public member variables of an object. |
||
| 546 | * This method is provided such that we can get the public member variables of an object. |
||
| 547 | * It is different from "get_object_vars()" because the latter will return private |
||
| 548 | * and protected variables if it is called within the object itself. |
||
| 549 | * @param object $object the object to be handled |
||
| 550 | * @return array the public member variables of the object |
||
| 551 | */ |
||
| 552 | 3 | public static function getObjectVars($object) |
|
| 556 | } |
||
| 557 |