Complex classes like InMemoryAdapter 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 InMemoryAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class InMemoryAdapter implements DataAdapterInterface |
||
| 22 | { |
||
| 23 | const EXPRESSION_IN_ARRAY = 'in'; |
||
| 24 | const EXPRESSION_WILDCARD = 'like'; |
||
| 25 | |||
| 26 | /** @var array */ |
||
| 27 | private $dataStorage; |
||
| 28 | /** @var string */ |
||
| 29 | private $dataGroup = 'default'; |
||
| 30 | /** @var string */ |
||
| 31 | private $idKey = 'id'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * PDOAdapter constructor. |
||
| 35 | * |
||
| 36 | * @param mixed $dataStorage |
||
| 37 | * |
||
| 38 | * @throws InvalidArgumentException |
||
| 39 | */ |
||
| 40 | 12 | public function __construct($dataStorage = null) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Returns the Data Storage instance. |
||
| 61 | * |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | 9 | public function getDataStorage() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Set adapter data group. |
||
| 71 | * |
||
| 72 | * @param string $dataGroup |
||
| 73 | * |
||
| 74 | * @throws InitException |
||
| 75 | * |
||
| 76 | * @return InMemoryAdapter |
||
| 77 | */ |
||
| 78 | 6 | public function setDataGroup($dataGroup) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Set adapter ID key. For Databases this can be the Primary key. Only simple key is allowed. |
||
| 96 | * |
||
| 97 | * @param string $idKey |
||
| 98 | * |
||
| 99 | * @throws InitException |
||
| 100 | * |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | 6 | public function setIdKey($idKey) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Get exactly one "row" of data according to the expression. |
||
| 117 | * |
||
| 118 | * @param mixed $identifier |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | 2 | public function getData($identifier) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Get a set of data according to the expression and the chunk. |
||
| 137 | * |
||
| 138 | * @param array $expression |
||
| 139 | * @param int $limit |
||
| 140 | * @param int $offset |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | 2 | public function getDataSet(array $expression, $limit = null, $offset = null) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Checks the data (row) array against the expressions. |
||
| 172 | * |
||
| 173 | * @param array $expression |
||
| 174 | * @param array $data |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 2 | private function isExpressionMatch(array $expression, array $data) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Gets expression type and also sets the expression subject. |
||
| 230 | * |
||
| 231 | * @param string $pattern |
||
| 232 | * @param string $subject |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | 1 | private function getExpressionType($pattern, &$subject) |
|
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the number of matched data in the set according to the expression. |
||
| 260 | * |
||
| 261 | * @param array $expression |
||
| 262 | * |
||
| 263 | * @return int |
||
| 264 | */ |
||
| 265 | 1 | public function getDataCardinality(array $expression) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Insert or update entity in the storage. |
||
| 274 | * |
||
| 275 | * @param mixed $identifier |
||
| 276 | * @param array $data |
||
| 277 | * |
||
| 278 | * @return mixed The ID of the saved entity in the storage |
||
| 279 | */ |
||
| 280 | 5 | public function saveData($identifier, array $data) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Removes an entity from the storage. |
||
| 307 | * |
||
| 308 | * @param mixed $identifier |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | 1 | public function deleteData($identifier) |
|
| 323 | } |
||
| 324 |