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 | 18 | 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 | 11 | 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 | 11 | public function setIdKey(string $idKey) : ConnectorInterface |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get exactly one "row" of data according to the expression. |
||
| 123 | * |
||
| 124 | * @param int $identifier |
||
| 125 | * @return array |
||
| 126 | * |
||
| 127 | * @codeCoverageIgnore Don't test external library. |
||
| 128 | */ |
||
| 129 | public function getData(int $identifier) : array |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get a set of data according to the expression and the chunk. |
||
| 149 | * |
||
| 150 | * @param array $expression |
||
| 151 | * @param array $options |
||
| 152 | * @return array |
||
| 153 | * |
||
| 154 | * @codeCoverageIgnore Don't test external library. |
||
| 155 | */ |
||
| 156 | public function getDataSet(array $expression, array $options = []) : array |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the number of matched data in the set according to the expression. |
||
| 173 | * |
||
| 174 | * @param array $expression |
||
| 175 | * @return int |
||
| 176 | * |
||
| 177 | * @codeCoverageIgnore Don't test external library. |
||
| 178 | */ |
||
| 179 | public function getDataCardinality(array $expression) : int |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Builds SQL query from the expression. |
||
| 193 | * |
||
| 194 | * @param array $expression |
||
| 195 | * @param array $queryBinds |
||
| 196 | * @param array $options |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 9 | protected function getSelectQueryForExpression( |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Gets the GROUP BY expression. |
||
| 236 | * |
||
| 237 | * @param array $options |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 9 | protected function getQueryGroup(array $options) : string |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Gets the HAVING expression only when the GROUP BY option exists. |
||
| 247 | * |
||
| 248 | * @param array $options |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | 9 | protected function getQueryHaving(array $options) : string |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Gets the ORDER BY expression. The default value is the primary key. |
||
| 258 | * |
||
| 259 | * @param array $options |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | 9 | protected function getQueryOrder(array $options) : string |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Gets the LIMIT expression. |
||
| 269 | * |
||
| 270 | * @param array $options |
||
| 271 | * @return int |
||
| 272 | */ |
||
| 273 | 9 | protected function getQueryLimit(array $options) : int |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Gets the OFFSET expression. |
||
| 280 | * |
||
| 281 | * @param array $options |
||
| 282 | * @return int |
||
| 283 | */ |
||
| 284 | 5 | protected function getQueryOffset(array $options) : int |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Creates a WHERE expression for the SQL query. |
||
| 291 | * |
||
| 292 | * @param array $expression |
||
| 293 | * @param array $queryBinds |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | 12 | protected function getWhereExpression(array $expression, array&$queryBinds) : string |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Set the query params and quaery bindings according to the `column` and `value`. |
||
| 314 | * |
||
| 315 | * @param string $column |
||
| 316 | * @param mixed $value |
||
| 317 | * @param array $queryParams |
||
| 318 | * @param array $queryBinds |
||
| 319 | */ |
||
| 320 | 11 | protected function setParamsAndBinds(string $column, $value, array&$queryParams, array&$queryBinds) : void |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Gets a simple condition for the column. |
||
| 336 | * |
||
| 337 | * @param string $column |
||
| 338 | * @return string 'my_column = ?' |
||
| 339 | */ |
||
| 340 | 11 | protected function getSimpleColumnCondition(string $column) : string |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Gets a 'LIKE' condition for the column. |
||
| 347 | * |
||
| 348 | * Allows special cases: |
||
| 349 | * @example ['my_column LIKE ?' => 'some value%'] |
||
| 350 | * @example ['my_column LIKE' => 'some value%'] |
||
| 351 | * @example ['my_column' => 'some value%'] |
||
| 352 | * |
||
| 353 | * @param string $column |
||
| 354 | * @return string 'my_column LIKE ?' |
||
| 355 | */ |
||
| 356 | 4 | protected function getLikeColumnCondition(string $column) : string |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Gets an 'IN' condition for the column. |
||
| 365 | * |
||
| 366 | * Allows special cases: |
||
| 367 | * @example ['my_column IN (?)' => [1,2,3]] |
||
| 368 | * @example ['my_column IN ?' => [1,2,3]] |
||
| 369 | * @example ['my_column IN' => [1,2,3]] |
||
| 370 | * @example ['my_column' => [1,2,3]] |
||
| 371 | * |
||
| 372 | * @param string $column |
||
| 373 | * @param int $parameterCount |
||
| 374 | * @return string 'my_column IN (?,?,?)' |
||
| 375 | */ |
||
| 376 | 5 | protected function getInColumnCondition(string $column, int $parameterCount = 1) : string |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Insert or update entity in the storage. |
||
| 387 | * |
||
| 388 | * @param int $identifier |
||
| 389 | * @param array $data |
||
| 390 | * @throws RuntimeException |
||
| 391 | * @return int The ID of the saved entity in the storage |
||
| 392 | * |
||
| 393 | * @codeCoverageIgnore Don't test external library. |
||
| 394 | */ |
||
| 395 | public function saveData(? int $identifier = null, array $data = []) : int |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Binds values to the statement. |
||
| 430 | * |
||
| 431 | * @param PDOStatement $statement |
||
| 432 | * @param array $queryBind |
||
| 433 | * @return void |
||
| 434 | * |
||
| 435 | * @codeCoverageIgnore Don't test external library. |
||
| 436 | */ |
||
| 437 | protected function bindValuesToStatement(PDOStatement&$statement, array $queryBind) : void |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Removes an entity from the storage. |
||
| 454 | * |
||
| 455 | * @param int $identifier |
||
| 456 | * @return bool |
||
| 457 | * |
||
| 458 | * @codeCoverageIgnore Don't test external library. |
||
| 459 | */ |
||
| 460 | public function deleteData(int $identifier) : bool |
||
| 466 | } |
||
| 467 |