From 642aec420b03aca6278efb9afca0d6483bc59118 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Thu, 21 Dec 2023 18:20:32 +0100 Subject: [PATCH] Refactor with Rector SetList::CODE_QUALITY, SetList::DEAD_CODE, SetList::EARLY_RETURN, SetList::TYPE_DECLARATION, LevelSetList::UP_TO_PHP_81 --- composer.json | 1 + composer.lock | 120 +++++++++++++++++++++++++++- rector.php | 26 ++++++ src/Commands/BaseBuildCommand.php | 19 +---- src/Commands/BuildCommand.php | 27 +++---- src/Commands/EpubCommand.php | 37 +-------- src/Commands/InitCommand.php | 13 +-- src/Commands/SampleCommand.php | 16 ---- src/Commands/SortContentCommand.php | 15 +--- 9 files changed, 167 insertions(+), 107 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 42aaf14..9f3b6e9 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ }, "require-dev": { "laravel/pint": "^1.2", + "rector/rector": "^0.18.13", "symfony/var-dumper": "^6.0|^7.0" }, "autoload": { diff --git a/composer.lock b/composer.lock index a7cdc15..0d517c5 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": "889c14e2832e43df2b9d234c9eb8b9ae", + "content-hash": "489d0b170454c26c02c4dc7164bf3dab", "packages": [ { "name": "carbonphp/carbon-doctrine-types", @@ -3411,6 +3411,124 @@ }, "time": "2023-12-05T19:43:12+00:00" }, + { + "name": "phpstan/phpstan", + "version": "1.10.50", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "06a98513ac72c03e8366b5a0cb00750b487032e4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/06a98513ac72c03e8366b5a0cb00750b487032e4", + "reference": "06a98513ac72c03e8366b5a0cb00750b487032e4", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2023-12-13T10:59:42+00:00" + }, + { + "name": "rector/rector", + "version": "0.18.13", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "f8011a76d36aa4f839f60f3b4f97707d97176618" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/f8011a76d36aa4f839f60f3b4f97707d97176618", + "reference": "f8011a76d36aa4f839f60f3b4f97707d97176618", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0", + "phpstan/phpstan": "^1.10.35" + }, + "conflict": { + "rector/rector-doctrine": "*", + "rector/rector-downgrade-php": "*", + "rector/rector-phpunit": "*", + "rector/rector-symfony": "*" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Instant Upgrade and Automated Refactoring of any PHP code", + "keywords": [ + "automation", + "dev", + "migration", + "refactoring" + ], + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/0.18.13" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2023-12-20T16:08:01+00:00" + }, { "name": "symfony/var-dumper", "version": "v7.0.0", diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..c3a30c5 --- /dev/null +++ b/rector.php @@ -0,0 +1,26 @@ +paths([ + __DIR__ . '/src', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + // define sets of rules + $rectorConfig->sets([ + SetList::CODE_QUALITY, + SetList::DEAD_CODE, + SetList::EARLY_RETURN, + SetList::TYPE_DECLARATION, + LevelSetList::UP_TO_PHP_81 + ]); +}; diff --git a/src/Commands/BaseBuildCommand.php b/src/Commands/BaseBuildCommand.php index 9693c0a..4bd8096 100644 --- a/src/Commands/BaseBuildCommand.php +++ b/src/Commands/BaseBuildCommand.php @@ -47,14 +47,14 @@ protected function preExecute(InputInterface $input, OutputInterface $output) } if (!$this->disk->isDirectory($this->contentDirectory)) { $this->output->writeln('Error, check if ' . $this->contentDirectory . ' exists.'); - exit -1; + exit; } $this->output->writeln('Loading content from: ' . $this->contentDirectory . ''); $configIbisFile = $this->currentPath . '/ibis.php'; if (!$this->disk->isFile($configIbisFile)) { $this->output->writeln('Error, check if ' . $configIbisFile . ' exists.'); - exit -1; + exit; } $this->config = require $configIbisFile; @@ -62,11 +62,6 @@ protected function preExecute(InputInterface $input, OutputInterface $output) } - /** - * @param string $path - * @param array $config - * @return Collection - */ protected function buildHtml(string $path, array $config): Collection { $this->output->writeln('==> Parsing Markdown ...'); @@ -127,11 +122,10 @@ protected function buildHtml(string $path, array $config): Collection /** - * @param string $html * @param $file * @return string|string[] */ - protected function prepareHtmlForEbook(string $html, $file) + protected function prepareHtmlForEbook(string $html, $file): string { $commands = [ '[break]' => '
' @@ -146,16 +140,11 @@ protected function prepareHtmlForEbook(string $html, $file) $html = str_replace("
\n

{warning}", "

Warning:", $html); $html = str_replace("

\n

{quote}", "

", $html); - $html = str_replace(array_keys($commands), array_values($commands), $html); - - return $html; + return str_replace(array_keys($commands), array_values($commands), $html); } - /** - * @param string $currentPath - */ protected function ensureExportDirectoryExists(string $currentPath): void { $this->output->writeln('==> Preparing Export Directory ...'); diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index c71cf23..42cc596 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -54,9 +54,6 @@ protected function configure() /** * Execute the command. * - * @param InputInterface $input - * @param OutputInterface $output - * @return int * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @throws \Mpdf\MpdfException */ @@ -89,10 +86,6 @@ public function execute(InputInterface $input, OutputInterface $output): int /** - * @param Collection $chapters - * @param array $config - * @param string $currentPath - * @param string $theme * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @throws \Mpdf\MpdfException */ @@ -130,7 +123,7 @@ protected function buildPdf(Collection $chapters, array $config, string $current $pdf->SetMargins(400, 100, 12); $coverImage = "cover.jpg"; - if (key_exists("image", $config['cover'])) { + if (array_key_exists("image", $config['cover'])) { $coverImage = $config['cover']['image']; } if ($this->disk->isFile($currentPath . '/assets/' . $coverImage)) { @@ -168,7 +161,7 @@ protected function buildPdf(Collection $chapters, array $config, string $current $theme ); //dd($chapters); - foreach ($chapters as $key => $chapter) { + foreach ($chapters as $chapter) { //if ( is_string($chapter) ) { dd($key, $chapter);} $this->output->writeln('==> ❇️ ' . $chapter["mdfile"] . ' ...'); if (array_key_exists('header', $config)) { @@ -200,7 +193,7 @@ protected function buildPdf(Collection $chapters, array $config, string $current * @return string * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ - private function getTheme($currentPath, $themeName) + private function getTheme(string $currentPath, $themeName) { return $this->disk->get($currentPath . "/assets/theme-$themeName.html"); } @@ -209,14 +202,12 @@ private function getTheme($currentPath, $themeName) * @param $fontData * @return array */ - protected function fonts($config, $fontData) + protected function fonts(array $config, $fontData): float|int|array { - return $fontData + collect($config['fonts'])->mapWithKeys(function ($file, $name) { - return [ - $name => [ - 'R' => $file - ] - ]; - })->toArray(); + return $fontData + collect($config['fonts'])->mapWithKeys(fn($file, $name): array => [ + $name => [ + 'R' => $file + ] + ])->toArray(); } } diff --git a/src/Commands/EpubCommand.php b/src/Commands/EpubCommand.php index 1e89478..7defb54 100644 --- a/src/Commands/EpubCommand.php +++ b/src/Commands/EpubCommand.php @@ -3,8 +3,6 @@ namespace Ibis\Commands; use Ibis\Ibis; -use Mpdf\Config\FontVariables; -use Mpdf\Config\ConfigVariables; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -38,9 +36,6 @@ protected function configure() /** * Execute the command. * - * @param InputInterface $input - * @param OutputInterface $output - * @return int * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @throws \Mpdf\MpdfException */ @@ -80,33 +75,13 @@ public function execute(InputInterface $input, OutputInterface $output): int /** - * @param Collection $chapters - * @param array $config - * @param string $currentPath * @param string $theme * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @throws \Mpdf\MpdfException */ - protected function buildEpub(Collection $chapters, array $config, string $currentPath) + protected function buildEpub(Collection $chapters, array $config, string $currentPath): bool { - $defaultConfig = (new ConfigVariables())->getDefaults(); - $fontDirs = $defaultConfig['fontDir']; - $defaultFontConfig = (new FontVariables())->getDefaults(); - $fontData = $defaultFontConfig['fontdata']; - - - $content_start = - "\n" - . "\n" - . "\n" - . "" - . "\n" - . "\n" - . "Test Book\n" - . "\n" - . "\n"; $content_start = "\n" . "\n" @@ -133,7 +108,7 @@ protected function buildEpub(Collection $chapters, array $config, string $curren $content_end = ""; $cover .= $content_end; $coverImage = "cover.jpg"; - if (key_exists("image", $config['cover'])) { + if (array_key_exists("image", $config['cover'])) { $coverImage = $config['cover']['image']; } if ($this->disk->isFile($currentPath . '/assets/' . $coverImage)) { @@ -160,15 +135,11 @@ protected function buildEpub(Collection $chapters, array $config, string $curren $book->finalize(); $epubFilename = 'export/' . Ibis::outputFileName() . '.epub'; - $zipData = $book->saveBook($epubFilename); + $book->saveBook($epubFilename); $this->output->writeln('==> EPUB file ' . $epubFilename . ' created'); return true; - - $this->output->writeln('==> Writing EPUB To Disk ...'); - - $this->output->writeln(''); } /** @@ -177,7 +148,7 @@ protected function buildEpub(Collection $chapters, array $config, string $curren * @return string * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ - private function getStyle($currentPath, $themeName) + private function getStyle(string $currentPath, string $themeName) { return $this->disk->get($currentPath . "/assets/$themeName.css"); } diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index 1a5dcb0..f7413c2 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -9,15 +9,9 @@ class InitCommand extends Command { - /** - * @var OutputInterface - */ - private $output; + private ?\Symfony\Component\Console\Output\OutputInterface $output = null; - /** - * @var Filesystem - */ - private $disk; + private ?\Illuminate\Filesystem\Filesystem $disk = null; /** * Configure the command. @@ -34,9 +28,6 @@ protected function configure() /** * Execute the command. * - * @param InputInterface $input - * @param OutputInterface $output - * @return int * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @throws \Mpdf\MpdfException */ diff --git a/src/Commands/SampleCommand.php b/src/Commands/SampleCommand.php index 9ae4783..b3c85c6 100644 --- a/src/Commands/SampleCommand.php +++ b/src/Commands/SampleCommand.php @@ -3,7 +3,6 @@ namespace Ibis\Commands; use Ibis\Ibis; -use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -11,16 +10,6 @@ class SampleCommand extends Command { - /** - * @var OutputInterface - */ - private $output; - - /** - * @var Filesystem - */ - private $disk; - /** * Configure the command. * @@ -37,16 +26,11 @@ protected function configure() /** * Execute the command. * - * @param InputInterface $input - * @param OutputInterface $output - * @return int * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @throws \Mpdf\MpdfException */ public function execute(InputInterface $input, OutputInterface $output): int { - $this->disk = new Filesystem(); - $currentPath = getcwd(); $config = require $currentPath . '/ibis.php'; diff --git a/src/Commands/SortContentCommand.php b/src/Commands/SortContentCommand.php index a07b0c5..00d96b9 100644 --- a/src/Commands/SortContentCommand.php +++ b/src/Commands/SortContentCommand.php @@ -10,15 +10,7 @@ class SortContentCommand extends Command { - /** - * @var OutputInterface - */ - private $output; - - /** - * @var Filesystem - */ - private $disk; + private ?\Illuminate\Filesystem\Filesystem $disk = null; /** * Configure the command. @@ -35,9 +27,6 @@ protected function configure() /** * Execute the command. * - * @param InputInterface $input - * @param OutputInterface $output - * @return int * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @throws \Mpdf\MpdfException */ @@ -47,7 +36,7 @@ public function execute(InputInterface $input, OutputInterface $output): int $currentPath = getcwd(); - collect($this->disk->files($currentPath . '/content'))->each(function ($file, $index) use ($currentPath) { + collect($this->disk->files($currentPath . '/content'))->each(function ($file, $index) use ($currentPath): void { $markdown = $this->disk->get( $file->getPathname() );