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
8 changes: 5 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/Cache/Stores/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();

Expand Down
11 changes: 4 additions & 7 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
defined('ABSPATH') || exit;

use Closure;
use Exception;
use ReflectionClass;
use ReflectionParameter;
use TInstance;
Expand Down Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions src/DTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 5 additions & 12 deletions src/Database/Concerns/ExecuteQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use RuntimeException;

use function Framework\collection;
use function Framework\throw_if;

trait ExecuteQueries
{
Expand Down Expand Up @@ -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);

Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/Database/Concerns/HasDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use InvalidArgumentException;

use function Framework\throw_anyway;

trait HasDictionary
{
/**
Expand All @@ -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
);
}

Expand Down
32 changes: 16 additions & 16 deletions src/Database/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
Expand Down
6 changes: 2 additions & 4 deletions src/Database/Query/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
Loading
Loading