Skip to content

Commit

Permalink
Fix phpmd
Browse files Browse the repository at this point in the history
  • Loading branch information
moririnson committed May 1, 2023
1 parent 3544b41 commit 297f110
Show file tree
Hide file tree
Showing 17 changed files with 340 additions and 535 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
"scripts": {
"test": "phpunit --debug tests",
"cs": "phpcs",
"md": "phpmd --ignore-violations-on-exit src,examples/EchoBot/src,examples/EchoBot/public,examples/KitchenSink/src,examples/KitchenSink/public text phpmd.xml",
"stan": "devtool/check_phpstan.sh"
"md": "phpmd --ignore-violations-on-exit src/constants,src/laravel,src/parser,examples/EchoBot/src,examples/EchoBot/public,examples/KitchenSink/src,examples/KitchenSink/public text phpmd.xml",
"stan": "phpstan analyse --memory-limit=-1"
},
"extra": {
"laravel": {
Expand Down
7 changes: 3 additions & 4 deletions examples/EchoBot/src/LINEBot/EchoBot/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class Route
public function register(\Slim\App $app)
{
$app->post('/callback', function (\Psr\Http\Message\RequestInterface $req, \Psr\Http\Message\ResponseInterface $res) {
/** @var \LINE\Clients\MessagingApi\Api\MessagingApiApi $bot */
$bot = $this->get('botMessagingApi');
$bot = $this->get(\LINE\Clients\MessagingApi\Api\MessagingApiApi::class);
$logger = $this->get(\Psr\Log\LoggerInterface::class);

$signature = $req->getHeader(HTTPHeader::LINE_SIGNATURE);
Expand All @@ -44,14 +43,14 @@ public function register(\Slim\App $app)
// Check request with signature and parse request
try {
$secret = $this->get('settings')['bot']['channelSecret'];
$events = EventRequestParser::parseEventRequest($req->getBody(), $secret, $signature[0]);
$parsedEvents = EventRequestParser::parseEventRequest($req->getBody(), $secret, $signature[0]);
} catch (InvalidSignatureException $e) {
return $res->withStatus(400, 'Invalid signature');
} catch (InvalidEventRequestException $e) {
return $res->withStatus(400, "Invalid event request");
}

foreach ($events as $event) {
foreach ($parsedEvents->getEvents() as $event) {
if (!($event instanceof MessageEvent)) {
$logger->info('Non message event has come');
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

namespace LINE\LINEBot\KitchenSink;

use LINE\ChannelAccessToken\Model\TextMessage;
use LINE\Clients\MessagingApi\Api\MessagingApiApi;
use LINE\Clients\MessagingApi\Model\ReplyMessageRequest;
use LINE\Clients\MessagingApi\Model\TextMessage;
use LINE\Constants\MessageType;
use LINE\Webhook\Model\AccountLinkEvent;

Expand All @@ -36,7 +36,6 @@ class AccountLinkEventHandler
/**
* AccountLinkEventHandler constructor.
* @param MessagingApiApi $bot
* @param \Psr\Log\LoggerInterface $logger
* @param AccountLinkEvent $accountLinkEvent
*/
public function __construct(MessagingApiApi $bot, \Psr\Log\LoggerInterface $logger, AccountLinkEvent $accountLinkEvent)
Expand All @@ -51,10 +50,15 @@ public function __construct(MessagingApiApi $bot, \Psr\Log\LoggerInterface $logg
*/
public function handle()
{
$link = $this->accountLinkEvent->getLink();
$this->logger->info('Link result: ' . $link->getResult());
$request = new ReplyMessageRequest([
'replyToken' => $this->accountLinkEvent->getReplyToken(),
'messages' => [
new TextMessage(['type' => MessageType::TEXT, 'text' => 'Got account link event ' . $this->accountLinkEvent->getLink()->getNonce()]),
new TextMessage([
'type' => MessageType::TEXT,
'text' => 'Got account link event ' . $link->getNonce(),
]),
],
]);
$this->bot->replyMessage($request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use LINE\Webhook\Model\ImageMessageContent;
use LINE\Webhook\Model\MessageEvent;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ImageMessageHandler implements EventHandler
{
/** @var = $bot */
Expand Down
4 changes: 2 additions & 2 deletions examples/KitchenSink/src/LINEBot/KitchenSink/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public function register(\Slim\App $app)
// Check request with signature and parse request
try {
$secret = $this->get('settings')['bot']['channelSecret'];
$events = EventRequestParser::parseEventRequest($req->getBody(), $secret, $signature[0]);
$parsedEvents = EventRequestParser::parseEventRequest($req->getBody(), $secret, $signature[0]);
} catch (InvalidSignatureException $e) {
return $res->withStatus(400, 'Invalid signature');
} catch (InvalidEventRequestException $e) {
return $res->withStatus(400, "Invalid event request");
}

foreach ($events as $event) {
foreach ($parsedEvents->getEvents() as $event) {
/** @var EventHandler $handler */
$handler = null;

Expand Down
34 changes: 34 additions & 0 deletions phpmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<ruleset name="line-bot-sdk-php PHPMD rule set" xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">

<description>line-bot-sdk-php phpmd custom rules</description>

<exclude-pattern>EchoBot/Route.php</exclude-pattern>
<exclude-pattern>KitchenSink/Route.php</exclude-pattern>
<exclude-pattern>EchoBot/Dependency.php</exclude-pattern>
<exclude-pattern>KitchenSink/Dependency.php</exclude-pattern>
<exclude-pattern>test/EventRequestParserTest.php</exclude-pattern>

<rule ref="rulesets/cleancode.xml">
<exclude name="ElseExpression" />
<exclude name="StaticAccess" />
</rule>

<rule ref="rulesets/codesize.xml" />

<rule ref="rulesets/controversial.xml">
<exclude name="Superglobals" />
</rule>

<rule ref="rulesets/design.xml" />
<rule ref="rulesets/unusedcode.xml" />

<rule ref="rulesets/naming.xml">
<exclude name="ShortVariable" />
<exclude name="LongVariable" />
</rule>
</ruleset>
11 changes: 11 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parameters:
level: 5
paths:
- src/constants
- src/laravel
- src/parser
ignoreErrors:
# Ignore laravel facade unknown.
- message: '#Call to static method getFacadeRoot\(\) on an unknown class [a-zA-Z]+\.#'
paths:
- src\laravel\test\Facades\FacadesTest.php
142 changes: 40 additions & 102 deletions src/Laravel/lib/LINEBotServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,41 @@

namespace LINE\Laravel;

use Guzzle\Http\Client;
use LINE\Clients\ChannelAccessToken\Api\ChannelAccessTokenApi;
use LINE\Clients\Insight\Api\InsightApi;
use LINE\Clients\ChannelAccessToken\Configuration as ChannelAccessTokenConfiguration;
use LINE\Clients\Insight\Configuration as InsightConfiguration;
use LINE\Clients\Liff\Api\LiffApi;
use LINE\Clients\Liff\Configuration as LiffConfiguration;
use LINE\Clients\ManageAudience\Api\ManageAudienceApi;
use LINE\Clients\ManageAudience\Api\ManageAudienceBlobApi;
use LINE\Clients\ManageAudience\Configuration as ManageAudienceConfiguration;
use LINE\Clients\MessagingApi\Api\MessagingApiApi;
use LINE\Clients\MessagingApi\Api\MessagingApiBlobApi;
use LINE\Clients\MessagingApi\Configuration as MessagingApiConfiguration;
use \GuzzleHttp\Client as GuzzleHttpClient;

class LINEBotServiceProvider extends \Illuminate\Support\ServiceProvider
{
private static $apiBindings = [
'line-bot-channel-access-token-api' => [
'config' => \LINE\Clients\ChannelAccessToken\Configuration::class,
'api' => \LINE\Clients\ChannelAccessToken\Api\ChannelAccessTokenApi::class,
],
'line-bot-insight-api' => [
'config' => \LINE\Clients\Insight\Configuration::class,
'api' => \LINE\Clients\Insight\Api\InsightApi::class,
],
'line-bot-liff-api' => [
'config' => \LINE\Clients\Liff\Configuration::class,
'api' => \LINE\Clients\Liff\Api\LiffApi::class,
],
'line-bot-manage-audience-api' => [
'config' => \LINE\Clients\ManageAudience\Configuration::class,
'api' => \LINE\Clients\ManageAudience\Api\ManageAudienceApi::class,
],
'line-bot-manage-audience-blob-api' => [
'config' => \LINE\Clients\ManageAudience\Configuration::class,
'api' => \LINE\Clients\ManageAudience\Api\ManageAudienceBlobApi::class,
],
'line-bot-messaging-api' => [
'config' => \LINE\Clients\MessagingApi\Configuration::class,
'api' => \LINE\Clients\MessagingApi\Api\MessagingApiApi::class,
],
'line-bot-messaging-blob-api' => [
'config' => \LINE\Clients\MessagingApi\Configuration::class,
'api' => \LINE\Clients\MessagingApi\Api\MessagingApiBlobApi::class,
],
];

/**
* Register bindings in the container.
*
Expand All @@ -46,103 +65,22 @@ public function register()
'line-bot'
);
$this->app->bind('line-bot-http-client', function () {
return new \GuzzleHttp\Client(
return new GuzzleHttpClient(
config: config('line-bot.client.config'),
);
});
$this->bindChannelAccessTokenApi();
$this->bindInsightApi();
$this->bindLiffApi();
$this->bindManageAudienceApi();
$this->bindManageAudienceBlobApi();
$this->bindMessagingApi();
$this->bindMessagingBlobApi();
}

private function bindChannelAccessTokenApi()
{
$this->app->bind('line-bot-channel-access-token-api', function ($app) {
$httpClient = $app->make('line-bot-http-client');
$config = new ChannelAccessTokenConfiguration();
return new ChannelAccessTokenApi(
client: $httpClient,
config: $config,
);
});
}

private function bindInsightApi()
{
$this->app->bind('line-bot-insight-api', function ($app) {
$httpClient = $app->make('line-bot-http-client');
$config = new InsightConfiguration();
$config->setAccessToken(config('line-bot.channel_access_token'));
return new InsightApi(
client: $httpClient,
config: $config,
);
});
}

private function bindLiffApi()
{
$this->app->bind('line-bot-liff-api', function ($app) {
$httpClient = $app->make('line-bot-http-client');
$config = new LiffConfiguration();
$config->setAccessToken(config('line-bot.channel_access_token'));
return new LiffApi(
client: $httpClient,
config: $config,
);
});
}

private function bindManageAudienceApi()
{
$this->app->bind('line-bot-manage-audience-api', function ($app) {
$httpClient = $app->make('line-bot-http-client');
$config = new ManageAudienceConfiguration();
$config->setAccessToken(config('line-bot.channel_access_token'));
return new ManageAudienceApi(
client: $httpClient,
config: $config,
);
});
}

private function bindManageAudienceBlobApi()
{
$this->app->bind('line-bot-manage-audience-blob-api', function ($app) {
$httpClient = $app->make('line-bot-http-client');
$config = new ManageAudienceConfiguration();
$config->setAccessToken(config('line-bot.channel_access_token'));
return new ManageAudienceBlobApi(
client: $httpClient,
config: $config,
);
});
}

private function bindMessagingApi()
{
$this->app->bind('line-bot-messaging-api', function ($app) {
$httpClient = $app->make('line-bot-http-client');
$config = new MessagingApiConfiguration();
$config->setAccessToken(config('line-bot.channel_access_token'));
return new MessagingApiApi(
client: $httpClient,
config: $config,
);
});
foreach (self::$apiBindings as $facadeName => $classes) {
$this->bindApis($facadeName, $classes['api'], $classes['config']);
}
}

private function bindMessagingBlobApi()
private function bindApis($facadeName, $clientClass, $configClass)
{
$this->app->bind('line-bot-messaging-blob-api', function ($app) {
$this->app->bind($facadeName, function ($app) use ($clientClass, $configClass) {
$httpClient = $app->make('line-bot-http-client');
$config = new MessagingApiConfiguration();
$config = new $configClass();
$config->setAccessToken(config('line-bot.channel_access_token'));
return new MessagingApiBlobApi(
return new $clientClass(
client: $httpClient,
config: $config,
);
Expand Down
26 changes: 17 additions & 9 deletions src/laravel/test/Facades/FacadesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,42 @@
* under the License.
*/

namespace LINE\Tests\Laravel\Facade;
namespace LINE\Tests\Laravel\Facades;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class FacadesTest extends \Orchestra\Testbench\TestCase
{
/**
* Load package service provider
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageProviders($app)
{
return ['LINE\Laravel\LINEBotServiceProvider'];
return [\LINE\Laravel\LINEBotServiceProvider::class];
}

/**
* Load package alias
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageAliases($app)
{
return [
'LINEChannelAccessTokenApi' => 'LINE\Laravel\Facades\LINEChannelAccessTokenApi',
'LINEInsightApi' => 'LINE\Laravel\Facades\LINEInsightApi',
'LINELiffApi' => 'LINE\Laravel\Facades\LINELiffApi',
'LINEManageAudienceApi' => 'LINE\Laravel\Facades\LINEManageAudienceApi',
'LINEManageAudienceBlobApi' => 'LINE\Laravel\Facades\LINEManageAudienceBlobApi',
'LINEMessagingApi' => 'LINE\Laravel\Facades\LINEMessagingApi',
'LINEMessagingBlobApi' => 'LINE\Laravel\Facades\LINEMessagingBlobApi',
'LINEChannelAccessTokenApi' => \LINE\Laravel\Facades\LINEChannelAccessTokenApi::class,
'LINEInsightApi' => \LINE\Laravel\Facades\LINEInsightApi::class,
'LINELiffApi' => \LINE\Laravel\Facades\LINELiffApi::class,
'LINEManageAudienceApi' => \LINE\Laravel\Facades\LINEManageAudienceApi::class,
'LINEManageAudienceBlobApi' => \LINE\Laravel\Facades\LINEManageAudienceBlobApi::class,
'LINEMessagingApi' => \LINE\Laravel\Facades\LINEMessagingApi::class,
'LINEMessagingBlobApi' => \LINE\Laravel\Facades\LINEMessagingBlobApi::class,
];
}

Expand All @@ -62,6 +69,7 @@ public function testConfigLoaded()
/**
* Testing LINEBot facade instance
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @return void
*/
public function testLINEBotFacadeInstance()
Expand Down
Loading

0 comments on commit 297f110

Please sign in to comment.