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 |
||
15 | class FormAnswerAdd extends Model |
||
16 | { |
||
17 | public $name; |
||
18 | public $email; |
||
19 | public $message; |
||
20 | |||
21 | /** @var \Apps\ActiveRecord\FeedbackPost $_post */ |
||
22 | protected $_post; |
||
23 | protected $_userId; |
||
24 | protected $_ip; |
||
25 | |||
26 | /** |
||
27 | * FormAnswerAdd constructor. Pass active record of comment post and user id |
||
28 | * @param $recordPost |
||
29 | * @param int $userId |
||
30 | */ |
||
31 | public function __construct($recordPost, $userId = 0) |
||
37 | |||
38 | /** |
||
39 | * Define local properties if user is authorized |
||
40 | */ |
||
41 | public function before() |
||
50 | |||
51 | /** |
||
52 | * Labels for display form |
||
53 | */ |
||
54 | View Code Duplication | public function labels() |
|
62 | |||
63 | /** |
||
64 | * Form validation rules |
||
65 | */ |
||
66 | View Code Duplication | public function rules() |
|
75 | |||
76 | /** |
||
77 | * Add new row to database and set post is unreaded |
||
78 | */ |
||
79 | public function make() |
||
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.