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 | 19 | public function __construct(string $name, DriverInterface $dataDriver) |
|
| 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 | 12 | 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 | 12 | public function setIdKey(string $idKey) : ConnectorInterface |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Returns the CREATE TABLE statement. |
||
| 123 | * |
||
| 124 | * @param string $tableName |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getTableDefinition(string $tableName) : string |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get exactly one "row" of data according to the expression. |
||
| 143 | * |
||
| 144 | * @param int $identifier |
||
| 145 | * @return array |
||
| 146 | * |
||
| 147 | * @codeCoverageIgnore Don't test external library. |
||
| 148 | */ |
||
| 149 | public function getData(int $identifier) : array |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get a set of data according to the expression and the chunk. |
||
| 169 | * |
||
| 170 | * @param array $expression |
||
| 171 | * @param array $options |
||
| 172 | * @return array |
||
| 173 | * |
||
| 174 | * @codeCoverageIgnore Don't test external library. |
||
| 175 | */ |
||
| 176 | public function getDataSet(array $expression, array $options = []) : array |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the number of matched data in the set according to the expression. |
||
| 193 | * |
||
| 194 | * @param array $expression |
||
| 195 | * @return int |
||
| 196 | * |
||
| 197 | * @codeCoverageIgnore Don't test external library. |
||
| 198 | */ |
||
| 199 | public function getDataCardinality(array $expression) : int |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Builds SQL query from the expression. |
||
| 213 | * |
||
| 214 | * @param array $expression |
||
| 215 | * @param array $queryBinds |
||
| 216 | * @param array $options |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | 10 | protected function getSelectQueryForExpression( |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Gets the GROUP BY expression. |
||
| 256 | * |
||
| 257 | * @param array $options |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 10 | protected function getQueryGroup(array $options) : string |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Gets the HAVING expression only when the GROUP BY option exists. |
||
| 267 | * |
||
| 268 | * @param array $options |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 10 | protected function getQueryHaving(array $options) : string |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Gets the ORDER BY expression. The default value is the primary key. |
||
| 278 | * |
||
| 279 | * @param array $options |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | 10 | protected function getQueryOrder(array $options) : string |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Gets the LIMIT expression. |
||
| 289 | * |
||
| 290 | * @param array $options |
||
| 291 | * @return int |
||
| 292 | */ |
||
| 293 | 10 | protected function getQueryLimit(array $options) : int |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Gets the OFFSET expression. |
||
| 300 | * |
||
| 301 | * @param array $options |
||
| 302 | * @return int |
||
| 303 | */ |
||
| 304 | 5 | protected function getQueryOffset(array $options) : int |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Creates a WHERE expression for the SQL query. |
||
| 311 | * |
||
| 312 | * @param array $expression |
||
| 313 | * @param array $queryBinds |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | 13 | protected function getWhereExpression(array $expression, array&$queryBinds) : string |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Set the query params and quaery bindings according to the `column` and `value`. |
||
| 334 | * |
||
| 335 | * @param string $column |
||
| 336 | * @param mixed $value |
||
| 337 | * @param array $queryParams |
||
| 338 | * @param array $queryBinds |
||
| 339 | */ |
||
| 340 | 12 | protected function setParamsAndBinds(string $column, $value, array&$queryParams, array&$queryBinds) : void |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Gets a simple condition for the column. |
||
| 358 | * |
||
| 359 | * @param string $column |
||
| 360 | * @return string 'my_column = ?' |
||
| 361 | */ |
||
| 362 | 11 | protected function getSimpleColumnCondition(string $column) : string |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Gets a 'LIKE' condition for the column. |
||
| 369 | * |
||
| 370 | * Allows special cases: |
||
| 371 | * @example ['my_column LIKE ?' => 'some value%'] |
||
| 372 | * @example ['my_column LIKE' => 'some value%'] |
||
| 373 | * @example ['my_column' => 'some value%'] |
||
| 374 | * |
||
| 375 | * @param string $column |
||
| 376 | * @return string 'my_column LIKE ?' |
||
| 377 | */ |
||
| 378 | 4 | protected function getLikeColumnCondition(string $column) : string |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Gets an 'IN' condition for the column. |
||
| 387 | * |
||
| 388 | * Allows special cases: |
||
| 389 | * @example ['my_column IN (?)' => [1,2,3]] |
||
| 390 | * @example ['my_column IN ?' => [1,2,3]] |
||
| 391 | * @example ['my_column IN' => [1,2,3]] |
||
| 392 | * @example ['my_column' => [1,2,3]] |
||
| 393 | * |
||
| 394 | * @param string $column |
||
| 395 | * @param int $parameterCount |
||
| 396 | * @return string 'my_column IN (?,?,?)' |
||
| 397 | */ |
||
| 398 | 5 | protected function getInColumnCondition(string $column, int $parameterCount = 1) : string |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Insert or update entity in the storage. |
||
| 409 | * |
||
| 410 | * @param int $identifier |
||
| 411 | * @param array $data |
||
| 412 | * @throws RuntimeException |
||
| 413 | * @return int The ID of the saved entity in the storage |
||
| 414 | * |
||
| 415 | * @codeCoverageIgnore Don't test external library. |
||
| 416 | */ |
||
| 417 | public function saveData(? int $identifier = null, array $data = []) : int |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Binds values to the statement. |
||
| 452 | * |
||
| 453 | * @param PDOStatement $statement |
||
| 454 | * @param array $queryBind |
||
| 455 | * @return void |
||
| 456 | * |
||
| 457 | * @codeCoverageIgnore Don't test external library. |
||
| 458 | */ |
||
| 459 | protected function bindValuesToStatement(PDOStatement&$statement, array $queryBind) : void |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Removes an entity from the storage. |
||
| 476 | * |
||
| 477 | * @param int $identifier |
||
| 478 | * @return bool |
||
| 479 | * |
||
| 480 | * @codeCoverageIgnore Don't test external library. |
||
| 481 | */ |
||
| 482 | public function deleteData(int $identifier) : bool |
||
| 488 | } |
||
| 489 |