| Conditions | 8 |
| Paths | 8 |
| Total Lines | 73 |
| Code Lines | 49 |
| 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 |
||
| 96 | public function connect(array $config) |
||
| 97 | {
|
||
| 98 | $this->memcached = new \Memcached(); |
||
| 99 | $this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, false); |
||
| 100 | |||
| 101 | if (empty($config)) {
|
||
| 102 | $this->config = [ |
||
| 103 | 'host' => '127.0.0.1', |
||
| 104 | 'port' => 11211, |
||
| 105 | 'weight' => 1, |
||
| 106 | ]; |
||
| 107 | |||
| 108 | $this->memcached->addserver($this->config[ 'host' ], $this->config[ 'port' ]); |
||
| 109 | } elseif (isset($config[ 'servers' ])) {
|
||
| 110 | foreach ($config[ 'servers' ] as $server) {
|
||
| 111 | $this->config[ $server[ 'host' ] ] = array_merge( |
||
| 112 | [ |
||
| 113 | 'host' => '127.0.0.1', |
||
| 114 | 'port' => 11211, |
||
| 115 | 'weight' => 1, |
||
| 116 | ], |
||
| 117 | $server |
||
| 118 | ); |
||
| 119 | |||
| 120 | if (array_key_exists('status', $server)) {
|
||
| 121 | if ($server[ 'status' ] === false) {
|
||
| 122 | $this->config[ $server[ 'host' ] ][ 'retryInterval' ] = -1; |
||
| 123 | |||
| 124 | $this->memcached->addserver( |
||
| 125 | $this->config[ $server[ 'host' ] ][ 'host' ], |
||
| 126 | $this->config[ $server[ 'host' ] ][ 'port' ], |
||
| 127 | $this->config[ $server[ 'host' ] ][ 'weight' ] |
||
| 128 | ); |
||
| 129 | |||
| 130 | continue; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | $this->memcached->addserver( |
||
| 135 | $this->config[ $server[ 'host' ] ][ 'host' ], |
||
| 136 | $this->config[ $server[ 'host' ] ][ 'port' ], |
||
| 137 | $this->config[ $server[ 'host' ] ][ 'weight' ] |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | } else {
|
||
| 141 | $this->config = array_merge( |
||
| 142 | [ |
||
| 143 | 'host' => '127.0.0.1', |
||
| 144 | 'port' => 11211, |
||
| 145 | 'weight' => 1, |
||
| 146 | ], |
||
| 147 | $config |
||
| 148 | ); |
||
| 149 | |||
| 150 | if (isset($this->config[ 'status' ])) {
|
||
| 151 | if ($this->config[ 'status' ] === false) {
|
||
| 152 | $this->memcached->addserver( |
||
| 153 | $this->config[ 'host' ], |
||
| 154 | $this->config[ 'port' ], |
||
| 155 | $this->config[ 'weight' ] |
||
| 156 | ); |
||
| 157 | } else {
|
||
| 158 | $this->memcached->addserver( |
||
| 159 | $this->config[ 'host' ], |
||
| 160 | $this->config[ 'port' ], |
||
| 161 | $this->config[ 'weight' ] |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | } else {
|
||
| 165 | $this->memcached->addserver( |
||
| 166 | $this->config[ 'host' ], |
||
| 167 | $this->config[ 'port' ], |
||
| 168 | $this->config[ 'weight' ] |
||
| 169 | ); |
||
| 272 | } |