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 |
||
24 | class RecordSource extends Component implements SourceInterface, \Countable |
||
25 | { |
||
26 | use SaturateTrait; |
||
27 | |||
28 | /** |
||
29 | * Linked document model. ORM can automatically index and link user sources to models based on |
||
30 | * value of this constant. |
||
31 | */ |
||
32 | const RECORD = null; |
||
33 | |||
34 | /** |
||
35 | * Associated document class. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $class = null; |
||
40 | |||
41 | /** |
||
42 | * @var RecordSelector |
||
43 | */ |
||
44 | private $selector = null; |
||
45 | |||
46 | /** |
||
47 | * @invisible |
||
48 | * |
||
49 | * @var ORM |
||
50 | */ |
||
51 | protected $orm = null; |
||
52 | |||
53 | /** |
||
54 | * @param string $class |
||
55 | * @param ORMInterface $orm |
||
56 | * |
||
57 | * @throws SugarException |
||
58 | */ |
||
59 | View Code Duplication | public function __construct($class = null, ORMInterface $orm = null) |
|
73 | |||
74 | /** |
||
75 | * Create new Record based on set of provided fields. |
||
76 | * |
||
77 | * @final Change static method of entity, not this one. |
||
78 | * |
||
79 | * @param array $fields |
||
80 | * |
||
81 | * @return RecordEntity |
||
82 | */ |
||
83 | final public function create($fields = []) |
||
88 | |||
89 | /** |
||
90 | * Find record by it's primary key. |
||
91 | * |
||
92 | * @see findOne() |
||
93 | * |
||
94 | * @param string|int $id Primary key value. |
||
95 | * |
||
96 | * @return RecordEntity|null |
||
97 | */ |
||
98 | public function findByPK($id) |
||
102 | |||
103 | /** |
||
104 | * Select one record from mongo collection. |
||
105 | * |
||
106 | * @param array $where Where conditions in array form. |
||
107 | * @param array $orderBy In a form of [key => direction]. |
||
108 | * |
||
109 | * @return RecordEntity|null |
||
110 | */ |
||
111 | public function findOne(array $where = [], array $orderBy = []) |
||
115 | |||
116 | /** |
||
117 | * Get associated record selection with pre-configured query (if any). |
||
118 | * |
||
119 | * @param array $where Where conditions in array form. |
||
120 | * |
||
121 | * @return RecordSelector |
||
122 | */ |
||
123 | public function find(array $where = []) |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function count() |
||
135 | |||
136 | /** |
||
137 | * @return RecordSelector |
||
138 | */ |
||
139 | final protected function selector() |
||
144 | |||
145 | /** |
||
146 | * @param RecordSelector $selector |
||
147 | */ |
||
148 | protected function setSelector(RecordSelector $selector) |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | protected function container() |
||
164 | } |
||
165 |
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.