Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Store 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 Store, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Store |
||
25 | { |
||
26 | /** |
||
27 | * @var MetadataFactory |
||
28 | */ |
||
29 | private $mf; |
||
30 | |||
31 | /** |
||
32 | * @var TypeFactory |
||
33 | */ |
||
34 | private $typeFactory; |
||
35 | |||
36 | /** |
||
37 | * The storage layer manager. |
||
38 | * Retrieves the appropriate persister and search client for handling records. |
||
39 | * |
||
40 | * @var StorageLayerManager |
||
41 | */ |
||
42 | private $storageManager; |
||
43 | |||
44 | /** |
||
45 | * Contains all models currently loaded in memory. |
||
46 | * |
||
47 | * @var Cache |
||
48 | */ |
||
49 | private $cache; |
||
50 | |||
51 | /** |
||
52 | * The event dispatcher for firing model lifecycle events. |
||
53 | * |
||
54 | * @var EventDispatcher |
||
55 | */ |
||
56 | private $dispatcher; |
||
57 | |||
58 | /** |
||
59 | * Constructor. |
||
60 | * |
||
61 | * @param MetadataFactory $mf |
||
62 | * @param StorageLayerManager $storageManager |
||
63 | * @param TypeFactory $typeFactory |
||
64 | * @param EventDispatcher $dispatcher |
||
65 | */ |
||
66 | public function __construct(MetadataFactory $mf, StorageLayerManager $storageManager, TypeFactory $typeFactory, EventDispatcher $dispatcher) |
||
74 | |||
75 | /** |
||
76 | * Finds a single record from the persistence layer, by type and id. |
||
77 | * Will return a Model object if found, or throw an exception if not. |
||
78 | * |
||
79 | * @api |
||
80 | * @param string $typeKey The model type. |
||
81 | * @param string $identifier The model id. |
||
82 | * @return Model |
||
83 | */ |
||
84 | public function find($typeKey, $identifier) |
||
92 | |||
93 | /** |
||
94 | * Finds all records (or filtered by specific identifiers) for a type. |
||
95 | * |
||
96 | * @todo Add sorting and pagination (limit/skip). |
||
97 | * @todo Handle find all with identifiers. |
||
98 | * @param string $typeKey The model type. |
||
99 | * @param array $idenitifiers The model identifiers (optional). |
||
|
|||
100 | * @return Collection |
||
101 | */ |
||
102 | public function findAll($typeKey, array $identifiers = []) |
||
112 | |||
113 | /** |
||
114 | * Queries records based on a provided set of criteria. |
||
115 | * |
||
116 | * @param string $typeKey The model type. |
||
117 | * @param array $criteria The query criteria. |
||
118 | * @param array $fields Fields to include/exclude. |
||
119 | * @param array $sort The sort criteria. |
||
120 | * @param int $offset The starting offset, aka the number of Models to skip. |
||
121 | * @param int $limit The number of Models to limit. |
||
122 | * @return Collection |
||
123 | */ |
||
124 | public function findQuery($typeKey, array $criteria, array $fields = [], array $sort = [], $offset = 0, $limit = 0) |
||
134 | |||
135 | /** |
||
136 | * Searches for records (via the search layer) for a specific type, attribute, and value. |
||
137 | * Uses the autocomplete logic to fullfill the request. |
||
138 | * |
||
139 | * @todo Determine if full models should be return, or only specific fields. |
||
140 | * Autocompleters needs to be fast. If only specific fields are returned, do we need to exclude nulls in serialization? |
||
141 | * @todo Is search enabled for all models, by default, where everything is stored? |
||
142 | * |
||
143 | * @param string $typeKey |
||
144 | * @param string $attributeKey |
||
145 | * @param string $searchValue |
||
146 | * @return Collection |
||
147 | */ |
||
148 | public function searchAutocomplete($typeKey, $attributeKey, $searchValue) |
||
156 | |||
157 | /** |
||
158 | * Creates a new record. |
||
159 | * The model will not be comitted to the persistence layer until $model->save() is called. |
||
160 | * |
||
161 | * @api |
||
162 | * @param string $typeKey The model type. |
||
163 | * @param string|null $identifier The model identifier. Generally should be null unless client-side id generation is in place. |
||
164 | * @return Model |
||
165 | */ |
||
166 | public function create($typeKey, $identifier = null) |
||
173 | |||
174 | /** |
||
175 | * Deletes a model. |
||
176 | * The moel will be immediately deleted once retrieved. |
||
177 | * |
||
178 | * @api |
||
179 | * @param string $typeKey The model type. |
||
180 | * @param string|null $identifier The model identifier. |
||
181 | * @return Model |
||
182 | */ |
||
183 | public function delete($typeKey, $identifier) |
||
188 | |||
189 | /** |
||
190 | * Retrieves a Record object from the persistence layer. |
||
191 | * |
||
192 | * @param string $typeKey The model type. |
||
193 | * @param string $identifier The model identifier. |
||
194 | * @return Record |
||
195 | * @throws StoreException If the record cannot be found. |
||
196 | */ |
||
197 | public function retrieveRecord($typeKey, $identifier) |
||
206 | |||
207 | /** |
||
208 | * Retrieves multiple Record objects from the persistence layer. |
||
209 | * |
||
210 | * @todo Implement sorting and pagination (limit/skip). |
||
211 | * @param string $typeKey The model type. |
||
212 | * @param array $identifiers The model identifier. |
||
213 | * @return Record[] |
||
214 | */ |
||
215 | public function retrieveRecords($typeKey, array $identifiers) |
||
220 | |||
221 | /** |
||
222 | * Retrieves multiple Record objects from the persistence layer for an inverse relationship. |
||
223 | * |
||
224 | * @todo Need to find a way to query all inverse at the same time for a findAll query, as it's queried multiple times. |
||
225 | * @param string $ownerTypeKey |
||
226 | * @param string $relTypeKey |
||
227 | * @param array $identifiers |
||
228 | * @param string $inverseField |
||
229 | * @return Record[] |
||
230 | */ |
||
231 | public function retrieveInverseRecords($ownerTypeKey, $relTypeKey, array $identifiers, $inverseField) |
||
242 | |||
243 | /** |
||
244 | * Loads/creates a model from a persistence layer Record. |
||
245 | * |
||
246 | * @param string $typeKey The model type. |
||
247 | * @param Record $record The persistence layer record. |
||
248 | * @return Model |
||
249 | */ |
||
250 | protected function loadModel($typeKey, Record $record) |
||
264 | |||
265 | /** |
||
266 | * Loads/creates multiple models from persistence layer Records. |
||
267 | * |
||
268 | * @param string $typeKey The model type. |
||
269 | * @param Record[] $record The persistence layer records. |
||
270 | * @return Model[] |
||
271 | */ |
||
272 | protected function loadModels($typeKey, array $records) |
||
280 | |||
281 | /** |
||
282 | * Dispatches a model lifecycle event via the event dispatcher. |
||
283 | * |
||
284 | * @param string $eventName |
||
285 | * @param Model $model |
||
286 | * @return self |
||
287 | */ |
||
288 | protected function dispatchLifecycleEvent($eventName, Model $model) |
||
294 | |||
295 | /** |
||
296 | * Creates a new Model instance. |
||
297 | * Will not be persisted until $model->save() is called. |
||
298 | * |
||
299 | * @param string $typeKey The model type. |
||
300 | * @param string $identifier The model identifier. |
||
301 | * @return Model |
||
302 | */ |
||
303 | protected function createModel($typeKey, $identifier) |
||
317 | |||
318 | /** |
||
319 | * Loads a has-one model proxy. |
||
320 | * |
||
321 | * @param string $relatedTypeKey |
||
322 | * @param string $identifier |
||
323 | * @return Model |
||
324 | */ |
||
325 | public function loadProxyModel($relatedTypeKey, $identifier) |
||
337 | |||
338 | /** |
||
339 | * Loads a has-many inverse model collection. |
||
340 | * |
||
341 | * @param RelationshipMetadata $relMeta |
||
342 | * @param Model $owner |
||
343 | * @return InverseCollection |
||
344 | */ |
||
345 | public function createInverseCollection(RelationshipMetadata $relMeta, Model $owner) |
||
350 | |||
351 | /** |
||
352 | * Loads a has-many model collection. |
||
353 | * |
||
354 | * @param RelationshipMetadata $relMeta |
||
355 | * @param array|null $references |
||
356 | * @return Collection |
||
357 | */ |
||
358 | public function createCollection(RelationshipMetadata $relMeta, array $references = null) |
||
373 | |||
374 | /** |
||
375 | * Loads/fills a collection of empty (unloaded) models with data from the persistence layer. |
||
376 | * |
||
377 | * @param AbstractCollection $collection |
||
378 | * @return Model[] |
||
379 | */ |
||
380 | public function loadCollection(AbstractCollection $collection) |
||
403 | |||
404 | /** |
||
405 | * Commits a model by persisting it to the database. |
||
406 | * |
||
407 | * @todo Eventually we'll want to schedule models and allow for mutiple commits, flushes, etc. |
||
408 | * @todo Will need to handle cascade saving of new or modified relationships?? |
||
409 | * @param Model $model The model to commit. |
||
410 | * @return Model |
||
411 | */ |
||
412 | public function commit(Model $model) |
||
438 | |||
439 | /** |
||
440 | * Performs a Model creation commit and persists to the database. |
||
441 | * |
||
442 | * @param Model $model |
||
443 | * @return Model |
||
444 | */ |
||
445 | View Code Duplication | private function doCommitCreate(Model $model) |
|
457 | |||
458 | /** |
||
459 | * Performs a Model delete commit and persists to the database. |
||
460 | * |
||
461 | * @param Model $model |
||
462 | * @return Model |
||
463 | */ |
||
464 | View Code Duplication | private function doCommitDelete(Model $model) |
|
474 | |||
475 | /** |
||
476 | * Performs a Model update commit and persists to the database. |
||
477 | * |
||
478 | * @param Model $model |
||
479 | * @return Model |
||
480 | */ |
||
481 | View Code Duplication | private function doCommitUpdate(Model $model) |
|
492 | |||
493 | /** |
||
494 | * Validates that a model type can be set to an owning metadata type. |
||
495 | * |
||
496 | * @param EntityMetadata $owningMeta The metadata the type will be added to. |
||
497 | * @param string $typeToAdd The type to add. |
||
498 | * @return self |
||
499 | * @throws StoreException If the type to add is not supported. |
||
500 | */ |
||
501 | public function validateRelationshipSet(EntityMetadata $owningMeta, $typeToAdd) |
||
513 | |||
514 | /** |
||
515 | * Converts an attribute value to the proper Modlr data type. |
||
516 | * |
||
517 | * @param string $dataType The data type, such as string, integer, boolean, etc. |
||
518 | * @param mixed $value The value to convert. |
||
519 | * @return mixed |
||
520 | */ |
||
521 | public function convertAttributeValue($dataType, $value) |
||
525 | |||
526 | /** |
||
527 | * Determines if a model is eligible for commit. |
||
528 | * |
||
529 | * @todo Does delete need to be here? |
||
530 | * @param Model $model |
||
531 | * @return bool |
||
532 | */ |
||
533 | protected function shouldCommit(Model $model) |
||
538 | |||
539 | /** |
||
540 | * Determines the persister to use for the provided model key. |
||
541 | * |
||
542 | * @param string $typeKey The model type key. |
||
543 | * @return PersisterInterface |
||
544 | */ |
||
545 | protected function getPersisterFor($typeKey) |
||
550 | |||
551 | /** |
||
552 | * Generates a new identifier value for a model type. |
||
553 | * |
||
554 | * @param string $typeKey The model type. |
||
555 | * @return string |
||
556 | */ |
||
557 | protected function generateIdentifier($typeKey) |
||
561 | |||
562 | /** |
||
563 | * Converts the id value to a normalized string. |
||
564 | * |
||
565 | * @param mixed $identenfier The identifier to convert. |
||
566 | * @return string |
||
567 | */ |
||
568 | protected function convertId($identifier) |
||
572 | |||
573 | /** |
||
574 | * Gets the metadata for a model type. |
||
575 | * |
||
576 | * @param string $typeKey The model type. |
||
577 | * @return EntityMetadata |
||
578 | */ |
||
579 | public function getMetadataForType($typeKey) |
||
583 | |||
584 | /** |
||
585 | * Gets the metadata for a relationship. |
||
586 | * |
||
587 | * @param RelationshipMetadata $relMeta The relationship metadata. |
||
588 | * @return EntityMetadata |
||
589 | */ |
||
590 | public function getMetadataForRelationship(RelationshipMetadata $relMeta) |
||
594 | |||
595 | /** |
||
596 | * Determines if an array is sequential. |
||
597 | * |
||
598 | * @param array $arr |
||
599 | * @return bool |
||
600 | */ |
||
601 | protected function isSequentialArray(array $arr) |
||
608 | } |
||
609 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.