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 |
||
| 23 | trait DraftableEntityAwareTrait |
||
| 24 | { |
||
| 25 | View Code Duplication | public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Find entities in draft mode. |
||
| 40 | * |
||
| 41 | * Sets the key 'isDraft' with the value true and proxies to parent::findBy() |
||
| 42 | * |
||
| 43 | * @param array $criteria |
||
| 44 | * @param array|null $sort |
||
| 45 | * @param null|int $limit |
||
| 46 | * @param null|int $skip |
||
| 47 | * |
||
| 48 | * @return \MongoCursor |
||
| 49 | */ |
||
| 50 | public function findDraftsBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
||
| 58 | |||
| 59 | View Code Duplication | public function findOneBy(array $criteria) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Finds one entity in draft mode. |
||
| 73 | * |
||
| 74 | * Sets the key 'isDraft' to true and proxies to parent::findOneBy() |
||
| 75 | * |
||
| 76 | * @param array $criteria |
||
| 77 | * |
||
| 78 | * @return \MongoCursor |
||
| 79 | */ |
||
| 80 | public function findOneDraftBy(array $criteria) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Creates a query builder. |
||
| 91 | * |
||
| 92 | * Prepopulate the constraint for 'isDraft' according to $findDrafts: |
||
| 93 | * - true: queryBuilder will return only drafts |
||
| 94 | * - false: queryBuilder will return only non drafts. |
||
| 95 | * - null: queryBuilder will return all entities matching the additional constraints. |
||
| 96 | * |
||
| 97 | * @param bool|null $findDrafts |
||
| 98 | * |
||
| 99 | * @return \Doctrine\MongoDB\Query\Builder |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function createQueryBuilder($findDrafts = false) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Creates an entity in draft mode. |
||
| 116 | * |
||
| 117 | * @param array $data |
||
| 118 | * @param bool $persist |
||
| 119 | * |
||
| 120 | * @return EntityInterface |
||
| 121 | */ |
||
| 122 | public function createDraft(array $data = null, $persist = false) |
||
| 128 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.