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 |
||
| 22 | class bbcodes_installer extends acp_manager |
||
| 23 | { |
||
| 24 | /** @var \acp_bbcodes */ |
||
| 25 | protected $acp_bbcodes; |
||
| 26 | |||
| 27 | /** @var string */ |
||
| 28 | protected $phpbb_root_path; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | protected $php_ext; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Constructor |
||
| 35 | * |
||
| 36 | * @param driver_interface $db |
||
| 37 | * @param request $request |
||
| 38 | * @param user $user |
||
| 39 | * @param string $phpbb_root_path |
||
| 40 | * @param string $php_ext |
||
| 41 | * @access public |
||
| 42 | */ |
||
| 43 | 2 | public function __construct(driver_interface $db, request $request, user $user, $phpbb_root_path, $php_ext) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Installs bbcodes, used by migrations to perform add/updates |
||
| 55 | * |
||
| 56 | * @param array $bbcodes Array of bbcodes to install |
||
| 57 | * @return null |
||
| 58 | * @access public |
||
| 59 | */ |
||
| 60 | 2 | public function install_bbcodes(array $bbcodes) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Get the acp_bbcodes class |
||
| 81 | * |
||
| 82 | * @return \acp_bbcodes |
||
| 83 | * @access protected |
||
| 84 | */ |
||
| 85 | 2 | protected function get_acp_bbcodes() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Build the bbcode |
||
| 97 | * |
||
| 98 | * @param array $bbcode_data Initial bbcode data |
||
| 99 | * @return array Complete bbcode data array |
||
| 100 | * @access protected |
||
| 101 | */ |
||
| 102 | 2 | protected function build_bbcode(array $bbcode_data) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Get the max bbcode id value |
||
| 119 | * |
||
| 120 | * @return int bbcode identifier |
||
| 121 | * @access protected |
||
| 122 | */ |
||
| 123 | 2 | View Code Duplication | protected function get_max_bbcode_id() |
| 133 | |||
| 134 | /** |
||
| 135 | * Check if bbcode exists |
||
| 136 | * |
||
| 137 | * @param string $bbcode_name Name of bbcode |
||
| 138 | * @param string $bbcode_tag Tag name of bbcode |
||
| 139 | * @return mixed Existing bbcode data array or false if not found |
||
| 140 | * @access protected |
||
| 141 | */ |
||
| 142 | 2 | protected function bbcode_exists($bbcode_name, $bbcode_tag) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Update existing bbcode |
||
| 157 | * |
||
| 158 | * @param array $old_bbcode Existing bbcode data |
||
| 159 | * @param array $new_bbcode New bbcode data |
||
| 160 | * @return null |
||
| 161 | * @access protected |
||
| 162 | */ |
||
| 163 | 1 | protected function update_bbcode(array $old_bbcode, array $new_bbcode) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Add new bbcode |
||
| 173 | * |
||
| 174 | * @param array $bbcode_data New bbcode data |
||
| 175 | * @return null |
||
| 176 | * @access protected |
||
| 177 | */ |
||
| 178 | 2 | protected function add_bbcode(array $bbcode_data) |
|
| 196 | } |
||
| 197 |
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.