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 |
||
| 21 | class DocumentSource extends Component implements SourceInterface, \Countable |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Sugary! |
||
| 25 | */ |
||
| 26 | use SaturateTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Linked document model. ODM can automatically index and link user sources to models based on |
||
| 30 | * value of this constant. |
||
| 31 | */ |
||
| 32 | const DOCUMENT = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Associated document class. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $class = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var DocumentSelector |
||
| 43 | */ |
||
| 44 | private $selector = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @invisible |
||
| 48 | * @var ODM |
||
| 49 | */ |
||
| 50 | protected $odm = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $class |
||
| 54 | * @param ODM $odm |
||
| 55 | * @throws SourceException |
||
| 56 | */ |
||
| 57 | View Code Duplication | public function __construct($class = null, ODM $odm = null) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Create new DocumentEntity based on set of provided fields. |
||
| 74 | * |
||
| 75 | * @final Change static method of entity, not this one. |
||
| 76 | * @param array $fields |
||
| 77 | * @param string $class Due ODM models can be inherited you can use this argument to specify |
||
| 78 | * custom model class. |
||
| 79 | * @return DocumentEntity |
||
| 80 | */ |
||
| 81 | final public function create($fields = [], $class = null) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Find document by it's primary key. |
||
| 93 | * |
||
| 94 | * @see findOne() |
||
| 95 | * @param string|\MongoId $id Primary key value. |
||
| 96 | * @return DocumentEntity|null |
||
| 97 | */ |
||
| 98 | public function findByPK($id) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Select one document from mongo collection. |
||
| 105 | * |
||
| 106 | * @param array $query Fields and conditions to query by. |
||
| 107 | * @param array $sortBy |
||
| 108 | * @return DocumentEntity|null |
||
| 109 | */ |
||
| 110 | public function findOne(array $query = [], array $sortBy = []) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get associated document selection with pre-configured query (if any). |
||
| 117 | * |
||
| 118 | * @param array $query |
||
| 119 | * @return DocumentSelector |
||
| 120 | */ |
||
| 121 | public function find(array $query = []) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function count() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param DocumentSelector $selector |
||
| 136 | */ |
||
| 137 | protected function setSelector(DocumentSelector $selector) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return DocumentSelector |
||
| 144 | */ |
||
| 145 | final protected function selector() |
||
| 150 | } |
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.