Skip to content

Commit

Permalink
chore: cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Feb 24, 2025
1 parent e220a52 commit 7a33d42
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 42 deletions.
10 changes: 6 additions & 4 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function compile(): self
$importDir = [];
$importDir[] = Util::joinPath($this->config->getStaticPath());
$importDir[] = Util::joinPath($this->config->getAssetsPath());
$scssDir = $this->config->get('assets.compile.import');
$scssDir = (array) $this->config->get('assets.compile.import');
$themes = $this->config->getTheme() ?? [];
foreach ($scssDir as $dir) {
$importDir[] = Util::joinPath($this->config->getStaticPath(), $dir);
Expand Down Expand Up @@ -602,7 +602,7 @@ public function getVideo(): array
throw new RuntimeException(\sprintf('Not able to get video infos of "%s".', $this->data['path']));
}

return \Clwu\Mp4::getInfo($this->data['file']);
return (array) \Clwu\Mp4::getInfo($this->data['file']);
}

/**
Expand Down Expand Up @@ -665,6 +665,8 @@ public function isImageInCdn()
* Load file data.
*
* @throws RuntimeException
*
* @return string[]
*/
private function loadFile(string $path, bool $ignore_missing = false, ?string $remote_fallback = null, bool $force_slash = true): array
{
Expand Down Expand Up @@ -793,7 +795,7 @@ private function findFile(string $path, ?string $remote_fallback = null): string
}

// checks in each themes/<theme>/assets/
foreach ($this->config->getTheme() as $theme) {
foreach ($this->config->getTheme() ?? [] as $theme) {
$filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'assets'), $path);
if (Util\File::getFS()->exists($filePath)) {
return $filePath;
Expand All @@ -807,7 +809,7 @@ private function findFile(string $path, ?string $remote_fallback = null): string
}

