Skip to content

Commit

Permalink
Merge pull request #8 from Hi-Folks/refactor/add-rector
Browse files Browse the repository at this point in the history
Refactor with Rector
  • Loading branch information
roberto-butti authored Dec 21, 2023
2 parents c73135e + 642aec4 commit 27e7767
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 107 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"require-dev": {
"laravel/pint": "^1.2",
"rector/rector": "^0.18.13",
"symfony/var-dumper": "^6.0|^7.0"
},
"autoload": {
Expand Down
120 changes: 119 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->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
]);
};
19 changes: 4 additions & 15 deletions src/Commands/BaseBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,21 @@ protected function preExecute(InputInterface $input, OutputInterface $output)
}
if (!$this->disk->isDirectory($this->contentDirectory)) {
$this->output->writeln('<error>Error, check if ' . $this->contentDirectory . ' exists.</error>');
exit -1;
exit;
}
$this->output->writeln('<info>Loading content from: ' . $this->contentDirectory . '</info>');

$configIbisFile = $this->currentPath . '/ibis.php';
if (!$this->disk->isFile($configIbisFile)) {
$this->output->writeln('<error>Error, check if ' . $configIbisFile . ' exists.</error>');
exit -1;
exit;
}

$this->config = require $configIbisFile;

}


/**
* @param string $path
* @param array $config
* @return Collection
*/
protected function buildHtml(string $path, array $config): Collection
{
$this->output->writeln('<fg=yellow>==></> Parsing Markdown ...');
Expand Down Expand Up @@ -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]' => '<div style="page-break-after: always;"></div>'
Expand All @@ -146,16 +140,11 @@ protected function prepareHtmlForEbook(string $html, $file)
$html = str_replace("<blockquote>\n<p>{warning}", "<blockquote class='warning'><p><strong>Warning:</strong>", $html);
$html = str_replace("<blockquote>\n<p>{quote}", "<blockquote class='quote'><p>", $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('<fg=yellow>==></> Preparing Export Directory ...');
Expand Down
27 changes: 9 additions & 18 deletions src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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('<fg=yellow>==></> ❇️ ' . $chapter["mdfile"] . ' ...');
if (array_key_exists('header', $config)) {
Expand Down Expand Up @@ -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");
}
Expand All @@ -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();
}
}
37 changes: 4 additions & 33 deletions src/Commands/EpubCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"
. " \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
. "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
. "<head>"
. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
. "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\" />\n"
. "<title>Test Book</title>\n"
. "</head>\n"
. "<body>\n";
$content_start =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
. "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
Expand All @@ -133,7 +108,7 @@ protected function buildEpub(Collection $chapters, array $config, string $curren
$content_end = "</body></html>";
$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)) {
Expand All @@ -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('<fg=green>==></> EPUB file ' . $epubFilename . ' created');
return true;

$this->output->writeln('<fg=yellow>==></> Writing EPUB To Disk ...');

$this->output->writeln('');
}

/**
Expand All @@ -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");
}
Expand Down
Loading

0 comments on commit 27e7767

Please sign in to comment.