Skip to content

Commit a121e9c

Browse files
authored
Merge pull request #1414 from hydephp/code-quality
Additional minor code quality and type improvements
2 parents 561a55f + 8f9d1dd commit a121e9c

File tree

16 files changed

+73
-26
lines changed

16 files changed

+73
-26
lines changed

packages/framework/src/Console/Commands/BuildSiteCommand.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public function handle(): int
4646

4747
$this->service = new BuildService($this->output);
4848

49-
$this->taskService = app(BuildTaskService::class);
50-
$this->taskService->setOutput($this->output);
49+
$this->configureBuildTaskService();
5150

5251
$this->runPreBuildActions();
5352

@@ -62,11 +61,21 @@ public function handle(): int
6261
return $this->getExitCode();
6362
}
6463

64+
protected function configureBuildTaskService(): void
65+
{
66+
/** @var BuildTaskService $taskService */
67+
$taskService = app(BuildTaskService::class);
68+
69+
$this->taskService = $taskService;
70+
$this->taskService->setOutput($this->output);
71+
}
72+
6573
protected function runPreBuildActions(): void
6674
{
6775
if ($this->option('no-api')) {
6876
$this->info('Disabling external API calls');
6977
$this->newLine();
78+
/** @var array<string, string> $config */
7079
$config = Config::getArray('hyde.features', []);
7180
unset($config[array_search('torchlight', $config)]);
7281
Config::set(['hyde.features' => $config]);
@@ -132,7 +141,7 @@ protected function runNodeCommand(string $command, string $message, ?string $act
132141

133142
$output = shell_exec(sprintf(
134143
'%s%s',
135-
app()->environment() === 'testing' ? 'echo ' : '',
144+
(string) app()->environment() === 'testing' ? 'echo ' : '',
136145
$command
137146
));
138147

@@ -150,7 +159,7 @@ protected function hasWarnings(): bool
150159
protected function getExitCode(): int
151160
{
152161
if ($this->hasWarnings() && BuildWarnings::reportsWarningsAsExceptions()) {
153-
return self::INVALID;
162+
return Command::INVALID;
154163
}
155164

156165
return Command::SUCCESS;

packages/framework/src/Console/Commands/DebugCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ protected function printVerbosePathInformation(): void
7070

7171
protected function printEnabledFeatures(): void
7272
{
73-
foreach (Config::getArray('hyde.features') as $feature) {
73+
/** @var array<string, string> $features */
74+
$features = Config::getArray('hyde.features', []);
75+
76+
foreach ($features as $feature) {
7477
$this->line(" - $feature");
7578
}
7679
}

packages/framework/src/Console/Commands/MakePageCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected function validateOptions(): void
8080
protected function getTitle(): string
8181
{
8282
return $this->argument('title')
83-
?? $this->ask('What is the title of the page?')
83+
?? $this->askForString('What is the title of the page?')
8484
?? 'My New Page';
8585
}
8686

@@ -116,4 +116,9 @@ protected function getTypeOption(): ?string
116116

117117
return null;
118118
}
119+
120+
protected function askForString(string $question, ?string $default = null): ?string
121+
{
122+
return $this->ask($question, $default);
123+
}
119124
}

packages/framework/src/Facades/Config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static function getFloat(string $key, float $default = null): float
4646
/** @experimental Could possibly be merged by allowing null returns if default is null? Preferably with generics so the type is matched by IDE support. */
4747
public static function getNullableString(string $key, string $default = null): ?string
4848
{
49+
/** @var array|string|int|bool|float|null $value */
4950
$value = static::get($key, $default);
5051

5152
if ($value === null) {

packages/framework/src/Facades/Filesystem.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Hyde\Foundation\HydeKernel;
88
use Hyde\Framework\Concerns\Internal\ForwardsIlluminateFilesystem;
99
use Illuminate\Support\Collection;
10-
use Illuminate\Support\Facades\File;
1110

1211
use function app;
1312

packages/framework/src/Foundation/Internal/LoadYamlConfiguration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected function hasYamlConfigFile(): bool
4545
|| file_exists(Hyde::path('hyde.yaml'));
4646
}
4747

48+
/** @return array<string, mixed> */
4849
protected function getYaml(): array
4950
{
5051
return (array) Yaml::parse(file_get_contents($this->getFile()));
@@ -64,6 +65,7 @@ protected function mergeParsedConfiguration(): void
6465
// If the Yaml file contains namespaces, we merge those using more granular logic
6566
// that only applies the namespace data to each configuration namespace.
6667
if ($this->configurationContainsNamespaces($yaml)) {
68+
/** @var array<string, array<string, scalar>> $yaml */
6769
foreach ($yaml as $namespace => $data) {
6870
$this->mergeConfiguration($namespace, (array) $data);
6971
}

packages/framework/src/Framework/Actions/PreBuildTasks/CleanSiteDirectory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ protected function askIfUnsafeDirectoryShouldBeEmptied(): bool
6161
protected function safeOutputDirectories(): array
6262
{
6363
/** @var array<string> $directories */
64-
return Config::getArray('hyde.safe_output_directories', ['_site', 'docs', 'build']);
64+
$directories = Config::getArray('hyde.safe_output_directories', ['_site', 'docs', 'build']);
65+
66+
return $directories;
6567
}
6668
}

packages/framework/src/Framework/Factories/NavigationDataFactory.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,13 @@ private function searchForPriorityInFrontMatter(): ?int
139139

140140
private function searchForLabelInConfig(): ?string
141141
{
142-
return Config::getArray('hyde.navigation.labels', [
142+
/** @var array<string, string> $config */
143+
$config = Config::getArray('hyde.navigation.labels', [
143144
'index' => 'Home',
144145
DocumentationPage::homeRouteName() => 'Docs',
145-
])[$this->routeKey] ?? null;
146+
]);
147+
148+
return $config[$this->routeKey] ?? null;
146149
}
147150

148151
private function searchForPriorityInConfigs(): ?int
@@ -160,19 +163,25 @@ private function searchForPriorityInSidebarConfig(): ?int
160163
// Adding an offset makes so that pages with a front matter priority that is lower can be shown first.
161164
// This is all to make it easier to mix ways of adding priorities.
162165

166+
/** @var array<string> $config */
167+
$config = Config::getArray('docs.sidebar_order', []);
168+
163169
return $this->offset(
164-
array_flip(Config::getArray('docs.sidebar_order', []))[$this->identifier] ?? null,
170+
array_flip($config)[$this->identifier] ?? null,
165171
self::CONFIG_OFFSET
166172
);
167173
}
168174

169175
private function searchForPriorityInNavigationConfig(): ?int
170176
{
171-
return Config::getArray('hyde.navigation.order', [
177+
/** @var array<string, int> $config */
178+
$config = Config::getArray('hyde.navigation.order', [
172179
'index' => 0,
173180
'posts' => 10,
174181
'docs/index' => 100,
175-
])[$this->routeKey] ?? null;
182+
]);
183+
184+
return $config[$this->routeKey] ?? null;
176185
}
177186

178187
private function canUseSubdirectoryForGroups(): bool

packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ protected function getContentLengthForRemoteImage(): int
226226
{
227227
// Check if the --no-api flag is set when running the build command, and if so, skip the API call.
228228
if (Config::getBool('hyde.api_calls', true)) {
229+
/** @var string[][] $headers */
229230
$headers = Http::withHeaders([
230231
'User-Agent' => Config::getString('hyde.http_user_agent', 'RSS Request Client'),
231232
])->head($this->getSource())->headers();

packages/framework/src/Framework/Features/Navigation/NavItem.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ public function isCurrent(): bool
125125

126126
protected static function getRouteGroup(Route $route): ?string
127127
{
128-
return static::normalizeGroupKey($route->getPage()->data('navigation.group'));
128+
/** @var string|null $group */
129+
$group = $route->getPage()->data('navigation.group');
130+
131+
return static::normalizeGroupKey($group);
129132
}
130133

131134
protected static function normalizeGroupKey(?string $group): ?string

0 commit comments

Comments
 (0)