Conditions | 14 |
Paths | 71 |
Total Lines | 54 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
55 | public function controller($controller, $name = '', $db_conn = FALSE){ |
||
56 | if (is_array($controller)){ |
||
57 | foreach ($controller as $babe){ |
||
58 | $this->controller($babe); |
||
59 | } |
||
60 | return; |
||
61 | } |
||
62 | if ($controller == ''){ |
||
63 | return; |
||
64 | } |
||
65 | $path = ''; |
||
66 | // Is the controller in a sub-folder? If so, parse out the filename and path. |
||
67 | if (($last_slash = strrpos($controller, '/')) !== FALSE){ |
||
68 | // The path is in front of the last slash |
||
69 | $path = substr($controller, 0, $last_slash + 1); |
||
70 | // And the controller name behind it |
||
71 | $controller = substr($controller, $last_slash + 1); |
||
72 | } |
||
73 | |||
74 | if ($name == ''){ |
||
75 | $name = $controller; |
||
76 | } |
||
77 | |||
78 | if (in_array($name, $this->_my_controllers, TRUE)){ |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | $CI =$this->getCi(); |
||
83 | if (isset($CI->$name)){ |
||
84 | show_error('The controller name you are loading is the name of a resource that is already being used: '.$name); |
||
85 | } |
||
86 | $controller = strtolower($controller); |
||
87 | foreach ($this->_my_controller_paths as $mod_path){ |
||
88 | if ( ! file_exists($mod_path.'controllers/'.$path.$controller.'.php')){ |
||
89 | continue; |
||
90 | } |
||
91 | if ($db_conn !== FALSE AND ! class_exists('CI_DB')){ |
||
|
|||
92 | if ($db_conn === TRUE){ |
||
93 | $db_conn = ''; |
||
94 | } |
||
95 | $CI->load->database($db_conn, FALSE, TRUE); |
||
96 | } |
||
97 | if ( ! class_exists('CI_Controller')){ |
||
98 | load_class('Controller', 'core'); |
||
99 | } |
||
100 | require_once($mod_path.'controllers/'.$path.$controller.'.php'); |
||
101 | $controller = ucfirst($controller); |
||
102 | $CI->$name = new $controller(); |
||
103 | |||
104 | $this->_my_controllers[] = $name; |
||
105 | return; |
||
106 | } |
||
107 | show_error('Unable to locate the controller you have specified: '.$controller); |
||
108 | } |
||
109 | } |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.