From c3504dad928bc0c839cbd8ad8a0f369a0a503b56 Mon Sep 17 00:00:00 2001 From: github-actions Date: Sun, 29 Oct 2023 13:41:26 +0000 Subject: [PATCH] Merge pull request #1414 from hydephp/code-quality Additional minor code quality and type improvements https://github.com/hydephp/develop/commit/a121e9cc478bd877d7b5b06ea9c7cc5602d8227e --- src/Console/Commands/BuildSiteCommand.php | 17 +++++++++++++---- src/Console/Commands/DebugCommand.php | 5 ++++- src/Console/Commands/MakePageCommand.php | 7 ++++++- src/Facades/Config.php | 1 + src/Facades/Filesystem.php | 1 - .../Internal/LoadYamlConfiguration.php | 2 ++ .../PreBuildTasks/CleanSiteDirectory.php | 4 +++- .../Factories/NavigationDataFactory.php | 19 ++++++++++++++----- .../Blogging/Models/FeaturedImage.php | 1 + src/Framework/Features/Navigation/NavItem.php | 5 ++++- .../XmlGenerators/SitemapGenerator.php | 4 ++-- src/Framework/Services/ValidationService.php | 3 ++- .../Processing/CodeblockFilepathProcessor.php | 1 + src/Pages/DocumentationPage.php | 11 ++++++++--- src/Support/BuildWarnings.php | 14 ++++++++++---- 15 files changed, 71 insertions(+), 24 deletions(-) diff --git a/src/Console/Commands/BuildSiteCommand.php b/src/Console/Commands/BuildSiteCommand.php index b7458af6..0de82bb3 100644 --- a/src/Console/Commands/BuildSiteCommand.php +++ b/src/Console/Commands/BuildSiteCommand.php @@ -46,8 +46,7 @@ public function handle(): int $this->service = new BuildService($this->output); - $this->taskService = app(BuildTaskService::class); - $this->taskService->setOutput($this->output); + $this->configureBuildTaskService(); $this->runPreBuildActions(); @@ -62,11 +61,21 @@ public function handle(): int return $this->getExitCode(); } + protected function configureBuildTaskService(): void + { + /** @var BuildTaskService $taskService */ + $taskService = app(BuildTaskService::class); + + $this->taskService = $taskService; + $this->taskService->setOutput($this->output); + } + protected function runPreBuildActions(): void { if ($this->option('no-api')) { $this->info('Disabling external API calls'); $this->newLine(); + /** @var array $config */ $config = Config::getArray('hyde.features', []); unset($config[array_search('torchlight', $config)]); Config::set(['hyde.features' => $config]); @@ -132,7 +141,7 @@ protected function runNodeCommand(string $command, string $message, ?string $act $output = shell_exec(sprintf( '%s%s', - app()->environment() === 'testing' ? 'echo ' : '', + (string) app()->environment() === 'testing' ? 'echo ' : '', $command )); @@ -150,7 +159,7 @@ protected function hasWarnings(): bool protected function getExitCode(): int { if ($this->hasWarnings() && BuildWarnings::reportsWarningsAsExceptions()) { - return self::INVALID; + return Command::INVALID; } return Command::SUCCESS; diff --git a/src/Console/Commands/DebugCommand.php b/src/Console/Commands/DebugCommand.php index ab9b34b9..cf8db2f3 100644 --- a/src/Console/Commands/DebugCommand.php +++ b/src/Console/Commands/DebugCommand.php @@ -70,7 +70,10 @@ protected function printVerbosePathInformation(): void protected function printEnabledFeatures(): void { - foreach (Config::getArray('hyde.features') as $feature) { + /** @var array $features */ + $features = Config::getArray('hyde.features', []); + + foreach ($features as $feature) { $this->line(" - $feature"); } } diff --git a/src/Console/Commands/MakePageCommand.php b/src/Console/Commands/MakePageCommand.php index bbcc6210..d5edac94 100644 --- a/src/Console/Commands/MakePageCommand.php +++ b/src/Console/Commands/MakePageCommand.php @@ -80,7 +80,7 @@ protected function validateOptions(): void protected function getTitle(): string { return $this->argument('title') - ?? $this->ask('What is the title of the page?') + ?? $this->askForString('What is the title of the page?') ?? 'My New Page'; } @@ -116,4 +116,9 @@ protected function getTypeOption(): ?string return null; } + + protected function askForString(string $question, ?string $default = null): ?string + { + return $this->ask($question, $default); + } } diff --git a/src/Facades/Config.php b/src/Facades/Config.php index cc0c3b9a..405aa727 100644 --- a/src/Facades/Config.php +++ b/src/Facades/Config.php @@ -46,6 +46,7 @@ public static function getFloat(string $key, float $default = null): float /** @experimental Could possibly be merged by allowing null returns if default is null? Preferably with generics so the type is matched by IDE support. */ public static function getNullableString(string $key, string $default = null): ?string { + /** @var array|string|int|bool|float|null $value */ $value = static::get($key, $default); if ($value === null) { diff --git a/src/Facades/Filesystem.php b/src/Facades/Filesystem.php index 56991dd0..3229bc58 100644 --- a/src/Facades/Filesystem.php +++ b/src/Facades/Filesystem.php @@ -7,7 +7,6 @@ use Hyde\Foundation\HydeKernel; use Hyde\Framework\Concerns\Internal\ForwardsIlluminateFilesystem; use Illuminate\Support\Collection; -use Illuminate\Support\Facades\File; use function app; diff --git a/src/Foundation/Internal/LoadYamlConfiguration.php b/src/Foundation/Internal/LoadYamlConfiguration.php index 47fb8f66..59fcc1da 100644 --- a/src/Foundation/Internal/LoadYamlConfiguration.php +++ b/src/Foundation/Internal/LoadYamlConfiguration.php @@ -45,6 +45,7 @@ protected function hasYamlConfigFile(): bool || file_exists(Hyde::path('hyde.yaml')); } + /** @return array */ protected function getYaml(): array { return (array) Yaml::parse(file_get_contents($this->getFile())); @@ -64,6 +65,7 @@ protected function mergeParsedConfiguration(): void // If the Yaml file contains namespaces, we merge those using more granular logic // that only applies the namespace data to each configuration namespace. if ($this->configurationContainsNamespaces($yaml)) { + /** @var array> $yaml */ foreach ($yaml as $namespace => $data) { $this->mergeConfiguration($namespace, (array) $data); } diff --git a/src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php b/src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php index d03c6a9d..f00e37ce 100644 --- a/src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php +++ b/src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php @@ -61,6 +61,8 @@ protected function askIfUnsafeDirectoryShouldBeEmptied(): bool protected function safeOutputDirectories(): array { /** @var array $directories */ - return Config::getArray('hyde.safe_output_directories', ['_site', 'docs', 'build']); + $directories = Config::getArray('hyde.safe_output_directories', ['_site', 'docs', 'build']); + + return $directories; } } diff --git a/src/Framework/Factories/NavigationDataFactory.php b/src/Framework/Factories/NavigationDataFactory.php index 3068855c..dd6d9a8f 100644 --- a/src/Framework/Factories/NavigationDataFactory.php +++ b/src/Framework/Factories/NavigationDataFactory.php @@ -139,10 +139,13 @@ private function searchForPriorityInFrontMatter(): ?int private function searchForLabelInConfig(): ?string { - return Config::getArray('hyde.navigation.labels', [ + /** @var array $config */ + $config = Config::getArray('hyde.navigation.labels', [ 'index' => 'Home', DocumentationPage::homeRouteName() => 'Docs', - ])[$this->routeKey] ?? null; + ]); + + return $config[$this->routeKey] ?? null; } private function searchForPriorityInConfigs(): ?int @@ -160,19 +163,25 @@ private function searchForPriorityInSidebarConfig(): ?int // Adding an offset makes so that pages with a front matter priority that is lower can be shown first. // This is all to make it easier to mix ways of adding priorities. + /** @var array $config */ + $config = Config::getArray('docs.sidebar_order', []); + return $this->offset( - array_flip(Config::getArray('docs.sidebar_order', []))[$this->identifier] ?? null, + array_flip($config)[$this->identifier] ?? null, self::CONFIG_OFFSET ); } private function searchForPriorityInNavigationConfig(): ?int { - return Config::getArray('hyde.navigation.order', [ + /** @var array $config */ + $config = Config::getArray('hyde.navigation.order', [ 'index' => 0, 'posts' => 10, 'docs/index' => 100, - ])[$this->routeKey] ?? null; + ]); + + return $config[$this->routeKey] ?? null; } private function canUseSubdirectoryForGroups(): bool diff --git a/src/Framework/Features/Blogging/Models/FeaturedImage.php b/src/Framework/Features/Blogging/Models/FeaturedImage.php index ac016f42..02b2ab3e 100644 --- a/src/Framework/Features/Blogging/Models/FeaturedImage.php +++ b/src/Framework/Features/Blogging/Models/FeaturedImage.php @@ -226,6 +226,7 @@ protected function getContentLengthForRemoteImage(): int { // Check if the --no-api flag is set when running the build command, and if so, skip the API call. if (Config::getBool('hyde.api_calls', true)) { + /** @var string[][] $headers */ $headers = Http::withHeaders([ 'User-Agent' => Config::getString('hyde.http_user_agent', 'RSS Request Client'), ])->head($this->getSource())->headers(); diff --git a/src/Framework/Features/Navigation/NavItem.php b/src/Framework/Features/Navigation/NavItem.php index db775d43..7a57b554 100644 --- a/src/Framework/Features/Navigation/NavItem.php +++ b/src/Framework/Features/Navigation/NavItem.php @@ -125,7 +125,10 @@ public function isCurrent(): bool protected static function getRouteGroup(Route $route): ?string { - return static::normalizeGroupKey($route->getPage()->data('navigation.group')); + /** @var string|null $group */ + $group = $route->getPage()->data('navigation.group'); + + return static::normalizeGroupKey($group); } protected static function normalizeGroupKey(?string $group): ?string diff --git a/src/Framework/Features/XmlGenerators/SitemapGenerator.php b/src/Framework/Features/XmlGenerators/SitemapGenerator.php index 933f1e61..64b5690c 100644 --- a/src/Framework/Features/XmlGenerators/SitemapGenerator.php +++ b/src/Framework/Features/XmlGenerators/SitemapGenerator.php @@ -8,6 +8,7 @@ use Hyde\Hyde; use SimpleXMLElement; +use Hyde\Facades\Config; use Hyde\Pages\BladePage; use Hyde\Pages\MarkdownPage; use Hyde\Pages\MarkdownPost; @@ -18,7 +19,6 @@ use function filemtime; use function in_array; -use function config; use function date; use function time; @@ -61,7 +61,7 @@ protected function addRoute(Route $route): void $this->addChild($urlItem, 'lastmod', $this->getLastModDate($route->getSourcePath())); $this->addChild($urlItem, 'changefreq', 'daily'); - if (config('hyde.sitemap.dynamic_priority', true)) { + if (Config::getBool('hyde.sitemap.dynamic_priority', true)) { $this->addChild($urlItem, 'priority', $this->getPriority( $route->getPageClass(), $route->getPage()->getIdentifier() )); diff --git a/src/Framework/Services/ValidationService.php b/src/Framework/Services/ValidationService.php index 44023770..da7c0fa7 100644 --- a/src/Framework/Services/ValidationService.php +++ b/src/Framework/Services/ValidationService.php @@ -13,6 +13,7 @@ use Hyde\Support\Models\ValidationResult as Result; use function count; +use function call_user_func; use function get_class_methods; use function array_intersect; use function file_exists; @@ -38,7 +39,7 @@ public static function checks(): array public function run(string $check): Result { - return $this->$check(new Result); + return call_user_func([$this, $check], new Result); } public function check_validators_can_run(Result $result): Result diff --git a/src/Markdown/Processing/CodeblockFilepathProcessor.php b/src/Markdown/Processing/CodeblockFilepathProcessor.php index b75f9125..97070ebb 100644 --- a/src/Markdown/Processing/CodeblockFilepathProcessor.php +++ b/src/Markdown/Processing/CodeblockFilepathProcessor.php @@ -28,6 +28,7 @@ class CodeblockFilepathProcessor implements MarkdownPreProcessorContract, Markdo { protected static string $torchlightKey = ''; + /** @var array */ protected static array $patterns = [ '// filepath: ', '// filepath ', diff --git a/src/Pages/DocumentationPage.php b/src/Pages/DocumentationPage.php index 79bba5cf..2dd04b03 100644 --- a/src/Pages/DocumentationPage.php +++ b/src/Pages/DocumentationPage.php @@ -10,6 +10,11 @@ use Hyde\Pages\Concerns\BaseMarkdownPage; use Hyde\Support\Models\Route; +use function trim; +use function sprintf; +use function unslash; +use function basename; + /** * Page class for documentation pages. * @@ -37,16 +42,16 @@ public static function homeRouteName(): string /** @see https://hydephp.com/docs/1.x/documentation-pages#automatic-edit-page-button */ public function getOnlineSourcePath(): string|false { - if (config('docs.source_file_location_base') === null) { + if (Config::getNullableString('docs.source_file_location_base') === null) { return false; } - return trim((string) config('docs.source_file_location_base'), '/').'/'.$this->identifier.'.md'; + return sprintf('%s/%s.md', trim(Config::getString('docs.source_file_location_base'), '/'), $this->identifier); } public static function hasTableOfContents(): bool { - return config('docs.table_of_contents.enabled', true); + return Config::getBool('docs.table_of_contents.enabled', true); } /** diff --git a/src/Support/BuildWarnings.php b/src/Support/BuildWarnings.php index 99f9fd13..84a0db7f 100644 --- a/src/Support/BuildWarnings.php +++ b/src/Support/BuildWarnings.php @@ -18,10 +18,10 @@ */ class BuildWarnings { - /** @var array */ + /** @var array<\Hyde\Framework\Exceptions\BuildWarning> */ protected array $warnings = []; - public static function getInstance(): static + public static function getInstance(): self { $app = Container::getInstance(); @@ -29,7 +29,10 @@ public static function getInstance(): static $app->singleton(self::class); } - return $app->make(self::class); + /** @var \Hyde\Support\BuildWarnings $instance */ + $instance = $app->make(self::class); + + return $instance; } public static function report(BuildWarning|string $warning): void @@ -79,8 +82,11 @@ protected static function renderWarnings(OutputStyle $output, bool $verbose): vo protected static function renderWarningsAsExceptions(OutputStyle $output): void { + /** @var ExceptionHandler $handler */ + $handler = app(ExceptionHandler::class); + foreach (static::getWarnings() as $warning) { - app(ExceptionHandler::class)->renderForConsole($output, $warning); + $handler->renderForConsole($output, $warning); } } }