Complex classes like MysqliAdapter 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 MysqliAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class MysqliAdapter implements AdapterInterface, TransactionAdapterInterface, ReconnectableAdapterInterface |
||
| 24 | { |
||
| 25 | |||
| 26 | use ConfigurableTrait; |
||
| 27 | |||
| 28 | const OPT_RESOLVE_NAMED_PARAMS = 'resolve_named_params'; |
||
| 29 | const OPT_ENABLE_PARALLEL_QUERIES = 'enable_parallel_queries'; |
||
| 30 | const OPT_EMULATE_PREPARED_STATEMENTS = 'emulate_prepared_statements'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var mysqli |
||
| 34 | */ |
||
| 35 | private $cnx; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var CredentialsInterface |
||
| 39 | */ |
||
| 40 | private $credentials; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | private $reconnectAttempts = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * MysqliAdapter constructor. |
||
| 49 | * @param mysqli $cnx |
||
| 50 | * @param CredentialsInterface|null $credentials |
||
| 51 | * @param array|null $options |
||
| 52 | */ |
||
| 53 | protected function __construct(mysqli $cnx, CredentialsInterface $credentials = null, array $options = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @inheritDoc |
||
| 68 | */ |
||
| 69 | public function getWrappedConnection(): mysqli |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @inheritDoc |
||
| 76 | */ |
||
| 77 | public function getCredentials(): ?CredentialsInterface |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @inheritDoc |
||
| 84 | */ |
||
| 85 | public function isConnected(): bool |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @inheritDoc |
||
| 92 | */ |
||
| 93 | public function shouldReconnect(): bool |
||
| 97 | |||
| 98 | private function reconnect() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $queryString |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | protected function hasNamedParameters(string $queryString): bool |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @inheritDoc |
||
| 136 | */ |
||
| 137 | public function prepare(string $queryString, array $values = null): StatementInterface |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $queryString |
||
| 166 | * @param array|null $values |
||
| 167 | * @return EmulatedStatement |
||
| 168 | */ |
||
| 169 | private function emulatePrepare(string $queryString, array $values = null): EmulatedStatement |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param Statement|string $stmt |
||
| 176 | * @param array|null $values |
||
| 177 | * @return Result |
||
| 178 | */ |
||
| 179 | public function execute($stmt, array $values = null): ResultInterface |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @inheritdoc |
||
| 206 | */ |
||
| 207 | public function executeAsync($stmt, array $values = null): PromiseInterface |
||
| 223 | |||
| 224 | /** |
||
| 225 | * EXPERIMENTAL ! Executes a statement asynchronously. |
||
| 226 | * The promise will return a Result object. |
||
| 227 | * |
||
| 228 | * @param $stmt |
||
| 229 | * @param array|null $values |
||
| 230 | * @return PromiseInterface |
||
| 231 | */ |
||
| 232 | private function executeParallel($stmt, array $values = null): PromiseInterface |
||
| 267 | |||
| 268 | private function runStmt(Statement $stmt) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @inheritDoc |
||
| 287 | */ |
||
| 288 | public function beginTransaction(): void |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @inheritDoc |
||
| 295 | */ |
||
| 296 | public function commit(): void |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @inheritDoc |
||
| 304 | */ |
||
| 305 | public function rollback(): void |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Convert a query with named parameters (not natively supported by mysqli) |
||
| 313 | * |
||
| 314 | * @param string $queryString |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | private function convertToRunnableQuery(string $queryString): string |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @inheritDoc |
||
| 324 | */ |
||
| 325 | public function getDefaultOptions(): array |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param CredentialsInterface $credentials |
||
| 338 | * @param bool $resolveNamedParameters |
||
| 339 | * @return MysqliAdapter |
||
| 340 | */ |
||
| 341 | public static function factory(CredentialsInterface $credentials, array $options = null): self |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param mysqli $link |
||
| 348 | * @param CredentialsInterface|null $credentials |
||
| 349 | * @return MysqliAdapter |
||
| 350 | */ |
||
| 351 | public static function createFromLink(mysqli $link, CredentialsInterface $credentials = null): self |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param CredentialsInterface $credentials |
||
| 358 | * @return mysqli |
||
| 359 | */ |
||
| 360 | private static function createLink(CredentialsInterface $credentials): mysqli |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param callable $run |
||
| 372 | * @return mixed|void |
||
| 373 | */ |
||
| 374 | private static function wrapWithErrorHandler(callable $run) |
||
| 384 | } |
||
| 385 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.