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 |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $sqlFile; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $sqlContent; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var \CI_DB_active_record |
||
| 23 | */ |
||
| 24 | protected $db; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var name of module folder |
||
| 28 | * used in self::setAutoload() |
||
| 29 | */ |
||
| 30 | protected $moduleName = 'xbanners'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $bannerImagesFolder; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $currentTemplate; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Regular expression for selecting all CREATE queries from sql string |
||
| 44 | */ |
||
| 45 | const DROP_PATTERN = '/DROP TABLE IF EXISTS \`[0-9a-z\_]+\`\;/i'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Regular expression for selecting all DROP queries from sql string |
||
| 49 | */ |
||
| 50 | const CREATE_EXPRESSION = '/CREATE TABLE \`[0-9a-z\_]+\`[\s\S]+?\;/i'; |
||
| 51 | |||
| 52 | public function __construct() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Create all tebles used in module |
||
| 62 | * |
||
| 63 | * @return boolean |
||
| 64 | */ |
||
| 65 | public function install() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Drop all tebles used in module |
||
| 74 | * |
||
| 75 | * @return boolean |
||
| 76 | */ |
||
| 77 | public function deinstall() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param boolean $bool |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function setAutoload($bool) { |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param boolean $bool |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function setEnabled($bool) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Drop module row from components |
||
| 106 | * |
||
| 107 | * @return boolean |
||
| 108 | */ |
||
| 109 | public function dropModuleFromComponents() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Write places from "template_places.php" to db |
||
| 115 | */ |
||
| 116 | public function installTemplatePlaces() { |
||
| 119 | |||
| 120 | public function updateTemplatePlaces() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return boolean |
||
| 126 | */ |
||
| 127 | protected function makeBannersDirectory() { |
||
| 134 | |||
| 135 | protected function removeBannersDirectory() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get content of sql file |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | protected function getSqlFile() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Run self::getQueries() with DROP_PATTERN |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | protected function getDropQueries() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Run self::getQueries() with CREATE_EXPRESSION |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | protected function getCreateQueries() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Search all queries by passed pattern |
||
| 171 | * |
||
| 172 | * @param string $pattern |
||
| 173 | * @return boolean|array |
||
| 174 | */ |
||
| 175 | protected function getQueries($pattern) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Execute array of queries (DROP OR CREATE) |
||
| 187 | * |
||
| 188 | * @param array $queries |
||
| 189 | * @return bool Returns true only if all queries are successfull |
||
| 190 | * @throws \Exception |
||
| 191 | */ |
||
| 192 | protected function executeQueries(array $queries) { |
||
| 204 | |||
| 205 | } |