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|null $credentials |
||
45 | * @param array|null $options |
||
46 | */ |
||
47 | protected function __construct(PDO $cnx, CredentialsInterface $credentials = null, array $options = null) |
||
58 | |||
59 | /** |
||
60 | * @inheritDoc |
||
61 | */ |
||
62 | public function getWrappedConnection(): PDO |
||
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() |
||
127 | |||
128 | /** |
||
129 | * @inheritDoc |
||
130 | */ |
||
131 | public function prepare(string $query, array $values = null): StatementInterface |
||
144 | |||
145 | /** |
||
146 | * @inheritDoc |
||
147 | */ |
||
148 | public function execute($stmt, array $values = null): ResultInterface |
||
171 | |||
172 | /** |
||
173 | * @inheritDoc |
||
174 | */ |
||
175 | public function executeAsync($stmt, array $values = null): PromiseInterface |
||
186 | |||
187 | /** |
||
188 | * @param \PDOStatement $wrappedStmt |
||
|
|||
189 | */ |
||
190 | private function runStmt(Statement $stmt) |
||
208 | |||
209 | /** |
||
210 | * @inheritDoc |
||
211 | */ |
||
212 | public function beginTransaction(): void |
||
216 | |||
217 | /** |
||
218 | * @inheritDoc |
||
219 | */ |
||
220 | public function commit(): void |
||
224 | |||
225 | /** |
||
226 | * @inheritDoc |
||
227 | */ |
||
228 | public function rollback(): void |
||
232 | |||
233 | /** |
||
234 | * @inheritDoc |
||
235 | */ |
||
236 | public function getDefaultOptions(): array |
||
243 | |||
244 | /** |
||
245 | * @param CredentialsInterface $credentials |
||
246 | * @return PDOAdapter |
||
247 | */ |
||
248 | public static function factory(CredentialsInterface $credentials, array $options = null): self |
||
252 | |||
253 | /** |
||
254 | * @param PDO $link |
||
255 | * @param CredentialsInterface|null $credentials |
||
256 | * @return PDOAdapter |
||
257 | */ |
||
258 | public static function createFromLink(PDO $link, CredentialsInterface $credentials = null): self |
||
262 | |||
263 | /** |
||
264 | * @param CredentialsInterface $credentials |
||
265 | * @return PDO |
||
266 | */ |
||
267 | private static function createLink(CredentialsInterface $credentials, array $options = null): PDO |
||
287 | |||
288 | |||
289 | /** |
||
290 | * @param callable $run |
||
291 | * @return mixed|void |
||
292 | */ |
||
293 | private static function wrapWithErrorHandler(callable $run) |
||
303 | } |
||
304 |
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.