Complex classes like ConnectorAdapter 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 ConnectorAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class ConnectorAdapter implements ConnectorInterface |
||
| 27 | { |
||
| 28 | /** @var string */ |
||
| 29 | protected $name; |
||
| 30 | /** @var PDO */ |
||
| 31 | protected $dataDriver; |
||
| 32 | /** @var string */ |
||
| 33 | protected $dataGroup = null; |
||
| 34 | /** @var string */ |
||
| 35 | protected $idKey = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * ConnectorAdapter constructor. |
||
| 39 | * |
||
| 40 | * @param string $name |
||
| 41 | * @param DriverInterface $dataDriver |
||
| 42 | * @throws InvalidArgumentException |
||
| 43 | */ |
||
| 44 | 21 | public function __construct(string $name, DriverInterface $dataDriver) |
|
| 45 | { |
||
| 46 | 21 | if (!$dataDriver instanceof DriverAdapter) { |
|
| 47 | 1 | $type = gettype($dataDriver); |
|
| 48 | |||
| 49 | 1 | if ($type == 'object') { |
|
| 50 | 1 | $type = get_class($dataDriver); |
|
| 51 | } |
||
| 52 | |||
| 53 | 1 | $message = sprintf( |
|
| 54 | 1 | 'Can\'t create %s instance. The parameter must be an instance of MySQLDriver, %s given.', |
|
| 55 | 1 | __CLASS__, |
|
| 56 | 1 | $type |
|
| 57 | ); |
||
| 58 | |||
| 59 | 1 | throw new InvalidArgumentException($message, 1001); |
|
| 60 | } |
||
| 61 | |||
| 62 | 21 | $this->name = $name; |
|
| 63 | 21 | $this->dataDriver = $dataDriver; |
|
| 64 | 21 | } |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Stuffs to reset upon cloning. |
||
| 68 | */ |
||
| 69 | 1 | public function __clone() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Returns the name of the connector. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | 3 | public function getConnectorName() : string |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the Data Storage instance. |
||
| 87 | * |
||
| 88 | * @return DriverInterface |
||
| 89 | */ |
||
| 90 | 1 | public function getDataDriver() : DriverInterface |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Set adapter data group. For Databases this can be the Tables. |
||
| 97 | * |
||
| 98 | * @param string $dataGroup |
||
| 99 | * @return ConnectorInterface |
||
| 100 | */ |
||
| 101 | 14 | public function setDataGroup(string $dataGroup) : ConnectorInterface |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Set adapter ID key. For Databases this can be the Primary key. Only simple key is allowed. |
||
| 110 | * |
||
| 111 | * @param string $idKey |
||
| 112 | * @return ConnectorInterface |
||
| 113 | */ |
||
| 114 | 14 | public function setIdKey(string $idKey) : ConnectorInterface |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Returns the CREATE TABLE statement. |
||
| 123 | * |
||
| 124 | * @param string $tableName |
||
| 125 | * @return string |
||
| 126 | * |
||
| 127 | * @codeCoverageIgnore Don't test external library. |
||
| 128 | */ |
||
| 129 | public function getTableDefinition(string $tableName) : string |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get exactly one "row" of data according to the expression. |
||
| 145 | * |
||
| 146 | * @param int $identifier |
||
| 147 | * @return array |
||
| 148 | * |
||
| 149 | * @codeCoverageIgnore Don't test external library. |
||
| 150 | */ |
||
| 151 | public function getData(int $identifier) : array |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get a set of data according to the expression and the chunk. |
||
| 171 | * |
||
| 172 | * @param array $expression |
||
| 173 | * @param array $options |
||
| 174 | * @return array |
||
| 175 | * |
||
| 176 | * @codeCoverageIgnore Don't test external library. |
||
| 177 | */ |
||
| 178 | public function getDataSet(array $expression, array $options = []) : array |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the number of matched data in the set according to the expression. |
||
| 195 | * |
||
| 196 | * @param array $expression |
||
| 197 | * @return int |
||
| 198 | * |
||
| 199 | * @codeCoverageIgnore Don't test external library. |
||
| 200 | */ |
||
| 201 | public function getDataCardinality(array $expression) : int |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Builds SQL query from the expression. |
||
| 215 | * |
||
| 216 | * @param array $expression |
||
| 217 | * @param array $queryBinds |
||
| 218 | * @param array $options |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | 12 | protected function getSelectQueryForExpression( |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Gets the GROUP BY expression. |
||
| 258 | * |
||
| 259 | * @param array $options |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | 12 | protected function getQueryGroup(array $options) : string |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Gets the HAVING expression only when the GROUP BY option exists. |
||
| 269 | * |
||
| 270 | * @param array $options |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | 12 | protected function getQueryHaving(array $options) : string |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Gets the ORDER BY expression. The default value is the primary key. |
||
| 280 | * |
||
| 281 | * @param array $options |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | 12 | protected function getQueryOrder(array $options) : string |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Gets the LIMIT expression. |
||
| 291 | * |
||
| 292 | * @param array $options |
||
| 293 | * @return int |
||
| 294 | */ |
||
| 295 | 12 | protected function getQueryLimit(array $options) : int |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Gets the OFFSET expression. |
||
| 302 | * |
||
| 303 | * @param array $options |
||
| 304 | * @return int |
||
| 305 | */ |
||
| 306 | 5 | protected function getQueryOffset(array $options) : int |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Creates a WHERE expression for the SQL query. |
||
| 313 | * |
||
| 314 | * @param array $expression |
||
| 315 | * @param array $queryBinds |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | 15 | protected function getWhereExpression(array $expression, array&$queryBinds) : string |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Set the query params and quaery bindings according to the `column` and `value`. |
||
| 336 | * |
||
| 337 | * @param string $column |
||
| 338 | * @param mixed $value |
||
| 339 | * @param array $queryParams |
||
| 340 | * @param array $queryBinds |
||
| 341 | */ |
||
| 342 | 14 | protected function setParamsAndBinds(string $column, $value, array&$queryParams, array&$queryBinds) : void |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Gets a simple condition for the column. |
||
| 362 | * |
||
| 363 | * @param string $column |
||
| 364 | * @return string 'my_column = ?' |
||
| 365 | */ |
||
| 366 | 11 | protected function getSimpleColumnCondition(string $column) : string |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Gets a 'LIKE' condition for the column. |
||
| 373 | * |
||
| 374 | * Allows special cases: |
||
| 375 | * @example ['my_column LIKE ?' => 'some value%'] |
||
| 376 | * @example ['my_column NOT' => 'some value%'] |
||
| 377 | * @example ['my_column' => 'some value%'] |
||
| 378 | * |
||
| 379 | * @param string $column |
||
| 380 | * @return string 'my_column LIKE ?' or 'my_column NOT LIKE ?' |
||
| 381 | */ |
||
| 382 | 4 | protected function getLikeColumnCondition(string $column) : string |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Gets an 'IN' condition for the column. |
||
| 393 | * |
||
| 394 | * Allows special cases: |
||
| 395 | * @example ['my_column IN (?)' => [1,2,3]] |
||
| 396 | * @example ['my_column IN ?' => [1,2,3]] |
||
| 397 | * @example ['my_column IN' => [1,2,3]] |
||
| 398 | * @example ['my_column' => [1,2,3]] |
||
| 399 | * |
||
| 400 | * @param string $column |
||
| 401 | * @param int $parameterCount |
||
| 402 | * @return string 'my_column IN (?,?,?)' |
||
| 403 | */ |
||
| 404 | 5 | protected function getInColumnCondition(string $column, int $parameterCount = 1) : string |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Insert or update entity in the storage. |
||
| 415 | * |
||
| 416 | * @param int $identifier |
||
| 417 | * @param array $data |
||
| 418 | * @throws RuntimeException |
||
| 419 | * @return int The ID of the saved entity in the storage |
||
| 420 | * |
||
| 421 | * @codeCoverageIgnore Don't test external library. |
||
| 422 | */ |
||
| 423 | public function saveData(? int $identifier = null, array $data = []) : int |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Binds values to the statement. |
||
| 458 | * |
||
| 459 | * @param PDOStatement $statement |
||
| 460 | * @param array $queryBind |
||
| 461 | * @return void |
||
| 462 | * |
||
| 463 | * @codeCoverageIgnore Don't test external library. |
||
| 464 | */ |
||
| 465 | protected function bindValuesToStatement(PDOStatement&$statement, array $queryBind) : void |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Removes an entity from the storage. |
||
| 482 | * |
||
| 483 | * @param int $identifier |
||
| 484 | * @return bool |
||
| 485 | * |
||
| 486 | * @codeCoverageIgnore Don't test external library. |
||
| 487 | */ |
||
| 488 | public function deleteData(int $identifier) : bool |
||
| 494 | } |
||
| 495 |