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 PDO */ |
||
| 29 | protected $dataDriver; |
||
| 30 | /** @var string */ |
||
| 31 | protected $dataGroup = null; |
||
| 32 | /** @var string */ |
||
| 33 | protected $idKey = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * ConnectorAdapter constructor. |
||
| 37 | * |
||
| 38 | * @param DriverInterface $dataDriver |
||
| 39 | * @throws InvalidArgumentException |
||
| 40 | */ |
||
| 41 | 16 | public function __construct(DriverInterface $dataDriver) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Returns the Data Storage instance. |
||
| 64 | * |
||
| 65 | * @return DriverInterface |
||
| 66 | */ |
||
| 67 | 1 | public function getDataDriver() : DriverInterface |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Set adapter data group. For Databases this can be the Tables. |
||
| 74 | * |
||
| 75 | * @param string $dataGroup |
||
| 76 | * @return ConnectorInterface |
||
| 77 | */ |
||
| 78 | 11 | public function setDataGroup(string $dataGroup) : ConnectorInterface |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Set adapter ID key. For Databases this can be the Primary key. Only simple key is allowed. |
||
| 87 | * |
||
| 88 | * @param string $idKey |
||
| 89 | * @return ConnectorInterface |
||
| 90 | */ |
||
| 91 | 11 | public function setIdKey(string $idKey) : ConnectorInterface |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get exactly one "row" of data according to the expression. |
||
| 100 | * |
||
| 101 | * @param int $identifier |
||
| 102 | * @return array |
||
| 103 | * |
||
| 104 | * @codeCoverageIgnore Don't test external library. |
||
| 105 | */ |
||
| 106 | public function getData(int $identifier) : array |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get a set of data according to the expression and the chunk. |
||
| 126 | * |
||
| 127 | * @param array $expression |
||
| 128 | * @param array $options |
||
| 129 | * @return array |
||
| 130 | * |
||
| 131 | * @codeCoverageIgnore Don't test external library. |
||
| 132 | */ |
||
| 133 | public function getDataSet(array $expression, array $options = []) : array |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the number of matched data in the set according to the expression. |
||
| 150 | * |
||
| 151 | * @param array $expression |
||
| 152 | * @return int |
||
| 153 | * |
||
| 154 | * @codeCoverageIgnore Don't test external library. |
||
| 155 | */ |
||
| 156 | public function getDataCardinality(array $expression) : int |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Builds SQL query from the expression. |
||
| 170 | * |
||
| 171 | * @param array $expression |
||
| 172 | * @param array $queryBinds |
||
| 173 | * @param array $options |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 9 | protected function getSelectQueryForExpression( |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Gets the GROUP BY expression. |
||
| 213 | * |
||
| 214 | * @param array $options |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 9 | protected function getQueryGroup(array $options) : string |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Gets the HAVING expression only when the GROUP BY option exists. |
||
| 224 | * |
||
| 225 | * @param array $options |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | 9 | protected function getQueryHaving(array $options) : string |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Gets the ORDER BY expression. The default value is the primary key. |
||
| 235 | * |
||
| 236 | * @param array $options |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | 9 | protected function getQueryOrder(array $options) : string |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Gets the LIMIT expression. |
||
| 246 | * |
||
| 247 | * @param array $options |
||
| 248 | * @return int |
||
| 249 | */ |
||
| 250 | 9 | protected function getQueryLimit(array $options) : int |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Gets the OFFSET expression. |
||
| 257 | * |
||
| 258 | * @param array $options |
||
| 259 | * @return int |
||
| 260 | */ |
||
| 261 | 5 | protected function getQueryOffset(array $options) : int |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Creates a WHERE expression for the SQL query. |
||
| 268 | * |
||
| 269 | * @param array $expression |
||
| 270 | * @param array $queryBinds |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | 12 | protected function getWhereExpression(array $expression, array&$queryBinds) : string |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Set the query params and quaery bindings according to the `column` and `value`. |
||
| 291 | * |
||
| 292 | * @param string $column |
||
| 293 | * @param mixed $value |
||
| 294 | * @param array $queryParams |
||
| 295 | * @param array $queryBinds |
||
| 296 | */ |
||
| 297 | 11 | protected function setParamsAndBinds(string $column, $value, array&$queryParams, array&$queryBinds) : void |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Gets a simple condition for the column. |
||
| 313 | * |
||
| 314 | * @param string $column |
||
| 315 | * @return string 'my_column = ?' |
||
| 316 | */ |
||
| 317 | 11 | protected function getSimpleColumnCondition(string $column) : string |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Gets a 'LIKE' condition for the column. |
||
| 324 | * |
||
| 325 | * Allows special cases: |
||
| 326 | * @example ['my_column LIKE ?' => 'some value%'] |
||
| 327 | * @example ['my_column LIKE' => 'some value%'] |
||
| 328 | * @example ['my_column' => 'some value%'] |
||
| 329 | * |
||
| 330 | * @param string $column |
||
| 331 | * @return string 'my_column LIKE ?' |
||
| 332 | */ |
||
| 333 | 4 | protected function getLikeColumnCondition(string $column) : string |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Gets an 'IN' condition for the column. |
||
| 342 | * |
||
| 343 | * Allows special cases: |
||
| 344 | * @example ['my_column IN (?)' => [1,2,3]] |
||
| 345 | * @example ['my_column IN ?' => [1,2,3]] |
||
| 346 | * @example ['my_column IN' => [1,2,3]] |
||
| 347 | * @example ['my_column' => [1,2,3]] |
||
| 348 | * |
||
| 349 | * @param string $column |
||
| 350 | * @param int $parameterCount |
||
| 351 | * @return string 'my_column IN (?,?,?)' |
||
| 352 | */ |
||
| 353 | 5 | protected function getInColumnCondition(string $column, int $parameterCount = 1) : string |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Insert or update entity in the storage. |
||
| 364 | * |
||
| 365 | * @param int $identifier |
||
| 366 | * @param array $data |
||
| 367 | * @throws RuntimeException |
||
| 368 | * @return int The ID of the saved entity in the storage |
||
| 369 | * |
||
| 370 | * @codeCoverageIgnore Don't test external library. |
||
| 371 | */ |
||
| 372 | public function saveData(? int $identifier = null, array $data) : int |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Binds values to the statement. |
||
| 407 | * |
||
| 408 | * @param PDOStatement $statement |
||
| 409 | * @param array $queryBind |
||
| 410 | * @return void |
||
| 411 | * |
||
| 412 | * @codeCoverageIgnore Don't test external library. |
||
| 413 | */ |
||
| 414 | protected function bindValuesToStatement(PDOStatement&$statement, array $queryBind) : void |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Removes an entity from the storage. |
||
| 431 | * |
||
| 432 | * @param int $identifier |
||
| 433 | * @return bool |
||
| 434 | * |
||
| 435 | * @codeCoverageIgnore Don't test external library. |
||
| 436 | */ |
||
| 437 | public function deleteData(int $identifier) : bool |
||
| 443 | } |
||
| 444 |