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:
| 1 | <?php |
||
| 34 | class ClassMetadataBuilder |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var \Doctrine\ORM\Mapping\ClassMetadataInfo |
||
| 38 | */ |
||
| 39 | private $cm; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param \Doctrine\ORM\Mapping\ClassMetadataInfo $cm |
||
| 43 | */ |
||
| 44 | 38 | public function __construct(ClassMetadataInfo $cm) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @return ClassMetadata |
||
| 51 | */ |
||
| 52 | 20 | public function getClassMetadata() |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Marks the class as mapped superclass. |
||
| 59 | * |
||
| 60 | * @return ClassMetadataBuilder |
||
| 61 | */ |
||
| 62 | 1 | public function setMappedSuperClass() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Marks the class as embeddable. |
||
| 72 | * |
||
| 73 | * @return ClassMetadataBuilder |
||
| 74 | */ |
||
| 75 | 1 | public function setEmbeddable() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Adds and embedded class |
||
| 85 | * |
||
| 86 | * @param string $fieldName |
||
| 87 | * @param string $class |
||
| 88 | * @param string|null $columnPrefix |
||
| 89 | * |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | 2 | public function addEmbedded($fieldName, $class, $columnPrefix = null) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Sets custom Repository class name. |
||
| 107 | * |
||
| 108 | * @param string $repositoryClassName |
||
| 109 | * |
||
| 110 | * @return ClassMetadataBuilder |
||
| 111 | */ |
||
| 112 | 1 | public function setCustomRepositoryClass($repositoryClassName) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Marks class read only. |
||
| 121 | * |
||
| 122 | * @return ClassMetadataBuilder |
||
| 123 | */ |
||
| 124 | 1 | public function setReadOnly() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Sets the table name. |
||
| 133 | * |
||
| 134 | * @param string $name |
||
| 135 | * |
||
| 136 | * @return ClassMetadataBuilder |
||
| 137 | */ |
||
| 138 | 2 | public function setTable($name) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Adds Index. |
||
| 147 | * |
||
| 148 | * @param array $columns |
||
| 149 | * @param string $name |
||
| 150 | * |
||
| 151 | * @return ClassMetadataBuilder |
||
| 152 | */ |
||
| 153 | 2 | View Code Duplication | public function addIndex(array $columns, $name) |
| 154 | { |
||
| 155 | 2 | if (!isset($this->cm->table['indexes'])) { |
|
| 156 | 2 | $this->cm->table['indexes'] = []; |
|
| 157 | } |
||
| 158 | |||
| 159 | 2 | $this->cm->table['indexes'][$name] = ['columns' => $columns]; |
|
| 160 | |||
| 161 | 2 | return $this; |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Adds Unique Constraint. |
||
| 166 | * |
||
| 167 | * @param array $columns |
||
| 168 | * @param string $name |
||
| 169 | * |
||
| 170 | * @return ClassMetadataBuilder |
||
| 171 | */ |
||
| 172 | 2 | View Code Duplication | public function addUniqueConstraint(array $columns, $name) |
| 173 | { |
||
| 174 | 2 | if ( ! isset($this->cm->table['uniqueConstraints'])) { |
|
| 175 | 2 | $this->cm->table['uniqueConstraints'] = []; |
|
| 176 | } |
||
| 177 | |||
| 178 | 2 | $this->cm->table['uniqueConstraints'][$name] = ['columns' => $columns]; |
|
| 179 | |||
| 180 | 2 | return $this; |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Adds named query. |
||
| 185 | * |
||
| 186 | * @param string $name |
||
| 187 | * @param string $dqlQuery |
||
| 188 | * |
||
| 189 | * @return ClassMetadataBuilder |
||
| 190 | */ |
||
| 191 | public function addNamedQuery($name, $dqlQuery) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Sets class as root of a joined table inheritance hierarchy. |
||
| 205 | * |
||
| 206 | * @return ClassMetadataBuilder |
||
| 207 | */ |
||
| 208 | 1 | public function setJoinedTableInheritance() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Sets class as root of a single table inheritance hierarchy. |
||
| 217 | * |
||
| 218 | * @return ClassMetadataBuilder |
||
| 219 | */ |
||
| 220 | 1 | public function setSingleTableInheritance() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Sets the discriminator column details. |
||
| 229 | * |
||
| 230 | * @param string $name |
||
| 231 | * @param string $type |
||
| 232 | * @param int $length |
||
| 233 | * |
||
| 234 | * @return ClassMetadataBuilder |
||
| 235 | */ |
||
| 236 | 1 | public function setDiscriminatorColumn($name, $type = 'string', $length = 255) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Adds a subclass to this inheritance hierarchy. |
||
| 251 | * |
||
| 252 | * @param string $name |
||
| 253 | * @param string $class |
||
| 254 | * |
||
| 255 | * @return ClassMetadataBuilder |
||
| 256 | */ |
||
| 257 | 1 | public function addDiscriminatorMapClass($name, $class) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Sets deferred explicit change tracking policy. |
||
| 266 | * |
||
| 267 | * @return ClassMetadataBuilder |
||
| 268 | */ |
||
| 269 | 1 | public function setChangeTrackingPolicyDeferredExplicit() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Sets notify change tracking policy. |
||
| 278 | * |
||
| 279 | * @return ClassMetadataBuilder |
||
| 280 | */ |
||
| 281 | 1 | public function setChangeTrackingPolicyNotify() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Adds lifecycle event. |
||
| 290 | * |
||
| 291 | * @param string $methodName |
||
| 292 | * @param string $event |
||
| 293 | * |
||
| 294 | * @return ClassMetadataBuilder |
||
| 295 | */ |
||
| 296 | 1 | public function addLifecycleEvent($methodName, $event) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Adds Field. |
||
| 305 | * |
||
| 306 | * @param string $name |
||
| 307 | * @param string $type |
||
| 308 | * @param array $mapping |
||
| 309 | * |
||
| 310 | * @return ClassMetadataBuilder |
||
| 311 | */ |
||
| 312 | 1 | public function addField($name, $type, array $mapping = []) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Creates a field builder. |
||
| 324 | * |
||
| 325 | * @param string $name |
||
| 326 | * @param string $type |
||
| 327 | * |
||
| 328 | * @return FieldBuilder |
||
| 329 | */ |
||
| 330 | 5 | public function createField($name, $type) |
|
| 331 | { |
||
| 332 | 5 | return new FieldBuilder( |
|
| 333 | 5 | $this, |
|
| 334 | [ |
||
| 335 | 5 | 'fieldName' => $name, |
|
| 336 | 5 | 'type' => $type |
|
| 337 | ] |
||
| 338 | ); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Creates an embedded builder. |
||
| 343 | * |
||
| 344 | * @param string $fieldName |
||
| 345 | * @param string $class |
||
| 346 | * |
||
| 347 | * @return EmbeddedBuilder |
||
| 348 | */ |
||
| 349 | 2 | public function createEmbedded($fieldName, $class) |
|
| 350 | { |
||
| 351 | 2 | return new EmbeddedBuilder( |
|
| 352 | 2 | $this, |
|
| 353 | [ |
||
| 354 | 2 | 'fieldName' => $fieldName, |
|
| 355 | 2 | 'class' => $class, |
|
| 356 | 'columnPrefix' => null |
||
| 357 | ] |
||
| 358 | ); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Adds a simple many to one association, optionally with the inversed by field. |
||
| 363 | * |
||
| 364 | * @param string $name |
||
| 365 | * @param string $targetEntity |
||
| 366 | * @param string|null $inversedBy |
||
| 367 | * |
||
| 368 | * @return ClassMetadataBuilder |
||
| 369 | */ |
||
| 370 | View Code Duplication | public function addManyToOne($name, $targetEntity, $inversedBy = null) |
|
| 371 | { |
||
| 372 | $builder = $this->createManyToOne($name, $targetEntity); |
||
| 373 | |||
| 374 | if ($inversedBy) { |
||
|
|
|||
| 375 | $builder->inversedBy($inversedBy); |
||
| 376 | } |
||
| 377 | |||
| 378 | return $builder->build(); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Creates a ManyToOne Association Builder. |
||
| 383 | * |
||
| 384 | * Note: This method does not add the association, you have to call build() on the AssociationBuilder. |
||
| 385 | * |
||
| 386 | * @param string $name |
||
| 387 | * @param string $targetEntity |
||
| 388 | * |
||
| 389 | * @return AssociationBuilder |
||
| 390 | */ |
||
| 391 | 3 | public function createManyToOne($name, $targetEntity) |
|
| 392 | { |
||
| 393 | 3 | return new AssociationBuilder( |
|
| 394 | 3 | $this, |
|
| 395 | [ |
||
| 396 | 3 | 'fieldName' => $name, |
|
| 397 | 3 | 'targetEntity' => $targetEntity |
|
| 398 | ], |
||
| 399 | 3 | ClassMetadata::MANY_TO_ONE |
|
| 400 | ); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Creates a OneToOne Association Builder. |
||
| 405 | * |
||
| 406 | * @param string $name |
||
| 407 | * @param string $targetEntity |
||
| 408 | * |
||
| 409 | * @return AssociationBuilder |
||
| 410 | */ |
||
| 411 | 4 | public function createOneToOne($name, $targetEntity) |
|
| 412 | { |
||
| 413 | 4 | return new AssociationBuilder( |
|
| 414 | 4 | $this, |
|
| 415 | [ |
||
| 416 | 4 | 'fieldName' => $name, |
|
| 417 | 4 | 'targetEntity' => $targetEntity |
|
| 418 | ], |
||
| 419 | 4 | ClassMetadata::ONE_TO_ONE |
|
| 420 | ); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Adds simple inverse one-to-one association. |
||
| 425 | * |
||
| 426 | * @param string $name |
||
| 427 | * @param string $targetEntity |
||
| 428 | * @param string $mappedBy |
||
| 429 | * |
||
| 430 | * @return ClassMetadataBuilder |
||
| 431 | */ |
||
| 432 | public function addInverseOneToOne($name, $targetEntity, $mappedBy) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Adds simple owning one-to-one association. |
||
| 442 | * |
||
| 443 | * @param string $name |
||
| 444 | * @param string $targetEntity |
||
| 445 | * @param string|null $inversedBy |
||
| 446 | * |
||
| 447 | * @return ClassMetadataBuilder |
||
| 448 | */ |
||
| 449 | View Code Duplication | public function addOwningOneToOne($name, $targetEntity, $inversedBy = null) |
|
| 450 | { |
||
| 451 | $builder = $this->createOneToOne($name, $targetEntity); |
||
| 452 | |||
| 453 | if ($inversedBy) { |
||
| 454 | $builder->inversedBy($inversedBy); |
||
| 455 | } |
||
| 456 | |||
| 457 | return $builder->build(); |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Creates a ManyToMany Association Builder. |
||
| 462 | * |
||
| 463 | * @param string $name |
||
| 464 | * @param string $targetEntity |
||
| 465 | * |
||
| 466 | * @return ManyToManyAssociationBuilder |
||
| 467 | */ |
||
| 468 | 3 | public function createManyToMany($name, $targetEntity) |
|
| 469 | { |
||
| 470 | 3 | return new ManyToManyAssociationBuilder( |
|
| 471 | 3 | $this, |
|
| 472 | [ |
||
| 473 | 3 | 'fieldName' => $name, |
|
| 474 | 3 | 'targetEntity' => $targetEntity |
|
| 475 | ], |
||
| 476 | 3 | ClassMetadata::MANY_TO_MANY |
|
| 477 | ); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Adds a simple owning many to many association. |
||
| 482 | * |
||
| 483 | * @param string $name |
||
| 484 | * @param string $targetEntity |
||
| 485 | * @param string|null $inversedBy |
||
| 486 | * |
||
| 487 | * @return ClassMetadataBuilder |
||
| 488 | */ |
||
| 489 | View Code Duplication | public function addOwningManyToMany($name, $targetEntity, $inversedBy = null) |
|
| 490 | { |
||
| 491 | $builder = $this->createManyToMany($name, $targetEntity); |
||
| 492 | |||
| 493 | if ($inversedBy) { |
||
| 494 | $builder->inversedBy($inversedBy); |
||
| 495 | } |
||
| 496 | |||
| 497 | return $builder->build(); |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Adds a simple inverse many to many association. |
||
| 502 | * |
||
| 503 | * @param string $name |
||
| 504 | * @param string $targetEntity |
||
| 505 | * @param string $mappedBy |
||
| 506 | * |
||
| 507 | * @return ClassMetadataBuilder |
||
| 508 | */ |
||
| 509 | public function addInverseManyToMany($name, $targetEntity, $mappedBy) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Creates a one to many association builder. |
||
| 519 | * |
||
| 520 | * @param string $name |
||
| 521 | * @param string $targetEntity |
||
| 522 | * |
||
| 523 | * @return OneToManyAssociationBuilder |
||
| 524 | */ |
||
| 525 | 3 | public function createOneToMany($name, $targetEntity) |
|
| 526 | { |
||
| 527 | 3 | return new OneToManyAssociationBuilder( |
|
| 528 | 3 | $this, |
|
| 529 | [ |
||
| 530 | 3 | 'fieldName' => $name, |
|
| 531 | 3 | 'targetEntity' => $targetEntity |
|
| 532 | ], |
||
| 533 | 3 | ClassMetadata::ONE_TO_MANY |
|
| 534 | ); |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Adds simple OneToMany association. |
||
| 539 | * |
||
| 540 | * @param string $name |
||
| 541 | * @param string $targetEntity |
||
| 542 | * @param string $mappedBy |
||
| 543 | * |
||
| 544 | * @return ClassMetadataBuilder |
||
| 545 | */ |
||
| 546 | public function addOneToMany($name, $targetEntity, $mappedBy) |
||
| 553 | } |
||
| 554 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: