Complex classes like PDOAdapter 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 PDOAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class PDOAdapter implements AdapterInterface, TransactionAdapterInterface, ReconnectableAdapterInterface |
||
23 | { |
||
24 | use ConfigurableTrait; |
||
25 | |||
26 | /** |
||
27 | * @var PDO |
||
28 | */ |
||
29 | private $cnx; |
||
30 | |||
31 | /** |
||
32 | * @var CredentialsInterface |
||
33 | */ |
||
34 | private $credentials; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | private $reconnectAttempts = 0; |
||
40 | |||
41 | /** |
||
42 | * PDOAdapter constructor. |
||
43 | * @param PDO $cnx |
||
44 | * @param CredentialsInterface $credentials |
||
45 | * @param array|null $options |
||
46 | */ |
||
47 | public function __construct(PDO $cnx, CredentialsInterface $credentials, array $options = null) |
||
58 | |||
59 | /** |
||
60 | * @inheritDoc |
||
61 | */ |
||
62 | public function getWrappedConnection() |
||
66 | |||
67 | /** |
||
68 | * @inheritDoc |
||
69 | */ |
||
70 | public function getCredentials(): CredentialsInterface |
||
74 | |||
75 | /** |
||
76 | * @inheritDoc |
||
77 | */ |
||
78 | public function isConnected(): bool |
||
89 | |||
90 | /** |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | public function shouldReconnect(): bool |
||
97 | |||
98 | /** |
||
99 | * Tries to reconnect to database. |
||
100 | */ |
||
101 | private function reconnect() |
||
102 | { |
||
103 | if (0 === (int) $this->getOption(self::OPT_MAX_RECONNECT_ATTEMPTS)) { |
||
104 | throw new MaxConnectAttempsException("Connection lost."); |
||
105 | } elseif ($this->reconnectAttempts === (int) $this->getOption(self::OPT_MAX_RECONNECT_ATTEMPTS)) { |
||
106 | throw new MaxConnectAttempsException("Max attempts to connect to database has been reached."); |
||
107 | } |
||
108 | try { |
||
109 | if (0 !== $this->reconnectAttempts) { |
||
110 | usleep((int) $this->getOption(self::OPT_USLEEP_AFTER_FIRST_ATTEMPT)); |
||
111 | } |
||
112 | $this->cnx = self::createLink($this->getCredentials(), $this->options); |
||
113 | if ($this->isConnected()) { |
||
114 | $this->reconnectAttempts = 0; |
||
115 | } else { |
||
116 | $this->reconnect(); |
||
117 | } |
||
118 | } catch (Throwable $e) { |
||
119 | $this->reconnectAttempts++; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @inheritDoc |
||
125 | */ |
||
126 | public function prepare(string $query, array $values = null): StatementInterface |
||
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | public function execute($stmt, array $values = null): ResultInterface |
||
144 | { |
||
145 | if (is_string($stmt)) { |
||
146 | $stmt = $this->prepare($stmt); |
||
147 | } |
||
148 | if (!$stmt instanceof Statement) { |
||
149 | throw new \InvalidArgumentException(sprintf('Expected %s object, got %s', Statement::class, get_class($stmt))); |
||
150 | } |
||
151 | if (null !== $values) { |
||
152 | $stmt = $stmt->withValues($values); |
||
153 | } |
||
154 | try { |
||
155 | $this->runStmt($stmt); |
||
156 | $result = $stmt->createResult(); |
||
157 | } catch (Throwable $e) { |
||
158 | if (!$this->isConnected()) { |
||
159 | $this->reconnect(); |
||
160 | return $this->execute($this->prepare((string) $stmt, $stmt->getValues())); |
||
161 | } |
||
162 | throw $e; |
||
163 | } |
||
164 | return $result; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | public function executeAsync($stmt, array $values = null): PromiseInterface |
||
171 | { |
||
172 | $promise = new Promise(function () use (&$promise, $stmt, $values) { |
||
173 | try { |
||
174 | $promise->resolve($this->execute($stmt, $values)); |
||
175 | } catch (DBALException $e) { |
||
176 | $promise->reject($e); |
||
177 | } |
||
178 | }); |
||
179 | return $promise; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param \PDOStatement $wrappedStmt |
||
|
|||
184 | */ |
||
185 | private function runStmt(Statement $stmt) |
||
186 | { |
||
187 | $wrappedStmt = $stmt->getWrappedStatement(); |
||
188 | try { |
||
189 | self::wrapWithErrorHandler(function () use ($stmt, $wrappedStmt) { |
||
190 | $stmt->bind(); |
||
191 | $wrappedStmt->execute(); |
||
192 | }); |
||
193 | } catch (\PDOException $e) { |
||
194 | if (false !== strpos($e->getMessage(), 'no parameters were bound')) { |
||
195 | throw new ParamBindingException($e->getMessage(), (int) $e->getCode(), $e, $stmt); |
||
196 | } |
||
197 | if (false !== strpos($e->getMessage(), 'number of bound variables does not match number')) { |
||
198 | throw new ParamBindingException($e->getMessage(), (int) $e->getCode(), $e, $stmt); |
||
199 | } |
||
200 | throw new DBALException($e->getMessage(), (int) $e->getCode(), $e); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @inheritDoc |
||
206 | */ |
||
207 | public function beginTransaction() |
||
211 | |||
212 | /** |
||
213 | * @inheritDoc |
||
214 | */ |
||
215 | public function commit() |
||
219 | |||
220 | /** |
||
221 | * @inheritDoc |
||
222 | */ |
||
223 | public function rollback() |
||
227 | |||
228 | /** |
||
229 | * @inheritDoc |
||
230 | */ |
||
231 | public function getDefaultOptions(): array |
||
238 | |||
239 | /** |
||
240 | * @param CredentialsInterface $credentials |
||
241 | * @return PDOAdapter |
||
242 | */ |
||
243 | public static function factory(CredentialsInterface $credentials, array $options = null): self |
||
247 | |||
248 | /** |
||
249 | * @param CredentialsInterface $credentials |
||
250 | * @return PDO |
||
251 | */ |
||
252 | private static function createLink(CredentialsInterface $credentials, array $options = null): PDO |
||
272 | |||
273 | |||
274 | /** |
||
275 | * @param callable $run |
||
276 | * @return mixed|void |
||
277 | */ |
||
278 | private static function wrapWithErrorHandler(callable $run) |
||
288 | } |
||
289 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.