From 1f36cf3e5dca040ea880c0e23300981f1555ec7b Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Tue, 18 Mar 2025 11:21:34 -0700 Subject: [PATCH] Additional code cleanup --- app/src/Factories/FinderFactory.php | 4 +--- app/src/Factories/TranslationFactory.php | 11 ++++------- app/src/ViewFunctions/Breadcrumbs.php | 16 +++++++--------- app/src/ViewFunctions/Markdown.php | 4 +--- app/src/ViewFunctions/ParentUrl.php | 10 ++++------ app/src/ViewFunctions/Url.php | 4 +--- tests/Factories/FinderFactoryTest.php | 11 +++++------ 7 files changed, 23 insertions(+), 37 deletions(-) diff --git a/app/src/Factories/FinderFactory.php b/app/src/Factories/FinderFactory.php index 0797032..4a9b8ff 100644 --- a/app/src/Factories/FinderFactory.php +++ b/app/src/Factories/FinderFactory.php @@ -32,9 +32,7 @@ class FinderFactory $finder->ignoreDotFiles($this->config->get('hide_dot_files')); if ($this->hiddenFiles->isNotEmpty()) { - $finder->filter(function (SplFileInfo $file): bool { - return ! $this->isHidden($file); - }); + $finder->filter(fn (SplFileInfo $file): bool => ! $this->isHidden($file)); } $sortOrder = $this->config->get('sort_order'); diff --git a/app/src/Factories/TranslationFactory.php b/app/src/Factories/TranslationFactory.php index 680d992..45abac8 100644 --- a/app/src/Factories/TranslationFactory.php +++ b/app/src/Factories/TranslationFactory.php @@ -48,12 +48,9 @@ class TranslationFactory */ protected function translations(): array { - return $this->cache->get('translations', function (): array { - return array_values(array_map(static function (SplFileInfo $file): string { - return $file->getBasename('.yaml'); - }, iterator_to_array( - Finder::create()->in($this->config->get('translations_path'))->name('*.yaml') - ))); - }); + return $this->cache->get('translations', fn (): array => array_values(array_map( + static fn (SplFileInfo $file): string => $file->getBasename('.yaml'), + iterator_to_array(Finder::create()->in($this->config->get('translations_path'))->name('*.yaml')) + ))); } } diff --git a/app/src/ViewFunctions/Breadcrumbs.php b/app/src/ViewFunctions/Breadcrumbs.php index e68aa63..dac904d 100644 --- a/app/src/ViewFunctions/Breadcrumbs.php +++ b/app/src/ViewFunctions/Breadcrumbs.php @@ -27,14 +27,12 @@ class Breadcrumbs extends ViewFunction { return Str::explode($path, $this->directorySeparator)->diffAssoc( explode($this->directorySeparator, $this->config->get('files_path')) - )->filter(static function (string $crumb): bool { - return ! in_array($crumb, [null, '.']); - })->reduce(function (Collection $carry, string $crumb): Collection { - return $carry->put($crumb, ltrim( - $carry->last() . $this->directorySeparator . rawurlencode($crumb), $this->directorySeparator - )); - }, new Collection)->map(static function (string $path): string { - return sprintf('?dir=%s', $path); - }); + )->filter( + static fn (string $crumb): bool => ! in_array($crumb, [null, '.']) + )->reduce(fn (Collection $carry, string $crumb): Collection => $carry->put($crumb, ltrim( + $carry->last() . $this->directorySeparator . rawurlencode($crumb), $this->directorySeparator + )), new Collection)->map( + static fn (string $path): string => sprintf('?dir=%s', $path) + ); } } diff --git a/app/src/ViewFunctions/Markdown.php b/app/src/ViewFunctions/Markdown.php index f540818..9413d54 100644 --- a/app/src/ViewFunctions/Markdown.php +++ b/app/src/ViewFunctions/Markdown.php @@ -21,9 +21,7 @@ class Markdown extends ViewFunction { return $this->cache->get( sprintf('markdown-%s', sha1($string)), - function () use ($string): string { - return (string) $this->converter->convert($string); - } + fn (): string => (string) $this->converter->convert($string) ); } } diff --git a/app/src/ViewFunctions/ParentUrl.php b/app/src/ViewFunctions/ParentUrl.php index d8dde19..45dc46d 100644 --- a/app/src/ViewFunctions/ParentUrl.php +++ b/app/src/ViewFunctions/ParentUrl.php @@ -19,12 +19,10 @@ class ParentUrl extends ViewFunction public function __invoke(string $path): string { $parentDir = Str::explode($path, $this->directorySeparator)->map( - static function (string $segment): string { - return rawurlencode($segment); - } - )->filter(static function (?string $value): bool { - return $value !== null; - })->slice(0, -1)->implode($this->directorySeparator); + static fn (string $segment): string => rawurlencode($segment) + )->filter( + static fn (?string $value): bool => $value !== null + )->slice(0, -1)->implode($this->directorySeparator); if ($parentDir === '') { return '.'; diff --git a/app/src/ViewFunctions/Url.php b/app/src/ViewFunctions/Url.php index eb220af..ba2dad8 100644 --- a/app/src/ViewFunctions/Url.php +++ b/app/src/ViewFunctions/Url.php @@ -53,9 +53,7 @@ class Url extends ViewFunction protected function escape(string $path): string { return Str::explode($path, $this->directorySeparator)->map( - static function (string $segment): string { - return rawurlencode($segment); - } + static fn (string $segment): string => rawurlencode($segment) )->implode($this->directorySeparator); } } diff --git a/tests/Factories/FinderFactoryTest.php b/tests/Factories/FinderFactoryTest.php index 949e05c..02293f4 100644 --- a/tests/Factories/FinderFactoryTest.php +++ b/tests/Factories/FinderFactoryTest.php @@ -42,9 +42,7 @@ class FinderFactoryTest extends TestCase public function it_can_sort_by_a_user_provided_closure(): void { $this->container->set('sort_order', \DI\value( - static function (SplFileInfo $file1, SplFileInfo $file2) { - return $file1->getSize() <=> $file2->getSize(); - } + static fn (SplFileInfo $file1, SplFileInfo $file2) => $file1->getSize() <=> $file2->getSize() )); $finder = (new FinderFactory( @@ -160,9 +158,10 @@ class FinderFactoryTest extends TestCase protected function getFilesArray(Finder $finder): array { - $files = array_map(static function (SplFileInfo $file) { - return $file->getFilename(); - }, iterator_to_array($finder)); + $files = array_map( + static fn (SplFileInfo $file): string => $file->getFilename(), + iterator_to_array($finder) + ); return array_values($files); }