Complex classes like AbstractSqlRepository 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 AbstractSqlRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class AbstractSqlRepository implements RepositoryInterface |
||
| 17 | { |
||
| 18 | use CollectionBuilderTrait; |
||
| 19 | use DecoratorTrait; |
||
| 20 | use QueryStringParserTrait; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var \Aura\Sql\ExtendedPdoInterface |
||
| 24 | */ |
||
| 25 | protected $dbal; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * |
||
| 29 | * @var mixed |
||
| 30 | */ |
||
| 31 | protected $relationships = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Construct. |
||
| 35 | * |
||
| 36 | * @param \Aura\Sql\ExtendedPdoInterface $dbal |
||
| 37 | */ |
||
| 38 | 2 | public function __construct(ExtendedPdoInterface $dbal) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | 1 | public function countFromRequest(ServerRequestInterface $request) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | 1 | public function getFromRequest(ServerRequestInterface $request) |
|
| 58 | { |
||
| 59 | 1 | $rules = $this->parseQueryString($request->getUri()->getQuery()); |
|
| 60 | |||
| 61 | 1 | list($query, $params) = $this->buildQueryFromRules($rules); |
|
| 62 | |||
| 63 | 1 | if (array_key_exists('sort', $rules) && ! array_key_exists('search', $rules)) { |
|
| 64 | 1 | $query .= sprintf(' ORDER BY %s ', (in_array($rules['sort'], ['rand', 'random'])) ? 'RAND()' : $rules['sort']); |
|
| 65 | 1 | $query .= (array_key_exists('sort_direction', $rules)) ? $rules['sort_direction'] : 'ASC'; |
|
| 66 | 1 | } |
|
| 67 | |||
| 68 | 1 | if (array_key_exists('search', $rules)) { |
|
| 69 | $query .= sprintf(' ORDER BY MATCH (%s) AGAINST (:match_bind) > :score_bind', $rules['search']['fields']); |
||
| 70 | } |
||
| 71 | |||
| 72 | 1 | if (array_key_exists('limit', $rules)) { |
|
| 73 | 1 | $query .= ' LIMIT '; |
|
| 74 | 1 | $query .= (array_key_exists('offset', $rules)) ? sprintf('%d,', $rules['offset']) : ''; |
|
| 75 | 1 | $query .= $rules['limit']; |
|
| 76 | 1 | } |
|
| 77 | |||
| 78 | 1 | $query = trim(preg_replace('!\s+!', ' ', $query)); |
|
| 79 | |||
| 80 | 1 | $data = $this->dbal->fetchAll($query, $params); |
|
| 81 | |||
| 82 | 1 | $collection = $this->buildCollection($data)->setTotal($this->countFromRequest($request)); |
|
| 83 | |||
| 84 | 1 | $this->decorate($collection, StoreInterface::ON_READ); |
|
| 85 | |||
| 86 | 1 | return $collection; |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Build a base query without sorting and limits from filter rules. |
||
| 91 | * |
||
| 92 | * @param array $rules |
||
| 93 | * @param string $start |
||
|
|
|||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 1 | protected function buildQueryFromRules(array $rules, $count = false) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritdoc} |
||
| 130 | */ |
||
| 131 | 1 | public function countByField($field, $value, ServerRequestInterface $request = null) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | 1 | public function getByField($field, $value, ServerRequestInterface $request = null) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | public function attachRelationships( |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Iterate a result set and attach the relationship to it's correct entity |
||
| 221 | * within a collection. |
||
| 222 | * |
||
| 223 | * @param \Percy\Entity\Collection $collection |
||
| 224 | * @param string $relationship |
||
| 225 | * @param array $data |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function attachRelationshipsToCollection(Collection $collection, $relationship, array $data) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return relationship bind conditional. |
||
| 263 | * |
||
| 264 | * @param \Percy\Entity\Collection $collection |
||
| 265 | * @param string $relationship |
||
| 266 | * @param string $key |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | protected function getRelationshipBinds(Collection $collection, $relationship, $key) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get possible relationships and the properties attached to them. |
||
| 287 | * |
||
| 288 | * @param string $relationship |
||
| 289 | * |
||
| 290 | * @throws \InvalidArgumentException when requested relationship is not defined |
||
| 291 | * @throws \RuntimeException when map structure is defined incorrectly |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function getRelationshipMap($relationship = null) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns table that repository is reading from. |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | abstract protected function getTable(); |
||
| 341 | } |
||
| 342 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.