Conditions | 10 |
Paths | 97 |
Total Lines | 73 |
Code Lines | 40 |
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 |
||
49 | function __construct( $params ) { |
||
50 | parent::__construct( $params ); |
||
51 | $params = $this->applyDefaultParams( $params ); |
||
52 | |||
53 | if ( $params['persistent'] ) { |
||
54 | // The pool ID must be unique to the server/option combination. |
||
55 | // The Memcached object is essentially shared for each pool ID. |
||
56 | // We can only reuse a pool ID if we keep the config consistent. |
||
57 | $this->client = new Memcached( md5( serialize( $params ) ) ); |
||
58 | if ( count( $this->client->getServerList() ) ) { |
||
59 | $this->logger->debug( __METHOD__ . ": persistent Memcached object already loaded." ); |
||
60 | return; // already initialized; don't add duplicate servers |
||
61 | } |
||
62 | } else { |
||
63 | $this->client = new Memcached; |
||
64 | } |
||
65 | |||
66 | if ( $params['use_binary_protocol'] ) { |
||
67 | $this->client->setOption( Memcached::OPT_BINARY_PROTOCOL, true ); |
||
68 | } |
||
69 | |||
70 | if ( isset( $params['retry_timeout'] ) ) { |
||
71 | $this->client->setOption( Memcached::OPT_RETRY_TIMEOUT, $params['retry_timeout'] ); |
||
72 | } |
||
73 | |||
74 | if ( isset( $params['server_failure_limit'] ) ) { |
||
75 | $this->client->setOption( Memcached::OPT_SERVER_FAILURE_LIMIT, $params['server_failure_limit'] ); |
||
76 | } |
||
77 | |||
78 | // The compression threshold is an undocumented php.ini option for some |
||
79 | // reason. There's probably not much harm in setting it globally, for |
||
80 | // compatibility with the settings for the PHP client. |
||
81 | ini_set( 'memcached.compression_threshold', $params['compress_threshold'] ); |
||
82 | |||
83 | // Set timeouts |
||
84 | $this->client->setOption( Memcached::OPT_CONNECT_TIMEOUT, $params['connect_timeout'] * 1000 ); |
||
85 | $this->client->setOption( Memcached::OPT_SEND_TIMEOUT, $params['timeout'] ); |
||
86 | $this->client->setOption( Memcached::OPT_RECV_TIMEOUT, $params['timeout'] ); |
||
87 | $this->client->setOption( Memcached::OPT_POLL_TIMEOUT, $params['timeout'] / 1000 ); |
||
88 | |||
89 | // Set libketama mode since it's recommended by the documentation and |
||
90 | // is as good as any. There's no way to configure libmemcached to use |
||
91 | // hashes identical to the ones currently in use by the PHP client, and |
||
92 | // even implementing one of the libmemcached hashes in pure PHP for |
||
93 | // forwards compatibility would require MemcachedClient::get_sock() to be |
||
94 | // rewritten. |
||
95 | $this->client->setOption( Memcached::OPT_LIBKETAMA_COMPATIBLE, true ); |
||
96 | |||
97 | // Set the serializer |
||
98 | switch ( $params['serializer'] ) { |
||
99 | case 'php': |
||
100 | $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP ); |
||
101 | break; |
||
102 | case 'igbinary': |
||
103 | if ( !Memcached::HAVE_IGBINARY ) { |
||
104 | throw new InvalidArgumentException( |
||
105 | __CLASS__ . ': the igbinary extension is not available ' . |
||
106 | 'but igbinary serialization was requested.' |
||
107 | ); |
||
108 | } |
||
109 | $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY ); |
||
110 | break; |
||
111 | default: |
||
112 | throw new InvalidArgumentException( |
||
113 | __CLASS__ . ': invalid value for serializer parameter' |
||
114 | ); |
||
115 | } |
||
116 | $servers = []; |
||
117 | foreach ( $params['servers'] as $host ) { |
||
118 | $servers[] = IP::splitHostAndPort( $host ); // (ip, port) |
||
119 | } |
||
120 | $this->client->addServers( $servers ); |
||
121 | } |
||
122 | |||
251 |
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: