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 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $name; |
||
| 32 | /** |
||
| 33 | * @var PDO |
||
| 34 | */ |
||
| 35 | protected $dataDriver; |
||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $dataGroup = null; |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $idKey = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * ConnectorAdapter constructor. |
||
| 47 | * |
||
| 48 | * @param string $name |
||
| 49 | * @param DriverInterface $dataDriver |
||
| 50 | * @throws InvalidArgumentException |
||
| 51 | */ |
||
| 52 | 21 | public function __construct(string $name, DriverInterface $dataDriver) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Runs initialization. |
||
| 61 | */ |
||
| 62 | 21 | protected function init() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Stuffs to reset upon cloning. |
||
| 83 | */ |
||
| 84 | 1 | public function __clone() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Returns the name of the connector. |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 3 | public function getConnectorName() : string |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Returns the Data Storage instance. |
||
| 102 | * |
||
| 103 | * @return DriverInterface |
||
| 104 | */ |
||
| 105 | 1 | public function getDataDriver() : DriverInterface |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Set adapter data group. For Databases this can be the Tables. |
||
| 112 | * |
||
| 113 | * @param string $dataGroup |
||
| 114 | * @return ConnectorInterface |
||
| 115 | */ |
||
| 116 | 14 | public function setDataGroup(string $dataGroup) : ConnectorInterface |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Set adapter ID key. For Databases this can be the Primary key. Only simple key is allowed. |
||
| 125 | * |
||
| 126 | * @param string $idKey |
||
| 127 | * @return ConnectorInterface |
||
| 128 | */ |
||
| 129 | 14 | public function setIdKey(string $idKey) : ConnectorInterface |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the CREATE TABLE statement. |
||
| 138 | * |
||
| 139 | * @param string $tableName |
||
| 140 | * @return string |
||
| 141 | * |
||
| 142 | * @codeCoverageIgnore Don't test external library. |
||
| 143 | */ |
||
| 144 | public function getTableDefinition(string $tableName) : string |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get exactly one "row" of data according to the expression. |
||
| 162 | * |
||
| 163 | * @param int $identifier |
||
| 164 | * @return array |
||
| 165 | * |
||
| 166 | * @codeCoverageIgnore Don't test external library. |
||
| 167 | */ |
||
| 168 | public function getData(int $identifier) : array |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get a set of data according to the expression and the chunk. |
||
| 188 | * |
||
| 189 | * @param array $expression |
||
| 190 | * @param array $options |
||
| 191 | * @return array |
||
| 192 | * |
||
| 193 | * @codeCoverageIgnore Don't test external library. |
||
| 194 | */ |
||
| 195 | public function getDataSet(array $expression, array $options = []) : array |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the number of matched data in the set according to the expression. |
||
| 212 | * |
||
| 213 | * @param array $expression |
||
| 214 | * @return int |
||
| 215 | * |
||
| 216 | * @codeCoverageIgnore Don't test external library. |
||
| 217 | */ |
||
| 218 | public function getDataCardinality(array $expression) : int |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Builds SQL query from the expression. |
||
| 232 | * |
||
| 233 | * @param array $expression |
||
| 234 | * @param array $queryBinds |
||
| 235 | * @param array $options |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | 12 | protected function getSelectQueryForExpression( |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Gets the GROUP BY expression. |
||
| 275 | * |
||
| 276 | * @param array $options |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | 12 | protected function getQueryGroup(array $options) : string |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Gets the HAVING expression only when the GROUP BY option exists. |
||
| 286 | * |
||
| 287 | * @param array $options |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | 12 | protected function getQueryHaving(array $options) : string |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Gets the ORDER BY expression. The default value is the primary key. |
||
| 297 | * |
||
| 298 | * @param array $options |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | 12 | protected function getQueryOrder(array $options) : string |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Gets the LIMIT expression. |
||
| 308 | * |
||
| 309 | * @param array $options |
||
| 310 | * @return int |
||
| 311 | */ |
||
| 312 | 12 | protected function getQueryLimit(array $options) : int |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Gets the OFFSET expression. |
||
| 319 | * |
||
| 320 | * @param array $options |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | 5 | protected function getQueryOffset(array $options) : int |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Creates a WHERE expression for the SQL query. |
||
| 330 | * |
||
| 331 | * @param array $expression |
||
| 332 | * @param array $queryBinds |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | 15 | protected function getWhereExpression(array $expression, array&$queryBinds) : string |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Set the query params and quaery bindings according to the `column` and `value`. |
||
| 353 | * |
||
| 354 | * @param string $column |
||
| 355 | * @param mixed $value |
||
| 356 | * @param array $queryParams |
||
| 357 | * @param array $queryBinds |
||
| 358 | */ |
||
| 359 | 14 | protected function setParamsAndBinds(string $column, $value, array&$queryParams, array&$queryBinds) : void |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Gets a simple condition for the column. |
||
| 379 | * |
||
| 380 | * @param string $column |
||
| 381 | * @return string 'my_column = ?' |
||
| 382 | */ |
||
| 383 | 11 | protected function getSimpleColumnCondition(string $column) : string |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Gets a 'LIKE' condition for the column. |
||
| 390 | * |
||
| 391 | * Allows special cases: |
||
| 392 | * |
||
| 393 | * @example ['my_column LIKE ?' => 'some value%'] |
||
| 394 | * @example ['my_column NOT' => 'some value%'] |
||
| 395 | * @example ['my_column' => 'some value%'] |
||
| 396 | * |
||
| 397 | * @param string $column |
||
| 398 | * @return string 'my_column LIKE ?' or 'my_column NOT LIKE ?' |
||
| 399 | */ |
||
| 400 | 4 | protected function getLikeColumnCondition(string $column) : string |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Gets an 'IN' condition for the column. |
||
| 411 | * |
||
| 412 | * Allows special cases: |
||
| 413 | * |
||
| 414 | * @example ['my_column IN (?)' => [1,2,3]] |
||
| 415 | * @example ['my_column IN ?' => [1,2,3]] |
||
| 416 | * @example ['my_column IN' => [1,2,3]] |
||
| 417 | * @example ['my_column' => [1,2,3]] |
||
| 418 | * |
||
| 419 | * @param string $column |
||
| 420 | * @param int $parameterCount |
||
| 421 | * @return string 'my_column IN (?,?,?)' |
||
| 422 | */ |
||
| 423 | 5 | protected function getInColumnCondition(string $column, int $parameterCount = 1) : string |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Insert or update entity in the storage. |
||
| 434 | * |
||
| 435 | * @param int $identifier |
||
| 436 | * @param array $data |
||
| 437 | * @throws RuntimeException |
||
| 438 | * @return int The ID of the saved entity in the storage |
||
| 439 | * |
||
| 440 | * @codeCoverageIgnore Don't test external library. |
||
| 441 | */ |
||
| 442 | public function saveData(? int $identifier = null, array $data = []) : int |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Binds values to the statement. |
||
| 477 | * |
||
| 478 | * @param PDOStatement $statement |
||
| 479 | * @param array $queryBind |
||
| 480 | * @return void |
||
| 481 | * |
||
| 482 | * @codeCoverageIgnore Don't test external library. |
||
| 483 | */ |
||
| 484 | protected function bindValuesToStatement(PDOStatement&$statement, array $queryBind) : void |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Removes an entity from the storage. |
||
| 501 | * |
||
| 502 | * @param int $identifier |
||
| 503 | * @return bool |
||
| 504 | * |
||
| 505 | * @codeCoverageIgnore Don't test external library. |
||
| 506 | */ |
||
| 507 | public function deleteData(int $identifier) : bool |
||
| 513 | } |
||
| 514 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..