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 |
||
23 | class bbcodes_installer extends acp_manager |
||
24 | { |
||
25 | /** @var \acp_bbcodes */ |
||
26 | protected $acp_bbcodes; |
||
27 | |||
28 | /** @var string */ |
||
29 | protected $phpbb_root_path; |
||
30 | |||
31 | /** @var string */ |
||
32 | protected $php_ext; |
||
33 | |||
34 | /** |
||
35 | * Constructor |
||
36 | * |
||
37 | * @param driver_interface $db |
||
38 | * @param helper $group_helper |
||
39 | * @param language $language |
||
40 | * @param request $request |
||
41 | * @param string $phpbb_root_path |
||
42 | * @param string $php_ext |
||
43 | 2 | * @access public |
|
44 | */ |
||
45 | 2 | public function __construct(driver_interface $db, helper $group_helper, language $language, request $request, $phpbb_root_path, $php_ext) |
|
46 | { |
||
47 | 2 | parent::__construct($db, $group_helper, $language, $request); |
|
48 | 2 | ||
49 | $this->phpbb_root_path = $phpbb_root_path; |
||
50 | 2 | $this->php_ext = $php_ext; |
|
51 | 2 | ||
52 | $this->acp_bbcodes = $this->get_acp_bbcodes(); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Installs bbcodes, used by migrations to perform add/updates |
||
57 | * |
||
58 | * @param array $bbcodes Array of bbcodes to install |
||
59 | 2 | * @access public |
|
60 | */ |
||
61 | 2 | public function install_bbcodes(array $bbcodes) |
|
62 | { |
||
63 | 2 | foreach ($bbcodes as $bbcode_name => $bbcode_data) |
|
64 | { |
||
65 | 2 | $bbcode_data = $this->build_bbcode($bbcode_data); |
|
66 | 2 | ||
67 | 1 | if ($bbcode = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag'])) |
|
68 | 1 | { |
|
69 | $this->update_bbcode($bbcode, $bbcode_data); |
||
70 | continue; |
||
71 | 2 | } |
|
72 | 2 | ||
73 | $this->add_bbcode($bbcode_data); |
||
74 | 2 | } |
|
75 | 2 | ||
76 | $this->resynchronize_bbcode_order(); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Deletes bbcodes, used by migrations to perform add/updates |
||
81 | * |
||
82 | * @param array $bbcodes Array of bbcodes to delete |
||
83 | 2 | * @access public |
|
84 | */ |
||
85 | 2 | public function delete_bbcodes(array $bbcodes) |
|
103 | |||
104 | 2 | /** |
|
105 | 2 | * Get the acp_bbcodes class |
|
106 | 2 | * |
|
107 | 2 | * @return \acp_bbcodes |
|
108 | 2 | * @access protected |
|
109 | 2 | */ |
|
110 | 2 | protected function get_acp_bbcodes() |
|
111 | { |
||
112 | 2 | if (!class_exists('acp_bbcodes')) |
|
113 | { |
||
114 | include $this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext; |
||
115 | } |
||
116 | |||
117 | return new \acp_bbcodes(); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | 2 | * Build the bbcode |
|
122 | * |
||
123 | 2 | * @param array $bbcode_data Initial bbcode data |
|
124 | * @return array Complete bbcode data array |
||
125 | * @access protected |
||
126 | */ |
||
127 | protected function build_bbcode(array $bbcode_data) |
||
128 | { |
||
129 | $data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']); |
||
130 | |||
131 | $bbcode_data = array_replace($bbcode_data, array( |
||
132 | 'bbcode_tag' => $data['bbcode_tag'], |
||
133 | 'first_pass_match' => $data['first_pass_match'], |
||
134 | 2 | 'first_pass_replace' => $data['first_pass_replace'], |
|
135 | 'second_pass_match' => $data['second_pass_match'], |
||
136 | 'second_pass_replace' => $data['second_pass_replace'], |
||
137 | 2 | )); |
|
138 | 2 | ||
139 | 2 | return $bbcode_data; |
|
140 | 2 | } |
|
141 | 2 | ||
142 | 2 | /** |
|
143 | * Get the max bbcode id value |
||
144 | 2 | * |
|
145 | * @return int bbcode identifier |
||
146 | * @access protected |
||
147 | */ |
||
148 | protected function get_max_bbcode_id() |
||
152 | |||
153 | /** |
||
154 | 1 | * Check if bbcode exists |
|
155 | * |
||
156 | 1 | * @param string $bbcode_name Name of bbcode |
|
157 | 1 | * @param string $bbcode_tag Tag name of bbcode |
|
158 | 1 | * @return mixed Existing bbcode data array or false if not found |
|
159 | 1 | * @access protected |
|
160 | 1 | */ |
|
161 | public function bbcode_exists($bbcode_name, $bbcode_tag) |
||
162 | { |
||
163 | $sql = 'SELECT bbcode_id |
||
164 | FROM ' . BBCODES_TABLE . " |
||
165 | WHERE LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_name)) . "' |
||
166 | OR LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_tag)) . "'"; |
||
167 | $result = $this->db->sql_query($sql); |
||
168 | 2 | $row = $this->db->sql_fetchrow($result); |
|
169 | $this->db->sql_freeresult($result); |
||
170 | 2 | ||
171 | return $row ? (array) $row : false; |
||
172 | 2 | } |
|
173 | 2 | ||
174 | 1 | /** |
|
175 | 1 | * Update existing bbcode |
|
176 | * |
||
177 | 2 | * @param array $old_bbcode Existing bbcode data |
|
178 | 2 | * @param array $new_bbcode New bbcode data |
|
179 | 2 | * @access protected |
|
180 | */ |
||
181 | 2 | View Code Duplication | protected function update_bbcode(array $old_bbcode, array $new_bbcode) |
182 | { |
||
183 | 2 | $sql = 'UPDATE ' . BBCODES_TABLE . ' |
|
184 | 2 | SET ' . $this->db->sql_build_array('UPDATE', $new_bbcode) . ' |
|
185 | 2 | WHERE bbcode_id = ' . (int) $old_bbcode['bbcode_id']; |
|
186 | $this->db->sql_query($sql); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Add new bbcode |
||
191 | * |
||
192 | * @param array $bbcode_data New bbcode data |
||
193 | * @access protected |
||
194 | */ |
||
195 | protected function add_bbcode(array $bbcode_data) |
||
213 | } |
||
214 |