Skip to content

Commit

Permalink
Merge pull request #1411 from hydephp/code-quality
Browse files Browse the repository at this point in the history
General code quality cleanups
  • Loading branch information
caendesilva authored Oct 28, 2023
2 parents 68471d7 + f7a13bc commit c00dbf4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
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

0 comments on commit c00dbf4

Please sign in to comment.