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 |
||
| 14 | abstract class AppAbstract |
||
| 15 | { |
||
| 16 | public $code; |
||
| 17 | public $title; |
||
| 18 | public $vendor; |
||
| 19 | public $version; |
||
| 20 | public $modules = []; |
||
| 21 | |||
| 22 | public $db; |
||
| 23 | public $lang; |
||
| 24 | |||
| 25 | abstract protected function init(); |
||
| 26 | |||
| 27 | final public function __construct() { |
||
| 35 | |||
| 36 | View Code Duplication | final public function link() |
|
| 55 | |||
| 56 | View Code Duplication | final public function redirect() |
|
| 75 | |||
| 76 | final public function getCode() |
||
| 80 | |||
| 81 | final public function getVendor() |
||
| 85 | |||
| 86 | final public function getTitle() |
||
| 90 | |||
| 91 | final public function getVersion() |
||
| 95 | |||
| 96 | final public function getModules() |
||
| 100 | |||
| 101 | final public function hasModule($module, $type) |
||
| 104 | |||
| 105 | final private function setInfo() |
||
| 124 | |||
| 125 | final public function getDef() |
||
| 143 | |||
| 144 | final public function definitionsExist($group, $language_code = null) |
||
| 145 | { |
||
| 146 | $language_code = isset($language_code) && $this->lang->exists($language_code) ? $language_code : $this->lang->get('code'); |
||
| 147 | |||
| 148 | $pathname = OSCOM::BASE_DIR . 'Apps/' . $this->vendor . '/' . $this->code . '/languages/' . $this->lang->get('directory', $language_code) . '/' . $group . '.txt'; |
||
| 149 | |||
| 150 | if (is_file($pathname)) { |
||
| 151 | return true; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($language_code != 'en') { |
||
| 155 | return call_user_func([$this, __FUNCTION__], $group, 'en'); |
||
| 156 | } |
||
| 157 | |||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | final public function loadDefinitions($group, $language_code = null) |
||
| 162 | { |
||
| 163 | $language_code = isset($language_code) && $this->lang->exists($language_code) ? $language_code : $this->lang->get('code'); |
||
| 164 | |||
| 165 | if ($language_code != 'en') { |
||
| 166 | $this->loadDefinitions($group, 'en'); |
||
| 167 | } |
||
| 168 | |||
| 169 | $pathname = OSCOM::BASE_DIR . 'Apps/' . $this->vendor . '/' . $this->code . '/languages/' . $this->lang->get('directory', $language_code) . '/' . $group . '.txt'; |
||
| 170 | |||
| 171 | $group = 'Apps/' . $this->vendor . '/' . $this->code . '/' . $group; |
||
| 172 | |||
| 173 | $defs = $this->lang->getDefinitions($group, $language_code, $pathname); |
||
| 174 | |||
| 175 | $this->lang->injectDefinitions($defs, $this->vendor . '-' . $this->code); |
||
| 176 | } |
||
| 177 | |||
| 178 | final public function saveCfgParam($key, $value, $title = null, $description = null, $set_func = null) |
||
| 218 | |||
| 219 | final public function deleteCfgParam($key) |
||
| 225 | } |
||
| 226 |
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.