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 |
||
18 | class ChangeTracker { |
||
19 | |||
20 | /** |
||
21 | * The global wpdb object. |
||
22 | * |
||
23 | * @var \wpdb |
||
24 | */ |
||
25 | protected $wpdb; |
||
26 | |||
27 | /** |
||
28 | * The ID of the currently-open changeset. |
||
29 | * |
||
30 | * @var integer |
||
31 | */ |
||
32 | private static $current_changeset_id = false; |
||
33 | |||
34 | /** |
||
35 | * The user comment on the currently-open changeset. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $current_changeset_comment = null; |
||
40 | |||
41 | /** |
||
42 | * The record prior to modification. |
||
43 | * |
||
44 | * @var \WordPress\Tabulate\DB\Record|false |
||
45 | */ |
||
46 | private $old_record = false; |
||
47 | |||
48 | /** |
||
49 | * Whether the changeset should be closed after the first after_save() call. |
||
50 | * |
||
51 | * @var boolean |
||
52 | */ |
||
53 | private static $keep_changeset_open = false; |
||
54 | |||
55 | /** |
||
56 | * Create a new change tracker. |
||
57 | * |
||
58 | * @param \wpdb $wpdb The global wpdb object. |
||
59 | * @param string $comment The user's comment about the change. |
||
60 | */ |
||
61 | public function __construct( $wpdb, $comment = null ) { |
||
65 | |||
66 | /** |
||
67 | * When destroying a ChangeTracker object, close the current changeset |
||
68 | * unless it has specifically been requested to be kept open. |
||
69 | */ |
||
70 | public function __destruct() { |
||
75 | |||
76 | /** |
||
77 | * Open a new changeset. If one is already open, this does nothing. |
||
78 | * |
||
79 | * @global \WP_User $current_user |
||
80 | * @param string $comment The user's comment on the changeset. |
||
81 | * @param boolean $keep_open Whether the changeset should be kept open (and manually closed) after after_save() is called. |
||
82 | * @throws Exception If the changeset row could not be saved. |
||
83 | */ |
||
84 | public function open_changeset( $comment, $keep_open = null ) { |
||
102 | |||
103 | /** |
||
104 | * Close the current changeset. |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function close_changeset() { |
||
112 | |||
113 | /** |
||
114 | * This method is called prior to a record being saved, and will open a new |
||
115 | * changeset if required, and save the old record for later use. |
||
116 | * |
||
117 | * @param Table $table The table into which the record is being saved. |
||
118 | * @param string $pk_value The primary key of the record being saved. May be null. |
||
119 | * @return boolean |
||
120 | */ |
||
121 | public function before_save( Table $table, $pk_value ) { |
||
133 | |||
134 | /** |
||
135 | * This method is called after a record has been saved, and is responsible |
||
136 | * for creating the actual change-tracking rows in the database. |
||
137 | * |
||
138 | * @param Table $table The table the record is being saved in. |
||
139 | * @param Record $new_record The record, after being saved. |
||
140 | * @return boolean |
||
141 | */ |
||
142 | public function after_save( Table $table, Record $new_record ) { |
||
180 | |||
181 | /** |
||
182 | * On plugin activation, create two new database tables. |
||
183 | * |
||
184 | * @param \wpdb $wpdb The global database object. |
||
185 | */ |
||
186 | public static function activate( \wpdb $wpdb ) { |
||
213 | |||
214 | /** |
||
215 | * Get the name of the changesets table. |
||
216 | * |
||
217 | * @global \WordPress\Tabulate\DB\wpdb $wpdb |
||
218 | * @return string |
||
219 | */ |
||
220 | public static function changesets_name() { |
||
224 | |||
225 | /** |
||
226 | * Get the name of the changes table. |
||
227 | * |
||
228 | * @global \WordPress\Tabulate\DB\wpdb $wpdb |
||
229 | * @return string |
||
230 | */ |
||
231 | public static function changes_name() { |
||
235 | |||
236 | /** |
||
237 | * Get a list of the names used by the change-tracking subsystem. |
||
238 | * |
||
239 | * @global wpdb $wpdb |
||
240 | * @return array|string |
||
241 | */ |
||
242 | public static function table_names() { |
||
245 | } |
||
246 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.