Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type coverage #1410

Merged
merged 23 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4a8d8d9
Add array generics for static helper method parameter
caendesilva Oct 28, 2023
cfdc103
Annotate return collection generics
caendesilva Oct 28, 2023
ec1ccc5
Annotate path array generics
caendesilva Oct 28, 2023
9750ee5
Specify collection key generics
caendesilva Oct 28, 2023
0b4f749
Add values call
caendesilva Oct 28, 2023
ab5bb32
Annotate config generics
caendesilva Oct 28, 2023
257f69b
Annotate metadata bag generics
caendesilva Oct 28, 2023
7d0de8c
Add string cast to last branch
caendesilva Oct 28, 2023
9eb57c5
Annotate return array generics
caendesilva Oct 28, 2023
426c54f
Annotate full loop generics
caendesilva Oct 28, 2023
27722a6
Annotate array return generics
caendesilva Oct 28, 2023
5337900
Add collection generics for return value
caendesilva Oct 28, 2023
8eb1d80
Annotate command data generics
caendesilva Oct 28, 2023
c242e50
Narrow down parsed BladeMatter array types
caendesilva Oct 28, 2023
a0cb717
Merge branch 'master' into code-quality
caendesilva Oct 28, 2023
85de426
Add generic array annotation
caendesilva Oct 28, 2023
cc5f355
Annotate class string parameter
caendesilva Oct 28, 2023
6fedad8
Annotate class string array return generics
caendesilva Oct 28, 2023
b2b9ae6
Extract helper method for increased type coverage
caendesilva Oct 28, 2023
7037a3f
Narrow down base class type hints to implementation unions
caendesilva Oct 28, 2023
b1318e6
Add generic variable annotation
caendesilva Oct 28, 2023
d03b7f0
Add class string parameter type annotation
caendesilva Oct 28, 2023
d8c09bd
Update RELEASE_NOTES.md
caendesilva Oct 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ This serves two purposes:
- Updated the realtime compiler to generate the documentation search index each time it's requested in https://github.com/hydephp/develop/pull/1405 (fixes https://github.com/hydephp/develop/issues/1404)
- Updated the navigation menu generator to remove duplicates after running the sorting method in https://github.com/hydephp/develop/pull/1407 (fixes https://github.com/hydephp/develop/issues/1406)
- Updated the exception message caused by missing source option for featured images to include the path of the file that caused the error in https://github.com/hydephp/develop/pull/1409
- Narrows down parsed `BladeMatter` array types to `array<string, scalar>` (Experimental feature not covered by BC promise) in https://github.com/hydephp/develop/pull/1410
- Internal code refactors and improvements in https://github.com/hydephp/develop/pull/1410

### Deprecated
- for soon-to-be removed features.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class PublishHomepageCommand extends Command
/** @var string */
protected $description = 'Publish one of the default homepages to index.blade.php.';

/** @var array<string, array{name: string, description: string, group: string}> */
protected array $options = [
'welcome'=> [
'name' => 'Welcome',
Expand Down Expand Up @@ -96,6 +97,7 @@ protected function formatPublishableChoices(): array
})->values()->toArray();
}

/** @return Collection<array{name: string, description: string, group: string}> */
protected function getTemplateOptions(): Collection
{
return new Collection($this->options);
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Facades/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function relativePath(string $path): string
*
* @param string $pattern
* @param int $flags
* @return \Illuminate\Support\Collection<string>
* @return \Illuminate\Support\Collection<int, string>
*/
public static function smartGlob(string $pattern, int $flags = 0): Collection
{
Expand Down
10 changes: 9 additions & 1 deletion packages/framework/src/Foundation/Kernel/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function path(string $path = ''): string
* Get an absolute file path from a supplied relative path.
*
* Input types are matched, meaning that if the input is a string so will the output be.
*
* @param string|array<string> $path
*/
public function pathToAbsolute(string|array $path): string|array
{
Expand Down Expand Up @@ -143,6 +145,8 @@ public function vendorPath(string $path = '', string $package = 'framework'): st

/**
* Touch one or more files in the project's directory.
*
* @param string|array<string> $path
*/
public function touch(string|array $path): bool
{
Expand All @@ -159,6 +163,8 @@ public function touch(string|array $path): bool

/**
* Unlink one or more files in the project's directory.
*
* @param string|array<string> $path
*/
public function unlink(string|array $path): bool
{
Expand All @@ -185,9 +191,11 @@ public function unlinkIfExists(string $path): bool
return false;
}

/** @return \Illuminate\Support\Collection<int, string> */
public function smartGlob(string $pattern, int $flags = 0): Collection
{
return collect(\Hyde\Facades\Filesystem::glob($pattern, $flags))
->map(fn (string $path): string => $this->pathToRelative($path));
->map(fn (string $path): string => $this->pathToRelative($path))
->values();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected static function extractValue(string $line): string
return trim($key);
}

/** @return scalar|array<string, scalar> */
protected static function getValueWithType(string $value): mixed
{
$value = trim($value);
Expand All @@ -138,6 +139,7 @@ protected static function getValueWithType(string $value): mixed
return json_decode($value) ?? $value;
}

/** @return array<string, scalar> */
protected static function parseArrayString(string $string): array
{
$array = [];
Expand Down Expand Up @@ -167,7 +169,9 @@ protected static function parseArrayString(string $string): array
$pair = explode('=>', $token);

// Add key/value pair to array
$array[static::getValueWithType(trim(trim($pair[0]), "'"))] = static::getValueWithType(trim(trim($pair[1]), "'"));
$key = (string) static::getValueWithType(trim(trim($pair[0]), "'"));
$value = static::getValueWithType(trim(trim($pair[1]), "'"));
$array[$key] = $value;
}

return $array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ protected function askIfUnsafeDirectoryShouldBeEmptied(): bool
));
}

/** @return array<string> */
protected function safeOutputDirectories(): array
{
/** @var array<string> $directories */
return Config::getArray('hyde.safe_output_directories', ['_site', 'docs', 'build']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ protected function getSubdirectoryConfiguration(): string
return Config::getString('hyde.navigation.subdirectories', 'hidden');
}

/** @param class-string $class */
protected function isInstanceOf(string $class): bool
{
return is_a($this->pageClass, $class, true);
Expand Down
16 changes: 13 additions & 3 deletions packages/framework/src/Framework/Features/Metadata/MetadataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@
*/
class MetadataBag implements Htmlable
{
/** @var array<string, MetadataElementContract> */
protected array $links = [];

/** @var array<string, MetadataElementContract> */
protected array $metadata = [];

/** @var array<string, MetadataElementContract> */
protected array $properties = [];

/** @var array<string> */
protected array $generics = [];

public function toHtml(): string
Expand Down Expand Up @@ -59,7 +66,7 @@ public function add(MetadataElementContract|string $element): static
return $this->addElement('properties', $element);
}

return $this->addGenericElement($element);
return $this->addGenericElement((string) $element);
}

protected function addElement(string $type, MetadataElementContract $element): static
Expand All @@ -76,12 +83,15 @@ protected function addGenericElement(string $element): static
return $this;
}

/** @return array<string, MetadataElementContract> */
protected function getPrefixedArray(string $type): array
{
/** @var array<string, MetadataElementContract> $bag */
$bag = $this->{$type};

$array = [];

/** @var MetadataElementContract $element */
foreach ($this->{$type} as $key => $element) {
foreach ($bag as $key => $element) {
$array["$type:$key"] = $element;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,27 @@ public function __construct(string $label, array $items, ?int $priority = null)
$this->items = $items;
}

/** @param array<NavItem> $items */
public static function fromArray(string $name, array $items): static
{
return new static($name, $items);
}

/** @return Collection<NavItem> */
public function getItems(): Collection
{
return collect($this->items);
}

private function searchForDropdownPriorityInNavigationConfig(string $groupKey): ?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,
])[$groupKey] ?? null;
]);

return $config[$groupKey] ?? null;
}
}
13 changes: 11 additions & 2 deletions packages/framework/src/Framework/Services/BuildTaskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct()
{
$this->registerFrameworkTasks();

$this->registerTasks(Config::getArray('hyde.build_tasks', []));
$this->registerTasks($this->findTasksInConfig());

$this->registerTasks($this->findTasksInAppDirectory());
}
Expand All @@ -52,7 +52,7 @@ public function setOutput(?OutputStyle $output): void
$this->output = $output;
}

/** @return array<class-string<\Hyde\Framework\Features\BuildTasks\BuildTask>> */
/** @return array<class-string<\Hyde\Framework\Features\BuildTasks\PreBuildTask>|class-string<\Hyde\Framework\Features\BuildTasks\PostBuildTask>> */
public function getRegisteredTasks(): array
{
return array_map(fn (BuildTask $task): string => $task::class, array_values($this->buildTasks));
Expand Down Expand Up @@ -87,20 +87,29 @@ protected function registerTaskInService(PreBuildTask|PostBuildTask $task): void
$this->buildTasks[$this->makeTaskIdentifier($task)] = $task;
}

/** @param class-string<\Hyde\Framework\Features\BuildTasks\PreBuildTask|\Hyde\Framework\Features\BuildTasks\PostBuildTask> $task */
protected function registerIf(string $task, bool $condition): void
{
if ($condition) {
$this->registerTask($task);
}
}

/** @param array<\Hyde\Framework\Features\BuildTasks\PreBuildTask|\Hyde\Framework\Features\BuildTasks\PostBuildTask|class-string<\Hyde\Framework\Features\BuildTasks\PreBuildTask|\Hyde\Framework\Features\BuildTasks\PostBuildTask>> $tasks */
protected function registerTasks(array $tasks): void
{
foreach ($tasks as $task) {
$this->registerTask($task);
}
}

/** @return array<class-string<\Hyde\Framework\Features\BuildTasks\PreBuildTask>|class-string<\Hyde\Framework\Features\BuildTasks\PostBuildTask>> */
protected function findTasksInConfig(): array
{
return Config::getArray('hyde.build_tasks', []);
}

/** @return array<class-string<\Hyde\Framework\Features\BuildTasks\PreBuildTask>|class-string<\Hyde\Framework\Features\BuildTasks\PostBuildTask>> */
protected function findTasksInAppDirectory(): array
{
return Filesystem::smartGlob('app/Actions/*BuildTask.php')->map(function (string $file): string {
Expand Down
Loading