💚 Update rules for PHPMD
continuous-integration/drone/push Build is passing Details
continuous-integration/drone Build is passing Details

This commit is contained in:
Clement Desmidt 2021-06-30 16:00:24 +02:00
parent aa9127262f
commit ba5910f3bd
9 changed files with 59 additions and 32 deletions

View File

@ -10,6 +10,6 @@ steps:
- name: test - name: test
image: php:7 image: php:7
commands: commands:
- vendor/bin/phpmd - vendor/bin/phpmd app text phpmd.xml
- vendor/bin/phpcs --standard=PSR2 app - vendor/bin/phpcs --standard=PSR2 app
- vendor/bin/phpcpd - vendor/bin/phpcpd app

View File

@ -2,6 +2,9 @@
namespace Shikiryu\Backup\Backup; namespace Shikiryu\Backup\Backup;
use Exception;
use ZipArchive;
abstract class BackupAbstract abstract class BackupAbstract
{ {
/** /**
@ -163,18 +166,18 @@ abstract class BackupAbstract
* Enabled via options * Enabled via options
* *
* @return $this * @return $this
* @throws \Exception * @throws Exception
*/ */
protected function setOptionZip() protected function setOptionZip()
{ {
$zip = new \ZipArchive(); $zip = new ZipArchive();
// Zip name // Zip name
$zip_name = !empty($this->options['name']) ? $this->options['name'] : time(); $zip_name = !empty($this->options['name']) ? $this->options['name'] : time();
$zip_name = sprintf('%s.zip', $zip_name); $zip_name = sprintf('%s.zip', $zip_name);
if (touch(TEMP_DIR . $zip_name) === false) { 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) { foreach ($this->files_to_backup as $file => $name) {
$zip->addFile($file, $name); // Adding files into zip $zip->addFile($file, $name); // Adding files into zip
} }
@ -184,7 +187,7 @@ abstract class BackupAbstract
} }
$zip->close(); $zip->close();
} else { } 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]; $this->files_to_backup = [TEMP_DIR . $zip_name => $zip_name];
@ -260,14 +263,14 @@ abstract class BackupAbstract
* *
* @param mixed $name option's name * @param mixed $name option's name
* *
* @throws \Exception * @throws Exception
* *
* @SuppressWarnings("unused") * @SuppressWarnings("unused")
*/ */
protected function setOptionName($name) protected function setOptionName($name)
{ {
if (empty($this->options['zip'])) { if (empty($this->options['zip'])) {
throw new \Exception('name option is for zip only.'); throw new Exception('name option is for zip only.');
} }
} }
} }

View File

