Skip to content

Commit

Permalink
Improve class validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Mar 22, 2019
1 parent 322dd21 commit 639d1cd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
30 changes: 15 additions & 15 deletions src/EasySms.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ public function gateway($name = null)
*/
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}\"");
}

Expand Down Expand Up @@ -227,7 +227,7 @@ protected function createGateway($name)
}

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;
Expand All @@ -245,8 +245,8 @@ protected function createGateway($name)
*/
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);
Expand All @@ -261,11 +261,11 @@ protected function makeGateway($gateway, $config)
*/
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";
}
Expand All @@ -279,7 +279,7 @@ protected function formatGatewayClassName($name)
*/
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}", []));
}

/**
Expand All @@ -293,7 +293,7 @@ protected function formatPhoneNumber($number)
return $number;
}

return new PhoneNumber(trim($number));
return new PhoneNumber(\trim($number));
}

/**
Expand All @@ -304,7 +304,7 @@ protected function formatPhoneNumber($number)
protected function formatMessage($message)
{
if (!($message instanceof MessageInterface)) {
if (!is_array($message)) {
if (!\is_array($message)) {
$message = [
'content' => $message,
'template' => $message,
Expand All @@ -329,16 +329,16 @@ protected function formatGateways(array $gateways)
$formatted = [];

foreach ($gateways as $gateway => $setting) {
if (is_int($gateway) && is_string($setting)) {
if (\is_int($gateway) && \is_string($setting)) {
$gateway = $setting;
$setting = [];
}

$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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Gateways/AliyunrestGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config

$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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getZeroPrefixedNumber()
/**
* @param string $prefix
*
* @return null|string
* @return string|null
*/
public function getPrefixedIDDCode($prefix)
{
Expand Down
19 changes: 15 additions & 4 deletions tests/EasySmsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ public function testGateway()

// 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([]);
Expand All @@ -58,9 +66,8 @@ public function testGatewayWithDefaultSetting()
$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();
Expand Down Expand Up @@ -181,6 +188,10 @@ public function testFormatGateways()
}
}

class DummyGatewayNotImplementsGatewayInterface
{
}

class DummyGatewayForTest implements GatewayInterface
{
public function getName()
Expand Down
2 changes: 1 addition & 1 deletion tests/Traits/HasHttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

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
Expand Down

0 comments on commit 639d1cd

Please sign in to comment.