// checks in each themes/<theme>/static/
foreach ($this->config->getTheme() as $theme) {
foreach ($this->config->getTheme() ?? [] as $theme) {
$filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'static'), $path);
if (Util\File::getFS()->exists($filePath)) {
return $filePath;
Expand Down
2 changes: 1 addition & 1 deletion src/Assets/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private function prune(string $key): bool
$keyAsArray = explode('__', $this->prepareKey($key), -1);
if (!empty($keyAsArray)) {
$pattern = Util::joinFile($this->cacheDir, $keyAsArray[0]) . '*';
foreach (glob($pattern) as $filename) {
foreach (glob($pattern) ?: [] as $filename) {
Util\File::getFS()->remove($filename);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Command/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,13 @@ private function buildSuccess(OutputInterface $output): void
// writes `changes.flag` file
Util\File::getFS()->dumpFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'changes.flag'), time());
// writes `headers.ini` file
if (null !== $headers = $this->getBuilder()->getConfig()->get('headers')) {
$headers = $this->getBuilder()->getConfig()->get('headers');
if (is_iterable($headers)) {
$output->writeln('Writing headers file...');
Util\File::getFS()->remove(Util::joinFile($this->getPath(), self::TMP_DIR, 'headers.ini'));
foreach ($headers as $entry) {
Util\File::getFS()->appendToFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'headers.ini'), "[{$entry['path']}]\n");
foreach ($entry['headers'] as $header) {
foreach ($entry['headers'] ?? [] as $header) {
Util\File::getFS()->appendToFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'headers.ini'), "{$header['key']} = \"{$header['value']}\"\n");
}
}
Expand Down
46 changes: 26 additions & 20 deletions src/Converter/Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,26 +567,28 @@ protected function parseAttributeData($attributeString)
$Data = [];
$HtmlAtt = [];

foreach ($attributes as $attribute) {
switch ($attribute[0]) {
case '#': // ID
$Data['id'] = substr($attribute, 1);
break;
case '.': // Classes
$classes[] = substr($attribute, 1);
break;
default: // Attributes
parse_str($attribute, $parsed);
$HtmlAtt = array_merge($HtmlAtt, $parsed);
if (is_iterable($attributes)) {
foreach ($attributes as $attribute) {
switch ($attribute[0]) {
case '#': // ID
$Data['id'] = substr($attribute, 1);
break;
case '.': // Classes
$classes[] = substr($attribute, 1);
break;
default: // Attributes
parse_str($attribute, $parsed);
$HtmlAtt = array_merge($HtmlAtt, $parsed);
}
}
}

if (isset($classes)) {
$Data['class'] = implode(' ', $classes);
}
if (!empty($HtmlAtt)) {
foreach ($HtmlAtt as $a => $v) {
$Data[$a] = trim($v, '"');
if (isset($classes)) {
$Data['class'] = implode(' ', $classes);
}
if (!empty($HtmlAtt)) {
foreach ($HtmlAtt as $a => $v) {
$Data[$a] = trim($v, '"');
}
}
}

Expand All @@ -597,20 +599,24 @@ protected function parseAttributeData($attributeString)
* {@inheritdoc}
*
* Converts XHTML '<br />' tag to '<br>'.
*
* @return string
*/
protected function unmarkedText($text)
{
return str_replace("<br />", "<br>", parent::unmarkedText($text)); // @phpstan-ignore staticMethod.notFound
return str_replace('<br />', '<br>', parent::unmarkedText($text)); // @phpstan-ignore staticMethod.notFound
}

/**
* {@inheritdoc}
*
* XHTML closing tag to HTML5 closing tag.
*
* @return string
*/
protected function element(array $Element)
{
return str_replace(" />", ">", parent::element($Element)); // @phpstan-ignore staticMethod.notFound
return str_replace(' />', '>', parent::element($Element)); // @phpstan-ignore staticMethod.notFound
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Alias extends AbstractGenerator implements GeneratorInterface
public function generate(): void
{
/** @var Page $page */
foreach ($this->builder->getPages() as $page) {
foreach ($this->builder->getPages() ?? [] as $page) {
$aliases = $this->getPageAliases($page);

if (!empty($aliases)) {
Expand Down
16 changes: 9 additions & 7 deletions src/Generator/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function generate(): void

// identifying sections from all pages
/** @var Page $page */
foreach ($this->builder->getPages() as $page) {
foreach ($this->builder->getPages() ?? [] as $page) {
// top level (root) sections
if ($page->getSection()) {
// do not add "not published" and "not excluded" pages to its section
Expand Down Expand Up @@ -62,13 +62,15 @@ public function generate(): void
// cascade variables
if ($page->hasVariable('cascade')) {
$cascade = $page->getVariable('cascade');
$subPages->map(function (Page $page) use ($cascade) {
foreach ($cascade as $key => $value) {
if (!$page->hasVariable($key)) {
$page->setVariable($key, $value);
if (is_array($cascade)) {
$subPages->map(function (Page $page) use ($cascade) {
foreach ($cascade as $key => $value) {
if (!$page->hasVariable($key)) {
$page->setVariable($key, $value);
}
}
}
});
});
}
}
// sorts pages
$pages = Section::sortSubPages($this->config, $page, $subPages);
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function finder(CollectionPage $page, string $format, \Cecil\Confi
// is it in `<theme>/layouts/` dir?
if ($config->hasTheme()) {
$themes = $config->getTheme();
foreach ($themes as $theme) {
foreach ($themes ?? [] as $theme) {
if (Util\File::getFS()->exists(Util::joinFile($config->getThemeDirPath($theme, 'layouts'), $layout))) {
return [
'scope' => $theme,
Expand Down
3 changes: 3 additions & 0 deletions src/Step/Menus/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public function process(): void
$countConfig = 0;

foreach ($menusConfig as $menuConfig => $entry) {
if (!is_array($entry)) {
break;
}
// add Menu if not exists
if (!$this->menus[$language['code']]->has($menuConfig)) {
$this->menus[$language['code']]->add(new Menu($menuConfig));
Expand Down
2 changes: 1 addition & 1 deletion src/Step/Pages/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function init(array $options): void
*/
public function process(): void
{
if (\count($this->builder->getPages()) == 0) {
if (!is_iterable($this->builder->getPages()) || \count($this->builder->getPages()) == 0) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Step/Pages/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function init(array $options): void
*/
public function process(): void
{
if (\count($this->builder->getPagesFiles()) == 0) {
if (!is_iterable($this->builder->getPagesFiles()) || \count($this->builder->getPagesFiles()) == 0) {
return;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Step/Pages/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function process(): void
// renders each page
$count = 0;
$postprocessors = [];
foreach ($this->config->get('output.postprocessors') as $name => $postprocessor) {
foreach ($this->config->get('output.postprocessors') ?? [] as $name => $postprocessor) {
try {
if (!class_exists($postprocessor)) {
throw new RuntimeException(\sprintf('Class "%s" not found', $postprocessor));
Expand Down Expand Up @@ -205,8 +205,7 @@ protected function getAllLayoutsPaths(): array
}
// <theme>/layouts/
if ($this->config->hasTheme()) {
$themes = $this->config->getTheme();
foreach ($themes as $theme) {
foreach ($this->config->getTheme() ?? [] as $theme) {
$paths[] = $this->config->getThemeDirPath($theme);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Step/StaticFiles/Copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function process(): void

// copying mounts
if ($this->config->get('static.mounts')) {
foreach ($this->config->get('static.mounts') as $source => $destination) {
foreach ($this->config->get('static.mounts') ?? [] as $source => $destination) {
$this->copy(Util::joinFile($this->config->getStaticPath(), (string) $source), (string) $destination);
}
}
Expand Down

0 comments on commit 7a33d42

Please sign in to comment.