Replace most of LogicException with more appropriate exceptions

This commit is contained in:
Giuseppe Criscione 2020-11-14 17:41:49 +01:00
parent fa0b9f88b0
commit 86ba25cd84
11 changed files with 32 additions and 32 deletions

View File

@ -14,7 +14,7 @@ use Formwork\Traits\SingletonTrait;
use Formwork\Utils\FileSystem;
use Formwork\Utils\HTTPRequest;
use Formwork\Utils\Uri;
use LogicException;
use BadMethodCallException;
use RuntimeException;
use Throwable;
@ -394,6 +394,6 @@ class Admin
if (method_exists(AdminTrait::class, $name)) {
return $this->$name(...$arguments);
}
throw new LogicException('Invalid method ' . static::class . '::' . $name);
throw new BadMethodCallException('Call to undefined method ' . static::class . '::' . $name . '()');
}
}

View File

@ -3,7 +3,7 @@ namespace Formwork\Admin\Fields;
use Formwork\Admin\View\View;
use Formwork\Data\DataSetter;
use LogicException;
use UnexpectedValueException;
class Field extends DataSetter
{
@ -151,11 +151,11 @@ class Field extends DataSetter
{
foreach ((array) $this->data['import'] as $key => $value) {
if ($key === 'import') {
throw new LogicException('Invalid key for import');
throw new UnexpectedValueException('Invalid key for import');
}
$callback = explode('::', $value, 2);
if (!is_callable($callback)) {
throw new LogicException('Invalid import callback "' . $value . '"');
throw new UnexpectedValueException('Invalid import callback "' . $value . '"');
}
$this->data[$key] = $callback();
}

View File

@ -5,7 +5,7 @@ namespace Formwork\Admin;
use Formwork\Languages\LanguageCodes;
use Formwork\Parsers\YAML;
use Formwork\Utils\FileSystem;
use LogicException;
use InvalidArgumentException;
use RuntimeException;
class Translation
@ -89,7 +89,7 @@ class Translation
if ($this->code !== self::FALLBACK_LANGUAGE_CODE) {
return $this->fallbackTranslation()->get($key, ...$arguments);
}
throw new LogicException('Invalid language string "' . $key . '"');
throw new InvalidArgumentException('Invalid language string "' . $key . '"');
}
if (!empty($arguments)) {

View File

@ -10,7 +10,7 @@ use Formwork\Template\Renderer;
use Formwork\Utils\FileSystem;
use Formwork\Utils\HTML;
use Formwork\Utils\Str;
use LogicException;
use BadMethodCallException;
use RuntimeException;
class View
@ -122,6 +122,6 @@ class View
if (isset(static::$helpers[$name])) {
return static::$helpers[$name](...$arguments);
}
throw new LogicException('Invalid method ' . static::class . '::' . $name);
throw new BadMethodCallException('Call to undefined method ' . static::class . '::' . $name . '()');
}
}

View File

@ -7,7 +7,7 @@ use Formwork\Utils\Arr;
use Formwork\Utils\FileSystem;
use Formwork\Utils\HTTPRequest;
use Formwork\Utils\Uri;
use LogicException;
use BadMethodCallException;
abstract class AbstractPage
{
@ -347,6 +347,6 @@ abstract class AbstractPage
if ($this->has($name)) {
return $this->get($name);
}
throw new LogicException('Invalid method ' . static::class . '::' . $name);
throw new BadMethodCallException('Call to undefined method ' . static::class . '::' . $name . '()');
}
}

View File

