Total Complexity | 40 |
Total Lines | 220 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Statement 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 Statement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Statement implements IteratorAggregate, DriverStatement |
||
20 | { |
||
21 | /** @var int */ |
||
22 | private $portability; |
||
23 | |||
24 | /** @var DriverStatement|ResultStatement */ |
||
25 | private $stmt; |
||
26 | |||
27 | /** @var int */ |
||
28 | private $case; |
||
29 | |||
30 | /** @var int */ |
||
31 | private $defaultFetchMode = FetchMode::MIXED; |
||
32 | |||
33 | /** |
||
34 | * Wraps <tt>Statement</tt> and applies portability measures. |
||
35 | * |
||
36 | * @param DriverStatement|ResultStatement $stmt |
||
37 | */ |
||
38 | public function __construct($stmt, Connection $conn) |
||
39 | { |
||
40 | $this->stmt = $stmt; |
||
41 | $this->portability = $conn->getPortability(); |
||
42 | $this->case = $conn->getFetchCase(); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) |
||
49 | { |
||
50 | assert($this->stmt instanceof DriverStatement); |
||
51 | |||
52 | return $this->stmt->bindParam($column, $variable, $type, $length); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function bindValue($param, $value, $type = ParameterType::STRING) |
||
59 | { |
||
60 | assert($this->stmt instanceof DriverStatement); |
||
61 | |||
62 | return $this->stmt->bindValue($param, $value, $type); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function closeCursor() |
||
69 | { |
||
70 | return $this->stmt->closeCursor(); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function columnCount() |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function errorCode() |
||
85 | { |
||
86 | assert($this->stmt instanceof DriverStatement); |
||
87 | |||
88 | return $this->stmt->errorCode(); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function errorInfo() |
||
95 | { |
||
96 | assert($this->stmt instanceof DriverStatement); |
||
97 | |||
98 | return $this->stmt->errorInfo(); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function execute($params = null) |
||
105 | { |
||
106 | assert($this->stmt instanceof DriverStatement); |
||
107 | |||
108 | return $this->stmt->execute($params); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function setFetchMode($fetchMode, ...$args) |
||
115 | { |
||
116 | $this->defaultFetchMode = $fetchMode; |
||
117 | |||
118 | return $this->stmt->setFetchMode($fetchMode, ...$args); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function getIterator() |
||
125 | { |
||
126 | return new StatementIterator($this); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function fetch($fetchMode = null, ...$args) |
||
133 | { |
||
134 | $fetchMode = $fetchMode ?? $this->defaultFetchMode; |
||
135 | |||
136 | $row = $this->stmt->fetch($fetchMode, ...$args); |
||
137 | |||
138 | $iterateRow = ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) !== 0; |
||
139 | $fixCase = $this->case !== null |
||
140 | && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED) |
||
141 | && ($this->portability & Connection::PORTABILITY_FIX_CASE) !== 0; |
||
142 | |||
143 | $row = $this->fixRow($row, $iterateRow, $fixCase); |
||
144 | |||
145 | return $row; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function fetchAll($fetchMode = null, ...$args) |
||
152 | { |
||
153 | $fetchMode = $fetchMode ?? $this->defaultFetchMode; |
||
154 | |||
155 | $rows = $this->stmt->fetchAll($fetchMode, ...$args); |
||
156 | |||
157 | $iterateRow = ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) !== 0; |
||
158 | $fixCase = $this->case !== null |
||
159 | && ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED) |
||
160 | && ($this->portability & Connection::PORTABILITY_FIX_CASE) !== 0; |
||
161 | |||
162 | if (! $iterateRow && ! $fixCase) { |
||
163 | return $rows; |
||
164 | } |
||
165 | |||
166 | if ($fetchMode === FetchMode::COLUMN) { |
||
167 | foreach ($rows as $num => $row) { |
||
168 | $rows[$num] = [$row]; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | foreach ($rows as $num => $row) { |
||
173 | $rows[$num] = $this->fixRow($row, $iterateRow, $fixCase); |
||
174 | } |
||
175 | |||
176 | if ($fetchMode === FetchMode::COLUMN) { |
||
177 | foreach ($rows as $num => $row) { |
||
178 | $rows[$num] = $row[0]; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | return $rows; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param mixed $row |
||
187 | * @param bool $iterateRow |
||
188 | * @param bool $fixCase |
||
189 | * |
||
190 | * @return mixed |
||
191 | */ |
||
192 | protected function fixRow($row, $iterateRow, $fixCase) |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function fetchColumn($columnIndex = 0) |
||
219 | { |
||
220 | $value = $this->stmt->fetchColumn($columnIndex); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function rowCount() : int |
||
239 | } |
||
240 | } |
||
241 |