@ -2,12 +2,14 @@
namespace Shikiryu\Backup\Backup; namespace Shikiryu\Backup\Backup;
use PDO;
class Mysql extends BackupAbstract class Mysql extends BackupAbstract
{ {
use IsLocalTrait; use IsLocalTrait;
/** /**
* @var $pdo \PDO * @var $pdo PDO
*/ */
private $pdo; private $pdo;
@ -106,7 +108,7 @@ class Mysql extends BackupAbstract
*/ */
protected function preBuild() 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);
} }
/** /**

View File

@ -2,6 +2,7 @@
namespace Shikiryu\Backup\Backup; namespace Shikiryu\Backup\Backup;
use Exception;
use phpseclib\Crypt\RSA; use phpseclib\Crypt\RSA;
use phpseclib\Net\SFTP as LibSFTP; use phpseclib\Net\SFTP as LibSFTP;
use phpseclib\Net\SSH2; use phpseclib\Net\SSH2;
@ -28,7 +29,7 @@ class SFTP extends BackupAbstract
public function __construct($config = []) public function __construct($config = [])
{ {
if (!isset($config['files'])) { if (!isset($config['files'])) {
throw new \Exception('Files needs a "files" configuration.'); throw new Exception('Files needs a "files" configuration.');
} }
$filesToBackup = $config['files']; $filesToBackup = $config['files'];
if (!empty($filesToBackup) && is_array($filesToBackup)) { if (!empty($filesToBackup) && is_array($filesToBackup)) {
@ -65,7 +66,7 @@ class SFTP extends BackupAbstract
/** /**
* @inheritDoc * @inheritDoc
* @throws \Exception * @throws Exception
*/ */
public function isValid() public function isValid()
{ {
@ -79,7 +80,7 @@ class SFTP extends BackupAbstract
$this->password->loadKey(file_get_contents($this->key)); $this->password->loadKey(file_get_contents($this->key));
} }
if (!$this->connection->login($this->login, $this->password)) { 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(); $this->connection->enableQuietMode();
@ -103,7 +104,7 @@ class SFTP extends BackupAbstract
try { try {
$tmp_backup = new Files(['files' => $tmp_files]); $tmp_backup = new Files(['files' => $tmp_files]);
unset($tmp_files); unset($tmp_files);
} catch (\Exception $e) { } catch (Exception $e) {
echo $e->getMessage(); echo $e->getMessage();
} }

View File

@ -2,7 +2,9 @@
namespace Shikiryu\Backup\Transport; namespace Shikiryu\Backup\Transport;
use Exception;
use Shikiryu\Backup\Backup\BackupAbstract; use Shikiryu\Backup\Backup\BackupAbstract;
use ZIPARCHIVE;
class Email extends TransportAbstract class Email extends TransportAbstract
{ {
@ -194,7 +196,7 @@ class Email extends TransportAbstract
* *
* @see #mail * @see #mail
* @return bool * @return bool
* @throws \Exception * @throws Exception
*/ */
public function send() public function send()
{ {
@ -202,9 +204,9 @@ class Email extends TransportAbstract
// TODO check if file is empty // TODO check if file is empty
// Checking files are selected // Checking files are selected
$zip = new \ZipArchive(); // Load zip library $zip = new ZipArchive(); // Load zip library
$zip_name = time(). '.zip'; // Zip name $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)) { if (!empty($this->files)) {
foreach ($this->files as $file => $name) { foreach ($this->files as $file => $name) {
$zip->addFile($file, $name); // Adding files into zip $zip->addFile($file, $name); // Adding files into zip
@ -212,7 +214,7 @@ class Email extends TransportAbstract
} }
$zip->close(); $zip->close();
} else { } 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); $this->files = array(TEMP_DIR.$zip_name=>$zip_name);

View File

@ -2,6 +2,8 @@
namespace Shikiryu\Backup\Transport; namespace Shikiryu\Backup\Transport;
use Exception;
class Ftp extends TransportAbstract class Ftp extends TransportAbstract
{ {
@ -26,13 +28,13 @@ class Ftp extends TransportAbstract
$this->connection = ftp_connect($this->host); $this->connection = ftp_connect($this->host);
if ($this->connection === false) { 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) { if ($login === false) {
$msg = sprintf('Connexion FTP %s refusée avec %s et %s', $this->host, $this->login, $this->password); $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()); $this->setFiles($this->backup->getFilesToBackup());
@ -57,7 +59,7 @@ class Ftp extends TransportAbstract
/** /**
* @return bool * @return bool
* @throws \Exception * @throws Exception
*/ */
public function send() public function send()
{ {
@ -89,7 +91,7 @@ class Ftp extends TransportAbstract
} }
if (!$sent) { if (!$sent) {
throw new \Exception('At least an upload didnt work.'); throw new Exception('At least an upload didnt work.');
} }
return $sent; return $sent;

View File

@ -2,6 +2,7 @@
namespace Shikiryu\Backup\Transport; namespace Shikiryu\Backup\Transport;
use Exception;
use Shikiryu\Backup\Backup\BackupAbstract; use Shikiryu\Backup\Backup\BackupAbstract;
class Folder extends TransportAbstract class Folder extends TransportAbstract
@ -25,7 +26,7 @@ class Folder extends TransportAbstract
/** /**
* @return bool * @return bool
* *
* @throws \Exception * @throws Exception
*/ */
public function send() public function send()
{ {
@ -35,7 +36,7 @@ class Folder extends TransportAbstract
if ($this->backup->isLocal()) { if ($this->backup->isLocal()) {
foreach ($this->backup->getFilesToBackup() as $file => $name) { foreach ($this->backup->getFilesToBackup() as $file => $name) {
if (copy($file, $this->folder . $name) === false) { 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) { foreach ($this->backup->getStreamsToBackup() as $name => $file) {
@ -43,7 +44,7 @@ class Folder extends TransportAbstract
$name = 'backup' . $name . '.txt'; $name = 'backup' . $name . '.txt';
} }
if (file_put_contents($this->folder . $name, $file) === false) { 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));
} }
} }
} }

View File

@ -2,7 +2,9 @@
namespace Shikiryu\Backup\Transport; namespace Shikiryu\Backup\Transport;
use Exception;
use phpseclib\Net\SFTP as LibSFTP; use phpseclib\Net\SFTP as LibSFTP;
use Shikiryu\Backup\Backup\BackupAbstract;
class Sftp extends TransportAbstract class Sftp extends TransportAbstract
{ {
@ -20,9 +22,9 @@ class Sftp extends TransportAbstract
private $connection; private $connection;
/** /**
* @param \Shikiryu\Backup\Backup\BackupAbstract $backup * @param BackupAbstract $backup
* @param array $config * @param array $config
* @throws \Exception * @throws Exception
*/ */
public function __construct($backup, $config) public function __construct($backup, $config)
{ {
@ -30,14 +32,14 @@ class Sftp extends TransportAbstract
$this->connection = new LibSFTP($this->host, $this->port); $this->connection = new LibSFTP($this->host, $this->port);
if (!$this->connection->login($this->login, $this->password)) { 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 * @return bool
* *
* @throws \Exception * @throws Exception
*/ */
public function send() public function send()
{ {
@ -65,7 +67,7 @@ class Sftp extends TransportAbstract
} }
if (!$sent) { if (!$sent) {
throw new \Exception('At least an upload didnt work.'); throw new Exception('At least an upload didnt work.');
} }
return $sent; return $sent;
} }

14
phpmd.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0"?>
<ruleset name="PHPMD">
<description>The coding standard for PHPMD.</description>
<rule ref="rulesets/cleancode.xml">
<exclude name="StaticAccess" />
<exclude name="ElseExpression" />
</rule>
<rule ref="rulesets/codesize.xml" />
<rule ref="rulesets/design.xml" />
<rule ref="rulesets/naming.xml" />
<rule ref="rulesets/unusedcode.xml" />
</ruleset>