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 MysqliStatement 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 MysqliStatement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class MysqliStatement implements IteratorAggregate, Statement |
||
28 | { |
||
29 | /** @var string[] */ |
||
30 | protected static $_paramTypeMap = [ |
||
31 | ParameterType::STRING => 's', |
||
32 | ParameterType::BINARY => 's', |
||
33 | ParameterType::BOOLEAN => 'i', |
||
34 | ParameterType::NULL => 's', |
||
35 | ParameterType::INTEGER => 'i', |
||
36 | ParameterType::LARGE_OBJECT => 'b', |
||
37 | ]; |
||
38 | |||
39 | /** @var mysqli */ |
||
40 | protected $_conn; |
||
41 | |||
42 | /** @var mysqli_stmt */ |
||
43 | protected $_stmt; |
||
44 | |||
45 | /** @var string[]|false|null */ |
||
46 | protected $_columnNames; |
||
47 | |||
48 | /** @var mixed[] */ |
||
49 | protected $_rowBindedValues = []; |
||
50 | |||
51 | /** @var mixed[] */ |
||
52 | protected $_bindedValues; |
||
53 | |||
54 | /** @var string */ |
||
55 | protected $types; |
||
56 | |||
57 | /** |
||
58 | * Contains ref values for bindValue(). |
||
59 | * |
||
60 | * @var mixed[] |
||
61 | */ |
||
62 | protected $_values = []; |
||
63 | |||
64 | /** @var int */ |
||
65 | protected $_defaultFetchMode = FetchMode::MIXED; |
||
66 | |||
67 | /** |
||
68 | * Indicates whether the statement is in the state when fetching results is possible |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | private $result = false; |
||
73 | |||
74 | /** |
||
75 | * @param string $prepareString |
||
76 | * |
||
77 | * @throws MysqliException |
||
78 | */ |
||
79 | 1543 | public function __construct(mysqli $conn, $prepareString) |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 1513 | public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 1543 | public function bindValue($param, $value, $type = ParameterType::STRING) |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 1543 | public function execute($params = null) |
|
206 | |||
207 | /** |
||
208 | * Binds parameters with known types previously bound to the statement |
||
209 | */ |
||
210 | 1543 | private function bindTypedParameters() : void |
|
245 | |||
246 | /** |
||
247 | * Handle $this->_longData after regular query parameters have been bound |
||
248 | * |
||
249 | * @param array<int, resource> $streams |
||
250 | * |
||
251 | * @throws MysqliException |
||
252 | */ |
||
253 | 1543 | private function sendLongData(array $streams) : void |
|
269 | |||
270 | /** |
||
271 | * Binds a array of values to bound parameters. |
||
272 | * |
||
273 | * @param mixed[] $values |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | 1447 | private function bindUntypedValues(array $values) |
|
288 | |||
289 | /** |
||
290 | * @return mixed[]|false|null |
||
291 | */ |
||
292 | 1537 | private function _fetch() |
|
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | 1537 | public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
|
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | */ |
||
361 | 1537 | public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
|
379 | |||
380 | /** |
||
381 | * {@inheritdoc} |
||
382 | */ |
||
383 | 1537 | View Code Duplication | public function fetchColumn($columnIndex = 0) |
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | public function errorCode() |
||
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | public function errorInfo() |
||
409 | |||
410 | /** |
||
411 | * {@inheritdoc} |
||
412 | */ |
||
413 | 912 | public function closeCursor() |
|
420 | |||
421 | /** |
||
422 | * {@inheritdoc} |
||
423 | */ |
||
424 | 1543 | public function rowCount() |
|
432 | |||
433 | /** |
||
434 | * {@inheritdoc} |
||
435 | */ |
||
436 | 912 | public function columnCount() |
|
440 | |||
441 | /** |
||
442 | * {@inheritdoc} |
||
443 | */ |
||
444 | 1537 | public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
|
450 | |||
451 | /** |
||
452 | * {@inheritdoc} |
||
453 | */ |
||
454 | 1590 | public function getIterator() |
|
458 | } |
||
459 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.