Skip to content

Commit

Permalink
Merge pull request #1414 from hydephp/code-quality
Browse files Browse the repository at this point in the history
Additional minor code quality and type improvements hydephp/develop@a121e9c
  • Loading branch information
github-actions committed Oct 29, 2023
1 parent 0ec601c commit c3504da
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 24 deletions.
17 changes: 13 additions & 4 deletions src/Console/Commands/BuildSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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<string, string> $config */
$config = Config::getArray('hyde.features', []);
unset($config[array_search('torchlight', $config)]);
Config::set(['hyde.features' => $config]);
Expand Down Expand Up @@ -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
));

Expand All @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/Console/Commands/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ protected function printVerbosePathInformation(): void

protected function printEnabledFeatures(): void
{
foreach (Config::getArray('hyde.features') as $feature) {
/** @var array<string, string> $features */
$features = Config::getArray('hyde.features', []);

foreach ($features as $feature) {
$this->line(" - $feature");
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Console/Commands/MakePageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down Expand Up @@ -116,4 +116,9 @@ protected function getTypeOption(): ?string

return null;
}

protected function askForString(string $question, ?string $default = null): ?string
{
return $this->ask($question, $default);
}
}
1 change: 1 addition & 0 deletions src/Facades/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/Facades/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/Foundation/Internal/LoadYamlConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected function hasYamlConfigFile(): bool
|| file_exists(Hyde::path('hyde.yaml'));
}

/** @return array<string, mixed> */
protected function getYaml(): array
{
return (array) Yaml::parse(file_get_contents($this->getFile()));
Expand All @@ -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<string, array<string, scalar>> $yaml */
foreach ($yaml as $namespace => $data) {
$this->mergeConfiguration($namespace, (array) $data);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ protected function askIfUnsafeDirectoryShouldBeEmptied(): bool
protected function safeOutputDirectories(): array
{
/** @var array<string> $directories */
return Config::getArray('hyde.safe_output_directories', ['_site', 'docs', 'build']);
$directories = Config::getArray('hyde.safe_output_directories', ['_site', 'docs', 'build']);

return $directories;
}
}
19 changes: 14 additions & 5 deletions src/Framework/Factories/NavigationDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,13 @@ private function searchForPriorityInFrontMatter(): ?int

private function searchForLabelInConfig(): ?string
{
return Config::getArray('hyde.navigation.labels', [
/** @var array<string, string> $config */
$config = Config::getArray('hyde.navigation.labels', [
'index' => 'Home',
DocumentationPage::homeRouteName() => 'Docs',
])[$this->routeKey] ?? null;
]);

return $config[$this->routeKey] ?? null;
}

private function searchForPriorityInConfigs(): ?int
Expand All @@ -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<string> $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<string, int> $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
Expand Down
1 change: 1 addition & 0 deletions src/Framework/Features/Blogging/Models/FeaturedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/Framework/Features/Navigation/NavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Features/XmlGenerators/SitemapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +19,6 @@

use function filemtime;
use function in_array;
use function config;
use function date;
use function time;

Expand Down Expand Up @@ -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()
));
Expand Down
3 changes: 2 additions & 1 deletion src/Framework/Services/ValidationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Markdown/Processing/CodeblockFilepathProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CodeblockFilepathProcessor implements MarkdownPreProcessorContract, Markdo
{
protected static string $torchlightKey = '<!-- Syntax highlighted by torchlight.dev -->';

/** @var array<string> */
protected static array $patterns = [
'// filepath: ',
'// filepath ',
Expand Down
11 changes: 8 additions & 3 deletions src/Pages/DocumentationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/Support/BuildWarnings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@
*/
class BuildWarnings
{
/** @var array<int, \Hyde\Framework\Exceptions\BuildWarning> */
/** @var array<\Hyde\Framework\Exceptions\BuildWarning> */
protected array $warnings = [];

public static function getInstance(): static
public static function getInstance(): self
{
$app = Container::getInstance();

if (! $app->bound(self::class)) {
$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
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit c3504da

Please sign in to comment.