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

General code quality cleanups #1411

Merged
merged 6 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This serves two purposes:
- 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
- Internal code refactors and improvements in https://github.com/hydephp/develop/pull/1410 and https://github.com/hydephp/develop/pull/1411

### Deprecated
- for soon-to-be removed features.
Expand Down
3 changes: 2 additions & 1 deletion packages/framework/src/Facades/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use TypeError;

use function sprintf;
use function call_user_func;

/**
* An extension of the Laravel Config facade with extra
Expand Down Expand Up @@ -56,7 +57,7 @@ public static function getNullableString(string $key, string $default = null): ?

protected static function validated(mixed $value, string $type, string $key): mixed
{
if (! ("is_$type")($value)) {
if (! call_user_func("is_$type", $value)) {
throw new TypeError(sprintf('%s(): Config value %s must be of type %s, %s given', __METHOD__, $key, $type, gettype($value)));
}

Expand Down
19 changes: 6 additions & 13 deletions packages/framework/src/Framework/Features/Metadata/MetadataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,12 @@ public function get(): array

public function add(MetadataElementContract|string $element): static
{
if ($element instanceof LinkElement) {
return $this->addElement('links', $element);
}

if ($element instanceof MetadataElement) {
return $this->addElement('metadata', $element);
}

if ($element instanceof OpenGraphElement) {
return $this->addElement('properties', $element);
}

return $this->addGenericElement((string) $element);
return match (true) {
$element instanceof LinkElement => $this->addElement('links', $element),
$element instanceof MetadataElement => $this->addElement('metadata', $element),
$element instanceof OpenGraphElement => $this->addElement('properties', $element),
default => $this->addGenericElement((string) $element),
};
}

protected function addElement(string $type, MetadataElementContract $element): static
Expand Down
9 changes: 9 additions & 0 deletions packages/framework/tests/Feature/TypedConfigFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hyde\Facades\Config;
use Hyde\Testing\TestCase;
use TypeError;
use stdClass;

use function config;

Expand Down Expand Up @@ -159,6 +160,14 @@ public function testGetNullableString()
$this->assertNull(Config::getNullableString('foo'));
}

public function testInvalidTypeMessage()
{
config(['foo' => new stdClass()]);
$this->expectException(TypeError::class);
$this->expectExceptionMessage('Hyde\Facades\Config::validated(): Config value foo must be of type array, object given');
Config::getArray('foo');
}

protected function runUnitTest($actual, $expected, $method): void
{
config(['foo' => $actual]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Foundation\Kernel\Hyperlinks::hasSiteUrl
* @covers \Hyde\Foundation\Kernel\Hyperlinks::url
* @covers \Hyde\Foundation\Kernel\Hyperlinks
* @covers \Hyde\Framework\Exceptions\BaseUrlNotSetException
*/
class HyperlinksUrlPathHelpersTest extends TestCase
Expand Down