Conditions | 11 |
Paths | 38 |
Total Lines | 40 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 /** MicroMemcachedDriver */ |
||
35 | public function __construct(array $config = []) |
||
36 | { |
||
37 | parent::__construct($config); |
||
38 | |||
39 | if (empty($config['type']) || !$this->check()) { |
||
40 | throw new Exception('Memcache(d) not installed or not select type'); |
||
41 | } |
||
42 | |||
43 | switch (strtolower($config['type'])) { |
||
44 | case 'memcached': |
||
45 | $this->driver = new \Memcached; |
||
46 | break; |
||
47 | |||
48 | case 'memcache': |
||
49 | $this->driver = new \Memcache; |
||
50 | break; |
||
51 | |||
52 | default: |
||
53 | throw new Exception('Selected type not valid in the driver'); |
||
54 | } |
||
55 | |||
56 | if (!empty($config['servers'])) { |
||
57 | $this->driver->addServers($config['servers']); |
||
|
|||
58 | } elseif ($config['server']) { |
||
59 | $conf = $config['server']; |
||
60 | $server = [ |
||
61 | 'hostname' => !empty($conf['hostname']) ? $conf['hostname'] : '127.0.0.1', |
||
62 | 'port' => !empty($conf['port']) ? $conf['port'] : 11211, |
||
63 | 'weight' => !empty($conf['weight']) ? $conf['weight'] : 1 |
||
64 | ]; |
||
65 | |||
66 | if (get_class($this->driver) === 'Memcached') { |
||
67 | $this->driver->addServer($server['hostname'], $server['port'], $server['weight']); |
||
68 | } else { |
||
69 | $this->driver->addServer($server['hostname'], $server['port'], true, $server['weight']); |
||
70 | } |
||
71 | } else { |
||
72 | throw new Exception('Server(s) not configured'); |
||
73 | } |
||
74 | } |
||
75 | |||
179 |
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: