Conditions | 13 |
Paths | 300 |
Total Lines | 100 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
91 | public function load_driver($child) |
||
92 | { |
||
93 | // Get CodeIgniter instance and subclass prefix |
||
94 | $prefix = config_item('subclass_prefix'); |
||
95 | |||
96 | if ( ! isset($this->lib_name)) |
||
97 | { |
||
98 | // Get library name without any prefix |
||
99 | $this->lib_name = str_replace(array('CI_', $prefix), '', get_class($this)); |
||
100 | } |
||
101 | |||
102 | // The child will be prefixed with the parent lib |
||
103 | $child_name = $this->lib_name.'_'.$child; |
||
104 | |||
105 | // See if requested child is a valid driver |
||
106 | if ( ! in_array($child, $this->valid_drivers)) |
||
107 | { |
||
108 | // The requested driver isn't valid! |
||
109 | $msg = 'Invalid driver requested: '.$child_name; |
||
110 | log_message('error', $msg); |
||
111 | show_error($msg); |
||
112 | } |
||
113 | |||
114 | // Get package paths and filename case variations to search |
||
115 | $CI = get_instance(); |
||
116 | $paths = $CI->load->get_package_paths(TRUE); |
||
117 | |||
118 | // Is there an extension? |
||
119 | $class_name = $prefix.$child_name; |
||
120 | $found = class_exists($class_name, FALSE); |
||
121 | if ( ! $found) |
||
122 | { |
||
123 | // Check for subclass file |
||
124 | foreach ($paths as $path) |
||
125 | { |
||
126 | // Does the file exist? |
||
127 | $file = $path.'libraries/'.$this->lib_name.'/drivers/'.$prefix.$child_name.'.php'; |
||
128 | if (file_exists($file)) |
||
129 | { |
||
130 | // Yes - require base class from BASEPATH |
||
131 | $basepath = BASEPATH.'libraries/'.$this->lib_name.'/drivers/'.$child_name.'.php'; |
||
132 | if ( ! file_exists($basepath)) |
||
133 | { |
||
134 | $msg = 'Unable to load the requested class: CI_'.$child_name; |
||
135 | log_message('error', $msg); |
||
136 | show_error($msg); |
||
137 | } |
||
138 | |||
139 | // Include both sources and mark found |
||
140 | include_once($basepath); |
||
141 | include_once($file); |
||
142 | $found = TRUE; |
||
143 | break; |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | |||
148 | // Do we need to search for the class? |
||
149 | if ( ! $found) |
||
150 | { |
||
151 | // Use standard class name |
||
152 | $class_name = 'CI_'.$child_name; |
||
153 | if ( ! class_exists($class_name, FALSE)) |
||
154 | { |
||
155 | // Check package paths |
||
156 | foreach ($paths as $path) |
||
157 | { |
||
158 | // Does the file exist? |
||
159 | $file = $path.'libraries/'.$this->lib_name.'/drivers/'.$child_name.'.php'; |
||
160 | if (file_exists($file)) |
||
161 | { |
||
162 | // Include source |
||
163 | include_once($file); |
||
164 | break; |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | |||
170 | // Did we finally find the class? |
||
171 | if ( ! class_exists($class_name, FALSE)) |
||
172 | { |
||
173 | if (class_exists($child_name, FALSE)) |
||
174 | { |
||
175 | $class_name = $child_name; |
||
176 | } |
||
177 | else |
||
178 | { |
||
179 | $msg = 'Unable to load the requested driver: '.$class_name; |
||
180 | log_message('error', $msg); |
||
181 | show_error($msg); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | // Instantiate, decorate and add child |
||
186 | $obj = new $class_name(); |
||
187 | $obj->decorate($this); |
||
188 | $this->$child = $obj; |
||
189 | return $this->$child; |
||
190 | } |
||
191 | |||
343 |
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.