Complex classes like CI_Loader 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 CI_Loader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
51 | class CI_Loader { |
||
52 | |||
53 | // All these are set automatically. Don't mess with them. |
||
54 | /** |
||
55 | * Nesting level of the output buffering mechanism |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $_ci_ob_level; |
||
60 | |||
61 | /** |
||
62 | * List of paths to load views from |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $_ci_view_paths = array(VIEWPATH => TRUE); |
||
67 | |||
68 | /** |
||
69 | * List of paths to load libraries from |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $_ci_library_paths = array(APPPATH, BASEPATH); |
||
74 | |||
75 | /** |
||
76 | * List of paths to load models from |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $_ci_model_paths = array(APPPATH); |
||
81 | |||
82 | /** |
||
83 | * List of paths to load helpers from |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $_ci_helper_paths = array(APPPATH, BASEPATH); |
||
88 | |||
89 | /** |
||
90 | * List of cached variables |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $_ci_cached_vars = array(); |
||
95 | |||
96 | /** |
||
97 | * List of loaded classes |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $_ci_classes = array(); |
||
102 | |||
103 | /** |
||
104 | * List of loaded models |
||
105 | * |
||
106 | * @var array |
||
107 | */ |
||
108 | protected $_ci_models = array(); |
||
109 | |||
110 | /** |
||
111 | * List of loaded helpers |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | protected $_ci_helpers = array(); |
||
116 | |||
117 | /** |
||
118 | * List of class name mappings |
||
119 | * |
||
120 | * @var array |
||
121 | */ |
||
122 | protected $_ci_varmap = array( |
||
123 | 'unit_test' => 'unit', |
||
124 | 'user_agent' => 'agent' |
||
125 | ); |
||
126 | |||
127 | // -------------------------------------------------------------------- |
||
128 | |||
129 | /** |
||
130 | * Class constructor |
||
131 | * |
||
132 | * Sets component load paths, gets the initial output buffering level. |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | public function __construct() |
||
143 | |||
144 | // -------------------------------------------------------------------- |
||
145 | |||
146 | /** |
||
147 | * Initializer |
||
148 | * |
||
149 | * @todo Figure out a way to move this to the constructor |
||
150 | * without breaking *package_path*() methods. |
||
151 | * @uses CI_Loader::_ci_autoloader() |
||
152 | * @used-by CI_Controller::__construct() |
||
153 | * @return void |
||
154 | */ |
||
155 | public function initialize() |
||
159 | |||
160 | // -------------------------------------------------------------------- |
||
161 | |||
162 | /** |
||
163 | * Is Loaded |
||
164 | * |
||
165 | * A utility method to test if a class is in the self::$_ci_classes array. |
||
166 | * |
||
167 | * @used-by Mainly used by Form Helper function _get_validation_object(). |
||
168 | * |
||
169 | * @param string $class Class name to check for |
||
170 | * @return string|bool Class object name if loaded or FALSE |
||
171 | */ |
||
172 | public function is_loaded($class) |
||
176 | |||
177 | // -------------------------------------------------------------------- |
||
178 | |||
179 | /** |
||
180 | * Library Loader |
||
181 | * |
||
182 | * Loads and instantiates libraries. |
||
183 | * Designed to be called from application controllers. |
||
184 | * |
||
185 | * @param string $library Library name |
||
186 | * @param array $params Optional parameters to pass to the library class constructor |
||
187 | * @param string $object_name An optional object name to assign to |
||
188 | * @return object |
||
189 | */ |
||
190 | public function library($library, $params = NULL, $object_name = NULL) |
||
221 | |||
222 | // -------------------------------------------------------------------- |
||
223 | |||
224 | /** |
||
225 | * Model Loader |
||
226 | * |
||
227 | * Loads and instantiates models. |
||
228 | * |
||
229 | * @param string $model Model name |
||
230 | * @param string $name An optional object name to assign to |
||
231 | * @param bool $db_conn An optional database connection configuration to initialize |
||
232 | * @return object |
||
233 | * |
||
234 | * modified by ci-phpunit-test |
||
235 | */ |
||
236 | public function model($model, $name = '', $db_conn = FALSE) |
||
314 | |||
315 | // -------------------------------------------------------------------- |
||
316 | |||
317 | /** |
||
318 | * Database Loader |
||
319 | * |
||
320 | * @param mixed $params Database configuration options |
||
321 | * @param bool $return Whether to return the database object |
||
322 | * @param bool $query_builder Whether to enable Query Builder |
||
323 | * (overrides the configuration setting) |
||
324 | * |
||
325 | * @return object|bool Database object if $return is set to TRUE, |
||
326 | * FALSE on failure, CI_Loader instance in any other case |
||
327 | */ |
||
328 | public function database($params = '', $return = FALSE, $query_builder = NULL) |
||
354 | |||
355 | // -------------------------------------------------------------------- |
||
356 | |||
357 | /** |
||
358 | * Load the Database Utilities Class |
||
359 | * |
||
360 | * @param object $db Database object |
||
361 | * @param bool $return Whether to return the DB Utilities class object or not |
||
362 | * @return object |
||
363 | */ |
||
364 | public function dbutil($db = NULL, $return = FALSE) |
||
386 | |||
387 | // -------------------------------------------------------------------- |
||
388 | |||
389 | /** |
||
390 | * Load the Database Forge Class |
||
391 | * |
||
392 | * @param object $db Database object |
||
393 | * @param bool $return Whether to return the DB Forge class object or not |
||
394 | * @return object |
||
395 | */ |
||
396 | public function dbforge($db = NULL, $return = FALSE) |
||
430 | |||
431 | // -------------------------------------------------------------------- |
||
432 | |||
433 | /** |
||
434 | * View Loader |
||
435 | * |
||
436 | * Loads "view" files. |
||
437 | * |
||
438 | * @param string $view View name |
||
439 | * @param array $vars An associative array of data |
||
440 | * to be extracted for use in the view |
||
441 | * @param bool $return Whether to return the view output |
||
442 | * or leave it to the Output class |
||
443 | * @return object|string |
||
444 | */ |
||
445 | public function view($view, $vars = array(), $return = FALSE) |
||
449 | |||
450 | // -------------------------------------------------------------------- |
||
451 | |||
452 | /** |
||
453 | * Generic File Loader |
||
454 | * |
||
455 | * @param string $path File path |
||
456 | * @param bool $return Whether to return the file output |
||
457 | * @return object|string |
||
458 | */ |
||
459 | public function file($path, $return = FALSE) |
||
463 | |||
464 | // -------------------------------------------------------------------- |
||
465 | |||
466 | /** |
||
467 | * Set Variables |
||
468 | * |
||
469 | * Once variables are set they become available within |
||
470 | * the controller class and its "view" files. |
||
471 | * |
||
472 | * @param array|object|string $vars |
||
473 | * An associative array or object containing values |
||
474 | * to be set, or a value's name if string |
||
475 | * @param string $val Value to set, only used if $vars is a string |
||
476 | * @return object |
||
477 | */ |
||
478 | public function vars($vars, $val = '') |
||
497 | |||
498 | // -------------------------------------------------------------------- |
||
499 | |||
500 | /** |
||
501 | * Clear Cached Variables |
||
502 | * |
||
503 | * Clears the cached variables. |
||
504 | * |
||
505 | * @return object |
||
506 | */ |
||
507 | public function clear_vars() |
||
512 | |||
513 | // -------------------------------------------------------------------- |
||
514 | |||
515 | /** |
||
516 | * Get Variable |
||
517 | * |
||
518 | * Check if a variable is set and retrieve it. |
||
519 | * |
||
520 | * @param string $key Variable name |
||
521 | * @return mixed The variable or NULL if not found |
||
522 | */ |
||
523 | public function get_var($key) |
||
527 | |||
528 | // -------------------------------------------------------------------- |
||
529 | |||
530 | /** |
||
531 | * Get Variables |
||
532 | * |
||
533 | * Retrieves all loaded variables. |
||
534 | * |
||
535 | * @return array |
||
536 | */ |
||
537 | public function get_vars() |
||
541 | |||
542 | // -------------------------------------------------------------------- |
||
543 | |||
544 | /** |
||
545 | * Helper Loader |
||
546 | * |
||
547 | * @param string|string[] $helpers Helper name(s) |
||
548 | * @return object |
||
549 | */ |
||
550 | public function helper($helpers = array()) |
||
608 | |||
609 | // -------------------------------------------------------------------- |
||
610 | |||
611 | /** |
||
612 | * Load Helpers |
||
613 | * |
||
614 | * An alias for the helper() method in case the developer has |
||
615 | * written the plural form of it. |
||
616 | * |
||
617 | * @uses CI_Loader::helper() |
||
618 | * @param string|string[] $helpers Helper name(s) |
||
619 | * @return object |
||
620 | */ |
||
621 | public function helpers($helpers = array()) |
||
625 | |||
626 | // -------------------------------------------------------------------- |
||
627 | |||
628 | /** |
||
629 | * Language Loader |
||
630 | * |
||
631 | * Loads language files. |
||
632 | * |
||
633 | * @param string|string[] $files List of language file names to load |
||
634 | * @param string Language name |
||
635 | * @return object |
||
636 | */ |
||
637 | public function language($files, $lang = '') |
||
642 | |||
643 | // -------------------------------------------------------------------- |
||
644 | |||
645 | /** |
||
646 | * Config Loader |
||
647 | * |
||
648 | * Loads a config file (an alias for CI_Config::load()). |
||
649 | * |
||
650 | * @uses CI_Config::load() |
||
651 | * @param string $file Configuration file name |
||
652 | * @param bool $use_sections Whether configuration values should be loaded into their own section |
||
653 | * @param bool $fail_gracefully Whether to just return FALSE or display an error message |
||
654 | * @return bool TRUE if the file was loaded correctly or FALSE on failure |
||
655 | */ |
||
656 | public function config($file, $use_sections = FALSE, $fail_gracefully = FALSE) |
||
660 | |||
661 | // -------------------------------------------------------------------- |
||
662 | |||
663 | /** |
||
664 | * Driver Loader |
||
665 | * |
||
666 | * Loads a driver library. |
||
667 | * |
||
668 | * @param string|string[] $library Driver name(s) |
||
669 | * @param array $params Optional parameters to pass to the driver |
||
670 | * @param string $object_name An optional object name to assign to |
||
671 | * |
||
672 | * @return object|bool Object or FALSE on failure if $library is a string |
||
673 | * and $object_name is set. CI_Loader instance otherwise. |
||
674 | */ |
||
675 | public function driver($library, $params = NULL, $object_name = NULL) |
||
706 | |||
707 | // -------------------------------------------------------------------- |
||
708 | |||
709 | /** |
||
710 | * Add Package Path |
||
711 | * |
||
712 | * Prepends a parent path to the library, model, helper and config |
||
713 | * path arrays. |
||
714 | * |
||
715 | * @see CI_Loader::$_ci_library_paths |
||
716 | * @see CI_Loader::$_ci_model_paths |
||
717 | * @see CI_Loader::$_ci_helper_paths |
||
718 | * @see CI_Config::$_config_paths |
||
719 | * |
||
720 | * @param string $path Path to add |
||
721 | * @param bool $view_cascade (default: TRUE) |
||
722 | * @return object |
||
723 | */ |
||
724 | public function add_package_path($path, $view_cascade = TRUE) |
||
740 | |||
741 | // -------------------------------------------------------------------- |
||
742 | |||
743 | /** |
||
744 | * Get Package Paths |
||
745 | * |
||
746 | * Return a list of all package paths. |
||
747 | * |
||
748 | * @param bool $include_base Whether to include BASEPATH (default: FALSE) |
||
749 | * @return array |
||
750 | */ |
||
751 | public function get_package_paths($include_base = FALSE) |
||
755 | |||
756 | // -------------------------------------------------------------------- |
||
757 | |||
758 | /** |
||
759 | * Remove Package Path |
||
760 | * |
||
761 | * Remove a path from the library, model, helper and/or config |
||
762 | * path arrays if it exists. If no path is provided, the most recently |
||
763 | * added path will be removed removed. |
||
764 | * |
||
765 | * @param string $path Path to remove |
||
766 | * @return object |
||
767 | */ |
||
768 | public function remove_package_path($path = '') |
||
811 | |||
812 | // -------------------------------------------------------------------- |
||
813 | |||
814 | /** |
||
815 | * Internal CI Data Loader |
||
816 | * |
||
817 | * Used to load views and files. |
||
818 | * |
||
819 | * Variables are prefixed with _ci_ to avoid symbol collision with |
||
820 | * variables made available to view files. |
||
821 | * |
||
822 | * @used-by CI_Loader::view() |
||
823 | * @used-by CI_Loader::file() |
||
824 | * @param array $_ci_data Data to load |
||
825 | * @return object |
||
826 | */ |
||
827 | protected function _ci_load($_ci_data) |
||
950 | |||
951 | // -------------------------------------------------------------------- |
||
952 | |||
953 | /** |
||
954 | * Internal CI Library Loader |
||
955 | * |
||
956 | * @used-by CI_Loader::library() |
||
957 | * @uses CI_Loader::_ci_init_library() |
||
958 | * |
||
959 | * @param string $class Class name to load |
||
960 | * @param mixed $params Optional parameters to pass to the class constructor |
||
961 | * @param string $object_name Optional object name to assign to |
||
962 | * @return void |
||
963 | * |
||
964 | * modified by ci-phpunit-test |
||
965 | */ |
||
966 | protected function _ci_load_library($class, $params = NULL, $object_name = NULL) |
||
1045 | |||
1046 | // -------------------------------------------------------------------- |
||
1047 | |||
1048 | /** |
||
1049 | * Internal CI Stock Library Loader |
||
1050 | * |
||
1051 | * @used-by CI_Loader::_ci_load_library() |
||
1052 | * @uses CI_Loader::_ci_init_library() |
||
1053 | * |
||
1054 | * @param string $library Library name to load |
||
1055 | * @param string $file_path Path to the library filename, relative to libraries/ |
||
1056 | * @param mixed $params Optional parameters to pass to the class constructor |
||
1057 | * @param string $object_name Optional object name to assign to |
||
1058 | * @return void |
||
1059 | * |
||
1060 | * modified by ci-phpunit-test |
||
1061 | */ |
||
1062 | protected function _ci_load_stock_library($library_name, $file_path, $params, $object_name) |
||
1134 | |||
1135 | // -------------------------------------------------------------------- |
||
1136 | |||
1137 | /** |
||
1138 | * Internal CI Library Instantiator |
||
1139 | * |
||
1140 | * @used-by CI_Loader::_ci_load_stock_library() |
||
1141 | * @used-by CI_Loader::_ci_load_library() |
||
1142 | * |
||
1143 | * @param string $class Class name |
||
1144 | * @param string $prefix Class name prefix |
||
1145 | * @param array|null|bool $config Optional configuration to pass to the class constructor: |
||
1146 | * FALSE to skip; |
||
1147 | * NULL to search in config paths; |
||
1148 | * array containing configuration data |
||
1149 | * @param string $object_name Optional object name to assign to |
||
1150 | * @return void |
||
1151 | */ |
||
1152 | protected function _ci_init_library($class, $prefix, $config = FALSE, $object_name = NULL) |
||
1241 | |||
1242 | // -------------------------------------------------------------------- |
||
1243 | |||
1244 | /** |
||
1245 | * CI Autoloader |
||
1246 | * |
||
1247 | * Loads component listed in the config/autoload.php file. |
||
1248 | * |
||
1249 | * @used-by CI_Loader::initialize() |
||
1250 | * @return void |
||
1251 | */ |
||
1252 | protected function _ci_autoloader() |
||
1328 | |||
1329 | // -------------------------------------------------------------------- |
||
1330 | |||
1331 | /** |
||
1332 | * CI Object to Array translator |
||
1333 | * |
||
1334 | * Takes an object as input and converts the class variables to |
||
1335 | * an associative array with key/value pairs. |
||
1336 | * |
||
1337 | * @param object $object Object data to translate |
||
1338 | * @return array |
||
1339 | */ |
||
1340 | protected function _ci_object_to_array($object) |
||
1344 | |||
1345 | // -------------------------------------------------------------------- |
||
1346 | |||
1347 | /** |
||
1348 | * CI Component getter |
||
1349 | * |
||
1350 | * Get a reference to a specific library or model. |
||
1351 | * |
||
1352 | * @param string $component Component name |
||
1353 | * @return bool |
||
1354 | */ |
||
1355 | protected function &_ci_get_component($component) |
||
1360 | |||
1361 | // -------------------------------------------------------------------- |
||
1362 | |||
1363 | /** |
||
1364 | * Prep filename |
||
1365 | * |
||
1366 | * This function prepares filenames of various items to |
||
1367 | * make their loading more reliable. |
||
1368 | * |
||
1369 | * @param string|string[] $filename Filename(s) |
||
1370 | * @param string $extension Filename extension |
||
1371 | * @return array |
||
1372 | */ |
||
1373 | protected function _ci_prep_filename($filename, $extension) |
||
1389 | |||
1390 | } |
||
1391 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.