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 SQLSrvStatement 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 SQLSrvStatement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class SQLSrvStatement implements IteratorAggregate, Statement |
||
33 | { |
||
34 | /** |
||
35 | * The SQLSRV Resource. |
||
36 | * |
||
37 | * @var resource |
||
38 | */ |
||
39 | private $conn; |
||
40 | |||
41 | /** |
||
42 | * The SQL statement to execute. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $sql; |
||
47 | |||
48 | /** |
||
49 | * The SQLSRV statement resource. |
||
50 | * |
||
51 | * @var resource |
||
52 | */ |
||
53 | private $stmt; |
||
54 | |||
55 | /** |
||
56 | * References to the variables bound as statement parameters. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $variables = []; |
||
61 | |||
62 | /** |
||
63 | * Bound parameter types. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $types = []; |
||
68 | |||
69 | /** |
||
70 | * Translations. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | private static $fetchMap = [ |
||
75 | PDO::FETCH_BOTH => SQLSRV_FETCH_BOTH, |
||
76 | PDO::FETCH_ASSOC => SQLSRV_FETCH_ASSOC, |
||
77 | PDO::FETCH_NUM => SQLSRV_FETCH_NUMERIC, |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * The name of the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | private $defaultFetchClass = '\stdClass'; |
||
86 | |||
87 | /** |
||
88 | * The constructor arguments for the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | private $defaultFetchClassCtorArgs = []; |
||
93 | |||
94 | /** |
||
95 | * The fetch style. |
||
96 | * |
||
97 | * @param integer |
||
98 | */ |
||
99 | private $defaultFetchMode = PDO::FETCH_BOTH; |
||
100 | |||
101 | /** |
||
102 | * The last insert ID. |
||
103 | * |
||
104 | * @var \Doctrine\DBAL\Driver\SQLSrv\LastInsertId|null |
||
105 | */ |
||
106 | private $lastInsertId; |
||
107 | |||
108 | /** |
||
109 | * Indicates whether the statement is in the state when fetching results is possible |
||
110 | * |
||
111 | * @var bool |
||
112 | */ |
||
113 | private $result = false; |
||
114 | |||
115 | /** |
||
116 | * Append to any INSERT query to retrieve the last insert id. |
||
117 | * |
||
118 | * @var string |
||
119 | * |
||
120 | * @deprecated do not rely on this constant in the future, as it will be completely removed |
||
121 | * @internal |
||
122 | */ |
||
123 | const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;'; |
||
124 | |||
125 | /** |
||
126 | * @param resource $conn |
||
127 | * @param string $sql |
||
128 | * @param \Doctrine\DBAL\Driver\SQLSrv\LastInsertId|null $lastInsertId |
||
129 | */ |
||
130 | public function __construct($conn, $sql, LastInsertId $lastInsertId = null) |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function bindValue($param, $value, $type = null) |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function bindParam($column, &$variable, $type = null, $length = null) |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function closeCursor() |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | public function columnCount() |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | View Code Duplication | public function errorCode() |
|
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function errorInfo() |
||
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | public function execute($params = null) |
||
243 | |||
244 | /** |
||
245 | * Prepares SQL Server statement resource |
||
246 | * |
||
247 | * @return resource |
||
248 | * @throws SQLSrvException |
||
249 | */ |
||
250 | private function prepare() |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | View Code Duplication | public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
|
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function getIterator() |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | * |
||
301 | * @throws SQLSrvException |
||
302 | */ |
||
303 | public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
||
332 | |||
333 | /** |
||
334 | * {@inheritdoc} |
||
335 | */ |
||
336 | View Code Duplication | public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
|
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | View Code Duplication | public function fetchColumn($columnIndex = 0) |
|
373 | |||
374 | /** |
||
375 | * {@inheritdoc} |
||
376 | */ |
||
377 | public function rowCount() |
||
381 | |||
382 | private function trackLastInsertId() : void |
||
398 | } |
||
399 |
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.