Complex classes like Repository 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 Repository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Repository |
||
| 26 | { |
||
| 27 | const RESULTS_ARRAY = 'array'; |
||
| 28 | const RESULTS_OBJECT = 'object'; |
||
| 29 | const RESULTS_RAW = 'raw'; |
||
| 30 | const RESULTS_RAW_ITERATOR = 'raw_iterator'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Manager |
||
| 34 | */ |
||
| 35 | private $manager; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string Fully qualified class name |
||
| 39 | */ |
||
| 40 | private $className; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string Elasticsearch type name |
||
| 44 | */ |
||
| 45 | private $type; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor. |
||
| 49 | * |
||
| 50 | * @param Manager $manager |
||
| 51 | * @param string $className |
||
| 52 | */ |
||
| 53 | public function __construct($manager, $className) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns elasticsearch manager used in the repository. |
||
| 71 | * |
||
| 72 | * @return Manager |
||
| 73 | */ |
||
| 74 | public function getManager() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public function getType() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns a single document data by ID or null if document is not found. |
||
| 89 | * |
||
| 90 | * @param string $id Document Id to find. |
||
| 91 | * @param string $resultType Result type returned. |
||
| 92 | * |
||
| 93 | * @return object|null |
||
| 94 | * |
||
| 95 | * @throws \LogicException |
||
| 96 | */ |
||
| 97 | public function find($id, $resultType = self::RESULTS_OBJECT) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Finds entities by a set of criteria. |
||
| 120 | * |
||
| 121 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
| 122 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
| 123 | * @param int|null $limit Example: 5. |
||
| 124 | * @param int|null $offset Example: 30. |
||
| 125 | * @param string $resultType Result type returned. |
||
| 126 | * |
||
| 127 | * @return array|DocumentIterator The objects. |
||
| 128 | */ |
||
| 129 | public function findBy( |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Finds only one entity by a set of criteria. |
||
| 160 | * |
||
| 161 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
| 162 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
| 163 | * @param string $resultType Result type returned. |
||
| 164 | * |
||
| 165 | * @throws \Exception |
||
| 166 | * |
||
| 167 | * @return object|null The object. |
||
| 168 | */ |
||
| 169 | public function findOneBy(array $criteria, array $orderBy = [], $resultType = self::RESULTS_OBJECT) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Returns search instance. |
||
| 188 | * |
||
| 189 | * @return Search |
||
| 190 | */ |
||
| 191 | public function createSearch() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Executes given search. |
||
| 198 | * |
||
| 199 | * @param Search $search |
||
| 200 | * @param string $resultsType |
||
| 201 | * |
||
| 202 | * @return DocumentIterator|RawIterator|array |
||
| 203 | * |
||
| 204 | * @throws \Exception |
||
| 205 | */ |
||
| 206 | public function execute(Search $search, $resultsType = self::RESULTS_OBJECT) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Counts documents by given search. |
||
| 217 | * |
||
| 218 | * @param Search $search |
||
| 219 | * @param array $params |
||
| 220 | * @param bool $returnRaw If set true returns raw response gotten from client. |
||
| 221 | * |
||
| 222 | * @return int|array |
||
| 223 | */ |
||
| 224 | public function count(Search $search, array $params = [], $returnRaw = false) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Delete by query. |
||
| 248 | * |
||
| 249 | * @param Search $search |
||
| 250 | * |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | public function deleteByQuery(Search $search) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Fetches next set of results. |
||
| 269 | * |
||
| 270 | * @param string $scrollId |
||
| 271 | * @param string $scrollDuration |
||
| 272 | * @param string $resultsType |
||
| 273 | * |
||
| 274 | * @return AbstractResultsIterator |
||
| 275 | * |
||
| 276 | * @throws \Exception |
||
| 277 | */ |
||
| 278 | public function scroll( |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Removes a single document data by ID. |
||
| 290 | * |
||
| 291 | * @param string $id Document ID to remove. |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | * |
||
| 295 | * @throws \LogicException |
||
| 296 | */ |
||
| 297 | public function remove($id) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Partial document update. |
||
| 312 | * |
||
| 313 | * @param string $id Document id to update. |
||
| 314 | * @param array $fields Fields array to update. |
||
| 315 | * @param string $script Groovy script to update fields. |
||
| 316 | * @param array $params Additional parameters to pass to the client. |
||
| 317 | * |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function update($id, array $fields = [], $script = null, array $params = []) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Resolves elasticsearch type by class name. |
||
| 344 | * |
||
| 345 | * @param string $className |
||
| 346 | * |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | private function resolveType($className) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Parses raw result. |
||
| 356 | * |
||
| 357 | * @param array $raw |
||
| 358 | * @param string $resultsType |
||
| 359 | * @param string $scrollDuration |
||
| 360 | * |
||
| 361 | * @return DocumentIterator|RawIterator|array |
||
| 362 | * |
||
| 363 | * @throws \Exception |
||
| 364 | */ |
||
| 365 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Normalizes response array. |
||
| 389 | * |
||
| 390 | * @param array $data |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | private function convertToNormalizedArray($data) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns fully qualified class name. |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getClassName() |
||
| 424 | } |
||
| 425 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: