提交 639d1cdf 编写于 作者: O overtrue

Improve class validation.

上级 322dd21f
......@@ -126,15 +126,15 @@ class EasySms
*/
public function strategy($strategy = null)
{
if (is_null($strategy)) {
if (\is_null($strategy)) {
$strategy = $this->config->get('default.strategy', OrderStrategy::class);
}
if (!class_exists($strategy)) {
$strategy = __NAMESPACE__.'\Strategies\\'.ucfirst($strategy);
if (!\class_exists($strategy)) {
$strategy = __NAMESPACE__.'\Strategies\\'.\ucfirst($strategy);
}
if (!class_exists($strategy)) {
if (!\class_exists($strategy)) {
throw new InvalidArgumentException("Unsupported strategy \"{$strategy}\"");
}
......@@ -227,7 +227,7 @@ class EasySms
}
if (!($gateway instanceof GatewayInterface)) {
throw new InvalidArgumentException(sprintf('Gateway "%s" not inherited from %s.', $name, GatewayInterface::class));
throw new InvalidArgumentException(\sprintf('Gateway "%s" must be inherit from %s.', $name, GatewayInterface::class));
}
return $gateway;
......@@ -245,8 +245,8 @@ class EasySms
*/
protected function makeGateway($gateway, $config)
{
if (!class_exists($gateway)) {
throw new InvalidArgumentException(sprintf('Gateway "%s" not exists.', $gateway));
if (!\class_exists($gateway) || !\in_array(GatewayInterface::class, \class_implements($gateway))) {
throw new InvalidArgumentException(\sprintf('Class "%s" is a invalid easy-sms gateway.', $gateway));
}
return new $gateway($config);
......@@ -261,11 +261,11 @@ class EasySms
*/
protected function formatGatewayClassName($name)
{
if (class_exists($name)) {
if (\class_exists($name)) {
return $name;
}
$name = ucfirst(str_replace(['-', '_', ''], '', $name));
$name = \ucfirst(\str_replace(['-', '_', ''], '', $name));
return __NAMESPACE__."\\Gateways\\{$name}Gateway";
}
......@@ -279,7 +279,7 @@ class EasySms
*/
protected function callCustomCreator($gateway)
{
return call_user_func($this->customCreators[$gateway], $this->config->get("gateways.{$gateway}", []));
return \call_user_func($this->customCreators[$gateway], $this->config->get("gateways.{$gateway}", []));
}
/**
......@@ -293,7 +293,7 @@ class EasySms
return $number;
}
return new PhoneNumber(trim($number));
return new PhoneNumber(\trim($number));
}
/**
......@@ -304,7 +304,7 @@ class EasySms
protected function formatMessage($message)
{
if (!($message instanceof MessageInterface)) {
if (!is_array($message)) {
if (!\is_array($message)) {
$message = [
'content' => $message,
'template' => $message,
......@@ -329,7 +329,7 @@ class EasySms
$formatted = [];
foreach ($gateways as $gateway => $setting) {
if (is_int($gateway) && is_string($setting)) {
if (\is_int($gateway) && \is_string($setting)) {
$gateway = $setting;
$setting = [];
}
......@@ -337,8 +337,8 @@ class EasySms
$formatted[$gateway] = $setting;
$globalSettings = $this->config->get("gateways.{$gateway}", []);
if (is_string($gateway) && !empty($globalSettings) && is_array($setting)) {
$formatted[$gateway] = new Config(array_merge($globalSettings, $setting));
if (\is_string($gateway) && !empty($globalSettings) && \is_array($setting)) {
$formatted[$gateway] = new Config(\array_merge($globalSettings, $setting));
}
}
......
......@@ -67,7 +67,7 @@ class AliyunrestGateway extends Gateway
$result = $this->post($this->getEndpointUrl($urlParams), $params);
if (isset($result['error_response']) && $result['error_response']['code'] != 0) {
if (isset($result['error_response']) && 0 != $result['error_response']['code']) {
throw new GatewayErrorException($result['error_response']['msg'], $result['error_response']['code'], $result);
}
......
......@@ -83,7 +83,7 @@ class PhoneNumber implements \Overtrue\EasySms\Contracts\PhoneNumberInterface
/**
* @param string $prefix
*
* @return null|string
* @return string|null
*/
public function getPrefixedIDDCode($prefix)
{
......
......@@ -32,11 +32,19 @@ class EasySmsTest extends TestCase
// invalid gateway
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Gateway "Overtrue\EasySms\Gateways\NotExistsGatewayNameGateway" not exists.');
$this->expectExceptionMessage('Class "Overtrue\EasySms\Gateways\NotExistsGatewayNameGateway" is a invalid easy-sms gateway.');
$easySms->gateway('NotExistsGatewayName');
}
public function testGatewayNameConflicts()
{
$easySms = \Mockery::mock(EasySms::class.'[makeGateway]', [['default' => DummyGatewayForTest::class]]);
$this->expectExceptionMessage('Class "Overtrue\EasySms\Tests\DummyGatewayNotImplementsGatewayInterface" is a invalid easy-sms gateway.');
$easySms->makeGateway(DummyGatewayNotImplementsGatewayInterface::class, []);
}
public function testGatewayWithoutDefaultSetting()
{
$easySms = new EasySms([]);
......@@ -58,9 +66,8 @@ class EasySmsTest extends TestCase
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
sprintf(
'Gateway "%s" not inherited from %s.',
DummyInvalidGatewayForTest::class,
GatewayInterface::class
'Class "%s" is a invalid easy-sms gateway.',
DummyInvalidGatewayForTest::class
)
);
$easySms->gateway();
......@@ -181,6 +188,10 @@ class EasySmsTest extends TestCase
}
}
class DummyGatewayNotImplementsGatewayInterface
{
}
class DummyGatewayForTest implements GatewayInterface
{
public function getName()
......
......@@ -13,8 +13,8 @@ namespace Overtrue\EasySms\Tests\Traits;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Overtrue\EasySms\Traits\HasHttpRequest;
use Overtrue\EasySms\Tests\TestCase;
use Overtrue\EasySms\Traits\HasHttpRequest;
use Psr\Http\Message\ResponseInterface;
class HasHttpRequestTest extends TestCase
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册