| Conditions | 8 |
| Paths | 8 |
| Total Lines | 93 |
| Code Lines | 68 |
| 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->memcache = new \Memcache(); |
||
| 99 | |||
| 100 | if (empty($config)) {
|
||
| 101 | $this->config = [ |
||
| 102 | 'host' => '127.0.0.1', |
||
| 103 | 'port' => 11211, |
||
| 104 | 'persistent' => true, |
||
| 105 | 'timeout' => 1, |
||
| 106 | 'retryInterval' => 15, |
||
| 107 | 'weight' => 1, |
||
| 108 | ]; |
||
| 109 | |||
| 110 | $this->memcache->addserver($this->config[ 'host' ], $this->config[ 'port' ]); |
||
| 111 | } elseif (isset($config[ 'servers' ])) {
|
||
| 112 | foreach ($config[ 'servers' ] as $server) {
|
||
| 113 | $this->config[ $server[ 'host' ] ] = array_merge( |
||
| 114 | [ |
||
| 115 | 'host' => '127.0.0.1', |
||
| 116 | 'port' => 11211, |
||
| 117 | 'persistent' => true, |
||
| 118 | 'timeout' => 1, |
||
| 119 | 'weight' => 1, |
||
| 120 | ], |
||
| 121 | $server |
||
| 122 | ); |
||
| 123 | |||
| 124 | if (array_key_exists('status', $server)) {
|
||
| 125 | if ($server[ 'status' ] === false) {
|
||
| 126 | $this->config[ $server[ 'host' ] ][ 'retryInterval' ] = -1; |
||
| 127 | |||
| 128 | $this->memcache->addserver( |
||
| 129 | $this->config[ $server[ 'host' ] ][ 'host' ], |
||
| 130 | $this->config[ $server[ 'host' ] ][ 'port' ], |
||
| 131 | $this->config[ $server[ 'host' ] ][ 'persistent' ], |
||
| 132 | $this->config[ $server[ 'host' ] ][ 'weight' ], |
||
| 133 | $this->config[ $server[ 'host' ] ][ 'timeout' ], |
||
| 134 | $this->config[ $server[ 'host' ] ][ 'retryInterval' ], |
||
| 135 | false |
||
| 136 | ); |
||
| 137 | |||
| 138 | continue; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | $this->memcache->addserver( |
||
| 143 | $this->config[ $server[ 'host' ] ][ 'host' ], |
||
| 144 | $this->config[ $server[ 'host' ] ][ 'port' ], |
||
| 145 | $this->config[ $server[ 'host' ] ][ 'persistent' ], |
||
| 146 | $this->config[ $server[ 'host' ] ][ 'weight' ], |
||
| 147 | $this->config[ $server[ 'host' ] ][ 'timeout' ] |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | } else {
|
||
| 151 | $this->config = array_merge( |
||
| 152 | [ |
||
| 153 | 'host' => '127.0.0.1', |
||
| 154 | 'port' => 11211, |
||
| 155 | 'persistent' => true, |
||
| 156 | 'timeout' => 1, |
||
| 157 | 'weight' => 1, |
||
| 158 | ], |
||
| 159 | $config |
||
| 160 | ); |
||
| 161 | |||
| 162 | if (isset($this->config[ 'status' ])) {
|
||
| 163 | if ($this->config[ 'status' ] === false) {
|
||
| 164 | $this->memcache->addserver( |
||
| 165 | $this->config[ 'host' ], |
||
| 166 | $this->config[ 'port' ], |
||
| 167 | $this->config[ 'persistent' ], |
||
| 168 | $this->config[ 'weight' ], |
||
| 169 | $this->config[ 'timeout' ], |
||
| 170 | -1, |
||
| 171 | false |
||
| 172 | ); |
||
| 173 | } else {
|
||
| 174 | $this->memcache->addserver( |
||
| 175 | $this->config[ 'host' ], |
||
| 176 | $this->config[ 'port' ], |
||
| 177 | $this->config[ 'persistent' ], |
||
| 178 | $this->config[ 'weight' ], |
||
| 179 | $this->config[ 'timeout' ] |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | } else {
|
||
| 183 | $this->memcache->addserver( |
||
| 184 | $this->config[ 'host' ], |
||
| 185 | $this->config[ 'port' ], |
||
| 186 | $this->config[ 'persistent' ], |
||
| 187 | $this->config[ 'weight' ], |
||
| 188 | $this->config[ 'timeout' ] |
||
| 189 | ); |
||
| 293 | } |