Complex classes like ApiPersister 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 ApiPersister, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class ApiPersister implements EntityPersister |
||
| 15 | { |
||
| 16 | /** @var SearchArgumentsTransformer */ |
||
| 17 | private $transformer; |
||
| 18 | /** @var EntityMetadata */ |
||
| 19 | private $metadata; |
||
| 20 | /** @var ApiEntityManager */ |
||
| 21 | private $manager; |
||
| 22 | /** @var CrudsApiInterface */ |
||
| 23 | private $api; |
||
| 24 | /** @var array */ |
||
| 25 | private $pendingInserts = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * ApiPersister constructor. |
||
| 29 | * |
||
| 30 | * @param ApiEntityManager $manager |
||
| 31 | * @param CrudsApiInterface $api |
||
| 32 | */ |
||
| 33 | 19 | public function __construct(ApiEntityManager $manager, CrudsApiInterface $api) |
|
| 40 | |||
| 41 | /** {@inheritdoc} */ |
||
| 42 | public function getClassMetadata() |
||
| 46 | |||
| 47 | /** {@inheritdoc} */ |
||
| 48 | 5 | public function getCrudsApi() |
|
| 52 | |||
| 53 | /** {@inheritdoc} */ |
||
| 54 | 1 | public function update($entity) |
|
| 65 | |||
| 66 | /** {@inheritdoc} */ |
||
| 67 | 1 | public function delete($entity) |
|
| 71 | |||
| 72 | /** {@inheritdoc} */ |
||
| 73 | 1 | public function count($criteria = []) |
|
| 77 | |||
| 78 | /** {@inheritdoc} */ |
||
| 79 | 3 | public function loadAll(array $criteria = [], array $orderBy = null, $limit = null, $offset = null) |
|
| 80 | { |
||
| 81 | 3 | $objects = $this->api->search( |
|
| 82 | 3 | $this->transformer->transformCriteria($criteria), |
|
| 83 | 3 | $this->transformer->transformOrder($orderBy), |
|
| 84 | 3 | $limit, |
|
| 85 | $offset |
||
| 86 | 3 | ); |
|
| 87 | |||
| 88 | 3 | $entities = []; |
|
| 89 | 3 | foreach ($objects as $object) { |
|
| 90 | 3 | $entities[] = $this->manager->getUnitOfWork()->getOrCreateEntity($this->metadata->getName(), $object); |
|
| 91 | 3 | } |
|
| 92 | |||
| 93 | 3 | return $entities; |
|
| 94 | } |
||
| 95 | |||
| 96 | /** {@inheritdoc} */ |
||
| 97 | public function loadOneToOneEntity(array $assoc, $sourceEntity, array $identifier = []) |
||
| 110 | |||
| 111 | /** {@inheritdoc} */ |
||
| 112 | 11 | public function loadById(array $identifiers, $entity = null) |
|
| 122 | |||
| 123 | /** {@inheritdoc} */ |
||
| 124 | public function refresh(array $id, $entity) |
||
| 128 | |||
| 129 | /** {@inheritdoc} */ |
||
| 130 | 2 | public function loadOneToManyCollection(array $assoc, $sourceEntity, AbstractLazyCollection $collection) |
|
| 159 | |||
| 160 | /** {@inheritdoc} */ |
||
| 161 | 10 | public function getOneToManyCollection(array $assoc, $sourceEntity, $limit = null, $offset = null) |
|
| 177 | |||
| 178 | /** {@inheritdoc} */ |
||
| 179 | 5 | public function getToOneEntity(array $mapping, $sourceEntity, array $identifiers) |
|
| 180 | { |
||
| 181 | 5 | $metadata = $this->manager->getClassMetadata(get_class($sourceEntity)); |
|
| 182 | |||
| 183 | 5 | if (!$mapping['isOwningSide']) { |
|
| 184 | $identifiers = $metadata->getIdentifierValues($sourceEntity); |
||
| 185 | 1 | } |
|
| 186 | |||
| 187 | 5 | return $this->manager->getReference($mapping['target'], $identifiers); |
|
| 188 | } |
||
| 189 | |||
| 190 | 5 | public function pushNewEntity($entity) |
|
| 194 | |||
| 195 | 5 | public function flushNewEntities() |
|
| 196 | { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Prepares the changeset of an entity for database insertion (UPDATE). |
||
| 216 | * |
||
| 217 | * The changeset is obtained from the currently running UnitOfWork. |
||
| 218 | * |
||
| 219 | * During this preparation the array that is passed as the second parameter is filled with |
||
| 220 | * <columnName> => <value> pairs, grouped by table name. |
||
| 221 | * |
||
| 222 | * Example: |
||
| 223 | * <code> |
||
| 224 | * array( |
||
| 225 | * 'foo_table' => array('column1' => 'value1', 'column2' => 'value2', ...), |
||
| 226 | * 'bar_table' => array('columnX' => 'valueX', 'columnY' => 'valueY', ...), |
||
| 227 | * ... |
||
| 228 | * ) |
||
| 229 | * </code> |
||
| 230 | * |
||
| 231 | * @param object $entity The entity for which to prepare the data. |
||
| 232 | * |
||
| 233 | * @return array The prepared data. |
||
| 234 | */ |
||
| 235 | 1 | protected function prepareUpdateData($entity) |
|
| 274 | |||
| 275 | 5 | private function convertEntityToData($entity) |
|
| 320 | } |
||
| 321 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.