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 |
||
11 | class Comment_Meta_Container extends Container { |
||
12 | protected $comment_id; |
||
13 | |||
14 | /** |
||
15 | * Create a new comment meta container |
||
16 | * |
||
17 | * @param string $title Unique title of the container |
||
18 | **/ |
||
19 | public function __construct( $title ) { |
||
26 | |||
27 | /** |
||
28 | * Perform instance initialization after calling setup() |
||
29 | **/ |
||
30 | public function init() { |
||
38 | |||
39 | /** |
||
40 | * Checks whether the current request is valid |
||
41 | * |
||
42 | * @return bool |
||
43 | **/ |
||
44 | public function is_valid_save() { |
||
51 | |||
52 | /** |
||
53 | * Add meta box to the comment |
||
54 | **/ |
||
55 | public function attach() { |
||
65 | |||
66 | /** |
||
67 | * Revert the result of attach() |
||
68 | **/ |
||
69 | View Code Duplication | public function detach() { |
|
80 | |||
81 | /** |
||
82 | * Output the container markup |
||
83 | **/ |
||
84 | public function render() { |
||
87 | |||
88 | /** |
||
89 | * Set the comment ID the container will operate with. |
||
90 | * |
||
91 | * @param int $comment_id |
||
92 | **/ |
||
93 | public function set_comment_id( $comment_id ) { |
||
97 | |||
98 | /** |
||
99 | * Perform save operation after successful is_valid_save() check. |
||
100 | * The call is propagated to all fields in the container. |
||
101 | * |
||
102 | * @param int $comment_id ID of the comment against which save() is ran |
||
103 | **/ |
||
104 | public function save( $comment_id ) { |
||
116 | |||
117 | /** |
||
118 | * Perform checks whether there is a field registered with the name $name. |
||
119 | * If not, the field name is recorded. |
||
120 | * |
||
121 | * @param string $name |
||
122 | **/ |
||
123 | public function verify_unique_field_name( $name ) { |
||
134 | |||
135 | /** |
||
136 | * Remove field name $name from the list of unique field names |
||
137 | * |
||
138 | * @param string $name |
||
139 | **/ |
||
140 | public function drop_unique_field_name( $name ) { |
||
146 | |||
147 | } |