Skip to content
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
29 changes: 15 additions & 14 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@
use Framework\Supports\Facades\File;
use Framework\Supports\Str;
use Framework\Supports\Traits\Macroable;
use Exception;
use Framework\Supports\Arr;
use InvalidArgumentException;
use RuntimeException;

use function Framework\throw_anyway;
use function Framework\throw_if;
use function Framework\throw_unless;

class Application extends Container
{
use Macroable;
Expand Down Expand Up @@ -497,9 +500,7 @@ public static function configure(string $base_path)
*/
public function use_routing(string $path)
{
if (!file_exists($path)) {
throw new Exception("Route file not found: $path");
}
throw_unless(file_exists($path), "Route file not found: $path");

include $path;

Expand Down Expand Up @@ -914,15 +915,15 @@ protected function register_app_defined_providers()
}

foreach ($providers as $provider) {
if (!class_exists($provider) || !is_subclass_of($provider, ServiceProvider::class)) {
throw new InvalidArgumentException(
sprintf(
'Class %s must be a subclass of %s.',
$provider,
ServiceProvider::class
)
);
}
throw_if(
!class_exists($provider) || !is_subclass_of($provider, ServiceProvider::class),
sprintf(
'Class %s must be a subclass of %s.',
$provider,
ServiceProvider::class
),
InvalidArgumentException::class
);

$this->register(new $provider($this));
}
Expand Down Expand Up @@ -1084,7 +1085,7 @@ public function get_namespace_for_path($relative_path)
}
}

throw new RuntimeException(sprintf('Unable to detect namespace for path [%s].', $relative_path));
throw_anyway(sprintf('Unable to detect namespace for path [%s].', $relative_path), RuntimeException::class);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

use function Framework\app;
use function Framework\config;
use function Framework\throw_anyway;
use function Framework\throw_if;

class CacheManager
{
Expand Down Expand Up @@ -202,11 +204,11 @@ public function store_config(string $name)
$configured = config('cache.stores');
$configured = is_array($configured) ? $configured : [];

if (!isset($defaults[$name]) && !isset($configured[$name])) {
throw new InvalidArgumentException(
sprintf('Cache store [%s] is not configured. Check the "cache.stores" configuration.', $name)
);
}
throw_if(
!isset($defaults[$name]) && !isset($configured[$name]),
sprintf('Cache store [%s] is not configured. Check the "cache.stores" configuration.', $name),
InvalidArgumentException::class
);

return array_merge($defaults[$name] ?? [], $configured[$name] ?? []);
}
Expand Down Expand Up @@ -278,8 +280,9 @@ protected function resolve(string $name)
return $this->create_file_repository($name, $config);
}

throw new InvalidArgumentException(
sprintf('Unsupported cache driver [%s] for store [%s].', (string) $driver, $name)
throw_anyway(
sprintf('Unsupported cache driver [%s] for store [%s].', (string) $driver, $name),
InvalidArgumentException::class
);
}

Expand Down
40 changes: 20 additions & 20 deletions src/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use Throwable;

use function Framework\app;
use function Framework\throw_if;
use function Framework\throw_unless;
use function Framework\value;

