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 |
||
3 | View Code Duplication | class Ressource extends \Phalcon\Mvc\Model |
|
4 | { |
||
5 | |||
6 | /** |
||
7 | * |
||
8 | * @var integer |
||
9 | */ |
||
10 | protected $id; |
||
11 | |||
12 | /** |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $libelle; |
||
17 | |||
18 | /** |
||
19 | * Method to set the value of field id |
||
20 | * |
||
21 | * @param integer $id |
||
22 | * @return $this |
||
23 | */ |
||
24 | public function setId($id) |
||
30 | |||
31 | /** |
||
32 | * Method to set the value of field libelle |
||
33 | * |
||
34 | * @param string $libelle |
||
35 | * @return $this |
||
36 | */ |
||
37 | public function setLibelle($libelle) |
||
43 | |||
44 | /** |
||
45 | * Returns the value of field id |
||
46 | * |
||
47 | * @return integer |
||
48 | */ |
||
49 | public function getId() |
||
53 | |||
54 | /** |
||
55 | * Returns the value of field libelle |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function getLibelle() |
||
63 | |||
64 | /** |
||
65 | * Initialize method for model. |
||
66 | */ |
||
67 | public function initialize() |
||
71 | |||
72 | /** |
||
73 | * Returns table name mapped in the model. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getSource() |
||
81 | |||
82 | /** |
||
83 | * Allows to query a set of records that match the specified conditions |
||
84 | * |
||
85 | * @param mixed $parameters |
||
86 | * @return Ressource[] |
||
87 | */ |
||
88 | public static function find($parameters = null) |
||
92 | |||
93 | /** |
||
94 | * Allows to query the first record that match the specified conditions |
||
95 | * |
||
96 | * @param mixed $parameters |
||
97 | * @return Ressource |
||
98 | */ |
||
99 | public static function findFirst($parameters = null) |
||
103 | |||
104 | public function toString() { |
||
107 | |||
108 | } |
||
109 |
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.