From ba5910f3bd4abe78b3a2499b55c183b90a9a7fd7 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Wed, 30 Jun 2021 16:00:24 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20Update=20rules=20for=20PHPMD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .drone.yml | 4 ++-- app/code/backup/BackupAbstract.php | 17 ++++++++++------- app/code/backup/Mysql.php | 6 ++++-- app/code/backup/SFTP.php | 9 +++++---- app/code/transport/Email.php | 10 ++++++---- app/code/transport/FTP.php | 12 +++++++----- app/code/transport/Folder.php | 7 ++++--- app/code/transport/SFTP.php | 12 +++++++----- phpmd.xml | 14 ++++++++++++++ 9 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 phpmd.xml diff --git a/.drone.yml b/.drone.yml index 4f42080..835daf3 100644 --- a/.drone.yml +++ b/.drone.yml @@ -10,6 +10,6 @@ steps: - name: test image: php:7 commands: - - vendor/bin/phpmd + - vendor/bin/phpmd app text phpmd.xml - vendor/bin/phpcs --standard=PSR2 app - - vendor/bin/phpcpd \ No newline at end of file + - vendor/bin/phpcpd app \ No newline at end of file diff --git a/app/code/backup/BackupAbstract.php b/app/code/backup/BackupAbstract.php index e759d9d..a9bc136 100644 --- a/app/code/backup/BackupAbstract.php +++ b/app/code/backup/BackupAbstract.php @@ -2,6 +2,9 @@ namespace Shikiryu\Backup\Backup; +use Exception; +use ZipArchive; + abstract class BackupAbstract { /** @@ -163,18 +166,18 @@ abstract class BackupAbstract * Enabled via options * * @return $this - * @throws \Exception + * @throws Exception */ protected function setOptionZip() { - $zip = new \ZipArchive(); + $zip = new ZipArchive(); // Zip name $zip_name = !empty($this->options['name']) ? $this->options['name'] : time(); $zip_name = sprintf('%s.zip', $zip_name); if (touch(TEMP_DIR . $zip_name) === false) { - throw new \Exception('Backup::Zip::Permission denied.'); + throw new Exception('Backup::Zip::Permission denied.'); } - if ($zip->open(TEMP_DIR . $zip_name, \ZipArchive::OVERWRITE) === true) { + if ($zip->open(TEMP_DIR . $zip_name, ZipArchive::OVERWRITE) === true) { foreach ($this->files_to_backup as $file => $name) { $zip->addFile($file, $name); // Adding files into zip } @@ -184,7 +187,7 @@ abstract class BackupAbstract } $zip->close(); } else { - throw new \Exception('Backup::Zip::Can\'t zip the given backup.'); + throw new Exception('Backup::Zip::Can\'t zip the given backup.'); } $this->files_to_backup = [TEMP_DIR . $zip_name => $zip_name]; @@ -260,14 +263,14 @@ abstract class BackupAbstract * * @param mixed $name option's name * - * @throws \Exception + * @throws Exception * * @SuppressWarnings("unused") */ protected function setOptionName($name) { if (empty($this->options['zip'])) { - throw new \Exception('name option is for zip only.'); + throw new Exception('name option is for zip only.'); } } } diff --git a/app/code/backup/Mysql.php b/app/code/backup/Mysql.php index e094f64..fc93a0e 100644 --- a/app/code/backup/Mysql.php +++ b/app/code/backup/Mysql.php @@ -2,12 +2,14 @@ namespace Shikiryu\Backup\Backup; +use PDO; + class Mysql extends BackupAbstract { use IsLocalTrait; /** - * @var $pdo \PDO + * @var $pdo PDO */ private $pdo; @@ -106,7 +108,7 @@ class Mysql extends BackupAbstract */ protected function preBuild() { - $this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->database, $this->login, $this->pwd); + $this->pdo = new PDO('mysql:host='.$this->host.';dbname='.$this->database, $this->login, $this->pwd); } /** diff --git a/app/code/backup/SFTP.php b/app/code/backup/SFTP.php index e27401d..4b9aa63 100644 --- a/app/code/backup/SFTP.php +++ b/app/code/backup/SFTP.php @@ -2,6 +2,7 @@ namespace Shikiryu\Backup\Backup; +use Exception; use phpseclib\Crypt\RSA; use phpseclib\Net\SFTP as LibSFTP; use phpseclib\Net\SSH2; @@ -28,7 +29,7 @@ class SFTP extends BackupAbstract public function __construct($config = []) { if (!isset($config['files'])) { - throw new \Exception('Files needs a "files" configuration.'); + throw new Exception('Files needs a "files" configuration.'); } $filesToBackup = $config['files']; if (!empty($filesToBackup) && is_array($filesToBackup)) { @@ -65,7 +66,7 @@ class SFTP extends BackupAbstract /** * @inheritDoc - * @throws \Exception + * @throws Exception */ public function isValid() { @@ -79,7 +80,7 @@ class SFTP extends BackupAbstract $this->password->loadKey(file_get_contents($this->key)); } if (!$this->connection->login($this->login, $this->password)) { - throw new \Exception(sprintf('I can\'t connect to the SFTP %s', $this->host)); + throw new Exception(sprintf('I can\'t connect to the SFTP %s', $this->host)); } $this->connection->enableQuietMode(); @@ -103,7 +104,7 @@ class SFTP extends BackupAbstract try { $tmp_backup = new Files(['files' => $tmp_files]); unset($tmp_files); - } catch (\Exception $e) { + } catch (Exception $e) { echo $e->getMessage(); } diff --git a/app/code/transport/Email.php b/app/code/transport/Email.php index f28f8e5..8c8602e 100644 --- a/app/code/transport/Email.php +++ b/app/code/transport/Email.php @@ -2,7 +2,9 @@ namespace Shikiryu\Backup\Transport; +use Exception; use Shikiryu\Backup\Backup\BackupAbstract; +use ZIPARCHIVE; class Email extends TransportAbstract { @@ -194,7 +196,7 @@ class Email extends TransportAbstract * * @see #mail * @return bool - * @throws \Exception + * @throws Exception */ public function send() { @@ -202,9 +204,9 @@ class Email extends TransportAbstract // TODO check if file is empty // Checking files are selected - $zip = new \ZipArchive(); // Load zip library + $zip = new ZipArchive(); // Load zip library $zip_name = time(). '.zip'; // Zip name - if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)===true) { + if ($zip->open(TEMP_DIR.$zip_name, ZIPARCHIVE::CREATE)===true) { if (!empty($this->files)) { foreach ($this->files as $file => $name) { $zip->addFile($file, $name); // Adding files into zip @@ -212,7 +214,7 @@ class Email extends TransportAbstract } $zip->close(); } else { - throw new \Exception('Transport::Email::Can\'t zip the given backup.'); + throw new Exception('Transport::Email::Can\'t zip the given backup.'); } $this->files = array(TEMP_DIR.$zip_name=>$zip_name); diff --git a/app/code/transport/FTP.php b/app/code/transport/FTP.php index d1b247a..f5dd9fd 100644 --- a/app/code/transport/FTP.php +++ b/app/code/transport/FTP.php @@ -2,6 +2,8 @@ namespace Shikiryu\Backup\Transport; +use Exception; + class Ftp extends TransportAbstract { @@ -26,13 +28,13 @@ class Ftp extends TransportAbstract $this->connection = ftp_connect($this->host); if ($this->connection === false) { - throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host)); + throw new Exception(sprintf('I can\'t connect to the FTP %s', $this->host)); } - $login = @ftp_login($this->connection, $this->login, $this->password); + $login = ftp_login($this->connection, $this->login, $this->password); if ($login === false) { $msg = sprintf('Connexion FTP %s refusée avec %s et %s', $this->host, $this->login, $this->password); - throw new \Exception($msg); + throw new Exception($msg); } $this->setFiles($this->backup->getFilesToBackup()); @@ -57,7 +59,7 @@ class Ftp extends TransportAbstract /** * @return bool - * @throws \Exception + * @throws Exception */ public function send() { @@ -89,7 +91,7 @@ class Ftp extends TransportAbstract } if (!$sent) { - throw new \Exception('At least an upload didnt work.'); + throw new Exception('At least an upload didnt work.'); } return $sent; diff --git a/app/code/transport/Folder.php b/app/code/transport/Folder.php index 9b23158..8a4447a 100644 --- a/app/code/transport/Folder.php +++ b/app/code/transport/Folder.php @@ -2,6 +2,7 @@ namespace Shikiryu\Backup\Transport; +use Exception; use Shikiryu\Backup\Backup\BackupAbstract; class Folder extends TransportAbstract @@ -25,7 +26,7 @@ class Folder extends TransportAbstract /** * @return bool * - * @throws \Exception + * @throws Exception */ public function send() { @@ -35,7 +36,7 @@ class Folder extends TransportAbstract if ($this->backup->isLocal()) { foreach ($this->backup->getFilesToBackup() as $file => $name) { if (copy($file, $this->folder . $name) === false) { - throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder)); + throw new Exception(sprintf('Copy of %s in %s failed', $name, $this->folder)); } } foreach ($this->backup->getStreamsToBackup() as $name => $file) { @@ -43,7 +44,7 @@ class Folder extends TransportAbstract $name = 'backup' . $name . '.txt'; } if (file_put_contents($this->folder . $name, $file) === false) { - throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder)); + throw new Exception(sprintf('Saving of %s in %s failed', $name, $this->folder)); } } } diff --git a/app/code/transport/SFTP.php b/app/code/transport/SFTP.php index 89978e2..bde83dc 100644 --- a/app/code/transport/SFTP.php +++ b/app/code/transport/SFTP.php @@ -2,7 +2,9 @@ namespace Shikiryu\Backup\Transport; +use Exception; use phpseclib\Net\SFTP as LibSFTP; +use Shikiryu\Backup\Backup\BackupAbstract; class Sftp extends TransportAbstract { @@ -20,9 +22,9 @@ class Sftp extends TransportAbstract private $connection; /** - * @param \Shikiryu\Backup\Backup\BackupAbstract $backup + * @param BackupAbstract $backup * @param array $config - * @throws \Exception + * @throws Exception */ public function __construct($backup, $config) { @@ -30,14 +32,14 @@ class Sftp extends TransportAbstract $this->connection = new LibSFTP($this->host, $this->port); if (!$this->connection->login($this->login, $this->password)) { - throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host)); + throw new Exception(sprintf('I can\'t connect to the FTP %s', $this->host)); } } /** * @return bool * - * @throws \Exception + * @throws Exception */ public function send() { @@ -65,7 +67,7 @@ class Sftp extends TransportAbstract } if (!$sent) { - throw new \Exception('At least an upload didnt work.'); + throw new Exception('At least an upload didnt work.'); } return $sent; } diff --git a/phpmd.xml b/phpmd.xml new file mode 100644 index 0000000..1fb0429 --- /dev/null +++ b/phpmd.xml @@ -0,0 +1,14 @@ + + + The coding standard for PHPMD. + + + + + + + + + + +