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 |
||
22 | class Record extends RecordEntity implements ActiveEntityInterface |
||
23 | { |
||
24 | /** |
||
25 | * Static find methods. |
||
26 | */ |
||
27 | use FindTrait; |
||
28 | |||
29 | /** |
||
30 | * Indication that save methods must be validated by default, can be altered by calling save |
||
31 | * method with user arguments. |
||
32 | */ |
||
33 | const VALIDATE_SAVE = true; |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | * |
||
38 | * Create or update record data in database. Record will validate all EMBEDDED and loaded |
||
39 | * relations. |
||
40 | * |
||
41 | * @see sourceTable() |
||
42 | * @see updateChriteria() |
||
43 | * @param bool|null $validate Overwrite default option declared in VALIDATE_SAVE to force or |
||
44 | * disable validation before saving. |
||
45 | * @return bool |
||
46 | * @throws RecordException |
||
47 | * @throws QueryException |
||
48 | * @event saving() |
||
49 | * @event saved() |
||
50 | * @event updating() |
||
51 | * @event updated() |
||
52 | */ |
||
53 | public function save($validate = null) |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | * |
||
106 | * @event deleting() |
||
107 | * @event deleted() |
||
108 | */ |
||
109 | public function delete() |
||
123 | |||
124 | /** |
||
125 | * Instance of ORM Selector associated with specific document. |
||
126 | * |
||
127 | * @see Component::staticContainer() |
||
128 | * @param ORM $orm ORM component, global container will be called if not instance provided. |
||
129 | * @return RecordSource |
||
130 | * @throws ORMException |
||
131 | */ |
||
132 | View Code Duplication | public static function source(ORM $orm = null) |
|
146 | |||
147 | /** |
||
148 | * Save embedded relations. |
||
149 | * |
||
150 | * @param bool $validate |
||
151 | */ |
||
152 | private function saveRelations($validate) |
||
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.