class Repository implements ArrayAccess
Expand Down Expand Up @@ -234,9 +236,7 @@ public function string($key, $default = null)
{
$value = $this->get($key, $default);

if (!is_string($value)) {
throw new InvalidArgumentException($this->type_error($key, 'a string', $value));
}
throw_unless(is_string($value), $this->type_error($key, 'a string', $value), InvalidArgumentException::class);

return $value;
}
Expand All @@ -257,9 +257,11 @@ public function integer($key, $default = null)
{
$value = $this->get($key, $default);

if (filter_var($value, FILTER_VALIDATE_INT) === false) {
throw new InvalidArgumentException($this->type_error($key, 'an integer', $value));
}
throw_if(
filter_var($value, FILTER_VALIDATE_INT) === false,
$this->type_error($key, 'an integer', $value),
InvalidArgumentException::class
);

return (int) $value;
}
Expand All @@ -280,9 +282,11 @@ public function float($key, $default = null)
{
$value = $this->get($key, $default);

if (filter_var($value, FILTER_VALIDATE_FLOAT) === false) {
throw new InvalidArgumentException($this->type_error($key, 'a float', $value));
}
throw_if(
filter_var($value, FILTER_VALIDATE_FLOAT) === false,
$this->type_error($key, 'a float', $value),
InvalidArgumentException::class
);

return (float) $value;
}
Expand All @@ -303,9 +307,7 @@ public function boolean($key, $default = null)
{
$value = $this->get($key, $default);

if (!is_bool($value)) {
throw new InvalidArgumentException($this->type_error($key, 'a boolean', $value));
}
throw_unless(is_bool($value), $this->type_error($key, 'a boolean', $value), InvalidArgumentException::class);

return $value;
}
Expand All @@ -326,9 +328,7 @@ public function array($key, $default = null)
{
$value = $this->get($key, $default);

if (!is_array($value)) {
throw new InvalidArgumentException($this->type_error($key, 'an array', $value));
}
throw_unless(is_array($value), $this->type_error($key, 'an array', $value), InvalidArgumentException::class);

return $value;
}
Expand Down Expand Up @@ -612,11 +612,11 @@ public function flexible($key, array $ttl, Closure $callback)
{
$key = (string) $key;

if (count($ttl) !== 2) {
throw new InvalidArgumentException(
sprintf('The flexible lifetime for key [%s] must be a fresh and stale pair.', $key)
);
}
throw_if(
count($ttl) !== 2,
sprintf('The flexible lifetime for key [%s] must be a fresh and stale pair.', $key),
InvalidArgumentException::class
);

$fresh = (int) $this->seconds_until($ttl[0]);
$stale = (int) $this->seconds_until($ttl[1]);
Expand Down
12 changes: 7 additions & 5 deletions src/Cache/Stores/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Framework\Filesystem\Filesystem;
use Throwable;

use function Framework\throw_if;

class FileStore implements Store, CacheEntryProvider
{
use HashesKeys;
Expand Down Expand Up @@ -142,11 +144,11 @@ public static function guard_supported()

$method = get_filesystem_method();

if ($method !== 'direct') {
throw new StoreUnavailableException(
sprintf('The file cache store requires direct filesystem access, got [%s].', (string) $method)
);
}
throw_if(
$method !== 'direct',
sprintf('The file cache store requires direct filesystem access, got [%s].', (string) $method),
StoreUnavailableException::class
);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Collections/Concerns/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Framework\Collections\HigherOrderCollectionProxy;

use function Framework\deep_get;
use function Framework\throw_unless;

/**
* Trait to enumerate values of the collection.
Expand Down Expand Up @@ -113,9 +114,11 @@ protected function value_retriever($value)
*/
public function __get($key)
{
if (!in_array($key, static::$proxies, true)) {
throw new Exception(sprintf('Property [%s] does not exist on this collection.', $key));
}
throw_unless(
in_array($key, static::$proxies, true),
sprintf('Property [%s] does not exist on this collection.', $key),
Exception::class
);

return new HigherOrderCollectionProxy($this, $key);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/DependencyResolvable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ReflectionParameter;

use function Framework\app;
use function Framework\throw_anyway;

trait DependencyResolvable
{
Expand All @@ -38,11 +39,11 @@ protected function resolve_primitive(ReflectionParameter $parameter)
return $parameter->getDefaultValue();
}

throw new ReflectionException(sprintf(
throw_anyway(sprintf(
'Unable to resolve primitive parameter "%s" in class "%s".',
$parameter->getName(),
$parameter->getDeclaringClass()->getName()
));
), ReflectionException::class);
}

/**
Expand Down
19 changes: 11 additions & 8 deletions src/Console/CommandManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use RuntimeException;

use function Framework\app;
use function Framework\throw_unless;

class CommandManager
{
Expand Down Expand Up @@ -59,18 +60,20 @@ public function register(string $name, $command)
protected function resolve($command)
{
if (is_object($command)) {
if (!$command instanceof CommandBase) {
throw new RuntimeException(
sprintf("Command [%s] must extend [%s]", get_class($command), CommandBase::class)
);
}
throw_unless(
$command instanceof CommandBase,
sprintf("Command [%s] must extend [%s]", get_class($command), CommandBase::class),
RuntimeException::class
);

return $command;
}

if (!class_exists($command)) {
throw new RuntimeException(sprintf("Command class [%s] not found", $command));
}
throw_unless(
class_exists($command),
sprintf("Command class [%s] not found", $command),
RuntimeException::class
);

return app()->make($command);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use LogicException;
use ReflectionNamedType;

use function Framework\throw_anyway;
use function Framework\throw_if;

class Container implements ContainerContract
{
/**
Expand Down Expand Up @@ -188,9 +191,7 @@ public function instance(string $abstract, $instance)
*/
public function alias(string $alias, string $abstract)
{
if ($alias === $abstract) {
throw new LogicException(sprintf('[%s] is aliased to itself.', $abstract));
}
throw_if($alias === $abstract, sprintf('[%s] is aliased to itself.', $abstract), LogicException::class);

$this->aliases[$alias] = $abstract;
}
Expand Down Expand Up @@ -328,7 +329,7 @@ protected function autowire(string $class, array $parameters = [])
// Check for circular dependencies
if ($this->resolved($class)) {
$chain = implode(' → ', $this->resolved) . " → {$class}";
throw new Exception(sprintf(
throw_anyway(sprintf(
'Circular dependency detected: %s',
$chain
));
Expand Down Expand Up @@ -416,7 +417,7 @@ protected function resolve_primitive(ReflectionParameter $parameter, array $prim
return $parameter->getDefaultValue();
}

throw new Exception(sprintf(
throw_anyway(sprintf(
'Unable to resolve primitive parameter "%s" in class "%s".',
$param_name,
$parameter->getDeclaringClass()->getName()
Expand Down
6 changes: 4 additions & 2 deletions src/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Framework\View\ViewContext;

use function Framework\config;
use function Framework\throw_anyway;

class CoreServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -146,8 +147,9 @@ protected function register_session_services()
return new ArraySessionHandler();
}

throw new InvalidArgumentException(
sprintf('Unsupported session driver [%s]. Use "database" or "array".', (string) $driver)
throw_anyway(
sprintf('Unsupported session driver [%s]. Use "database" or "array".', (string) $driver),
InvalidArgumentException::class
);
});

Expand Down
Loading
Loading