From d0270b00cab33f697b7f83e47d7e2db6b28df131 Mon Sep 17 00:00:00 2001 From: Jens True Date: Thu, 8 Jun 2023 12:44:59 +0000 Subject: [PATCH] PSR12 code standard. --- Makefile | 16 +++++---- composer.lock | 10 +++--- psalm.xml | 1 + src/App.php | 30 ++++++++--------- src/CommandBackup.php | 44 +++++++++++-------------- src/CommandShow.php | 37 +++++++++++---------- src/Rclone/Rclone.php | 69 ++++++++++++++++++--------------------- src/Twig/AppExtension.php | 17 +++++----- 8 files changed, 108 insertions(+), 116 deletions(-) diff --git a/Makefile b/Makefile index 9d58f03..b4cecc9 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,14 @@ -analyze-all: - @echo PHPMessDetector +analyze: analyze-phpmd analyze-phpstan analyze-psalm analyze-phpcs + +analyze-phpmd: -./vendor/bin/phpmd src text cleancode,codesize,controversial,design,naming,unusedcode - @echo PHPStan - -./vendor/bin/phpstan analyze --level=7 src/ backup - @echo Psalm +analyze-phpstan: + -./vendor/bin/phpstan analyze --level=7 --error-format=raw src/ backup +analyze-psalm: -./vendor/bin/psalm - @echo PHP_CodeSniffer - -./vendor/bin/phpcs src +analyze-phpcs: + -./vendor/bin/phpcs src backup --report=emacs --standard=PSR12 + install: php composer.phar install --no-dev install-dev: diff --git a/composer.lock b/composer.lock index ef3886f..ebb1003 100644 --- a/composer.lock +++ b/composer.lock @@ -2922,16 +2922,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.16", + "version": "1.10.18", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "352bdbb960bb523e3d71b834862589f910921c23" + "reference": "52b6416c579663eebdd2f1d97df21971daf3b43f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/352bdbb960bb523e3d71b834862589f910921c23", - "reference": "352bdbb960bb523e3d71b834862589f910921c23", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/52b6416c579663eebdd2f1d97df21971daf3b43f", + "reference": "52b6416c579663eebdd2f1d97df21971daf3b43f", "shasum": "" }, "require": { @@ -2980,7 +2980,7 @@ "type": "tidelift" } ], - "time": "2023-06-05T08:21:46+00:00" + "time": "2023-06-07T22:00:43+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/psalm.xml b/psalm.xml index 16d90e3..ab8f97f 100644 --- a/psalm.xml +++ b/psalm.xml @@ -6,6 +6,7 @@ xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" findUnusedBaselineEntry="true" + findUnusedCode="true" > diff --git a/src/App.php b/src/App.php index b5d543d..9687018 100644 --- a/src/App.php +++ b/src/App.php @@ -1,18 +1,18 @@ * @license https://opensource.org/licenses/gpl-license.php GNU Public License * @link https://jcktrue.dks @@ -20,43 +20,43 @@ use Psr\Log\NullLogger; class App { protected Logger $logger; - protected array $config; + protected mixed $config; /** * Create a new instance providing a config file - * + * * @param string $configFile Relative or full path to YML config. */ - function __construct(string $configFile) + public function __construct(string $configFile) { - $this->config = Yaml::parseFile($configFile); + $parser = new Yaml(); + $this->config = $parser->parseFile($configFile); $logger = new Logger('app'); if (isset($this->config['log'])) { $logger->pushHandler(new StreamHandler($this->getConfig()['log'])); } $logger->info("Initialization complete"); - $this->logger = $logger; } /** * Get the full configuration - * - * @return array Full configuration structure + * + * @return mixed Full configuration structure */ - function getConfig() : array + public function getConfig(): mixed { return $this->config; } /** - * Get the logger instance. - * + * Get the logger instance. + * * @return Logger Instance of logger */ - function getLogger() : Logger + public function getLogger(): Logger { return $this->logger; } -} \ No newline at end of file +} diff --git a/src/CommandBackup.php b/src/CommandBackup.php index a254654..e10fe9a 100644 --- a/src/CommandBackup.php +++ b/src/CommandBackup.php @@ -1,52 +1,46 @@ addArgument('config', InputArgument::OPTIONAL, 'Configuration file', "config.yml"); + $this->addArgument( + 'config', + InputArgument::OPTIONAL, + 'Configuration file', + "config.yml" + ); } protected function execute(InputInterface $input, OutputInterface $output): int { - $io = new SymfonyStyle($input, $output); - $io->title('Start backup process'); - + $sio = new SymfonyStyle($input, $output); + $sio->title('Start backup process'); + try { $app = new App($input->getArgument('config')); - } - catch (\Throwable $e) { - $io->error('Unable to parse the YAML string: '. $e->getMessage()); + } catch (\Throwable $e) { + $sio->error('Configuration error:' . $e->getMessage()); return Command::FAILURE; } - - + $rclone = new Rclone\Rclone(); $rclone->setLogger($app->getLogger()->withName('rclone')); @@ -57,7 +51,7 @@ class CommandBackup extends Command $twig = new Environment($loader); $twig->addExtension(new Twig\AppExtension()); - foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) { + foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) { $message = new Message(); $message->topic($app->getConfig()['notification']['topic']); $message->title($conf['title']); @@ -73,7 +67,7 @@ class CommandBackup extends Command $template['destination_size_after'] = $rclone->getSize($conf['destination']); $template['end'] = new DateTime(); - $message->body($twig->render('notify', $template)); + $message->body($twig->render('notify', $template)); } catch (\Throwable $e) { $message->body($e->getMessage()); $message->tags(["warning"]); @@ -82,7 +76,7 @@ class CommandBackup extends Command $client->send($message); } - $io->success("Complete"); + $sio->success("Complete"); return Command::SUCCESS; } -} \ No newline at end of file +} diff --git a/src/CommandShow.php b/src/CommandShow.php index 256e7a2..f8c7232 100644 --- a/src/CommandShow.php +++ b/src/CommandShow.php @@ -1,45 +1,46 @@ addArgument('config', InputArgument::OPTIONAL, 'Configuration file', "config.yml"); + $this->addArgument( + 'config', + InputArgument::OPTIONAL, + 'Configuration file', + "config.yml" + ); } protected function execute(InputInterface $input, OutputInterface $output): int { - $io = new SymfonyStyle($input, $output); - $io->title('List backup entities'); + $sio = new SymfonyStyle($input, $output); + $sio->title('List backup entities'); try { $app = new App($input->getArgument('config')); - } - catch (\Throwable $e) { - $io->error('Unable to parse the YAML string: '. $e->getMessage()); + } catch (\Throwable $e) { + $sio->error('Configuration error: ' . $e->getMessage()); return Command::FAILURE; } - $io->table(['Description', 'Source', 'Destination'], $app->getConfig()['backup']); + $sio->table( + ['Description', 'Source', 'Destination'], + $app->getConfig()['backup'] + ); - $io->success("Done"); + $sio->success("Done"); return Command::SUCCESS; } -} \ No newline at end of file +} diff --git a/src/Rclone/Rclone.php b/src/Rclone/Rclone.php index 3bdfe80..f5ba240 100644 --- a/src/Rclone/Rclone.php +++ b/src/Rclone/Rclone.php @@ -1,18 +1,17 @@ + * + * @var array */ protected array $globalOptions = []; protected string $version = ""; /** - * Create a new instance - * + * Create a new instance. + * * Default it looks for "rclone" on the path. * But the path can be configured to be absolute. - * + * * @param string $rclonePath Relative or absolute path */ - function __construct(string $rclonePath = "rclone") + public function __construct(string $rclonePath = "rclone") { $this->rclonePath = $rclonePath; - $this->setLogger(new NullLogger); - try - { + $this->setLogger(new NullLogger()); + + try { $version = $this->exec('--version'); $this->version = explode("\n", $version)[0]; - } - catch(ProcessFailedException $e) - { + } catch (ProcessFailedException $e) { throw new Exception("Check installation of rclone"); } - - if (!\str_contains($this->version, 'rclone')) { throw new Exception("Rclone not recognized"); } } /** - * Get the rclone version - * + * Get the rclone version. + * * @return string Version string */ - function getVersion(): string + public function getVersion(): string { return $this->version; } /** - * Calculate the size of a mount/path - * + * Calculate the size of a mount/path. + * * @param string $path mount/path. - * + * * @return int Size in bytes */ - function getSize(string $path): int + public function getSize(string $path): int { $output = $this->exec('size', ['--json', $path]); return (int)json_decode($output)->bytes; } /** - * Copy from src to dest - * + * Copy from source to destination. + * * @param $src Source mount and path * @param $dest Destination mount and path * @param $bandwidth Bandwidth limit provided as string - * + * * @return string Stdout from command */ - function copy(string $src, string $dest, string $bandwidth = null): string - { + public function copy(string $src, string $dest, string $bandwidth = null): string + { $options = array(); $options[] = $src; @@ -107,14 +102,14 @@ class Rclone } /** - * Execute a command on the rclone binary - * + * Execute a command on the rclone binary. + * * @param string $command Top level Rclone command * @param array $options Array of additional options - * + * * @return string stdout data. */ - protected function exec(string $command, array $options = array()) : string + protected function exec(string $command, array $options = array()): string { $process = new Process( array_merge( @@ -127,8 +122,8 @@ class Rclone if ($this->logger instanceof LoggerInterface) { $this->logger->info("Execute command", [$process->getCommandLine()]); } - - $process->setTimeout(4*3600); + + $process->setTimeout(4 * 3600); $process->run(); // executes after the command finishes @@ -143,4 +138,4 @@ class Rclone } return $process->getOutput(); } -} \ No newline at end of file +} diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index aa8d658..a3729df 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -8,9 +8,9 @@ use Twig\TwigFilter; /** * Twig extension - * + * * Additional formatters for templates - * + * * @author Jens True * @license https://opensource.org/licenses/gpl-license.php GNU Public License * @link https://jcktrue.dk @@ -19,10 +19,10 @@ class AppExtension extends AbstractExtension { /** * Extend the filters - * - * + * + * @return TwigFilter[] */ - public function getFilters() : array + public function getFilters(): array { return array( new TwigFilter('formatBytes', array($this, 'formatBytes')), @@ -31,10 +31,10 @@ class AppExtension extends AbstractExtension /** * Format a file size to be human readable - * + * * @param int $bytes Number of bytes * @param int $precision Precision - * + * * @return string Formatted string */ public function formatBytes($bytes, $precision = 2) @@ -43,5 +43,4 @@ class AppExtension extends AbstractExtension $fact = (int)(floor((strlen((string)$bytes) - 1) / 3)); return sprintf("%.{$precision}f", $bytes / pow(1024, $fact)) . $size[$fact]; } - -} \ No newline at end of file +}