Conditions | 19 |
Paths | 240 |
Total Lines | 47 |
Code Lines | 25 |
Lines | 16 |
Ratio | 34.04 % |
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 |
||
131 | protected function driverConnect() |
||
132 | { |
||
133 | $this->instance = new MemcachedSoftware(); |
||
134 | $this->instance->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); |
||
135 | $servers = (!empty($this->config[ 'servers' ]) && is_array($this->config[ 'servers' ]) ? $this->config[ 'servers' ] : []); |
||
136 | View Code Duplication | if (count($servers) < 1) { |
|
137 | $servers = [ |
||
138 | [ |
||
139 | 'host' => !empty($this->config[ 'host' ]) ? $this->config[ 'host' ] : '127.0.0.1', |
||
140 | 'path' => !empty($this->config[ 'path' ]) ? $this->config[ 'path' ] : false, |
||
141 | 'port' => !empty($this->config[ 'port' ]) ? $this->config[ 'port' ] : 11211, |
||
142 | 'sasl_user' => !empty($this->config[ 'sasl_user' ]) ? $this->config[ 'sasl_user' ] : false, |
||
143 | 'sasl_password' =>!empty($this->config[ 'sasl_password' ]) ? $this->config[ 'sasl_password' ]: false, |
||
144 | ], |
||
145 | ]; |
||
146 | } |
||
147 | |||
148 | foreach ($servers as $server) { |
||
149 | try { |
||
150 | /** |
||
151 | * If path is provided we consider it as an UNIX Socket |
||
152 | */ |
||
153 | View Code Duplication | if(!empty($server[ 'path' ]) && !$this->instance->addServer($server[ 'path' ], 0)){ |
|
154 | $this->fallback = true; |
||
155 | }else if (!empty($server[ 'host' ]) && !$this->instance->addServer($server[ 'host' ], $server[ 'port' ])) { |
||
156 | $this->fallback = true; |
||
157 | } |
||
158 | |||
159 | if (!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password' ])) { |
||
160 | $this->instance->setSaslAuthData($server[ 'sasl_user' ], $server[ 'sasl_password' ]); |
||
161 | } |
||
162 | |||
163 | } catch (\Exception $e) { |
||
164 | $this->fallback = true; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Since Memcached does not throw |
||
170 | * any error if not connected ... |
||
171 | */ |
||
172 | $version = $this->instance->getVersion(); |
||
173 | if(!$version || $this->instance->getResultCode() !== MemcachedSoftware::RES_SUCCESS){ |
||
174 | throw new phpFastCacheDriverException('Memcached seems to not be connected'); |
||
175 | } |
||
176 | return true; |
||
177 | } |
||
178 | |||
203 | } |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.