diff --git a/src/Application.php b/src/Application.php index 8a9159c..cbc3181 100644 --- a/src/Application.php +++ b/src/Application.php @@ -1061,9 +1061,11 @@ public function get_namespace_for_path($relative_path) $this->base_path('composer.json') ); - if (empty($composer['autoload']['psr-4'])) { - throw new RuntimeException('The composer must have a PSR-4 autoload configuration.'); - } + throw_if( + empty($composer['autoload']['psr-4']), + 'The composer must have a PSR-4 autoload configuration.', + RuntimeException::class + ); $resolved_path = $this->base_path($relative_path); $target = realpath($resolved_path); diff --git a/src/Cache/Stores/FileStore.php b/src/Cache/Stores/FileStore.php index f545bc5..fc30eb7 100644 --- a/src/Cache/Stores/FileStore.php +++ b/src/Cache/Stores/FileStore.php @@ -133,11 +133,11 @@ public static function guard_supported() if (!function_exists('get_filesystem_method')) { $include = ABSPATH . 'wp-admin/includes/file.php'; - if (!is_readable($include)) { - throw new StoreUnavailableException( - 'The file cache store cannot determine the filesystem method on this host.' - ); - } + throw_if( + !is_readable($include), + 'The file cache store cannot determine the filesystem method on this host.', + StoreUnavailableException::class + ); require_once $include; } diff --git a/src/Collections/Collection.php b/src/Collections/Collection.php index 0cbb4c4..a0a9b94 100644 --- a/src/Collections/Collection.php +++ b/src/Collections/Collection.php @@ -28,6 +28,8 @@ use function Framework\Polyfill\array_last; +use function Framework\throw_if; +use function Framework\throw_unless; use function Framework\value; // phpcs:disable Generic.Commenting.DocComment.TagValueIndent @@ -532,9 +534,7 @@ public function find($key, $default = null) return value($default); } - if (!$key instanceof Closure) { - throw new InvalidArgumentException('The key must be a Closure.'); - } + throw_unless($key instanceof Closure, 'The key must be a Closure.', InvalidArgumentException::class); return Arr::first($this->items, $key, $default); } @@ -762,9 +762,7 @@ public function union($items) */ public function only($keys) { - if (empty($keys)) { - throw new InvalidArgumentException('You must pass at least one key to the only method.'); - } + throw_if(empty($keys), 'You must pass at least one key to the only method.', InvalidArgumentException::class); $keys = is_array($keys) ? $keys : func_get_args(); diff --git a/src/Container.php b/src/Container.php index 2dfe021..a1c5f67 100644 --- a/src/Container.php +++ b/src/Container.php @@ -13,7 +13,6 @@ defined('ABSPATH') || exit; use Closure; -use Exception; use ReflectionClass; use ReflectionParameter; use TInstance; @@ -340,12 +339,10 @@ protected function autowire(string $class, array $parameters = []) try { $reflector = new ReflectionClass($class); - if (!$reflector->isInstantiable()) { - throw new Exception(sprintf( - 'Class "%s" is not instantiable.', - $class - )); - } + throw_if(!$reflector->isInstantiable(), sprintf( + 'Class "%s" is not instantiable.', + $class + )); $constructor = $reflector->getConstructor(); diff --git a/src/DTO.php b/src/DTO.php index 47bc643..7e6c303 100644 --- a/src/DTO.php +++ b/src/DTO.php @@ -15,9 +15,10 @@ use Framework\Contracts\Request; use Framework\Contracts\Support\Arrayable; use Framework\Exceptions\ValidationException; -use Exception; use JsonSerializable; +use function Framework\throw_anyway; + class DTO implements JsonSerializable, Arrayable { /** @@ -356,7 +357,7 @@ protected function traverse_and_cast_attribute($current_field_value, array $key_ return $cast(); } - throw new Exception('Cast must be an instance of ' . CastAttribute::class . ' or a callable'); + throw_anyway('Cast must be an instance of ' . CastAttribute::class . ' or a callable'); } $segment = array_shift($key_segments); diff --git a/src/Database/Concerns/ExecuteQueries.php b/src/Database/Concerns/ExecuteQueries.php index ccdb4a9..326a7e9 100644 --- a/src/Database/Concerns/ExecuteQueries.php +++ b/src/Database/Concerns/ExecuteQueries.php @@ -19,6 +19,7 @@ use RuntimeException; use function Framework\collection; +use function Framework\throw_if; trait ExecuteQueries { @@ -222,9 +223,7 @@ public function ordered_chunk_by_id( $last_id = is_null($last_result) ? null : $last_result[$alias]; - if (is_null($last_id)) { - throw new RuntimeException('No more results found'); - } + throw_if(is_null($last_id), 'No more results found', RuntimeException::class); unset($results); @@ -247,9 +246,7 @@ public function ordered_chunk_by_id( */ public function lazy($chunk_size = 1000) { - if ($chunk_size < 1) { - throw new InvalidArgumentException('Chunk size should be at least 1'); - } + throw_if($chunk_size < 1, 'Chunk size should be at least 1', InvalidArgumentException::class); $this->enforce_order_by_primary_key(); @@ -317,9 +314,7 @@ public function lazy_by_id_desc($chunk_size = 1000, $column = null, $alias = nul */ public function ordered_lazy_by_id($chunk_size = 1000, $column = null, $alias = null, $descending = false) { - if ($chunk_size < 1) { - throw new InvalidArgumentException('Chunk size should be at least 1'); - } + throw_if($chunk_size < 1, 'Chunk size should be at least 1', InvalidArgumentException::class); $column ??= $this->default_key_name(); $alias ??= $column; @@ -350,9 +345,7 @@ public function ordered_lazy_by_id($chunk_size = 1000, $column = null, $alias = $last_id = $results->last()[$alias]; - if (is_null($last_id)) { - throw new RuntimeException('The lazy_by_id operation was aborted.'); - } + throw_if(is_null($last_id), 'The lazy_by_id operation was aborted.', RuntimeException::class); } } diff --git a/src/Database/Concerns/HasDictionary.php b/src/Database/Concerns/HasDictionary.php index b8fa8a5..6f1c4ee 100644 --- a/src/Database/Concerns/HasDictionary.php +++ b/src/Database/Concerns/HasDictionary.php @@ -14,6 +14,8 @@ use InvalidArgumentException; +use function Framework\throw_anyway; + trait HasDictionary { /** @@ -38,8 +40,9 @@ protected function get_dictionary_key($attribute) return $attribute->__toString(); } - throw new InvalidArgumentException( - 'Attribute must be a string, integer, or object with a __toString method.' + throw_anyway( + 'Attribute must be a string, integer, or object with a __toString method.', + InvalidArgumentException::class ); } diff --git a/src/Database/Connection/Connection.php b/src/Database/Connection/Connection.php index 13a038d..28a46c9 100644 --- a/src/Database/Connection/Connection.php +++ b/src/Database/Connection/Connection.php @@ -347,16 +347,12 @@ protected function run_query_callback($query, array $bindings, Closure $callback try { $result = $callback($query, $bindings); - if (!empty($this->db->last_error)) { - throw new Exception($this->db->last_error); - } + throw_if(!empty($this->db->last_error), $this->db->last_error ?? ''); - if ($this->db->rows_affected < 0) { - throw new Exception(sprintf( - 'Query failed: %s', - $query - ), 500); - } + throw_if($this->db->rows_affected < 0, sprintf( + 'Query failed: %s', + $query + ), Exception::class, 500); return $result; } catch (Exception $error) { @@ -595,15 +591,19 @@ public function escape($value) } elseif (is_bool($value)) { return $this->escape_bool($value); } elseif (is_array($value)) { - throw new RuntimeException('Database connection does not support escaping arrays.'); + throw_anyway('Database connection does not support escaping arrays.', RuntimeException::class); } else { - if (str_contains($value, "\00")) { - throw new RuntimeException('Strings with null bytes cannot be escaped.'); - } + throw_if( + str_contains($value, "\00"), + 'Strings with null bytes cannot be escaped.', + RuntimeException::class + ); - if (preg_match('//u', $value) === false) { - throw new RuntimeException('Strings with invalid UTF-8 byte sequences cannot be escaped.'); - } + throw_if( + preg_match('//u', $value) === false, + 'Strings with invalid UTF-8 byte sequences cannot be escaped.', + RuntimeException::class + ); return sprintf("'%s'", esc_sql($value)); } diff --git a/src/Database/Query/Model.php b/src/Database/Query/Model.php index 08d7e6b..bbb6c9e 100644 --- a/src/Database/Query/Model.php +++ b/src/Database/Query/Model.php @@ -24,7 +24,6 @@ use Framework\Supports\Traits\Macroable; use Framework\Database\Concerns\GuardAttributes; use Framework\Supports\Facades\Date; -use Exception; use Framework\Database\Concerns\HasTimestamps; use Framework\Database\Connection\Connection; use Framework\Exceptions\MassAssignmentException; @@ -34,6 +33,7 @@ use function Framework\app; use function Framework\Polyfill\str_contains; use function Framework\throw_anyway; +use function Framework\throw_if; abstract class Model implements Arrayable, Jsonable, ArrayAccess, JsonSerializable { @@ -763,9 +763,7 @@ public function delete() { $this->merge_attributes_from_cached_class_casts(); - if (is_null($this->get_primary_key())) { - throw new Exception('No primary key defined on model.'); - } + throw_if(is_null($this->get_primary_key()), 'No primary key defined on model.'); if (!$this->exists) { return false; diff --git a/src/Database/Query/QueryBuilder.php b/src/Database/Query/QueryBuilder.php index d0fb8a8..81d02f3 100644 --- a/src/Database/Query/QueryBuilder.php +++ b/src/Database/Query/QueryBuilder.php @@ -1008,9 +1008,11 @@ public function where_in($column, $values, $boolean = 'and', $not = false) 'boolean' ); - if (count($values) !== count(Arr::flatten($values, 1))) { - throw new InvalidArgumentException('Nested array of values is not allowed'); - } + throw_if( + count($values) !== count(Arr::flatten($values, 1)), + 'Nested array of values is not allowed', + InvalidArgumentException::class + ); $this->add_bindings($this->clean_bindings($values), 'where'); @@ -2256,9 +2258,11 @@ public function order_by($column, $direction = 'asc') $direction = strtolower($direction); - if (!in_array($direction, ['asc', 'desc'], true)) { - throw new InvalidArgumentException('Order direction must be either "asc" or "desc".'); - } + throw_unless( + in_array($direction, ['asc', 'desc'], true), + 'Order direction must be either "asc" or "desc".', + InvalidArgumentException::class + ); $this->orders[] = [ 'column' => $column, @@ -3481,9 +3485,11 @@ public function insert_get_id(array $values) */ public function increment($column, $amount = 1, array $extra = []) { - if (!is_numeric($amount)) { - throw new InvalidArgumentException('Non-numeric value passed to increment method.'); - } + throw_unless( + is_numeric($amount), + 'Non-numeric value passed to increment method.', + InvalidArgumentException::class + ); return $this->increment_each([$column => $amount], $extra); } @@ -3503,11 +3509,16 @@ public function increment($column, $amount = 1, array $extra = []) public function increment_each(array $columns, array $extra = []) { foreach ($columns as $column => $amount) { - if (!is_numeric($amount)) { - throw new InvalidArgumentException('Non-numeric value passed to increment method.'); - } elseif (!is_string($column)) { - throw new InvalidArgumentException('Invalid column provided to increment method.'); - } + throw_unless( + is_numeric($amount), + 'Non-numeric value passed to increment method.', + InvalidArgumentException::class + ); + throw_unless( + is_string($column), + 'Invalid column provided to increment method.', + InvalidArgumentException::class + ); $columns[$column] = $this->raw( sprintf( @@ -3536,9 +3547,11 @@ public function increment_each(array $columns, array $extra = []) */ public function decrement($column, $amount = 1, array $extra = []) { - if (!is_numeric($amount)) { - throw new InvalidArgumentException('Non-numeric value passed to increment method.'); - } + throw_unless( + is_numeric($amount), + 'Non-numeric value passed to increment method.', + InvalidArgumentException::class + ); return $this->decrement_each([$column => $amount], $extra); } @@ -3558,11 +3571,16 @@ public function decrement($column, $amount = 1, array $extra = []) public function decrement_each(array $columns, array $extra = []) { foreach ($columns as $column => $amount) { - if (!is_numeric($amount)) { - throw new InvalidArgumentException('Non-numeric value passed to increment method.'); - } elseif (!is_string($column)) { - throw new InvalidArgumentException('Invalid column provided to increment method.'); - } + throw_unless( + is_numeric($amount), + 'Non-numeric value passed to increment method.', + InvalidArgumentException::class + ); + throw_unless( + is_string($column), + 'Invalid column provided to increment method.', + InvalidArgumentException::class + ); $columns[$column] = $this->raw( sprintf( @@ -3594,9 +3612,7 @@ public function sole($columns = ['*']) $count = $result->count(); - if ($count === 0) { - throw new RecordNotFoundException(); - } + throw_if($count === 0, '', RecordNotFoundException::class); throw_if($count > 1, $count, MultipleRecordsFoundException::class); @@ -4161,9 +4177,7 @@ protected function parse_subquery($query) } elseif (is_string($query)) { return [$query, []]; } else { - throw new InvalidArgumentException( - 'Invalid subquery provided' - ); + throw_anyway('Invalid subquery provided', InvalidArgumentException::class); } } @@ -4217,7 +4231,7 @@ protected function prepare_value_and_operator($value, $operator, $use_default = if ($use_default) { return [$operator, '=']; } elseif ($this->is_invalid_operator_and_value($operator, $value)) { - throw new InvalidArgumentException('Illegal operator and value combination.'); + throw_anyway('Illegal operator and value combination.', InvalidArgumentException::class); } return [$value, $operator]; @@ -4344,8 +4358,9 @@ public function __call(string $method, array $parameters) return $this->call_named_scope($method, $parameters); } - throw new BadMethodCallException( - sprintf('Method %s::%s does not exist.', QueryBuilder::class, esc_html($method)) + throw_anyway( + sprintf('Method %s::%s does not exist.', QueryBuilder::class, esc_html($method)), + BadMethodCallException::class ); } } diff --git a/src/Database/Query/Relations/Relation.php b/src/Database/Query/Relations/Relation.php index b9e2cb8..26913d3 100644 --- a/src/Database/Query/Relations/Relation.php +++ b/src/Database/Query/Relations/Relation.php @@ -20,6 +20,8 @@ use Framework\Database\Query\QueryBuilder; use Framework\Collections\Collection as BaseCollection; +use function Framework\throw_if; + abstract class Relation { /** @@ -539,11 +541,11 @@ public static function without_constraints(Closure $callback) */ public function __call($method, $parameters) { - if (!method_exists($this->query, $method)) { - throw new BadMethodCallException( - sprintf('Method %s::%s does not exist.', QueryBuilder::class, esc_html($method)) - ); - } + throw_if( + !method_exists($this->query, $method), + sprintf('Method %s::%s does not exist.', QueryBuilder::class, esc_html($method)), + BadMethodCallException::class + ); return $this->query->$method(...$parameters); } diff --git a/src/Facade.php b/src/Facade.php index 6e5305d..5531439 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -14,6 +14,7 @@ use RuntimeException; use function Framework\app; +use function Framework\throw_if; abstract class Facade { @@ -97,9 +98,7 @@ public static function __callStatic($method, $arguments) { $instance = static::resolved_facade_instance(static::get_accessor()); - if (!$instance) { - throw new RuntimeException('A facade has not been set.'); - } + throw_if(!$instance, 'A facade has not been set.', RuntimeException::class); return $instance->$method(...$arguments); } diff --git a/src/Filesystem/File.php b/src/Filesystem/File.php index f23f4d5..bdde829 100644 --- a/src/Filesystem/File.php +++ b/src/Filesystem/File.php @@ -78,7 +78,7 @@ public function move(string $directory, ?string $name = null) 'Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, - strip_tags($error ?? '') + wp_strip_all_tags($error ?? '') ), Exception::class ); diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index e29363d..d97c420 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -17,6 +17,7 @@ use Framework\Sanitizer; use Framework\Supports\Traits\Macroable; use Framework\Wordpress\Constants\Capabilities; +use RuntimeException; use WP_Filesystem_Base; use function Framework\Polyfill\str_starts_with; @@ -58,9 +59,11 @@ public function __construct() $this->filesystem = $wp_filesystem; - if (!$this->filesystem instanceof WP_Filesystem_Base) { - throw new \RuntimeException('WordPress filesystem is not available.'); - } + throw_unless( + $this->filesystem instanceof WP_Filesystem_Base, + 'WordPress filesystem is not available.', + RuntimeException::class + ); } /** diff --git a/src/Filesystem/UploadedFile.php b/src/Filesystem/UploadedFile.php index c87fbeb..769a9db 100644 --- a/src/Filesystem/UploadedFile.php +++ b/src/Filesystem/UploadedFile.php @@ -279,7 +279,7 @@ public function move(string $directory, ?string $name = null) 'upload.move_failed', $this->getPathname(), $target, - strip_tags($error ?? '') + wp_strip_all_tags($error ?? '') ), Exception::class ); diff --git a/src/Http/Client/MultipartStream.php b/src/Http/Client/MultipartStream.php index 3557fb9..90aef66 100644 --- a/src/Http/Client/MultipartStream.php +++ b/src/Http/Client/MultipartStream.php @@ -94,9 +94,7 @@ protected function create_stream(array $data) $stream = collection(); foreach ($data as $item) { - if (!is_array($item)) { - throw new UnexpectedValueException('Invalid data format'); - } + throw_unless(is_array($item), 'Invalid data format', UnexpectedValueException::class); $stream->push($this->create_stream_item($item)); } diff --git a/src/Http/Client/Response.php b/src/Http/Client/Response.php index 627750d..a8c0f8b 100644 --- a/src/Http/Client/Response.php +++ b/src/Http/Client/Response.php @@ -14,9 +14,9 @@ use ArrayAccess; use Framework\Concerns\DeepGettable; -use Exception; use function Framework\collection; +use function Framework\throw_anyway; defined('ABSPATH') || exit; @@ -309,7 +309,7 @@ public function offsetGet($offset) */ public function offsetSet($offset, $value): void { - throw new Exception('Response data may not be mutated using array access.'); + throw_anyway('Response data may not be mutated using array access.'); } /** @@ -325,7 +325,7 @@ public function offsetSet($offset, $value): void */ public function offsetUnset($offset): void { - throw new Exception('Response data may not be mutated using array access.'); + throw_anyway('Response data may not be mutated using array access.'); } /** diff --git a/src/Http/Cookie.php b/src/Http/Cookie.php index a68334f..1a53960 100644 --- a/src/Http/Cookie.php +++ b/src/Http/Cookie.php @@ -450,9 +450,7 @@ protected function normalize_same_site($same_site) */ protected function validate_name(string $name) { - if ($name === '') { - throw new InvalidArgumentException('The cookie name cannot be empty.'); - } + throw_if($name === '', 'The cookie name cannot be empty.', InvalidArgumentException::class); throw_if( strpbrk($name, static::RESERVED_CHARACTERS) !== false, diff --git a/src/Http/Request.php b/src/Http/Request.php index c59cfd9..1e4ebc0 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -26,6 +26,7 @@ use function Framework\app; use function Framework\config; use function Framework\message; +use function Framework\throw_if; use function Framework\throw_unless; use function Framework\user; use function Framework\value; @@ -836,9 +837,7 @@ public function all($keys = null) */ public function route($key, $default = null) { - if (empty($key)) { - throw new InvalidArgumentException('The route key is required.'); - } + throw_if(empty($key), 'The route key is required.', InvalidArgumentException::class); return Arr::get($this->route_params, $key, $default); } diff --git a/src/Managers/CookieManager.php b/src/Managers/CookieManager.php index b163164..ef71cdb 100644 --- a/src/Managers/CookieManager.php +++ b/src/Managers/CookieManager.php @@ -17,6 +17,7 @@ use function Framework\app; use function Framework\config; +use function Framework\throw_if; class CookieManager { @@ -167,9 +168,11 @@ public function expire(string $name, ?string $path = null, ?string $domain = nul */ public function queue(...$parameters) { - if (empty($parameters)) { - throw new InvalidArgumentException('A cookie instance or a cookie name is required to queue a cookie.'); - } + throw_if( + empty($parameters), + 'A cookie instance or a cookie name is required to queue a cookie.', + InvalidArgumentException::class + ); $cookie = $parameters[0] instanceof Cookie ? $parameters[0] diff --git a/src/Managers/EventManager.php b/src/Managers/EventManager.php index 27141b6..0f9eaf8 100644 --- a/src/Managers/EventManager.php +++ b/src/Managers/EventManager.php @@ -163,12 +163,10 @@ public function dispatch_unless(Closure $boolean, $event) */ protected function resolve($listener, $event) { - if (!is_subclass_of($listener, Listener::class)) { - throw new InvalidArgumentException(sprintf( - 'The listener [%s] must be a subclass of [%s]', - Listener::class - )); - } + throw_unless(is_subclass_of($listener, Listener::class), sprintf( + 'The listener [%s] must be a subclass of [%s]', + Listener::class + ), InvalidArgumentException::class); return (new $listener())->handle($event); } diff --git a/src/Supports/Arr.php b/src/Supports/Arr.php index 64d09f7..02b8552 100644 --- a/src/Supports/Arr.php +++ b/src/Supports/Arr.php @@ -30,6 +30,7 @@ use function Framework\Polyfill\array_first; use function Framework\Polyfill\array_last; use function Framework\Polyfill\str_contains; +use function Framework\throw_anyway; use function Framework\value; class Arr @@ -61,7 +62,7 @@ public static function from($items) case is_object($items): return (array) $items; default: - throw new InvalidArgumentException('Items cannot be represented by a scalar value.'); + throw_anyway('Items cannot be represented by a scalar value.', InvalidArgumentException::class); } } diff --git a/src/Supports/DataCaster.php b/src/Supports/DataCaster.php index 7a80e64..811919f 100644 --- a/src/Supports/DataCaster.php +++ b/src/Supports/DataCaster.php @@ -12,7 +12,7 @@ defined('ABSPATH') || exit; -use Exception; +use function Framework\throw_if; class DataCaster { @@ -85,9 +85,7 @@ public static function cast_value($value, $type = null) */ public static function cast_data($data, $map) { - if (!is_array($data) && !is_object($data)) { - throw new Exception('Data must be either an array or an object'); - } + throw_if(!is_array($data) && !is_object($data), 'Data must be either an array or an object'); if (is_object($data)) { foreach ($map as $key => $type) { diff --git a/src/Supports/MessagesBag.php b/src/Supports/MessagesBag.php index e7f8e7f..5a241a1 100644 --- a/src/Supports/MessagesBag.php +++ b/src/Supports/MessagesBag.php @@ -12,6 +12,7 @@ use function Framework\app; use function Framework\config; +use function Framework\throw_if; defined('ABSPATH') || exit; @@ -233,9 +234,7 @@ public function get(string $key, ...$args) $args = !empty($args) ? Arr::flatten($args) : []; $message = Arr::get($this->messages(), $key, ''); - if (is_array($message)) { - throw new InvalidArgumentException('You may forget to define the full path of the message key.'); - } + throw_if(is_array($message), 'You may forget to define the full path of the message key.', InvalidArgumentException::class); return vsprintf($message, $args); } diff --git a/src/Supports/Traits/Macroable.php b/src/Supports/Traits/Macroable.php index 1bdae7e..3802161 100644 --- a/src/Supports/Traits/Macroable.php +++ b/src/Supports/Traits/Macroable.php @@ -14,6 +14,8 @@ use BadMethodCallException; +use function Framework\throw_anyway; + trait Macroable { /** @@ -75,8 +77,9 @@ public function __call(string $method, array $arguments) ); } - throw new BadMethodCallException( - sprintf('Method %s::%s does not exist.', static::class, esc_html($method)) + throw_anyway( + sprintf('Method %s::%s does not exist.', static::class, esc_html($method)), + BadMethodCallException::class ); } @@ -101,8 +104,9 @@ public static function __callStatic(string $method, array $arguments) ); } - throw new BadMethodCallException( - sprintf('Method %s::%s does not exist.', static::class, esc_html($method)) + throw_anyway( + sprintf('Method %s::%s does not exist.', static::class, esc_html($method)), + BadMethodCallException::class ); } } diff --git a/src/Validation/Rules/ProhibitedIfRule.php b/src/Validation/Rules/ProhibitedIfRule.php index 1596387..1c9fada 100644 --- a/src/Validation/Rules/ProhibitedIfRule.php +++ b/src/Validation/Rules/ProhibitedIfRule.php @@ -14,6 +14,7 @@ use InvalidArgumentException; use function Framework\deep_get; +use function Framework\throw_if; defined('ABSPATH') || exit; @@ -77,9 +78,11 @@ protected function get_callback() if (!$other instanceof Closure) { $other = function () use ($other, $value) { - if (empty($value)) { - throw new InvalidArgumentException('The second argument must be a non-empty string.'); - } + throw_if( + empty($value), + 'The second argument must be a non-empty string.', + InvalidArgumentException::class + ); $data = deep_get($this->data, (string) $other); diff --git a/src/Validation/Rules/ProhibitedUnlessRule.php b/src/Validation/Rules/ProhibitedUnlessRule.php index 04e4239..68f7535 100644 --- a/src/Validation/Rules/ProhibitedUnlessRule.php +++ b/src/Validation/Rules/ProhibitedUnlessRule.php @@ -14,6 +14,7 @@ use InvalidArgumentException; use function Framework\deep_get; +use function Framework\throw_if; defined('ABSPATH') || exit; @@ -77,9 +78,11 @@ protected function get_callback() if (!$other instanceof Closure) { $other = function () use ($other, $value) { - if (empty($value)) { - throw new InvalidArgumentException('The second argument must be a non-empty string.'); - } + throw_if( + empty($value), + 'The second argument must be a non-empty string.', + InvalidArgumentException::class + ); $data = deep_get($this->data, (string) $other); diff --git a/src/Validation/Rules/RequiredIfRule.php b/src/Validation/Rules/RequiredIfRule.php index d96cd4a..49f8e5e 100644 --- a/src/Validation/Rules/RequiredIfRule.php +++ b/src/Validation/Rules/RequiredIfRule.php @@ -15,6 +15,7 @@ use function Framework\deep_get; use function Framework\Polyfill\array_first; +use function Framework\throw_if; defined('ABSPATH') || exit; @@ -75,9 +76,11 @@ protected function get_callback() if (!$other instanceof Closure) { $other = function () use ($other, $value) { - if (empty($value)) { - throw new InvalidArgumentException('The second argument must be a non-empty string.'); - } + throw_if( + empty($value), + 'The second argument must be a non-empty string.', + InvalidArgumentException::class + ); $data = deep_get($this->data, (string) $other); diff --git a/src/Validation/Rules/RequiredUnlessRule.php b/src/Validation/Rules/RequiredUnlessRule.php index 7af2ca5..31372d8 100644 --- a/src/Validation/Rules/RequiredUnlessRule.php +++ b/src/Validation/Rules/RequiredUnlessRule.php @@ -15,6 +15,7 @@ use function Framework\deep_get; use function Framework\Polyfill\array_first; +use function Framework\throw_if; defined('ABSPATH') || exit; @@ -75,9 +76,11 @@ protected function get_callback() if (!$other instanceof Closure) { $other = function () use ($other, $value) { - if (empty($value)) { - throw new InvalidArgumentException('The second argument must be a non-empty string.'); - } + throw_if( + empty($value), + 'The second argument must be a non-empty string.', + InvalidArgumentException::class + ); $data = deep_get($this->data, (string) $other); diff --git a/src/View/SectionManager.php b/src/View/SectionManager.php index 33d66f1..47651b1 100644 --- a/src/View/SectionManager.php +++ b/src/View/SectionManager.php @@ -76,9 +76,11 @@ public function start(string $name) */ public function end() { - if ($this->active_section === null) { - throw new RuntimeException('Cannot end section: no section is being captured.'); - } + throw_if( + $this->active_section === null, + 'Cannot end section: no section is being captured.', + RuntimeException::class + ); $this->sections[$this->active_section] = (string) ob_get_clean(); $this->active_section = null; diff --git a/src/Wordpress/Menu.php b/src/Wordpress/Menu.php index a97700f..68a3b33 100644 --- a/src/Wordpress/Menu.php +++ b/src/Wordpress/Menu.php @@ -14,9 +14,9 @@ use Framework\Wordpress\Constants\MenuTypes; use Framework\Supports\Arr; -use Exception; use function Framework\collection; +use function Framework\throw_if; class Menu { @@ -113,9 +113,7 @@ class Menu */ public function __construct() { - if (!$this->check_required_properties()) { - throw new Exception('Missing required properties for making a menu item'); - } + throw_if(!$this->check_required_properties(), 'Missing required properties for making a menu item'); } /**