Total Complexity | 50 |
Total Lines | 335 |
Duplicated Lines | 26.87 % |
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 DB2Statement 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 DB2Statement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class DB2Statement implements \IteratorAggregate, Statement |
||
26 | { |
||
27 | /** |
||
28 | * @var resource |
||
29 | */ |
||
30 | private $_stmt; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $_bindParam = []; |
||
36 | |||
37 | /** |
||
38 | * @var string Name of the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. |
||
39 | */ |
||
40 | private $defaultFetchClass = '\stdClass'; |
||
41 | |||
42 | /** |
||
43 | * @var string Constructor arguments for the default class to instantiate when fetch mode is \PDO::FETCH_CLASS. |
||
44 | */ |
||
45 | private $defaultFetchClassCtorArgs = []; |
||
46 | |||
47 | /** |
||
48 | * @var integer |
||
49 | */ |
||
50 | private $_defaultFetchMode = \PDO::FETCH_BOTH; |
||
51 | |||
52 | /** |
||
53 | * Indicates whether the statement is in the state when fetching results is possible |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | private $result = false; |
||
58 | |||
59 | /** |
||
60 | * DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | static private $_typeMap = [ |
||
65 | \PDO::PARAM_INT => DB2_LONG, |
||
|
|||
66 | \PDO::PARAM_STR => DB2_CHAR, |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * @param resource $stmt |
||
71 | */ |
||
72 | public function __construct($stmt) |
||
73 | { |
||
74 | $this->_stmt = $stmt; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function bindValue($param, $value, $type = null) |
||
81 | { |
||
82 | return $this->bindParam($param, $value, $type); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function bindParam($column, &$variable, $type = null, $length = null) |
||
89 | { |
||
90 | $this->_bindParam[$column] =& $variable; |
||
91 | |||
92 | if ($type && isset(self::$_typeMap[$type])) { |
||
93 | $type = self::$_typeMap[$type]; |
||
94 | } else { |
||
95 | $type = DB2_CHAR; |
||
96 | } |
||
97 | |||
98 | if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) { |
||
99 | throw new DB2Exception(db2_stmt_errormsg()); |
||
100 | } |
||
101 | |||
102 | return true; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function closeCursor() |
||
109 | { |
||
110 | if ( ! $this->_stmt) { |
||
111 | return false; |
||
112 | } |
||
113 | |||
114 | $this->_bindParam = []; |
||
115 | |||
116 | if (!db2_free_result($this->_stmt)) { |
||
117 | return false; |
||
118 | } |
||
119 | |||
120 | $this->result = false; |
||
121 | |||
122 | return true; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function columnCount() |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function errorCode() |
||
141 | { |
||
142 | return db2_stmt_error(); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function errorInfo() |
||
149 | { |
||
150 | return [ |
||
151 | db2_stmt_errormsg(), |
||
152 | db2_stmt_error(), |
||
153 | ]; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function execute($params = null) |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | View Code Duplication | public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function getIterator() |
||
202 | { |
||
203 | return new StatementIterator($this); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | View Code Duplication | public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
|
210 | { |
||
211 | // do not try fetching from the statement if it's not expected to contain result |
||
212 | // in order to prevent exceptional situation |
||
213 | if (!$this->result) { |
||
214 | return false; |
||
215 | } |
||
216 | |||
217 | $fetchMode = $fetchMode ?: $this->_defaultFetchMode; |
||
218 | switch ($fetchMode) { |
||
219 | case \PDO::FETCH_BOTH: |
||
220 | return db2_fetch_both($this->_stmt); |
||
221 | case \PDO::FETCH_ASSOC: |
||
222 | return db2_fetch_assoc($this->_stmt); |
||
223 | case \PDO::FETCH_CLASS: |
||
224 | $className = $this->defaultFetchClass; |
||
225 | $ctorArgs = $this->defaultFetchClassCtorArgs; |
||
226 | |||
227 | if (func_num_args() >= 2) { |
||
228 | $args = func_get_args(); |
||
229 | $className = $args[1]; |
||
230 | $ctorArgs = isset($args[2]) ? $args[2] : []; |
||
231 | } |
||
232 | |||
233 | $result = db2_fetch_object($this->_stmt); |
||
234 | |||
235 | if ($result instanceof \stdClass) { |
||
236 | $result = $this->castObject($result, $className, $ctorArgs); |
||
237 | } |
||
238 | |||
239 | return $result; |
||
240 | case \PDO::FETCH_NUM: |
||
241 | return db2_fetch_array($this->_stmt); |
||
242 | case \PDO::FETCH_OBJ: |
||
243 | return db2_fetch_object($this->_stmt); |
||
244 | default: |
||
245 | throw new DB2Exception('Given Fetch-Style ' . $fetchMode . ' is not supported.'); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | View Code Duplication | public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
|
253 | { |
||
254 | $rows = []; |
||
255 | |||
256 | switch ($fetchMode) { |
||
257 | case \PDO::FETCH_CLASS: |
||
258 | while ($row = call_user_func_array([$this, 'fetch'], func_get_args())) { |
||
259 | $rows[] = $row; |
||
260 | } |
||
261 | break; |
||
262 | case \PDO::FETCH_COLUMN: |
||
263 | while ($row = $this->fetchColumn()) { |
||
264 | $rows[] = $row; |
||
265 | } |
||
266 | break; |
||
267 | default: |
||
268 | while ($row = $this->fetch($fetchMode)) { |
||
269 | $rows[] = $row; |
||
270 | } |
||
271 | } |
||
272 | |||
273 | return $rows; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | View Code Duplication | public function fetchColumn($columnIndex = 0) |
|
288 | } |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | public function rowCount() |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Casts a stdClass object to the given class name mapping its' properties. |
||
300 | * |
||
301 | * @param \stdClass $sourceObject Object to cast from. |
||
302 | * @param string|object $destinationClass Name of the class or class instance to cast to. |
||
303 | * @param array $ctorArgs Arguments to use for constructing the destination class instance. |
||
304 | * |
||
305 | * @return object |
||
306 | * |
||
307 | * @throws DB2Exception |
||
308 | */ |
||
309 | private function castObject(\stdClass $sourceObject, $destinationClass, array $ctorArgs = []) |
||
360 | } |
||
361 | } |
||
362 |