Skip to content

Commit

Permalink
refactor: better handling of empty asset
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Jan 5, 2024
1 parent 166c0ef commit b33fac4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Asset implements \ArrayAccess
*
* @throws RuntimeException
*/
public function __construct(Builder $builder, $paths, array $options = null)
public function __construct(Builder $builder, string|array $paths, array|null $options = null)
{
$this->builder = $builder;
$this->config = $builder->getConfig();
Expand Down
6 changes: 5 additions & 1 deletion src/Renderer/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,12 @@ public function url(array $context, $value = null, array $options = null): strin
*
* @return Asset
*/
public function asset($path, array $options = null): Asset
public function asset($path, array|null $options = null): Asset
{
if (!\is_string($path) && !\is_array($path)) {
throw new RuntimeException(sprintf('Argument of "%s()" must a string or an array.', \Cecil\Util::formatMethodName(__METHOD__)));
}

return new Asset($this->builder, $path, $options);
}

Expand Down
14 changes: 14 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ public static function formatClassName($class, array $options = []): string
return $className;
}

/**
* Formats a method name.
*
* ie: "Cecil\Renderer\Extension\Core::asset()" become "asset()"
*
* @param string $method
*/
public static function formatMethodName(string $method): string
{
$methodName = explode('::', $method)[1];

return $methodName;
}

/**
* Converts an array of strings into a path.
*/
Expand Down

0 comments on commit b33fac4

Please sign in to comment.