Conditions | 12 |
Paths | 73 |
Total Lines | 79 |
Code Lines | 45 |
Lines | 52 |
Ratio | 65.82 % |
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 AgaviMySQLiDatabase parameter retrieval method ' . |
||
110 | '"%s"'; |
||
111 | $error = sprintf($error, $method); |
||
112 | throw new DatabaseException($error); |
||
113 | } |
||
114 | |||
115 | View Code Duplication | if ($password === null) { |
|
116 | if ($user === null) { |
||
117 | $args = array($host, null, null); |
||
118 | } else { |
||
119 | $args = array($host, $user, null); |
||
120 | } |
||
121 | } else { |
||
122 | $args = array($host, $user, $password); |
||
123 | } |
||
124 | |||
125 | $this->connection = new \mysqli($args[0], $args[1], $args[2]); |
||
126 | |||
127 | // make sure the connection went through |
||
128 | if ($this->connection === false) { |
||
129 | // the connection's foobar'd |
||
130 | $error = 'Failed to create a AgaviMySQLiDatabase connection'; |
||
131 | throw new DatabaseException($error); |
||
132 | } |
||
133 | |||
134 | View Code Duplication | if ($this->hasParameter('charset')) { |
|
135 | if (!$this->connection->set_charset($this->getParameter('charset'))) { |
||
136 | $error = 'Failed to set charset "%s"'; |
||
137 | $error = sprintf($error, $this->getParameter('charset')); |
||
138 | throw new DatabaseException($error); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | // select our database |
||
143 | View Code Duplication | if ($database !== null && !$this->connection->select_db($database)) { |
|
144 | // can't select the database |
||
145 | $error = 'Failed to select AgaviMySQLiDatabase "%s"'; |
||
146 | $error = sprintf($error, $database); |
||
147 | throw new DatabaseException($error); |
||
148 | } |
||
149 | |||
150 | // since we're not an abstraction layer, we copy the connection |
||
151 | // to the resource |
||
152 | $this->resource =& $this->connection; |
||
153 | |||
154 | foreach ((array)$this->getParameter('init_queries') as $query) { |
||
155 | $this->connection->query($query); |
||
156 | } |
||
157 | } |
||
158 | |||
173 |
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.