Skip to content

Commit

Permalink
Merge pull request #403 from hydephp/improve-types
Browse files Browse the repository at this point in the history
Improve types
  • Loading branch information
caendesilva authored Aug 11, 2022
2 parents d6f8f45 + 2b9f0c0 commit 77a1251
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/framework/src/Actions/BladeMatterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static function extractValue(string $line): string
}

/** @internal Return the proper type for the string */
public static function normalizeValue($value): mixed
public static function normalizeValue(string $value): mixed
{
$value = trim($value);

Expand Down
3 changes: 2 additions & 1 deletion packages/framework/src/Actions/SourceFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function __construct(string $pageClass, string $identifier)

protected function parseBladePage(): BladePage
{
return new BladePage($this->identifier,
return new BladePage(
$this->identifier,
(BladeMatterParser::parseFile(BladePage::qualifyBasename($this->identifier)))
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Commands/HydeMakePostCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function handle(): int
$this->comment('If you want to overwrite the file supply the --force flag.');
}

return $exception->getCode();
return (int) $exception->getCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
class HydePackageDiscoverCommand extends BaseCommand
{
/** @var true */
protected $hidden = true;

public function handle(PackageManifest $manifest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function handle(): int
return 0;
}

protected function publishOption($selected): void
protected function publishOption(string $selected): void
{
(new PublishesHydeViews($selected))->execute();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function handleException(Exception $exception): int
$this->error('Something went wrong!');
$this->warn($exception->getMessage());

return $exception->getCode();
return (int) $exception->getCode();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function makeDescription(): string
return substr($this->markdown, 0, 125).'...';
}

return $this->markdown;
return (string) $this->markdown;
}

protected function getAuthor(): ?Author
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Contracts/RouteFacadeContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function getFromModel(PageContract $page): RouteContract;
/**
* Get all routes from the Router index.
*
* @return \Illuminate\Support\Collection<\Hyde\Framework\Contracts\RouteContract>
* @return \Hyde\Framework\RouteCollection<\Hyde\Framework\Contracts\RouteContract>
*/
public static function all(): Collection;

Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/HydeKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getBasePath(): string
return $this->basePath;
}

public function setBasePath(string $basePath)
public function setBasePath(string $basePath): void
{
$this->basePath = rtrim($basePath, '/\\');
}
Expand Down
5 changes: 4 additions & 1 deletion packages/framework/src/Models/Pages/MarkdownPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ public function getCanonicalLink(): string
}

/** @deprecated v0.58.x-beta (pull description instead) */
public function getPostDescription(): string
public function getPostDescription(): string|null
{
return $this->description;
}

/**
* @return \Illuminate\Support\Collection<\Hyde\Framework\Models\Pages\MarkdownPost>
*/
public static function getLatestPosts(): Collection
{
return static::all()->sortByDesc('matter.date');
Expand Down
3 changes: 1 addition & 2 deletions packages/framework/src/Models/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\RoutingService;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;

/**
* @see \Hyde\Framework\Testing\Feature\RouteTest
Expand Down Expand Up @@ -131,7 +130,7 @@ public static function getFromModel(PageContract $page): RouteContract
}

/** @inheritDoc */
public static function all(): Collection
public static function all(): \Hyde\Framework\RouteCollection
{
return RoutingService::getInstance()->getRoutes();
}
Expand Down
8 changes: 4 additions & 4 deletions packages/framework/src/Models/ValidationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(string $defaultMessage = 'Generic check')
$this->message = $defaultMessage;
}

public function pass(?string $withMessage = null): self
public function pass(?string $withMessage = null): static
{
$this->passed = true;
if ($withMessage) {
Expand All @@ -29,7 +29,7 @@ public function pass(?string $withMessage = null): self
return $this;
}

public function fail(?string $withMessage = null): self
public function fail(?string $withMessage = null): static
{
$this->passed = false;
if ($withMessage) {
Expand All @@ -39,7 +39,7 @@ public function fail(?string $withMessage = null): self
return $this;
}

public function skip(?string $withMessage = null): self
public function skip(?string $withMessage = null): static
{
$this->skipped = true;
if ($withMessage) {
Expand All @@ -49,7 +49,7 @@ public function skip(?string $withMessage = null): self
return $this;
}

public function withTip(string $withTip): self
public function withTip(string $withTip): static
{
$this->tip = $withTip;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getMarkdownFiles(): array
* @param string $key for a subdirectory of the _data directory
* @return DataCollection<\Hyde\Framework\Models\MarkdownDocument>
*/
public static function markdown(string $key): DataCollection
public static function markdown(string $key): static
{
$collection = new DataCollection($key);
foreach ($collection->getMarkdownFiles() as $file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function process(string $html): string
{
$lines = explode("\n", $html);

/** @var int $index */
foreach ($lines as $index => $line) {
if (str_starts_with($line, '<!-- HYDE[Filepath]')) {
$path = static::trimHydeDirective($line);
Expand Down Expand Up @@ -90,7 +91,7 @@ protected static function resolveTemplate(string $path): string
return sprintf('<small class="filepath"><span class="sr-only">Filepath: </span>%s</small>', $path);
}

protected static function resolveTorchlightCodeLine(string $label, $lines): string|array
protected static function resolveTorchlightCodeLine(string $label, string $lines): string
{
return str_replace(
static::$torchlightKey,
Expand All @@ -99,7 +100,7 @@ protected static function resolveTorchlightCodeLine(string $label, $lines): stri
);
}

protected static function resolveCodeLine(string $label, $lines): string|array|null
protected static function resolveCodeLine(string $label, string $lines): string
{
return preg_replace(
'/<pre><code class="language-(.*?)">/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ protected static function getClassNameFromSignature(string $signature): string
return str_replace('>', '', $signature);
}

/**
* @return array (AbstractColoredBlockquote)[]
*/
public static function get(): array
{
return [
Expand Down
6 changes: 5 additions & 1 deletion packages/framework/src/PageCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getPage(string $sourcePath): PageContract
return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection');
}

public function getPages(?string $pageClass = null): Collection
public function getPages(?string $pageClass = null): self
{
return ! $pageClass ? $this : $this->filter(function (PageContract $page) use ($pageClass): bool {
return $page instanceof $pageClass;
Expand Down Expand Up @@ -67,6 +67,10 @@ protected function discoverPagesFor(string $pageClass): void
});
}

/**
* @param string<\Hyde\Framework\Contracts\PageContract> $pageClass
* @return \Illuminate\Support\Collection<\Hyde\Framework\Contracts\PageContract>
*/
protected function parsePagesFor(string $pageClass): Collection
{
$collection = new Collection();
Expand Down
10 changes: 8 additions & 2 deletions packages/framework/src/Services/BuildService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Framework\Contracts\RouteContract as Route;
use Hyde\Framework\Hyde;
use Hyde\Framework\RouteCollection;
use Hyde\Framework\StaticPageBuilder;
use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\OutputStyle;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;

/**
Expand Down Expand Up @@ -67,7 +67,10 @@ function ($filepath) {
$this->newLine(2);
}

protected function getDiscoveredModels(): Collection
/**
* @return \Hyde\Framework\RouteCollection<array-key, class-string<\Hyde\Framework\Contracts\PageContract>>
*/
protected function getDiscoveredModels(): RouteCollection
{
return $this->router->getRoutes()->map(function (Route $route) {
return $route->getPageType();
Expand All @@ -88,6 +91,9 @@ protected function compilePagesForClass(string $pageClass): void
$this->newLine(2);
}

/**
* @return \Closure(Route):string
*/
protected function compileRoute(): \Closure
{
return function (Route $route) {
Expand Down
6 changes: 3 additions & 3 deletions packages/framework/src/Services/MarkdownService.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ protected function determineIfTorchlightAttributionShouldBeInjected(): bool
protected function injectTorchlightAttribution(): string
{
return '<br>'.$this->converter->convert(config(
'torchlight.attribution.markdown',
'Syntax highlighted by torchlight.dev'
));
'torchlight.attribution.markdown',
'Syntax highlighted by torchlight.dev'
));
}
}
1 change: 1 addition & 0 deletions packages/framework/src/Services/ValidationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
class ValidationService
{
/** @return string[] */
public static function checks(): array
{
$service = new self();
Expand Down
3 changes: 2 additions & 1 deletion packages/framework/src/Views/Components/LinkComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public function __construct(string $href)
$this->href = Hyde::relativeLink($href);
}

public function render(): View|Factory
/** @interitDoc */
public function render(): Factory|View
{
return view('hyde::components.link');
}
Expand Down

0 comments on commit 77a1251

Please sign in to comment.