Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PradoBase 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 PradoBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class PradoBase |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * File extension for Prado class files. |
||
| 57 | */ |
||
| 58 | const CLASS_FILE_EXT = '.php'; |
||
| 59 | /** |
||
| 60 | * @var array list of path aliases |
||
| 61 | */ |
||
| 62 | private static $_aliases = [ |
||
| 63 | 'Prado' => PRADO_DIR, |
||
| 64 | 'Vendor' => PRADO_VENDORDIR |
||
| 65 | ]; |
||
| 66 | /** |
||
| 67 | * @var array list of namespaces currently in use |
||
| 68 | */ |
||
| 69 | private static $_usings = [ |
||
| 70 | 'Prado' => PRADO_DIR |
||
| 71 | ]; |
||
| 72 | /** |
||
| 73 | * @var array list of namespaces currently in use |
||
| 74 | */ |
||
| 75 | public static $classMap = []; |
||
| 76 | /** |
||
| 77 | * @var TApplication the application instance |
||
| 78 | */ |
||
| 79 | private static $_application = null; |
||
| 80 | /** |
||
| 81 | * @var TLogger logger instance |
||
| 82 | */ |
||
| 83 | private static $_logger = null; |
||
| 84 | /** |
||
| 85 | * @var array list of class exists checks |
||
| 86 | */ |
||
| 87 | protected static $classExists = []; |
||
| 88 | /** |
||
| 89 | * @return string the version of Prado framework |
||
| 90 | */ |
||
| 91 | 3 | public static function getVersion() |
|
| 95 | |||
| 96 | public static function init() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Loads the static classmap and registers the autoload function. |
||
| 104 | */ |
||
| 105 | public static function initAutoloader() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Initializes error handlers. |
||
| 114 | * This method set error and exception handlers to be functions |
||
| 115 | * defined in this class. |
||
| 116 | */ |
||
| 117 | public static function initErrorHandlers() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Class autoload loader. |
||
| 139 | * This method is provided to be invoked within an __autoload() magic method. |
||
| 140 | * @param string $className class name |
||
| 141 | */ |
||
| 142 | 9 | public static function autoload($className) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param int $logoType the type of "powered logo". Valid values include 0 and 1. |
||
| 149 | * @return string a string that can be displayed on your Web page showing powered-by-PRADO information |
||
| 150 | */ |
||
| 151 | public static function poweredByPrado($logoType = 0) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * PHP error handler. |
||
| 165 | * This method should be registered as PHP error handler using |
||
| 166 | * {@link set_error_handler}. The method throws an exception that |
||
| 167 | * contains the error information. |
||
| 168 | * @param int $errno the level of the error raised |
||
| 169 | * @param string $errstr the error message |
||
| 170 | * @param string $errfile the filename that the error was raised in |
||
| 171 | * @param int $errline the line number the error was raised at |
||
| 172 | */ |
||
| 173 | 14 | public static function phpErrorHandler($errno, $errstr, $errfile, $errline) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * PHP shutdown function used to catch fatal errors. |
||
| 182 | * This method should be registered as PHP error handler using |
||
| 183 | * {@link register_shutdown_function}. The method throws an exception that |
||
| 184 | * contains the error information. |
||
| 185 | */ |
||
| 186 | public static function phpFatalErrorHandler() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Default exception handler. |
||
| 198 | * This method should be registered as default exception handler using |
||
| 199 | * {@link set_exception_handler}. The method tries to use the errorhandler |
||
| 200 | * module of the Prado application to handle the exception. |
||
| 201 | * If the application or the module does not exist, it simply echoes the |
||
| 202 | * exception. |
||
| 203 | * @param Exception $exception exception that is not caught |
||
| 204 | */ |
||
| 205 | public static function exceptionHandler($exception) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Stores the application instance in the class static member. |
||
| 217 | * This method helps implement a singleton pattern for TApplication. |
||
| 218 | * Repeated invocation of this method or the application constructor |
||
| 219 | * will cause the throw of an exception. |
||
| 220 | * This method should only be used by framework developers. |
||
| 221 | * @param TApplication $application the application instance |
||
| 222 | * @throws TInvalidOperationException if this method is invoked twice or more. |
||
| 223 | */ |
||
| 224 | 55 | public static function setApplication($application) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return TApplication the application singleton, null if the singleton has not be created yet. |
||
| 234 | */ |
||
| 235 | 96 | public static function getApplication() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @return string the path of the framework |
||
| 242 | */ |
||
| 243 | 108 | public static function getFrameworkPath() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Convert old Prado namespaces to PHP namespaces |
||
| 250 | * @param string $type old class name in Prado3 namespace format |
||
| 251 | * @return string Equivalent class name in PHP namespace format |
||
| 252 | */ |
||
| 253 | 116 | protected static function prado3NamespaceToPhpNamespace($type) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Creates a component with the specified type. |
||
| 268 | * A component type can be either the component class name |
||
| 269 | * or a namespace referring to the path of the component class file. |
||
| 270 | * For example, 'TButton', '\Prado\Web\UI\WebControls\TButton' are both |
||
| 271 | * valid component type. |
||
| 272 | * This method can also pass parameters to component constructors. |
||
| 273 | * All parameters passed to this method except the first one (the component type) |
||
| 274 | * will be supplied as component constructor parameters. |
||
| 275 | * @param string $requestedType component type |
||
| 276 | * @param array $params |
||
| 277 | * @throws TInvalidDataValueException if the component type is unknown |
||
| 278 | * @return TComponent component instance of the specified type |
||
| 279 | */ |
||
| 280 | 103 | public static function createComponent($requestedType, ...$params) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Uses a namespace. |
||
| 312 | * A namespace ending with an asterisk '*' refers to a directory, otherwise it represents a PHP file. |
||
| 313 | * If the namespace corresponds to a directory, the directory will be appended |
||
| 314 | * to the include path. If the namespace corresponds to a file, it will be included (include_once). |
||
| 315 | * @param string $namespace namespace to be used |
||
| 316 | * @throws TInvalidDataValueException if the namespace is invalid |
||
| 317 | */ |
||
| 318 | 15 | public static function using($namespace) |
|
| 319 | { |
||
| 320 | 15 | $namespace = static::prado3NamespaceToPhpNamespace($namespace); |
|
| 321 | |||
| 322 | 15 | if (isset(self::$_usings[$namespace]) || |
|
| 323 | 15 | class_exists($namespace, false) || |
|
| 324 | 15 | interface_exists($namespace, false)) { |
|
| 325 | 2 | return; |
|
| 326 | } |
||
| 327 | |||
| 328 | 13 | if (array_key_exists($namespace, self::$classMap)) { |
|
| 329 | // fast autoload a Prado3 class name |
||
| 330 | 9 | $phpNamespace = self::$classMap[$namespace]; |
|
| 331 | 9 | View Code Duplication | if (class_exists($phpNamespace, true) || interface_exists($phpNamespace, true)) { |
| 332 | 9 | if (!class_exists($namespace) && !interface_exists($namespace)) { |
|
| 333 | 9 | class_alias($phpNamespace, $namespace); |
|
| 334 | } |
||
| 335 | 9 | return; |
|
| 336 | } |
||
| 337 | 5 | } elseif (($pos = strrpos($namespace, '\\')) === false) { |
|
| 338 | // trying to autoload an old class name |
||
| 339 | foreach (self::$_usings as $k => $v) { |
||
| 340 | $path = $v . DIRECTORY_SEPARATOR . $namespace . self::CLASS_FILE_EXT; |
||
| 341 | if (file_exists($path)) { |
||
| 342 | $phpNamespace = '\\' . $k . '\\' . $namespace; |
||
| 343 | View Code Duplication | if (class_exists($phpNamespace, true) || interface_exists($phpNamespace, true)) { |
|
| 344 | if (!class_exists($namespace) && !interface_exists($namespace)) { |
||
| 345 | class_alias($phpNamespace, $namespace); |
||
| 346 | } |
||
| 347 | return; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | } |
||
| 351 | 5 | } elseif (($path = self::getPathOfNamespace($namespace, self::CLASS_FILE_EXT)) !== null) { |
|
| 352 | 2 | $className = substr($namespace, $pos + 1); |
|
| 353 | 2 | if ($className === '*') { // a directory |
|
| 354 | self::$_usings[substr($namespace, 0, $pos)] = $path; |
||
| 355 | } else { // a file |
||
| 356 | 2 | if (class_exists($className, false) || interface_exists($className, false)) |
|
| 357 | return; |
||
| 358 | |||
| 359 | 2 | include_once($path); |
|
| 360 | 2 | if (!class_exists($className, false) && !interface_exists($className, false)) { |
|
| 361 | 2 | class_alias($namespace, $className); |
|
| 362 | } |
||
| 363 | } |
||
| 364 | } |
||
| 365 | 5 | } |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Translates a namespace into a file path. |
||
| 369 | * The first segment of the namespace is considered as a path alias |
||
| 370 | * which is replaced with the actual path. The rest segments are |
||
| 371 | * subdirectory names appended to the aliased path. |
||
| 372 | * If the namespace ends with an asterisk '*', it represents a directory; |
||
| 373 | * Otherwise it represents a file whose extension name is specified by the second parameter (defaults to empty). |
||
| 374 | * Note, this method does not ensure the existence of the resulting file path. |
||
| 375 | * @param string $namespace namespace |
||
| 376 | * @param string $ext extension to be appended if the namespace refers to a file |
||
| 377 | * @return string file path corresponding to the namespace, null if namespace is invalid |
||
| 378 | */ |
||
| 379 | 10 | public static function getPathOfNamespace($namespace, $ext = '') |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $alias alias to the path |
||
| 405 | * @return string the path corresponding to the alias, null if alias not defined. |
||
| 406 | */ |
||
| 407 | 9 | public static function getPathOfAlias($alias) |
|
| 411 | |||
| 412 | protected static function getPathAliases() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param string $alias alias to the path |
||
| 419 | * @param string $path the path corresponding to the alias |
||
| 420 | * @throws TInvalidOperationException $alias if the alias is already defined |
||
| 421 | * @throws TInvalidDataValueException $path if the path is not a valid file path |
||
| 422 | */ |
||
| 423 | 61 | public static function setPathOfAlias($alias, $path) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Fatal error handler. |
||
| 440 | * This method displays an error message together with the current call stack. |
||
| 441 | * The application will exit after calling this method. |
||
| 442 | * @param string $msg error message |
||
| 443 | */ |
||
| 444 | public static function fatalError($msg) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Returns a list of user preferred languages. |
||
| 507 | * The languages are returned as an array. Each array element |
||
| 508 | * represents a single language preference. The languages are ordered |
||
| 509 | * according to user preferences. The first language is the most preferred. |
||
| 510 | * @return array list of user preferred languages. |
||
| 511 | */ |
||
| 512 | 1 | public static function getUserLanguages() |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Returns the most preferred language by the client user. |
||
| 536 | * @return string the most preferred language by the client user, defaults to English. |
||
| 537 | */ |
||
| 538 | 113 | public static function getPreferredLanguage() |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Writes a log message. |
||
| 555 | * This method wraps {@link log()} by checking the application mode. |
||
| 556 | * When the application is in Debug mode, debug backtrace information is appended |
||
| 557 | * to the message and the message is logged at DEBUG level. |
||
| 558 | * When the application is in Performance mode, this method does nothing. |
||
| 559 | * Otherwise, the message is logged at INFO level. |
||
| 560 | * @param string $msg message to be logged |
||
| 561 | * @param string $category category of the message |
||
| 562 | * @param (string|TControl) $ctl control of the message |
||
| 563 | * @see log, getLogger |
||
| 564 | */ |
||
| 565 | 32 | public static function trace($msg, $category = 'Uncategorized', $ctl = null) |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Logs a message. |
||
| 584 | * Messages logged by this method may be retrieved via {@link TLogger::getLogs} |
||
| 585 | * and may be recorded in different media, such as file, email, database, using |
||
| 586 | * {@link TLogRouter}. |
||
| 587 | * @param string $msg message to be logged |
||
| 588 | * @param int $level level of the message. Valid values include |
||
| 589 | * TLogger::DEBUG, TLogger::INFO, TLogger::NOTICE, TLogger::WARNING, |
||
| 590 | * TLogger::ERROR, TLogger::ALERT, TLogger::FATAL. |
||
| 591 | * @param string $category category of the message |
||
| 592 | * @param (string|TControl) $ctl control of the message |
||
| 593 | */ |
||
| 594 | 32 | public static function log($msg, $level = TLogger::INFO, $category = 'Uncategorized', $ctl = null) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * @return TLogger message logger |
||
| 604 | */ |
||
| 605 | public static function getLogger() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Converts a variable into a string representation. |
||
| 615 | * This method achieves the similar functionality as var_dump and print_r |
||
| 616 | * but is more robust when handling complex objects such as PRADO controls. |
||
| 617 | * @param mixed $var variable to be dumped |
||
| 618 | * @param int $depth maximum depth that the dumper should go into the variable. Defaults to 10. |
||
| 619 | * @param bool $highlight whether to syntax highlight the output. Defaults to false. |
||
| 620 | * @return string the string representation of the variable |
||
| 621 | */ |
||
| 622 | public static function varDump($var, $depth = 10, $highlight = false) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Localize a text to the locale/culture specified in the globalization handler. |
||
| 629 | * @param string $text text to be localized. |
||
| 630 | * @param array $parameters a set of parameters to substitute. |
||
| 631 | * @param string $catalogue a different catalogue to find the localize text. |
||
| 632 | * @param string $charset the input AND output charset. |
||
| 633 | * @return string localized text. |
||
| 634 | * @see TTranslate::formatter() |
||
| 635 | * @see TTranslate::init() |
||
| 636 | */ |
||
| 637 | public static function localize($text, $parameters = [], $catalogue = null, $charset = null) |
||
| 673 | } |
||
| 674 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.