| Conditions | 24 |
| Paths | 9792 |
| Total Lines | 134 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 112 | public function backup() |
||
| 113 | { |
||
| 114 | $this->addGeneralCssFile(); |
||
| 115 | |||
| 116 | $this->view->designSecurity = new HtmlSecurity; // Security Design Class |
||
| 117 | |||
| 118 | $this->sTitle = t('Backup Management'); |
||
| 119 | $this->view->page_title = $this->sTitle; |
||
| 120 | $this->view->h1_title = $this->sTitle; |
||
| 121 | |||
| 122 | $aDumpList = $this->file->getFileList(PH7_PATH_BACKUP_SQL, static::BACKUP_FILE_EXTS); |
||
| 123 | |||
| 124 | // Removing the path |
||
| 125 | $aDumpList = array_map(function ($sValue) { |
||
| 126 | return str_replace(PH7_PATH_BACKUP_SQL, '', $sValue); |
||
| 127 | }, $aDumpList); |
||
| 128 | $this->view->aDumpList = $aDumpList; |
||
| 129 | |||
| 130 | |||
| 131 | $oSecurityToken = new Token; |
||
| 132 | |||
| 133 | if ($this->httpRequest->postExists('backup')) { |
||
| 134 | if (!$oSecurityToken->check('backup')) { |
||
| 135 | $this->design->setFlashMsg(Form::errorTokenMsg(), Design::ERROR_TYPE); |
||
| 136 | } else { |
||
| 137 | // Clean the site name to avoid bug with the backup path |
||
| 138 | $sSiteName = str_replace([' ', '/', '\\'], '_', $this->registry->site_name); |
||
| 139 | $sCurrentDate = (new CDateTime)->get()->date(); |
||
| 140 | |||
| 141 | switch ($this->httpRequest->post('backup_type')) { |
||
| 142 | case 'server': |
||
| 143 | $sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . $sCurrentDate . '.sql'; |
||
| 144 | (new Backup($sFullPath))->back()->save(); |
||
| 145 | $this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath); |
||
| 146 | break; |
||
| 147 | |||
| 148 | case 'server_archive': |
||
| 149 | $sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . $sCurrentDate . '.sql.gz'; |
||
| 150 | (new Backup($sFullPath))->back()->saveArchive(); |
||
| 151 | $this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath); |
||
| 152 | break; |
||
| 153 | |||
| 154 | case 'client': |
||
| 155 | (new Backup($sSiteName . '_' . $sCurrentDate . '.sql'))->back()->download(); |
||
| 156 | break; |
||
| 157 | |||
| 158 | case 'client_archive': |
||
| 159 | (new Backup($sSiteName . '_' . $sCurrentDate . '.sql.gz'))->back()->downloadArchive(); |
||
| 160 | break; |
||
| 161 | |||
| 162 | case 'show': |
||
| 163 | $this->view->sql_content = (new Backup)->back()->show(); |
||
| 164 | break; |
||
| 165 | |||
| 166 | default: |
||
| 167 | $this->design->setFlashMsg( |
||
| 168 | t('Please select a field.'), |
||
| 169 | Design::ERROR_TYPE |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | if ($this->httpRequest->postExists('restore_dump')) { |
||
| 176 | if (!$oSecurityToken->check('backup')) { |
||
| 177 | $this->design->setFlashMsg(Form::errorTokenMsg(), Design::ERROR_TYPE); |
||
| 178 | } else { |
||
| 179 | $sDumpFile = $this->httpRequest->post('dump_file'); |
||
| 180 | |||
| 181 | if (!empty($sDumpFile)) { |
||
| 182 | if ($this->file->getFileExt($sDumpFile) === Backup::SQL_FILE_EXT) { |
||
| 183 | $mStatus = (new Backup($sDumpFile))->restore(); |
||
| 184 | } elseif ($this->file->getFileExt($sDumpFile) === Backup::ARCHIVE_FILE_EXT) { |
||
| 185 | $mStatus = (new Backup(PH7_PATH_BACKUP_SQL . $sDumpFile))->restoreArchive(); |
||
| 186 | } else { |
||
| 187 | $mStatus = t('Dump file must be a SQL type (extension ".sql" or compressed archive ".gz")'); |
||
| 188 | } |
||
| 189 | } else { |
||
| 190 | $mStatus = t('Please select a dump file.'); |
||
| 191 | } |
||
| 192 | |||
| 193 | $sMsg = ($mStatus === true) ? t('Data successfully restored from server!') : $mStatus; |
||
| 194 | $sMsgType = ($mStatus === true) ? Design::SUCCESS_TYPE : Design::ERROR_TYPE; |
||
| 195 | $this->design->setFlashMsg($sMsg, $sMsgType); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | if ($this->httpRequest->postExists('remove_dump')) { |
||
| 200 | if (!$oSecurityToken->check('backup')) { |
||
| 201 | $this->design->setFlashMsg(Form::errorTokenMsg(), Design::ERROR_TYPE); |
||
| 202 | } else { |
||
| 203 | $sDumpFile = $this->httpRequest->post('dump_file'); |
||
| 204 | |||
| 205 | if (!empty($sDumpFile)) { |
||
| 206 | $this->file->deleteFile(PH7_PATH_BACKUP_SQL . $sDumpFile); |
||
| 207 | $this->design->setFlashMsg(t('Dump file successfully deleted!')); |
||
| 208 | } else { |
||
| 209 | $this->design->setFlashMsg( |
||
| 210 | t('Please select a dump file.'), |
||
| 211 | Design::ERROR_TYPE |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | unset($oSecurityToken); |
||
| 218 | |||
| 219 | |||
| 220 | if ($this->httpRequest->postExists('restore_sql_file')) { |
||
| 221 | if (!empty($_FILES['sql_file']['tmp_name'])) { |
||
| 222 | $sNameFile = $_FILES['sql_file']['name']; |
||
| 223 | $sTmpFile = $_FILES['sql_file']['tmp_name']; |
||
| 224 | |||
| 225 | if ($this->file->getFileExt($sNameFile) === Backup::SQL_FILE_EXT) { |
||
| 226 | $mStatus = (new Backup($sTmpFile))->restore(); |
||
| 227 | } elseif ($this->file->getFileExt($sNameFile) === Backup::ARCHIVE_FILE_EXT) { |
||
| 228 | $mStatus = (new Backup($sTmpFile))->restoreArchive(); |
||
| 229 | } else { |
||
| 230 | $mStatus = t('Dump file must be a SQL type (extension ".sql" or compressed archive ".gz")'); |
||
| 231 | } |
||
| 232 | |||
| 233 | // Remove the temporary file |
||
| 234 | $this->file->deleteFile($sTmpFile); |
||
| 235 | } else { |
||
| 236 | $mStatus = t('Please select a dump SQL file.'); |
||
| 237 | } |
||
| 238 | |||
| 239 | $sMsg = ($mStatus === true) ? t('Data successfully restored from desktop!') : $mStatus; |
||
| 240 | $sMsgType = ($mStatus === true) ? Design::SUCCESS_TYPE : Design::ERROR_TYPE; |
||
| 241 | $this->design->setFlashMsg($sMsg, $sMsgType); |
||
| 242 | } |
||
| 243 | |||
| 244 | $this->output(); |
||
| 245 | } |
||
| 246 | |||
| 304 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: