| Conditions | 11 |
| Paths | 38 |
| Total Lines | 40 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 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 /** MicroMemcachedCache */ |
||
| 34 | public function __construct(array $config = []) |
||
| 35 | { |
||
| 36 | parent::__construct($config); |
||
| 37 | |||
| 38 | if (empty($config['type']) || !$this->check()) { |
||
| 39 | throw new Exception('Memcache(d) not installed or not select type'); |
||
| 40 | } |
||
| 41 | |||
| 42 | switch (strtolower($config['type'])) { |
||
| 43 | case 'memcached': |
||
| 44 | $this->driver = new \Memcached; |
||
| 45 | break; |
||
| 46 | |||
| 47 | case 'memcache': |
||
| 48 | $this->driver = new \Memcache; |
||
| 49 | break; |
||
| 50 | |||
| 51 | default: |
||
| 52 | throw new Exception('Selected type not valid in the driver'); |
||
| 53 | } |
||
| 54 | |||
| 55 | if (!empty($config['servers'])) { |
||
| 56 | $this->driver->addServers($config['servers']); |
||
|
|
|||
| 57 | } elseif ($config['server']) { |
||
| 58 | $conf = $config['server']; |
||
| 59 | $server = [ |
||
| 60 | 'hostname' => (!empty($conf['hostname']) ? $conf['hostname'] : '127.0.0.1'), |
||
| 61 | 'port' => (!empty($conf['port']) ? $conf['port'] : 11211), |
||
| 62 | 'weight' => (!empty($conf['weight']) ? $conf['weight'] : 1) |
||
| 63 | ]; |
||
| 64 | |||
| 65 | if (get_class($this->driver) === 'Memcached') { |
||
| 66 | $this->driver->addServer($server['hostname'], $server['port'], $server['weight']); |
||
| 67 | } else { |
||
| 68 | $this->driver->addServer($server['hostname'], $server['port'], true, $server['weight']); |
||
| 69 | } |
||
| 70 | } else { |
||
| 71 | throw new Exception('Server(s) not configured'); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 180 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: