Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
32 | class Driver implements ExtendedCacheItemPoolInterface |
||
33 | { |
||
34 | use DriverBaseTrait, MemcacheDriverCollisionDetectorTrait; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $memcacheFlags = 0; |
||
40 | |||
41 | /** |
||
42 | * Driver constructor. |
||
43 | * @param array $config |
||
44 | * @throws phpFastCacheDriverException |
||
45 | */ |
||
46 | public function __construct(array $config = []) |
||
47 | { |
||
48 | self::checkCollision('Memcache'); |
||
49 | $this->setup($config); |
||
50 | |||
51 | if (!$this->driverCheck()) { |
||
52 | throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
||
53 | } else { |
||
54 | $this->driverConnect(); |
||
55 | |||
56 | if (array_key_exists('compress_data', $config) && $config[ 'compress_data' ] === true) { |
||
57 | $this->memcacheFlags = MEMCACHE_COMPRESSED; |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function driverCheck() |
||
69 | |||
70 | /** |
||
71 | * @param \Psr\Cache\CacheItemInterface $item |
||
72 | * @return mixed |
||
73 | * @throws phpFastCacheInvalidArgumentException |
||
74 | */ |
||
75 | protected function driverWrite(CacheItemInterface $item) |
||
86 | |||
87 | /** |
||
88 | * @param \Psr\Cache\CacheItemInterface $item |
||
89 | * @return mixed |
||
90 | */ |
||
91 | View Code Duplication | protected function driverRead(CacheItemInterface $item) |
|
101 | |||
102 | /** |
||
103 | * @param \Psr\Cache\CacheItemInterface $item |
||
104 | * @return bool |
||
105 | * @throws phpFastCacheInvalidArgumentException |
||
106 | */ |
||
107 | protected function driverDelete(CacheItemInterface $item) |
||
118 | |||
119 | /** |
||
120 | * @return bool |
||
121 | */ |
||
122 | protected function driverClear() |
||
126 | |||
127 | /** |
||
128 | * @return bool |
||
129 | */ |
||
130 | View Code Duplication | protected function driverConnect() |
|
131 | { |
||
132 | $servers = (!empty($this->config[ 'servers' ]) && is_array($this->config[ 'servers' ]) ? $this->config[ 'servers' ] : []); |
||
133 | if (count($servers) < 1) { |
||
134 | $servers = [ |
||
135 | [ |
||
136 | 'host' =>'127.0.0.1', |
||
137 | 'port' => 11211, |
||
138 | 'sasl_user' => false, |
||
139 | 'sasl_password' => false |
||
140 | ], |
||
141 | ]; |
||
142 | } |
||
143 | |||
144 | foreach ($servers as $server) { |
||
145 | try { |
||
146 | if (!$this->instance->addServer($server['host'], $server['port'])) { |
||
147 | $this->fallback = true; |
||
148 | } |
||
149 | if(!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password'])){ |
||
150 | $this->instance->setSaslAuthData($server[ 'sasl_user' ], $server[ 'sasl_password']); |
||
151 | } |
||
152 | } catch (\Exception $e) { |
||
153 | $this->fallback = true; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /******************** |
||
159 | * |
||
160 | * PSR-6 Extended Methods |
||
161 | * |
||
162 | *******************/ |
||
163 | |||
164 | /** |
||
165 | * @return driverStatistic |
||
166 | */ |
||
167 | View Code Duplication | public function getStats() |
|
182 | } |