@ -2,7 +2,7 @@
namespace Formwork\Languages;
use LogicException;
use InvalidArgumentException;
class LanguageCodes
{
@ -124,7 +124,7 @@ class LanguageCodes
public static function codeToName(string $code): string
{
if (!static::hasCode($code)) {
throw new LogicException('Invalid language code "' . $code . '"');
throw new InvalidArgumentException('Invalid language code "' . $code . '"');
}
return self::LANGUAGE_CODES[$code]['name'];
}
@ -135,7 +135,7 @@ class LanguageCodes
public static function codeToNativeName(string $code): string
{
if (!static::hasCode($code)) {
throw new LogicException('Invalid language code "' . $code . '"');
throw new InvalidArgumentException('Invalid language code "' . $code . '"');
}
return self::LANGUAGE_CODES[$code]['native'];
}

View File

@ -4,9 +4,9 @@ namespace Formwork\Router;
use Formwork\Utils\HTTPRequest;
use Formwork\Utils\Uri;
use BadMethodCallException;
use LogicException;
use InvalidArgumentException;
use RuntimeException;
use UnexpectedValueException;
class Router
{
@ -103,7 +103,7 @@ class Router
[$type, $method, $route, $callback] = $arguments;
break;
default:
throw new BadMethodCallException('Invalid arguments for ' . __METHOD__);
throw new InvalidArgumentException('Invalid arguments for ' . __METHOD__);
}
if (is_array($type)) {
foreach ($type as $t) {
@ -118,10 +118,10 @@ class Router
return;
}
if (!in_array($type, self::REQUEST_TYPES, true)) {
throw new LogicException('Invalid request type "' . $type . '"');
throw new InvalidArgumentException('Invalid request type "' . $type . '"');
}
if (!in_array($method, self::REQUEST_METHODS, true)) {
throw new LogicException('Invalid HTTP method "' . $method . '"');
throw new InvalidArgumentException('Invalid HTTP method "' . $method . '"');
}
if (is_array($route)) {
foreach ($route as $r) {
@ -151,7 +151,7 @@ class Router
$route['callback'] = [new $class(), $method];
}
if (!is_callable($route['callback'])) {
throw new LogicException('Invalid callback for ' . $route['route'] . ' route');
throw new UnexpectedValueException('Invalid callback for ' . $route['route'] . ' route');
}
return $route['callback']($this->params);
}

View File

@ -6,7 +6,7 @@ use Formwork\Core\Assets;
use Formwork\Core\Formwork;
use Formwork\Core\Page;
use Formwork\Utils\FileSystem;
use LogicException;
use BadMethodCallException;
use RuntimeException;
class Template
@ -246,7 +246,7 @@ class Template
$helper = TemplateHelpers::get($name);
return $helper(...$arguments);
}
throw new LogicException('Invalid method ' . static::class . '::' . $name);
throw new BadMethodCallException('Call to undefined method ' . static::class . '::' . $name . '()');
}
public function __toString(): string

View File

@ -4,8 +4,7 @@ namespace Formwork\Template;
use Formwork\Utils\HTML;
use Formwork\Utils\Str;
use LogicException;
use RuntimeException;
use InvalidArgumentException;
class TemplateHelpers
{
@ -35,10 +34,10 @@ class TemplateHelpers
public static function add(string $name, callable $helper): void
{
if (static::has($name)) {
throw new RuntimeException('Template helper ' . $name . ' is already defined');
throw new InvalidArgumentException('Template helper ' . $name . ' is already defined');
}
if (method_exists(Template::class, $name)) {
throw new LogicException('Template helpers must not conflict with ' . Template::class . ' methods. Invalid name ' . $name);
throw new InvalidArgumentException('Template helpers must not conflict with ' . Template::class . ' methods. Invalid name ' . $name);
}
static::$helpers[$name] = $helper;
}
@ -49,7 +48,7 @@ class TemplateHelpers
public static function get(string $name): callable
{
if (!static::has($name)) {
throw new RuntimeException('Template helper ' . $name . ' is not defined');
throw new InvalidArgumentException('Template helper ' . $name . ' is not defined');
}
return static::$helpers[$name];
}

View File

@ -2,7 +2,8 @@
namespace Formwork\Utils;
use LogicException;
use InvalidArgumentException;
use UnexpectedValueException;
class Cookie
{
@ -29,7 +30,7 @@ class Cookie
{
$options = array_merge(static::defaults(), (array) $options);
if (in_array(strtolower($name), array_keys($options), true)) {
throw new LogicException('Invalid cookie name "' . $name . '"');
throw new InvalidArgumentException('Invalid cookie name "' . $name . '"');
}
$data = [$name => rawurlencode($value)] + static::parseOptions($options);
Header::send('Set-Cookie', static::makeHeader($data), $replace);
@ -77,7 +78,7 @@ class Cookie
}
if (!empty($options['samesite'])) {
if (!in_array($options['samesite'], [self::SAMESITE_STRICT, self::SAMESITE_LAX], true)) {
throw new LogicException('Invalid value for cookie SameSite directive');
throw new UnexpectedValueException('Invalid value for cookie SameSite directive');
}
$data['SameSite'] = $options['samesite'];
}

View File

@ -2,7 +2,7 @@
namespace Formwork\Utils;
use LogicException;
use InvalidArgumentException;
use RuntimeException;
class Header
@ -98,7 +98,7 @@ class Header
public static function status(int $code, bool $send = true, bool $exit = false)
{
if (!isset(self::HTTP_STATUS[$code])) {
throw new LogicException('Unknown HTTP status code ' . $code);
throw new InvalidArgumentException('Unknown HTTP status code ' . $code);
}
$protocol = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0';
$status = $protocol . ' ' . $code . ' ' . self::HTTP_STATUS[$code];