Conditions | 13 |
Paths | 145 |
Total Lines | 86 |
Code Lines | 49 |
Lines | 52 |
Ratio | 60.47 % |
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 |
||
79 | protected function connect() |
||
80 | { |
||
81 | // determine how to get our |
||
82 | $method = $this->getParameter('method', 'normal'); |
||
83 | |||
84 | View Code Duplication | switch ($method) { |
|
85 | case 'normal': |
||
86 | // get parameters normally |
||
87 | $database = $this->getParameter('database'); |
||
88 | $host = $this->getParameter('host', 'localhost'); |
||
89 | $password = $this->getParameter('password'); |
||
90 | $user = $this->getParameter('username'); |
||
91 | break; |
||
92 | |||
93 | case 'server': |
||
94 | // construct a connection string from existing $_SERVER values |
||
95 | // and extract them to local scope |
||
96 | $parameters = $this->loadParameters($_SERVER); |
||
97 | extract($parameters); |
||
98 | break; |
||
99 | |||
100 | case 'env': |
||
101 | // construct a connection string from existing $_ENV values |
||
102 | // and extract them to local scope |
||
103 | $parameters = $this->loadParameters($_ENV); |
||
104 | extract($parameters); |
||
105 | break; |
||
106 | |||
107 | default: |
||
108 | // who knows what the user wants... |
||
109 | $error = 'Invalid AgaviMySQLDatabase parameter retrieval method ' . |
||
110 | '"%s"'; |
||
111 | $error = sprintf($error, $method); |
||
112 | throw new DatabaseException($error); |
||
113 | } |
||
114 | |||
115 | // let's see if we need a persistent connection |
||
116 | $persistent = $this->getParameter('persistent', false); |
||
117 | |||
118 | View Code Duplication | if ($password === null) { |
|
119 | if ($user === null) { |
||
120 | $args = array($host, null, null); |
||
121 | } else { |
||
122 | $args = array($host, $user, null); |
||
123 | } |
||
124 | } else { |
||
125 | $args = array($host, $user, $password); |
||
126 | } |
||
127 | |||
128 | if ($persistent) { |
||
129 | $this->connection = call_user_func_array('mysql_pconnect', $args); |
||
130 | } else { |
||
131 | $this->connection = call_user_func_array('mysql_connect', $args + array(true)); |
||
132 | } |
||
133 | |||
134 | // make sure the connection went through |
||
135 | if ($this->connection === false) { |
||
136 | // the connection's foobar'd |
||
137 | $error = 'Failed to create a AgaviMySQLDatabase connection'; |
||
138 | throw new DatabaseException($error); |
||
139 | } |
||
140 | |||
141 | View Code Duplication | if ($this->hasParameter('charset')) { |
|
142 | if (!mysql_set_charset($this->getParameter('charset'), $this->connection)) { |
||
143 | $error = 'Failed to set charset "%s"'; |
||
144 | $error = sprintf($error, $this->getParameter('charset')); |
||
145 | throw new DatabaseException($error); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // select our database |
||
150 | View Code Duplication | if ($database !== null && !@mysql_select_db($database, $this->connection)) { |
|
151 | // can't select the database |
||
152 | $error = 'Failed to select AgaviMySQLDatabase "%s"'; |
||
153 | $error = sprintf($error, $database); |
||
154 | throw new DatabaseException($error); |
||
155 | } |
||
156 | |||
157 | // since we're not an abstraction layer, we copy the connection |
||
158 | // to the resource |
||
159 | $this->resource =& $this->connection; |
||
160 | |||
161 | foreach ((array)$this->getParameter('init_queries') as $query) { |
||
162 | mysql_query($query, $this->connection); |
||
163 | } |
||
164 | } |
||
165 | |||
208 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.