From adead62fbf1752dd55a47aedda25bd885e167a48 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 31 Oct 2023 17:08:30 +0700 Subject: [PATCH 1/5] update phpstan test workflow --- .github/workflows/phpstan.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 31c5f8e..a17c22b 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -18,4 +18,4 @@ jobs: uses: ramsey/composer-install@v2 - name: Run PHPStan - run: vendor/bin/phpstan --error-format=github + run: composer analyse --error-format=github From ab7824eff35a1727cb97068c726914bfaefbbe83 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 31 Oct 2023 17:31:04 +0700 Subject: [PATCH 2/5] feat: add php cs fixer --- .gitignore | 1 + .php-cs-fixer.dist.php | 39 +++++++++++++++++++++++++++++++ composer.json | 11 +++++++-- tests/TelegramGitNorifierTest.php | 0 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 .php-cs-fixer.dist.php create mode 100644 tests/TelegramGitNorifierTest.php diff --git a/.gitignore b/.gitignore index 067c442..f09fb56 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ package-lock.json yarn.lock build +.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..aa7edfb --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,39 @@ +in([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->name('*.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); + +return (new PhpCsFixer\Config()) + ->setRules([ + '@PSR12' => true, + 'array_syntax' => ['syntax' => 'short'], + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + 'no_unused_imports' => true, + 'not_operator_with_successor_space' => true, + 'trailing_comma_in_multiline' => true, + 'phpdoc_scalar' => true, + 'unary_operator_spaces' => false, + 'binary_operator_spaces' => true, + 'blank_line_before_statement' => [ + 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], + ], + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_var_without_name' => true, + 'class_attributes_separation' => [ + 'elements' => [ + 'method' => 'one', + ], + ], + 'method_argument_space' => [ + 'on_multiline' => 'ensure_fully_multiline', + 'keep_multiple_spaces_after_comma' => true, + ], + 'single_trait_insert_per_statement' => true, + ]) + ->setFinder($finder); diff --git a/composer.json b/composer.json index 1577599..4da0ec3 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,11 @@ "common/helpers.php" ] }, + "autoload-dev": { + "psr-4": { + "LbilTech\\TelegramGitNotifier\\Tests\\": "tests" + } + }, "require": { "php": "^8.1", "eleirbag89/telegrambotphp": "^1.4", @@ -41,10 +46,12 @@ "vlucas/phpdotenv": "^5.5" }, "require-dev": { - "phpstan/phpstan": "^1.10.39" + "phpstan/phpstan": "^1.10.39", + "friendsofphp/php-cs-fixer": "^v3.37.1" }, "scripts": { - "analyse": "vendor/bin/phpstan" + "analyse": "vendor/bin/phpstan", + "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" }, "support": { "issues": "https://github.com/lbiltech/telegram-git-notifier/issues" diff --git a/tests/TelegramGitNorifierTest.php b/tests/TelegramGitNorifierTest.php new file mode 100644 index 0000000..e69de29 From e607c76524f13e74d7766e6b97d4ebb25689a6e0 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 31 Oct 2023 17:35:10 +0700 Subject: [PATCH 3/5] fix code style with php cs fixer --- .php-cs-fixer.dist.php | 3 +-- src/Bot.php | 2 +- src/Helpers/ConfigHelper.php | 2 ++ src/Interfaces/SettingInterface.php | 1 + src/Notifier.php | 3 ++- src/Structures/App.php | 17 +++++++++-------- src/Structures/Notification.php | 10 +++++----- src/Trait/BotSettingTrait.php | 4 ++-- src/Trait/EventSettingTrait.php | 6 ++++-- src/Trait/EventTrait.php | 2 +- 10 files changed, 28 insertions(+), 22 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index aa7edfb..70c6500 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -15,10 +15,9 @@ 'array_syntax' => ['syntax' => 'short'], 'ordered_imports' => ['sort_algorithm' => 'alpha'], 'no_unused_imports' => true, - 'not_operator_with_successor_space' => true, 'trailing_comma_in_multiline' => true, 'phpdoc_scalar' => true, - 'unary_operator_spaces' => false, + 'unary_operator_spaces' => true, 'binary_operator_spaces' => true, 'blank_line_before_statement' => [ 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], diff --git a/src/Bot.php b/src/Bot.php index 7fa2a04..1a23041 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -4,9 +4,9 @@ use LbilTech\TelegramGitNotifier\Constants\EventConstant; use LbilTech\TelegramGitNotifier\Interfaces\BotInterface; +use LbilTech\TelegramGitNotifier\Interfaces\EventInterface; use LbilTech\TelegramGitNotifier\Interfaces\SettingInterface; use LbilTech\TelegramGitNotifier\Interfaces\Structures\AppInterface; -use LbilTech\TelegramGitNotifier\Interfaces\EventInterface; use LbilTech\TelegramGitNotifier\Models\Event; use LbilTech\TelegramGitNotifier\Models\Setting; use LbilTech\TelegramGitNotifier\Structures\App; diff --git a/src/Helpers/ConfigHelper.php b/src/Helpers/ConfigHelper.php index d345474..2da04a4 100644 --- a/src/Helpers/ConfigHelper.php +++ b/src/Helpers/ConfigHelper.php @@ -36,6 +36,7 @@ public function execConfig(string $string): mixed $result = $result[$value]; } + return $result; } @@ -58,6 +59,7 @@ public function getTemplateData(string $partialPath, array $data = []): bool|str $content = ''; ob_start(); + try { extract($data, EXTR_SKIP); require_once $viewPathFile; diff --git a/src/Interfaces/SettingInterface.php b/src/Interfaces/SettingInterface.php index b5833ae..e7cfaef 100644 --- a/src/Interfaces/SettingInterface.php +++ b/src/Interfaces/SettingInterface.php @@ -68,6 +68,7 @@ public function getEndKeyboard(string $platform, ?string $parentEvent = null): a * @see EventSettingTrait::eventHandle() */ public function eventHandle(?string $callback = null, ?string $platform = null, string $platformFile = null): void; + /** * Get the platform from callback * diff --git a/src/Notifier.php b/src/Notifier.php index 33d9a0c..c5cfa4b 100644 --- a/src/Notifier.php +++ b/src/Notifier.php @@ -5,8 +5,8 @@ use GuzzleHttp\Client; use LbilTech\TelegramGitNotifier\Constants\EventConstant; use LbilTech\TelegramGitNotifier\Constants\NotificationConstant; -use LbilTech\TelegramGitNotifier\Interfaces\Structures\AppInterface; use LbilTech\TelegramGitNotifier\Interfaces\EventInterface; +use LbilTech\TelegramGitNotifier\Interfaces\Structures\AppInterface; use LbilTech\TelegramGitNotifier\Interfaces\Structures\NotificationInterface; use LbilTech\TelegramGitNotifier\Models\Event; use LbilTech\TelegramGitNotifier\Structures\App; @@ -56,6 +56,7 @@ public function parseNotifyChatIds(): array ? explode(NotificationConstant::THREAD_ID_SEPARATOR, $threadIds) : []; } + return $chatThreadMapping; } } diff --git a/src/Structures/App.php b/src/Structures/App.php index f3465d3..17972e5 100644 --- a/src/Structures/App.php +++ b/src/Structures/App.php @@ -21,9 +21,9 @@ public function setCurrentChatBotId(string $chatBotId = null): void private function createTelegramBaseContent(): array { return [ - 'chat_id' => $this->chatBotId, + 'chat_id' => $this->chatBotId, 'disable_web_page_preview' => true, - 'parse_mode' => 'HTML' + 'parse_mode' => 'HTML', ]; } @@ -71,8 +71,8 @@ public function answerCallbackQuery(string $text = null, array $options = []): v try { $options = array_merge([ 'callback_query_id' => $this->telegram->Callback_ID(), - 'text' => $text, - 'show_alert' => true + 'text' => $text, + 'show_alert' => true, ], $options); $this->telegram->answerCallbackQuery($options); } catch (Exception $e) { @@ -86,7 +86,7 @@ public function editMessageText( ): void { try { $content = array_merge([ - 'text' => $text ?? $this->getCallbackTextMessage() + 'text' => $text ?? $this->getCallbackTextMessage(), ], $this->setCallbackContentMessage($options)); $this->telegram->editMessageText($content); @@ -114,10 +114,10 @@ public function getCallbackTextMessage(): string public function setCallbackContentMessage(array $options = []): array { $content = [ - 'chat_id' => $this->telegram->Callback_ChatID(), - 'message_id' => $this->telegram->MessageID(), + 'chat_id' => $this->telegram->Callback_ChatID(), + 'message_id' => $this->telegram->MessageID(), 'disable_web_page_preview' => true, - 'parse_mode' => 'HTML', + 'parse_mode' => 'HTML', ]; $content['reply_markup'] = $options['reply_markup'] @@ -135,6 +135,7 @@ public function getBotName(): ?string public function getCommandMessage(): string { $text = $this->telegram->Text(); + return str_replace('@' . $this->getBotName(), '', $text); } } diff --git a/src/Structures/Notification.php b/src/Structures/Notification.php index 9426436..d8ec7f1 100644 --- a/src/Structures/Notification.php +++ b/src/Structures/Notification.php @@ -19,13 +19,13 @@ public function accessDenied( string $viewTemplate = null, ): void { $this->telegram->sendMessage([ - 'chat_id' => $this->chatBotId, - 'text' => view( + 'chat_id' => $this->chatBotId, + 'text' => view( $viewTemplate ?? config('telegram-git-notifier.view.globals.access_denied'), ['chatId' => $chatId] ), 'disable_web_page_preview' => true, - 'parse_mode' => 'HTML' + 'parse_mode' => 'HTML', ]); } @@ -67,7 +67,7 @@ private function setMessage(string $typeEvent): void $viewResult = view($viewTemplate, [ 'payload' => $this->payload, - 'event' => tgn_convert_event_name($typeEvent), + 'event' => tgn_convert_event_name($typeEvent), ]); if ($viewResult === null) { @@ -87,7 +87,7 @@ public function sendNotify(string $message = null, array $options = []): bool try { $response = $this->client->request('POST', $url, [ - 'form_params' => $queryParams + 'form_params' => $queryParams, ]); if ($response->getStatusCode() === 200) { diff --git a/src/Trait/BotSettingTrait.php b/src/Trait/BotSettingTrait.php index ef0ad2d..6524d4c 100644 --- a/src/Trait/BotSettingTrait.php +++ b/src/Trait/BotSettingTrait.php @@ -21,7 +21,7 @@ public function setMyCommands( ?string $view = null ): void { $this->telegram->setMyCommands([ - 'commands' => json_encode($menuCommand) + 'commands' => json_encode($menuCommand), ]); $this->sendMessage( view( @@ -59,7 +59,7 @@ public function settingMarkup(): array '', SettingConstant::SETTING_ALL_EVENTS_NOTIFY ), - ] + ], ]; $markup = $this->customEventMarkup($markup); diff --git a/src/Trait/EventSettingTrait.php b/src/Trait/EventSettingTrait.php index 51f98ad..ea84a9a 100644 --- a/src/Trait/EventSettingTrait.php +++ b/src/Trait/EventSettingTrait.php @@ -92,7 +92,7 @@ public function getEndKeyboard( '📚 Menu', '', SettingConstant::SETTING_BACK_TO_MAIN_MENU - ) + ), ]; } @@ -149,6 +149,7 @@ public function sendSettingEventMessage( ), ['reply_markup' => $this->eventMarkup(null, $platform)] ); + return true; } @@ -164,7 +165,7 @@ public function getEventFromCallback(?string $callback): string return str_replace([ EventConstant::EVENT_PREFIX, EventConstant::GITHUB_EVENT_SEPARATOR, - EventConstant::GITLAB_EVENT_SEPARATOR + EventConstant::GITLAB_EVENT_SEPARATOR, ], '', $callback); } @@ -186,6 +187,7 @@ public function handleEventWithActions( ), ['reply_markup' => $this->eventMarkup($event, $platform)] ); + return true; } diff --git a/src/Trait/EventTrait.php b/src/Trait/EventTrait.php index 2175b0d..1156ea3 100644 --- a/src/Trait/EventTrait.php +++ b/src/Trait/EventTrait.php @@ -2,8 +2,8 @@ namespace LbilTech\TelegramGitNotifier\Trait; -use Symfony\Component\HttpFoundation\Request; use LbilTech\TelegramGitNotifier\Constants\EventConstant; +use Symfony\Component\HttpFoundation\Request; trait EventTrait { From 8b3a642ad2c414a8ba64bc9ef56bba267feb0b0d Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 31 Oct 2023 17:43:17 +0700 Subject: [PATCH 4/5] feat: add php cs fixer checker with workflow --- .github/workflows/php-cs-fixer.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/php-cs-fixer.yml diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml new file mode 100644 index 0000000..90d3b99 --- /dev/null +++ b/.github/workflows/php-cs-fixer.yml @@ -0,0 +1,18 @@ +name: Check & fix styling + +on: [push] + +jobs: + php-cs-fixer: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + + - name: Run PHP CS Fixer + uses: docker://oskarstark/php-cs-fixer-ga + with: + args: --config=.php-cs-fixer.dist.php --allow-risky=yes From 1af54f40ee560685853453805651cad1d51c08dc Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 31 Oct 2023 17:52:51 +0700 Subject: [PATCH 5/5] fix: update multi php version for setup test workflow --- .github/workflows/setup_test.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/setup_test.yml b/.github/workflows/setup_test.yml index 983c9c2..726871d 100644 --- a/.github/workflows/setup_test.yml +++ b/.github/workflows/setup_test.yml @@ -6,11 +6,18 @@ jobs: tests: name: Composer setup and tests runs-on: ubuntu-latest + strategy: + matrix: + php: [ '8.1', '8.2' ] + laravel: [ 10.*, 9.* ] + include: + - laravel: 10.* + testbench: 8.* steps: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.2' + php-version: ${{ matrix.php }} - name: Checkout code uses: actions/checkout@v3 - name: Install dependencies