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 |
||
| 22 | final class Formatter |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Query operators. |
||
| 26 | * Organized into handling groups. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $ops = [ |
||
| 31 | 'root' => ['$and', '$or', '$nor'], |
||
| 32 | 'single' => ['$eq', '$gt', '$gte', '$lt', '$lte', '$ne'], |
||
| 33 | 'multiple' => ['$in', '$nin', '$all'], |
||
| 34 | 'recursive' => ['$not', '$elemMatch'], |
||
| 35 | 'ignore' => ['$exists', '$type', '$mod', '$size', '$regex', '$text', '$where'], |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Formats a set of query criteria for a Model. |
||
| 40 | * Ensures the id and type fields are properly applied. |
||
| 41 | * Ensures that values are properly converted to their database equivalents: e.g dates, mongo ids, etc. |
||
| 42 | * |
||
| 43 | * @param EntityMetadata $metadata |
||
| 44 | * @param Store $store |
||
| 45 | * @param array $criteria |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | public function formatQuery(EntityMetadata $metadata, Store $store, array $criteria) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Prepares and formats an attribute value for proper insertion into the database. |
||
| 73 | * |
||
| 74 | * @param AttributeMetadata $attrMeta |
||
| 75 | * @param mixed $value |
||
| 76 | * @return mixed |
||
| 77 | */ |
||
| 78 | public function getAttributeDbValue(AttributeMetadata $attrMeta, $value) |
||
| 79 | { |
||
| 80 | // Handle data type conversion, if needed. |
||
| 81 | if ('date' === $attrMeta->dataType && $value instanceof \DateTime) { |
||
| 82 | return new \MongoDate($value->getTimestamp(), $value->format('u')); |
||
| 83 | } |
||
| 84 | return $value; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Prepares and formats a has-one relationship model for proper insertion into the database. |
||
| 89 | * |
||
| 90 | * @param RelationshipMetadata $relMeta |
||
| 91 | * @param Model|null $model |
||
| 92 | * @return mixed |
||
| 93 | */ |
||
| 94 | public function getHasOneDbValue(RelationshipMetadata $relMeta, Model $model = null) |
||
| 95 | { |
||
| 96 | if (null === $model || true === $relMeta->isInverse) { |
||
| 97 | return null; |
||
| 98 | } |
||
| 99 | return $this->createReference($relMeta, $model); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Prepares and formats a has-many relationship model set for proper insertion into the database. |
||
| 104 | * |
||
| 105 | * @param RelationshipMetadata $relMeta |
||
| 106 | * @param Model[]|null $models |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | public function getHasManyDbValue(RelationshipMetadata $relMeta, array $models = null) |
||
| 110 | { |
||
| 111 | if (null === $models || true === $relMeta->isInverse) { |
||
| 112 | return null; |
||
| 113 | } |
||
| 114 | $references = []; |
||
| 115 | foreach ($models as $model) { |
||
| 116 | $references[] = $this->createReference($relMeta, $model); |
||
| 117 | } |
||
| 118 | return empty($references) ? null : $references; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritDoc} |
||
| 123 | */ |
||
| 124 | public function getIdentifierDbValue($identifier, $strategy = null) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Gets all possible identifier field keys (internal and persistence layer). |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public function getIdentifierFields() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Gets all possible model type keys (internal and persistence layer). |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function getTypeFields() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Determines if a field key is an identifier. |
||
| 157 | * Uses both the internal and persistence identifier keys. |
||
| 158 | * |
||
| 159 | * @param string $key |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function isIdentifierField($key) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Determines if the provided id strategy is supported. |
||
| 169 | * |
||
| 170 | * @param string|null $strategy |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function isIdStrategySupported($strategy) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Determines if a field key is a model type field. |
||
| 180 | * Uses both the internal and persistence model type keys. |
||
| 181 | * |
||
| 182 | * @param string $key |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function isTypeField($key) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Creates a reference for storage of a related model in the database |
||
| 192 | * |
||
| 193 | * @param RelationshipMetadata $relMeta |
||
| 194 | * @param Model $model |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | private function createReference(RelationshipMetadata $relMeta, Model $model) |
||
| 198 | { |
||
| 199 | $reference = []; |
||
| 200 | $identifier = $this->getIdentifierDbValue($model->getId()); |
||
| 201 | if (true === $relMeta->isPolymorphic()) { |
||
| 202 | $reference[Persister::IDENTIFIER_KEY] = $identifier; |
||
| 203 | $reference[Persister::TYPE_KEY] = $model->getType(); |
||
| 204 | return $reference; |
||
| 205 | } |
||
| 206 | return $identifier; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Formats a query element and ensures the correct key and value are set. |
||
| 211 | * Returns a tuple of the formatted key and value. |
||
| 212 | * |
||
| 213 | * @param string $key |
||
| 214 | * @param mixed $value |
||
| 215 | * @param EntityMetadata $metadata |
||
| 216 | * @param Store $store |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | private function formatQueryElement($key, $value, EntityMetadata $metadata, Store $store) |
||
| 220 | { |
||
| 221 | // Handle identifiers. |
||
| 222 | if (null !== $result = $this->formatQueryElementId($key, $value, $metadata)) { |
||
| 223 | return $result; |
||
| 224 | } |
||
| 225 | |||
| 226 | // Handle polymorphic model type fields. |
||
| 227 | if (null !== $result = $this->formatQueryElementType($key, $value)) { |
||
| 228 | return $result; |
||
| 229 | } |
||
| 230 | |||
| 231 | // Handle attributes. |
||
| 232 | if (null !== $result = $this->formatQueryElementAttr($key, $value, $metadata, $store)) { |
||
| 233 | return $result; |
||
| 234 | } |
||
| 235 | |||
| 236 | // Handle relationships. |
||
| 237 | if (null !== $result = $this->formatQueryElementRel($key, $value, $metadata, $store)) { |
||
| 238 | return $result; |
||
| 239 | } |
||
| 240 | |||
| 241 | // Handle dot notated fields. |
||
| 242 | if (null !== $result = $this->formatQueryElementDotted($key, $value, $metadata, $store)) { |
||
| 243 | return $result; |
||
| 244 | } |
||
| 245 | |||
| 246 | // Pass remaining elements unconverted. |
||
| 247 | return [$key, $value]; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Formats an attribute query element. |
||
| 252 | * Returns a tuple of the formatted key and value, or null if the key is not an attribute field. |
||
| 253 | * |
||
| 254 | * @param string $key |
||
| 255 | * @param mixed $value |
||
| 256 | * @param EntityMetadata $metadata |
||
| 257 | * @param Store $store |
||
| 258 | * @return array|null |
||
| 259 | */ |
||
| 260 | private function formatQueryElementAttr($key, $value, EntityMetadata $metadata, Store $store) |
||
| 261 | { |
||
| 262 | if (null !== $attrMeta = $metadata->getAttribute($key)) { |
||
| 263 | return; |
||
| 264 | } |
||
| 265 | |||
| 266 | $converter = $this->getQueryAttrConverter($store, $attrMeta); |
||
|
|
|||
| 267 | |||
| 268 | if (is_array($value)) { |
||
| 269 | |||
| 270 | if (true === $this->hasOperators($value)) { |
||
| 271 | return [$key, $this->formatQueryExpression($value, $converter)]; |
||
| 272 | } |
||
| 273 | |||
| 274 | if (in_array($attrMeta->dataType, ['array', 'object'])) { |
||
| 275 | return [$key, $value]; |
||
| 276 | } |
||
| 277 | return [$key, $this->formatQueryExpression(['$in' => $value], $converter)]; |
||
| 278 | } |
||
| 279 | return [$key, $converter($value)]; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Formats a dot-notated field. |
||
| 284 | * Returns a tuple of the formatted key and value, or null if the key is not a dot-notated field, or cannot be handled. |
||
| 285 | * |
||
| 286 | * @param string $key |
||
| 287 | * @param mixed $value |
||
| 288 | * @param EntityMetadata $metadata |
||
| 289 | * @param Store $store |
||
| 290 | * @return array|null |
||
| 291 | */ |
||
| 292 | private function formatQueryElementDotted($key, $value, EntityMetadata $metadata, Store $store) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Formats an identifier query element. |
||
| 330 | * Returns a tuple of the formatted key and value, or null if the key is not an identifier field. |
||
| 331 | * |
||
| 332 | * @param string $key |
||
| 333 | * @param mixed $value |
||
| 334 | * @param EntityMetadata $metadata |
||
| 335 | * @return array|null |
||
| 336 | */ |
||
| 337 | View Code Duplication | private function formatQueryElementId($key, $value, EntityMetadata $metadata) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Formats a relationship query element. |
||
| 355 | * Returns a tuple of the formatted key and value, or null if the key is not a relationship field. |
||
| 356 | * |
||
| 357 | * @param string $key |
||
| 358 | * @param mixed $value |
||
| 359 | * @param EntityMetadata $metadata |
||
| 360 | * @param Store $store |
||
| 361 | * @return array|null |
||
| 362 | */ |
||
| 363 | private function formatQueryElementRel($key, $value, EntityMetadata $metadata, Store $store) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Formats a model type query element. |
||
| 384 | * Returns a tuple of the formatted key and value, or null if the key is not a type field. |
||
| 385 | * |
||
| 386 | * @param string $key |
||
| 387 | * @param mixed $value |
||
| 388 | * @return array|null |
||
| 389 | */ |
||
| 390 | View Code Duplication | private function formatQueryElementType($key, $value) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Formats a query expression. |
||
| 408 | * |
||
| 409 | * @param array $expression |
||
| 410 | * @param Closure $converter |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | private function formatQueryExpression(array $expression, Closure $converter) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Gets the converter for handling attribute values in queries. |
||
| 447 | * |
||
| 448 | * @param Store $store |
||
| 449 | * @param AttributeMetadata $attrMeta |
||
| 450 | * @return Closure |
||
| 451 | */ |
||
| 452 | private function getQueryAttrConverter(Store $store, AttributeMetadata $attrMeta) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Gets the converter for handling identifier values in queries. |
||
| 463 | * |
||
| 464 | * @param EntityMetadata $metadata |
||
| 465 | * @return Closure |
||
| 466 | */ |
||
| 467 | private function getQueryIdConverter(EntityMetadata $metadata) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Gets the converter for handling relationship values in queries. |
||
| 476 | * |
||
| 477 | * @param Store $store |
||
| 478 | * @param RelationshipMetadata $relMeta |
||
| 479 | * @return Closure |
||
| 480 | */ |
||
| 481 | private function getQueryRelConverter(Store $store, RelationshipMetadata $relMeta) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Gets the converter for handling model type values in queries. |
||
| 489 | * |
||
| 490 | * @return Closure |
||
| 491 | */ |
||
| 492 | private function getQueryTypeConverter() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Determines whether a query value has additional query operators. |
||
| 501 | * |
||
| 502 | * @param mixed $value |
||
| 503 | * @return bool |
||
| 504 | */ |
||
| 505 | private function hasOperators($value) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Determines if a key is a query operator. |
||
| 526 | * |
||
| 527 | * @param string $key |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | private function isOperator($key) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Determines if a key is of a certain operator handling type. |
||
| 537 | * |
||
| 538 | * @param string $type |
||
| 539 | * @param string $key |
||
| 540 | * @return bool |
||
| 541 | */ |
||
| 542 | private function isOpType($type, $key) |
||
| 549 | } |
||
| 550 |
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: