From acd209b351d82fb652e81b00f2ce3f48bef7f1d5 Mon Sep 17 00:00:00 2001 From: Jens True Date: Fri, 26 May 2023 12:14:21 +0000 Subject: [PATCH] CodeSniffer initial steps --- .gitignore | 3 +- composer.json | 3 ++ composer.lock | 78 ++++++++++++++++++++++++++++++++++++++- src/CommandBackup.php | 7 ++-- src/CommandShow.php | 4 +- src/Ntfy/Ntfy.php | 17 +++++---- src/Rclone/Rclone.php | 21 +++++++---- src/Twig/AppExtension.php | 11 +++--- 8 files changed, 116 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index f3eae94..3862975 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ -config.yml \ No newline at end of file +config.yml +composer.phar \ No newline at end of file diff --git a/composer.json b/composer.json index 5e9b7a1..5a4e417 100644 --- a/composer.json +++ b/composer.json @@ -9,5 +9,8 @@ "psr-4": { "App\\": "src/" } + }, + "require-dev": { + "squizlabs/php_codesniffer": "*" } } diff --git a/composer.lock b/composer.lock index b666672..b9a32b6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "271f743cb0e2ed861cfb8fa7ac075589", + "content-hash": "d3057be657b112629e5a71cc9d06ebed", "packages": [ { "name": "psr/container", @@ -1090,7 +1090,81 @@ "time": "2023-05-03T19:06:57+00:00" } ], - "packages-dev": [], + "packages-dev": [ + { + "name": "squizlabs/php_codesniffer", + "version": "2.2.0", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "b301c98f19414d836fdaa678648745fcca5aeb4f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/b301c98f19414d836fdaa678648745fcca5aeb4f", + "reference": "b301c98f19414d836fdaa678648745fcca5aeb4f", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": ">=5.1.2" + }, + "bin": [ + "scripts/phpcs", + "scripts/phpcbf" + ], + "type": "library", + "autoload": { + "classmap": [ + "CodeSniffer.php", + "CodeSniffer/CLI.php", + "CodeSniffer/Exception.php", + "CodeSniffer/File.php", + "CodeSniffer/Fixer.php", + "CodeSniffer/Report.php", + "CodeSniffer/Reporting.php", + "CodeSniffer/Sniff.php", + "CodeSniffer/Tokens.php", + "CodeSniffer/Reports/", + "CodeSniffer/Tokenizers/", + "CodeSniffer/DocGenerators/", + "CodeSniffer/Standards/AbstractPatternSniff.php", + "CodeSniffer/Standards/AbstractScopeSniff.php", + "CodeSniffer/Standards/AbstractVariableSniff.php", + "CodeSniffer/Standards/IncorrectPatternException.php", + "CodeSniffer/Standards/Generic/Sniffs/", + "CodeSniffer/Standards/MySource/Sniffs/", + "CodeSniffer/Standards/PEAR/Sniffs/", + "CodeSniffer/Standards/PSR1/Sniffs/", + "CodeSniffer/Standards/PSR2/Sniffs/", + "CodeSniffer/Standards/Squiz/Sniffs/", + "CodeSniffer/Standards/Zend/Sniffs/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "http://www.squizlabs.com/php-codesniffer", + "keywords": [ + "phpcs", + "standards" + ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2015-01-21T22:44:05+00:00" + } + ], "aliases": [], "minimum-stability": "stable", "stability-flags": [], diff --git a/src/CommandBackup.php b/src/CommandBackup.php index e9c8b11..074690b 100644 --- a/src/CommandBackup.php +++ b/src/CommandBackup.php @@ -26,8 +26,8 @@ class CommandBackup extends Command $output->writeln('Opening: '.$input->getArgument('config')); try { $config = Yaml::parseFile($input->getArgument('config')); - } catch (ParseException $exception) { - $output->writeln('Unable to parse the YAML string: '. $exception->getMessage()); + } catch (ParseException $e) { + $output->writeln('Unable to parse the YAML string: '. $e->getMessage()); } $rclone = new Rclone\Rclone(); @@ -38,8 +38,7 @@ class CommandBackup extends Command $twig = new \Twig\Environment($loader); $twig->addExtension(new Twig\AppExtension()); - foreach($config['backup'] as $conf) - { + foreach ($config['backup'] as $conf) { try { $template['config'] = $conf; $template['start'] = new \DateTime(); diff --git a/src/CommandShow.php b/src/CommandShow.php index 83fef84..c9f780a 100644 --- a/src/CommandShow.php +++ b/src/CommandShow.php @@ -25,8 +25,8 @@ class CommandShow extends Command $output->writeln('Reading from: '.$input->getArgument('config')); try { $config = Yaml::parseFile($input->getArgument('config')); - } catch (ParseException $exception) { - $output->writeln('Unable to parse the YAML string: '. $exception->getMessage()); + } catch (ParseException $e) { + $output->writeln('Unable to parse the YAML string: '. $e->getMessage()); } $table = new Table($output); $table diff --git a/src/Ntfy/Ntfy.php b/src/Ntfy/Ntfy.php index 486edd5..8ed90b0 100644 --- a/src/Ntfy/Ntfy.php +++ b/src/Ntfy/Ntfy.php @@ -1,8 +1,9 @@ domain = $domain; @@ -10,16 +11,18 @@ class Ntfy { function send($topic,$title, $message) { - file_get_contents('https://'.$this->domain.'/'.$topic, false, stream_context_create([ - 'http' => [ + file_get_contents( + 'https://'.$this->domain.'/'.$topic, false, stream_context_create( + ['http' => [ 'method' => 'POST', 'header' => 'Content-Type: text/plain', 'header' => "Content-Type: text/plain\r\n" . "Title: $title\r\n", - 'content' => $message - ] - ])); + 'content' => $message] + ] + ) + ); } } \ No newline at end of file diff --git a/src/Rclone/Rclone.php b/src/Rclone/Rclone.php index bfdb2c1..7745db8 100644 --- a/src/Rclone/Rclone.php +++ b/src/Rclone/Rclone.php @@ -4,9 +4,10 @@ namespace App\Rclone; use Symfony\Component\Process\Process; use Symfony\Component\Process\Exception\ProcessFailedException; -class Rclone { - private $rclone_path; - private $global_options = []; +class Rclone +{ + protected $rclone_path; + protected $global_options = []; function __construct($rclone_path = "rclone") { @@ -31,8 +32,7 @@ class Rclone { $options[] = $src; $options[] = $dest; - if($bandwidth) - { + if ($bandwidth) { $options[] = "--bwlimit"; $options[] = $bandwidth; } @@ -40,9 +40,16 @@ class Rclone { return $this->exec('copy', $options); } - private function exec(string $command, array $options = array()) + protected function exec(string $command, array $options = array()) { - $process = new Process(array_merge([$this->rclone_path], $this->global_options, [$command], $options)); + $process = new Process( + array_merge( + [$this->rclone_path], + $this->global_options, + [$command], + $options + ) + ); $process->setTimeout(4*3600); $process->run(); diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 83fbfe8..9e63863 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -16,15 +16,16 @@ class AppExtension extends AbstractExtension } /** - * @param $bytes - * @param int $precision - * @return string + * 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) { $size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB']; - $factor = floor((strlen($bytes) - 1) / 3); - return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . @$size[$factor]; + $fact = floor((strlen($bytes) - 1) / 3); + return sprintf("%.{$precision}f", $bytes / pow(1024, $fact)) . $size[$fact]; } } \ No newline at end of file