在我正在构建的框架中,我正在努力使我的代码更具可测试性,因为我之前沉迷于 MVC+Singleton 模式并且拥有大量的静态类。从那时起,我开始更多地了解单元测试和 TDD,这促使我重构了很多代码。这种重构的一部分促使我尝试在 PHP 中正确使用扩展类,即不仅抛出 Exception 类,而且抛出更多相关的异常。
我有以下类(class):
<?php
namespace Framework;
class Uri {
public static function new_from_http() {
$uri = '';
if (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
} elseif (isset($_SERVER['PATH_INFO'])) {
$uri = $_SERVER['PATH_INFO'];
}
return static::new_from_string($uri);
}
public static function new_from_string($string) {
return new static(explode('/', $string));
}
protected $uri = [];
public function __construct(array $uri) {
$this->uri = array_values(array_filter($uri));
}
public function get_segment($offset, $default = null) {
if (!is_int($offset)) {
throw new \InvalidArgumentException(
sprintf('%s requires argument 1 to be an integer, %s given.',
__METHOD__,
gettype()
)
);
}
return isset($this->uri[$offset - 1])
? $this->uri[$offset - 1]
: $default;
}
}
一切都很好,如您所见,get_segment 方法需要一个整数,否则它会抛出 InvalidArgumentException。问题是,我想再创建一些方法,这些方法也需要整数作为参数,而且我不想在任何地方剪切和粘贴该代码。合并所有这些类型的参数检查的最佳选择是什么,以便我可以在不同的类和方法中使用它们,同时保持消息彼此一致。
我的一个想法是在框架命名空间下扩展异常类,并让构造函数采用不同的参数,例如:
namespace Framework;
class InvalidArgumentException extends \InvalidArgumentException {
public function __construct($method, $argument, $value) {
parent::__construct(
sprintf('%s requires argument 1 to be an integer, %s given.',
$method,
gettype($value)
)
);
}
}
这会被称为:
if (!is_int($arg)) {
throw new \Framework\InvalidArgumentException(__METHOD__, 1, $arg);
}
还可以改进 \Framework\InvalidArgumentException
可以通过回溯获取 __METHOD__
值。
我还有哪些其他选择,最好的是什么?
最佳答案
我会将 /InvalidArgumentException
扩展为 NonIntegerException
,否则做的事情基本相同。这样,如果您需要 strings
、arrays
或任何其他类型,您就可以创建新的异常,而不必用疯狂的逻辑来确定哪个消息使用。
关于PHP DRY 抛出 InvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16358404/