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 Formatter 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 Formatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | final class Formatter |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Query operators. |
||
| 28 | * Organized into handling groups. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $ops = [ |
||
| 33 | 'root' => ['$and', '$or', '$nor'], |
||
| 34 | 'single' => ['$eq', '$gt', '$gte', '$lt', '$lte', '$ne'], |
||
| 35 | 'multiple' => ['$in', '$nin', '$all'], |
||
| 36 | 'recursive' => ['$not', '$elemMatch'], |
||
| 37 | 'ignore' => ['$exists', '$type', '$mod', '$size', '$regex', '$text', '$where'], |
||
| 38 | ]; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Formats a set of query criteria for a Model. |
||
| 42 | * Ensures the id and type fields are properly applied. |
||
| 43 | * Ensures that values are properly converted to their database equivalents: e.g dates, mongo ids, etc. |
||
| 44 | * |
||
| 45 | * @param EntityMetadata $metadata |
||
| 46 | * @param Store $store |
||
| 47 | * @param array $criteria |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function formatQuery(EntityMetadata $metadata, Store $store, array $criteria) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Prepares and formats an attribute value for proper insertion into the database. |
||
| 75 | * |
||
| 76 | * @param AttributeMetadata $attrMeta |
||
| 77 | * @param mixed $value |
||
| 78 | * @return mixed |
||
| 79 | */ |
||
| 80 | public function getAttributeDbValue(AttributeMetadata $attrMeta, $value) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Prepares and formats a has-many embed for proper insertion into the database. |
||
| 98 | * |
||
| 99 | * @param EmbeddedPropMetadata $embeddedPropMeta |
||
| 100 | * @param array $embed |
||
|
|
|||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | public function getEmbedManyDbValue(EmbeddedPropMetadata $embeddedPropMeta, array $embeds = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Prepares and formats a has-one embed for proper insertion into the database. |
||
| 117 | * |
||
| 118 | * @param EmbeddedPropMetadata $embeddedPropMeta |
||
| 119 | * @param Embed|null $embed |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | public function getEmbedOneDbValue(EmbeddedPropMetadata $embeddedPropMeta, Embed $embed = null) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Prepares and formats a has-one relationship model for proper insertion into the database. |
||
| 132 | * |
||
| 133 | * @param RelationshipMetadata $relMeta |
||
| 134 | * @param Model|null $model |
||
| 135 | * @return mixed |
||
| 136 | */ |
||
| 137 | public function getHasOneDbValue(RelationshipMetadata $relMeta, Model $model = null) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Prepares and formats a has-many relationship model set for proper insertion into the database. |
||
| 147 | * |
||
| 148 | * @param RelationshipMetadata $relMeta |
||
| 149 | * @param Model[]|null $models |
||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | public function getHasManyDbValue(RelationshipMetadata $relMeta, array $models = null) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritDoc} |
||
| 166 | */ |
||
| 167 | public function getIdentifierDbValue($identifier, $strategy = null) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Gets all possible identifier field keys (internal and persistence layer). |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public function getIdentifierFields() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Gets all possible model type keys (internal and persistence layer). |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getTypeFields() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Determines if a field key is an identifier. |
||
| 200 | * Uses both the internal and persistence identifier keys. |
||
| 201 | * |
||
| 202 | * @param string $key |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function isIdentifierField($key) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Determines if the provided id strategy is supported. |
||
| 212 | * |
||
| 213 | * @param string|null $strategy |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function isIdStrategySupported($strategy) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Determines if a field key is a model type field. |
||
| 223 | * Uses both the internal and persistence model type keys. |
||
| 224 | * |
||
| 225 | * @param string $key |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | public function isTypeField($key) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Creates an embed for storage of an embed model in the database |
||
| 235 | * |
||
| 236 | * @param EmbeddedPropMetadata $embeddedPropMeta |
||
| 237 | * @param Embed $embed |
||
| 238 | * @return array|null |
||
| 239 | */ |
||
| 240 | private function createEmbed(EmbeddedPropMetadata $embeddedPropMeta, Embed $embed) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Creates a reference for storage of a related model in the database |
||
| 264 | * |
||
| 265 | * @param RelationshipMetadata $relMeta |
||
| 266 | * @param Model $model |
||
| 267 | * @return mixed |
||
| 268 | */ |
||
| 269 | private function createReference(RelationshipMetadata $relMeta, Model $model) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Formats a query element and ensures the correct key and value are set. |
||
| 283 | * Returns a tuple of the formatted key and value. |
||
| 284 | * |
||
| 285 | * @param string $key |
||
| 286 | * @param mixed $value |
||
| 287 | * @param EntityMetadata $metadata |
||
| 288 | * @param Store $store |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | private function formatQueryElement($key, $value, EntityMetadata $metadata, Store $store) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Formats an attribute query element. |
||
| 319 | * Returns a tuple of the formatted key and value, or null if the key is not an attribute field. |
||
| 320 | * |
||
| 321 | * @param string $key |
||
| 322 | * @param mixed $value |
||
| 323 | * @param EntityMetadata $metadata |
||
| 324 | * @param Store $store |
||
| 325 | * @return array|null |
||
| 326 | */ |
||
| 327 | private function formatQueryElementAttr($key, $value, EntityMetadata $metadata, Store $store) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Formats a dot-notated field. |
||
| 351 | * Returns a tuple of the formatted key and value, or null if the key is not a dot-notated field, or cannot be handled. |
||
| 352 | * |
||
| 353 | * @param string $key |
||
| 354 | * @param mixed $value |
||
| 355 | * @param EntityMetadata $metadata |
||
| 356 | * @param Store $store |
||
| 357 | * @return array|null |
||
| 358 | */ |
||
| 359 | private function formatQueryElementDotted($key, $value, EntityMetadata $metadata, Store $store) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Formats a relationship query element. |
||
| 397 | * Returns a tuple of the formatted key and value, or null if the key is not a relationship field. |
||
| 398 | * |
||
| 399 | * @param string $key |
||
| 400 | * @param mixed $value |
||
| 401 | * @param EntityMetadata $metadata |
||
| 402 | * @param Store $store |
||
| 403 | * @return array|null |
||
| 404 | */ |
||
| 405 | private function formatQueryElementRel($key, $value, EntityMetadata $metadata, Store $store) |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Formats a root query element: either id or model type. |
||
| 427 | * Returns a tuple of the formatted key and value, or null if the key is not a root field. |
||
| 428 | * |
||
| 429 | * @param string $key |
||
| 430 | * @param mixed $value |
||
| 431 | * @param EntityMetadata $metadata |
||
| 432 | * @return array|null |
||
| 433 | */ |
||
| 434 | private function formatQueryElementRoot($key, $value, EntityMetadata $metadata) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Formats a query expression. |
||
| 454 | * |
||
| 455 | * @param array $expression |
||
| 456 | * @param Closure $converter |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | private function formatQueryExpression(array $expression, Closure $converter) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Gets the converter for handling attribute values in queries. |
||
| 493 | * |
||
| 494 | * @param Store $store |
||
| 495 | * @param AttributeMetadata $attrMeta |
||
| 496 | * @return Closure |
||
| 497 | */ |
||
| 498 | private function getQueryAttrConverter(Store $store, AttributeMetadata $attrMeta) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Gets the converter for handling relationship values in queries. |
||
| 513 | * |
||
| 514 | * @param Store $store |
||
| 515 | * @param RelationshipMetadata $relMeta |
||
| 516 | * @return Closure |
||
| 517 | */ |
||
| 518 | private function getQueryRelConverter(Store $store, RelationshipMetadata $relMeta) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Gets the converter for handling root field values in queries (id or model type). |
||
| 526 | * |
||
| 527 | * @param EntityMetadata $metadata |
||
| 528 | * @param string $key |
||
| 529 | * @return Closure |
||
| 530 | */ |
||
| 531 | private function getQueryRootConverter(EntityMetadata $metadata, $key) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Determines whether a query value has additional query operators. |
||
| 544 | * |
||
| 545 | * @param mixed $value |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | private function hasOperators($value) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Determines if a key is a query operator. |
||
| 569 | * |
||
| 570 | * @param string $key |
||
| 571 | * @return bool |
||
| 572 | */ |
||
| 573 | private function isOperator($key) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Determines if a key is of a certain operator handling type. |
||
| 580 | * |
||
| 581 | * @param string $type |
||
| 582 | * @param string $key |
||
| 583 | * @return bool |
||
| 584 | */ |
||
| 585 | private function isOpType($type, $key) |
||
| 592 | } |
||
| 593 |
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
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.