Total Complexity | 43 |
Total Lines | 308 |
Duplicated Lines | 5.52 % |
Coverage | 0% |
Changes | 0 |
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:
Complex classes like MasterSlaveConnection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MasterSlaveConnection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
83 | class MasterSlaveConnection extends Connection |
||
84 | { |
||
85 | /** |
||
86 | * Master and slave connection (one of the randomly picked slaves). |
||
87 | * |
||
88 | * @var \Doctrine\DBAL\Driver\Connection[] |
||
89 | */ |
||
90 | protected $connections = ['master' => null, 'slave' => null]; |
||
91 | |||
92 | /** |
||
93 | * You can keep the slave connection and then switch back to it |
||
94 | * during the request if you know what you are doing. |
||
95 | * |
||
96 | * @var boolean |
||
97 | */ |
||
98 | protected $keepSlave = false; |
||
99 | |||
100 | /** |
||
101 | * Creates Master Slave Connection. |
||
102 | * |
||
103 | * @param array $params |
||
104 | * @param \Doctrine\DBAL\Driver $driver |
||
105 | * @param \Doctrine\DBAL\Configuration|null $config |
||
106 | * @param \Doctrine\Common\EventManager|null $eventManager |
||
107 | * |
||
108 | * @throws \InvalidArgumentException |
||
109 | */ |
||
110 | public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) |
||
111 | { |
||
112 | if ( !isset($params['slaves']) || !isset($params['master'])) { |
||
113 | throw new \InvalidArgumentException('master or slaves configuration missing'); |
||
114 | } |
||
115 | if (count($params['slaves']) == 0) { |
||
116 | throw new \InvalidArgumentException('You have to configure at least one slaves.'); |
||
117 | } |
||
118 | |||
119 | $params['master']['driver'] = $params['driver']; |
||
120 | foreach ($params['slaves'] as $slaveKey => $slave) { |
||
121 | $params['slaves'][$slaveKey]['driver'] = $params['driver']; |
||
122 | } |
||
123 | |||
124 | $this->keepSlave = (bool) ($params['keepSlave'] ?? false); |
||
125 | |||
126 | parent::__construct($params, $driver, $config, $eventManager); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Checks if the connection is currently towards the master or not. |
||
131 | * |
||
132 | * @return boolean |
||
133 | */ |
||
134 | public function isConnectedToMaster() |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * {@inheritDoc} |
||
141 | */ |
||
142 | public function connect($connectionName = null) |
||
143 | { |
||
144 | $requestedConnectionChange = ($connectionName !== null); |
||
145 | $connectionName = $connectionName ?: 'slave'; |
||
146 | |||
147 | if ($connectionName !== 'slave' && $connectionName !== 'master') { |
||
148 | throw new \InvalidArgumentException("Invalid option to connect(), only master or slave allowed."); |
||
149 | } |
||
150 | |||
151 | // If we have a connection open, and this is not an explicit connection |
||
152 | // change request, then abort right here, because we are already done. |
||
153 | // This prevents writes to the slave in case of "keepSlave" option enabled. |
||
154 | if (isset($this->_conn) && $this->_conn && !$requestedConnectionChange) { |
||
155 | return false; |
||
156 | } |
||
157 | |||
158 | $forceMasterAsSlave = false; |
||
159 | |||
160 | if ($this->getTransactionNestingLevel() > 0) { |
||
161 | $connectionName = 'master'; |
||
162 | $forceMasterAsSlave = true; |
||
163 | } |
||
164 | |||
165 | if (isset($this->connections[$connectionName]) && $this->connections[$connectionName]) { |
||
166 | $this->_conn = $this->connections[$connectionName]; |
||
167 | |||
168 | if ($forceMasterAsSlave && ! $this->keepSlave) { |
||
169 | $this->connections['slave'] = $this->_conn; |
||
170 | } |
||
171 | |||
172 | return false; |
||
173 | } |
||
174 | |||
175 | if ($connectionName === 'master') { |
||
176 | $this->connections['master'] = $this->_conn = $this->connectTo($connectionName); |
||
|
|||
177 | |||
178 | // Set slave connection to master to avoid invalid reads |
||
179 | if ( ! $this->keepSlave) { |
||
180 | $this->connections['slave'] = $this->connections['master']; |
||
181 | } |
||
182 | } else { |
||
183 | $this->connections['slave'] = $this->_conn = $this->connectTo($connectionName); |
||
184 | } |
||
185 | |||
186 | View Code Duplication | if ($this->_eventManager->hasListeners(Events::postConnect)) { |
|
187 | $eventArgs = new ConnectionEventArgs($this); |
||
188 | $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); |
||
189 | } |
||
190 | |||
191 | return true; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Connects to a specific connection. |
||
196 | * |
||
197 | * @param string $connectionName |
||
198 | * |
||
199 | * @return \Doctrine\DBAL\Driver |
||
200 | */ |
||
201 | View Code Duplication | protected function connectTo($connectionName) |
|
202 | { |
||
203 | $params = $this->getParams(); |
||
204 | |||
205 | $driverOptions = $params['driverOptions'] ?? []; |
||
206 | |||
207 | $connectionParams = $this->chooseConnectionConfiguration($connectionName, $params); |
||
208 | |||
209 | $user = $connectionParams['user'] ?? null; |
||
210 | $password = $connectionParams['password'] ?? null; |
||
211 | |||
212 | return $this->_driver->connect($connectionParams, $user, $password, $driverOptions); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @param string $connectionName |
||
217 | * @param array $params |
||
218 | * |
||
219 | * @return mixed |
||
220 | */ |
||
221 | protected function chooseConnectionConfiguration($connectionName, $params) |
||
222 | { |
||
223 | if ($connectionName === 'master') { |
||
224 | return $params['master']; |
||
225 | } |
||
226 | |||
227 | $config = $params['slaves'][array_rand($params['slaves'])]; |
||
228 | |||
229 | if ( ! isset($config['charset']) && isset($params['master']['charset'])) { |
||
230 | $config['charset'] = $params['master']['charset']; |
||
231 | } |
||
232 | |||
233 | return $config; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * {@inheritDoc} |
||
238 | */ |
||
239 | public function executeUpdate($query, array $params = [], array $types = []) |
||
240 | { |
||
241 | $this->connect('master'); |
||
242 | |||
243 | return parent::executeUpdate($query, $params, $types); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * {@inheritDoc} |
||
248 | */ |
||
249 | public function beginTransaction() |
||
250 | { |
||
251 | $this->connect('master'); |
||
252 | |||
253 | parent::beginTransaction(); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * {@inheritDoc} |
||
258 | */ |
||
259 | public function commit() |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * {@inheritDoc} |
||
268 | */ |
||
269 | public function rollBack() |
||
270 | { |
||
271 | $this->connect('master'); |
||
272 | |||
273 | return parent::rollBack(); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * {@inheritDoc} |
||
278 | */ |
||
279 | public function delete($tableName, array $identifier, array $types = []) |
||
280 | { |
||
281 | $this->connect('master'); |
||
282 | |||
283 | return parent::delete($tableName, $identifier, $types); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * {@inheritDoc} |
||
288 | */ |
||
289 | public function close() |
||
290 | { |
||
291 | unset($this->connections['master']); |
||
292 | unset($this->connections['slave']); |
||
293 | |||
294 | parent::close(); |
||
295 | |||
296 | $this->_conn = null; |
||
297 | $this->connections = ['master' => null, 'slave' => null]; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * {@inheritDoc} |
||
302 | */ |
||
303 | public function update($tableName, array $data, array $identifier, array $types = []) |
||
304 | { |
||
305 | $this->connect('master'); |
||
306 | |||
307 | return parent::update($tableName, $data, $identifier, $types); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * {@inheritDoc} |
||
312 | */ |
||
313 | public function insert($tableName, array $data, array $types = []) |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * {@inheritDoc} |
||
322 | */ |
||
323 | public function exec($statement) |
||
324 | { |
||
325 | $this->connect('master'); |
||
326 | |||
327 | return parent::exec($statement); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * {@inheritDoc} |
||
332 | */ |
||
333 | public function createSavepoint($savepoint) |
||
334 | { |
||
335 | $this->connect('master'); |
||
336 | |||
337 | parent::createSavepoint($savepoint); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * {@inheritDoc} |
||
342 | */ |
||
343 | public function releaseSavepoint($savepoint) |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * {@inheritDoc} |
||
352 | */ |
||
353 | public function rollbackSavepoint($savepoint) |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * {@inheritDoc} |
||
362 | */ |
||
363 | public function query() |
||
364 | { |
||
365 | $this->connect('master'); |
||
366 | |||
367 | $args = func_get_args(); |
||
368 | |||
369 | $logger = $this->getConfiguration()->getSQLLogger(); |
||
370 | if ($logger) { |
||
371 | $logger->startQuery($args[0]); |
||
372 | } |
||
373 | |||
374 | $statement = $this->_conn->query(...$args); |
||
375 | |||
376 | if ($logger) { |
||
377 | $logger->stopQuery(); |
||
378 | } |
||
379 | |||
380 | return $statement; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * {@inheritDoc} |
||
385 | */ |
||
386 | public function prepare($statement) |
||
391 | } |
||
392 | } |
||
393 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..