From 30ee57953d7c22d289f1ca2c4feffad5e3e7385d Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Sat, 28 Oct 2023 18:12:11 +0700 Subject: [PATCH 1/7] workflow: add phpstan --- .github/workflows/phpstan.yml | 21 +++++++++++++++++++ README.md | 2 +- composer.json | 8 +++++++ .../TelegramGitNotifierException.php | 4 ++-- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/phpstan.yml diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..8b7463a --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,21 @@ +name: PHPStan + +on: [push, pull_request] + +jobs: + phpstan: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@2.26.0 + with: + php-version: '8.0' + + - name: Install composer dependencies + uses: ramsey/composer-install@v2 + + - name: Run PHPStan + run: vendor/bin/phpstan --error-format=github diff --git a/README.md b/README.md index d67a58c..e72d98d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Telegram Git Notifier -This package provides the ability to integrate the Telegram messaging service and GitHub/GitLab. +This package provides the ability to integrate the Telegram messaging service and GitHub and GitLab. With this package, you can create a Telegram bot to receive notifications from GitHub or GitLab events and manage customization through messages and buttons on Telegram. diff --git a/composer.json b/composer.json index 3de2514..b590016 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.0", "eleirbag89/telegrambotphp": "^1.4", @@ -40,6 +45,9 @@ "symfony/http-foundation": "^6.3", "vlucas/phpdotenv": "^5.5" }, + "require-dev": { + "phpstan/phpstan": "^1.10.39" + }, "support": { "issues": "https://github.com/lbiltech/telegram-git-notifier/issues" }, diff --git a/src/Exceptions/TelegramGitNotifierException.php b/src/Exceptions/TelegramGitNotifierException.php index e9e0337..3d201ea 100644 --- a/src/Exceptions/TelegramGitNotifierException.php +++ b/src/Exceptions/TelegramGitNotifierException.php @@ -8,11 +8,11 @@ class TelegramGitNotifierException extends Exception { public static function isEmpty(): self { - return new static('Telegram Git Notifier is empty'); + return new self('Telegram Git Notifier is empty'); } public static function invalid(): self { - return new static('Telegram Git Notifier is invalid'); + return new self('Telegram Git Notifier is invalid'); } } From 06aa6c9b26cc7c7445384a0caf77cfa8b58624d4 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Sat, 28 Oct 2023 18:36:18 +0700 Subject: [PATCH 2/7] upgrade php version --- .github/workflows/phpstan.yml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 8b7463a..31c5f8e 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -12,7 +12,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@2.26.0 with: - php-version: '8.0' + php-version: '8.1' - name: Install composer dependencies uses: ramsey/composer-install@v2 diff --git a/README.md b/README.md index e72d98d..80fd7c3 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ and manage customization through messages and buttons on Telegram. ## 📋 Requirements -- PHP ^8.0 +- PHP ^8.1 - Composer - Telegram Bot From eef4e50a5eba74fc4b2ee95e5a056c1a1664820e Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Mon, 30 Oct 2023 20:02:59 +0700 Subject: [PATCH 3/7] feat: add phpstan configs and fix phpstan test errors --- .gitignore | 4 +++- common/helpers.php | 7 ++++--- composer.json | 5 ++++- phpstan.neon.dist | 6 ++++++ src/Exceptions/EntryNotFoundException.php | 4 ++-- src/Helpers/ConfigHelper.php | 3 +++ src/Interfaces/SettingInterface.php | 4 ++-- src/Interfaces/Structures/AppInterface.php | 10 ++-------- src/Trait/EventSettingTrait.php | 2 +- src/Trait/EventTrait.php | 2 +- 10 files changed, 28 insertions(+), 19 deletions(-) create mode 100644 phpstan.neon.dist diff --git a/.gitignore b/.gitignore index 4a6e97a..067c442 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,6 @@ npm-debug.log /log package-lock.json -yarn.lock \ No newline at end of file +yarn.lock + +build diff --git a/common/helpers.php b/common/helpers.php index 90119f6..ba9a497 100755 --- a/common/helpers.php +++ b/common/helpers.php @@ -124,10 +124,11 @@ function config(string $string): mixed * @param string $partialPath * @param array $data * - * @return bool|string + * @return null|string */ - function view(string $partialPath, array $data = []): bool|string + function view(string $partialPath, array $data = []): null|string { - return (new ConfigHelper())->getTemplateData($partialPath, $data); + $content = (new ConfigHelper())->getTemplateData($partialPath, $data); + return $content ?: null; } } diff --git a/composer.json b/composer.json index b590016..7f894cd 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ } }, "require": { - "php": "^8.0", + "php": "^8.1", "eleirbag89/telegrambotphp": "^1.4", "guzzlehttp/guzzle": "^7.8", "symfony/http-foundation": "^6.3", @@ -48,6 +48,9 @@ "require-dev": { "phpstan/phpstan": "^1.10.39" }, + "scripts": { + "analyse": "vendor/bin/phpstan" + }, "support": { "issues": "https://github.com/lbiltech/telegram-git-notifier/issues" }, diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..2e837cd --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,6 @@ +parameters: + level: 8 + paths: + - src + tmpDir: build/phpstan + checkMissingIterableValueType: false diff --git a/src/Exceptions/EntryNotFoundException.php b/src/Exceptions/EntryNotFoundException.php index 2e20763..04b3bf6 100644 --- a/src/Exceptions/EntryNotFoundException.php +++ b/src/Exceptions/EntryNotFoundException.php @@ -9,12 +9,12 @@ public static function fileNotFound(): self return new self('File not found'); } - public static function configNotFound($config): self + public static function configNotFound(string $config): self { return new self("Config {$config} not found"); } - public static function viewNotFound($view): self + public static function viewNotFound(string $view): self { return new self("View {$view} not found"); } diff --git a/src/Helpers/ConfigHelper.php b/src/Helpers/ConfigHelper.php index 7626aba..ee6e3cd 100644 --- a/src/Helpers/ConfigHelper.php +++ b/src/Helpers/ConfigHelper.php @@ -8,6 +8,9 @@ class ConfigHelper { + /** + * @var array + */ public array $config; public function __construct() diff --git a/src/Interfaces/SettingInterface.php b/src/Interfaces/SettingInterface.php index c08a6f7..b5833ae 100644 --- a/src/Interfaces/SettingInterface.php +++ b/src/Interfaces/SettingInterface.php @@ -38,12 +38,12 @@ public function getCallbackData(string $event, string $platform, array|bool $val * Get event name for markup * * @param string $event - * @param $value + * @param bool|array $value * * @return string * @see EventSettingTrait::getEventName() */ - public function getEventName(string $event, $value): string; + public function getEventName(string $event, bool|array $value): string; /** * Get end keyboard buttons diff --git a/src/Interfaces/Structures/AppInterface.php b/src/Interfaces/Structures/AppInterface.php index 9395f2e..83f3767 100644 --- a/src/Interfaces/Structures/AppInterface.php +++ b/src/Interfaces/Structures/AppInterface.php @@ -17,10 +17,7 @@ interface AppInterface * @throws MessageIsEmptyException * @see App::sendMessage() */ - public function sendMessage( - string $message = '', - array $options = [] - ): void; + public function sendMessage(string $message = '', array $options = []): void; /** * Send a photo to telegram @@ -54,10 +51,7 @@ public function answerCallbackQuery(string $text = null): void; * @return void * @see App::editMessageText() */ - public function editMessageText( - ?string $text = null, - array $options = [] - ): void; + public function editMessageText(string $text = null, array $options = []): void; /** * Edit message reply markup from a telegram diff --git a/src/Trait/EventSettingTrait.php b/src/Trait/EventSettingTrait.php index 87c1532..486d204 100644 --- a/src/Trait/EventSettingTrait.php +++ b/src/Trait/EventSettingTrait.php @@ -65,7 +65,7 @@ public function getCallbackData( return $prefix . $event . EventConstant::EVENT_UPDATE_SEPARATOR; } - public function getEventName(string $event, $value): string + public function getEventName(string $event, bool|array $value = false): string { if (is_array($value)) { return '⚙ ' . $event; diff --git a/src/Trait/EventTrait.php b/src/Trait/EventTrait.php index 98ec7d9..9bd5ae5 100644 --- a/src/Trait/EventTrait.php +++ b/src/Trait/EventTrait.php @@ -11,7 +11,7 @@ trait EventTrait public function setPlatFormForEvent(string $platform, string $platformFile = null): void { - /** @var array $platformFileDefaults */ + /** @var array $platformFileDefaults */ $platformFileDefaults = config('telegram-git-notifier.data_file.platform'); $this->event->setPlatformFile($platformFile ?? $platformFileDefaults[$platform]); $this->event->setEventConfig($platform); From 75aadbdb55c1d88c68a19c864acfa14e8db67529 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Mon, 30 Oct 2023 21:46:30 +0700 Subject: [PATCH 4/7] fix: phpstan test errors --- src/Helpers/ConfigHelper.php | 5 +++-- src/Interfaces/EventInterface.php | 4 ++-- src/Interfaces/Structures/NotificationInterface.php | 4 ++-- src/Models/Event.php | 6 ++++-- src/Models/Setting.php | 10 ++++++---- src/Objects/Validator.php | 4 ++-- src/Structures/Notification.php | 2 +- src/Trait/ActionEventTrait.php | 8 ++++---- 8 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Helpers/ConfigHelper.php b/src/Helpers/ConfigHelper.php index ee6e3cd..d345474 100644 --- a/src/Helpers/ConfigHelper.php +++ b/src/Helpers/ConfigHelper.php @@ -42,12 +42,12 @@ public function execConfig(string $string): mixed /** * Return template data * - * @param $partialPath + * @param string $partialPath * @param array $data * * @return bool|string */ - public function getTemplateData($partialPath, array $data = []): bool|string + public function getTemplateData(string $partialPath, array $data = []): bool|string { $viewPathFile = $this->execConfig('telegram-git-notifier.view.path') . '/' . str_replace('.', '/', $partialPath) . '.php'; @@ -56,6 +56,7 @@ public function getTemplateData($partialPath, array $data = []): bool|string return ''; } + $content = ''; ob_start(); try { extract($data, EXTR_SKIP); diff --git a/src/Interfaces/EventInterface.php b/src/Interfaces/EventInterface.php index 846a260..df856ab 100644 --- a/src/Interfaces/EventInterface.php +++ b/src/Interfaces/EventInterface.php @@ -10,12 +10,12 @@ interface EventInterface /** * Get action name of event from payload data * - * @param $payload + * @param object $payload * * @return string * @see ActionEventTrait::getActionOfEvent() */ - public function getActionOfEvent($payload): string; + public function getActionOfEvent(object $payload): string; /** * Set platform and platform file for event diff --git a/src/Interfaces/Structures/NotificationInterface.php b/src/Interfaces/Structures/NotificationInterface.php index 863f01f..1e65546 100644 --- a/src/Interfaces/Structures/NotificationInterface.php +++ b/src/Interfaces/Structures/NotificationInterface.php @@ -50,12 +50,12 @@ public function sendNotify(string $message = null, array $options = []): bool; /** * Get action name of event from payload data * - * @param $payload + * @param object $payload * * @return string * @see EventTrait::getActionOfEvent() */ - public function getActionOfEvent($payload): string; + public function getActionOfEvent(object $payload): string; /** * Convert chat and thread ids to array diff --git a/src/Models/Event.php b/src/Models/Event.php index a6a8287..6bab8f5 100644 --- a/src/Models/Event.php +++ b/src/Models/Event.php @@ -50,8 +50,10 @@ public function setEventConfig( ): void { $this->platform = $platform; - $json = file_get_contents($this->platformFile); - $this->eventConfig = json_decode($json, true); + if (file_exists($this->platformFile)) { + $json = file_get_contents($this->platformFile); + $this->eventConfig = json_decode($json, true); + } } /** diff --git a/src/Models/Setting.php b/src/Models/Setting.php index 6c07913..0f53e13 100644 --- a/src/Models/Setting.php +++ b/src/Models/Setting.php @@ -43,8 +43,10 @@ public function getSettingFile(): string */ public function setSettingConfig(): void { - $json = file_get_contents($this->settingFile); - $this->settings = json_decode($json, true); + if (file_exists($this->settingFile)) { + $json = file_get_contents($this->settingFile); + $this->settings = json_decode($json, true); + } } /** @@ -81,13 +83,13 @@ public function isNotified(): bool * Update setting item value and save to file * * @param string $settingName - * @param $settingValue + * @param array|string|bool|int|null $settingValue * * @return bool */ public function updateSetting( string $settingName, - $settingValue = null + mixed $settingValue = null ): bool { $settingKeys = explode('.', $settingName); $lastKey = array_pop($settingKeys); diff --git a/src/Objects/Validator.php b/src/Objects/Validator.php index 944529d..6c2b4b8 100644 --- a/src/Objects/Validator.php +++ b/src/Objects/Validator.php @@ -25,14 +25,14 @@ public function __construct(Setting $setting, Event $event) * * @param string $platform Source code platform (GitHub, GitLab) * @param string $event Event name (push, pull_request) - * @param $payload + * @param object $payload * * @return bool */ public function isAccessEvent( string $platform, string $event, - $payload + object $payload ): bool { if (!$this->setting->isNotified()) { return false; diff --git a/src/Structures/Notification.php b/src/Structures/Notification.php index cfdbef3..2a04d26 100644 --- a/src/Structures/Notification.php +++ b/src/Structures/Notification.php @@ -9,7 +9,7 @@ trait Notification { - public mixed $payload; + public object $payload; public string $message = ''; diff --git a/src/Trait/ActionEventTrait.php b/src/Trait/ActionEventTrait.php index 7c05b68..5161e0a 100644 --- a/src/Trait/ActionEventTrait.php +++ b/src/Trait/ActionEventTrait.php @@ -4,11 +4,11 @@ trait ActionEventTrait { - public function getActionOfEvent($payload): string + public function getActionOfEvent(object $payload): string { - $action = $payload?->action - ?? $payload?->object_attributes?->action - ?? $payload?->object_attributes?->noteable_type + $action = $payload->action + ?? $payload->object_attributes?->action + ?? $payload->object_attributes?->noteable_type ?? ''; if (!empty($action)) { From 07b74e4e199633aed80a61d04b3802a64e1d30e3 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Mon, 30 Oct 2023 22:39:13 +0700 Subject: [PATCH 5/7] fix: phpstan test errors --- src/Interfaces/EventInterface.php | 5 +++-- src/Models/Event.php | 11 ++++++----- src/Models/Setting.php | 5 +++-- src/Trait/EventSettingTrait.php | 4 ++-- src/Trait/EventTrait.php | 2 +- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Interfaces/EventInterface.php b/src/Interfaces/EventInterface.php index df856ab..a2de979 100644 --- a/src/Interfaces/EventInterface.php +++ b/src/Interfaces/EventInterface.php @@ -2,6 +2,7 @@ namespace LbilTech\TelegramGitNotifier\Interfaces; +use LbilTech\TelegramGitNotifier\Constants\EventConstant; use LbilTech\TelegramGitNotifier\Trait\ActionEventTrait; use Symfony\Component\HttpFoundation\Request; @@ -20,13 +21,13 @@ public function getActionOfEvent(object $payload): string; /** * Set platform and platform file for event * - * @param string $platform + * @param string|null $platform * @param string|null $platformFile * * @return void * @see EventTrait::setPlatFormForEvent() */ - public function setPlatFormForEvent(string $platform, string $platformFile = null): void; + public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void; /** * Set event config and get event name diff --git a/src/Models/Event.php b/src/Models/Event.php index 6bab8f5..d3a12b2 100644 --- a/src/Models/Event.php +++ b/src/Models/Event.php @@ -41,17 +41,18 @@ public function getEventConfig(): array /** * Set event config * - * @param string $platform + * @param string|null $platform * * @return void */ public function setEventConfig( - string $platform = EventConstant::DEFAULT_PLATFORM + string $platform = null ): void { - $this->platform = $platform; + $this->platform = $platform ?? EventConstant::DEFAULT_PLATFORM; - if (file_exists($this->platformFile)) { - $json = file_get_contents($this->platformFile); + $json = file_get_contents($this->platformFile); + + if (!empty($json)) { $this->eventConfig = json_decode($json, true); } } diff --git a/src/Models/Setting.php b/src/Models/Setting.php index 0f53e13..f7b6d8c 100644 --- a/src/Models/Setting.php +++ b/src/Models/Setting.php @@ -43,8 +43,9 @@ public function getSettingFile(): string */ public function setSettingConfig(): void { - if (file_exists($this->settingFile)) { - $json = file_get_contents($this->settingFile); + $json = file_get_contents($this->settingFile); + + if (!empty($json)) { $this->settings = json_decode($json, true); } } diff --git a/src/Trait/EventSettingTrait.php b/src/Trait/EventSettingTrait.php index 486d204..6f5d925 100644 --- a/src/Trait/EventSettingTrait.php +++ b/src/Trait/EventSettingTrait.php @@ -124,9 +124,9 @@ public function getPlatformFromCallback( return $platform; } - if (str_contains($callback, EventConstant::GITHUB_EVENT_SEPARATOR)) { + if ($callback && str_contains($callback, EventConstant::GITHUB_EVENT_SEPARATOR)) { return 'github'; - } elseif (str_contains($callback, EventConstant::GITLAB_EVENT_SEPARATOR)) { + } elseif ($callback && str_contains($callback, EventConstant::GITLAB_EVENT_SEPARATOR)) { return 'gitlab'; } diff --git a/src/Trait/EventTrait.php b/src/Trait/EventTrait.php index 9bd5ae5..2175b0d 100644 --- a/src/Trait/EventTrait.php +++ b/src/Trait/EventTrait.php @@ -9,7 +9,7 @@ trait EventTrait { use ActionEventTrait; - public function setPlatFormForEvent(string $platform, string $platformFile = null): void + public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void { /** @var array $platformFileDefaults */ $platformFileDefaults = config('telegram-git-notifier.data_file.platform'); From 7b1d14ebb5e5aa2f9aad03aaad629d012c914e92 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Mon, 30 Oct 2023 23:47:52 +0700 Subject: [PATCH 6/7] fix: phpstan test errors --- src/Interfaces/Structures/AppInterface.php | 4 ++-- src/Structures/App.php | 2 +- src/Structures/Notification.php | 21 ++++++++++++++++++--- src/Trait/EventSettingTrait.php | 4 ++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Interfaces/Structures/AppInterface.php b/src/Interfaces/Structures/AppInterface.php index 83f3767..2bc4867 100644 --- a/src/Interfaces/Structures/AppInterface.php +++ b/src/Interfaces/Structures/AppInterface.php @@ -10,14 +10,14 @@ interface AppInterface /** * Send a message to telegram * - * @param string $message + * @param string|null $message * @param array $options * * @return void * @throws MessageIsEmptyException * @see App::sendMessage() */ - public function sendMessage(string $message = '', array $options = []): void; + public function sendMessage(?string $message = '', array $options = []): void; /** * Send a photo to telegram diff --git a/src/Structures/App.php b/src/Structures/App.php index 907df5d..f3465d3 100644 --- a/src/Structures/App.php +++ b/src/Structures/App.php @@ -27,7 +27,7 @@ private function createTelegramBaseContent(): array ]; } - public function sendMessage(string $message = '', array $options = []): void + public function sendMessage(?string $message = '', array $options = []): void { if (empty($message)) { throw MessageIsEmptyException::create(); diff --git a/src/Structures/Notification.php b/src/Structures/Notification.php index 2a04d26..9426436 100644 --- a/src/Structures/Notification.php +++ b/src/Structures/Notification.php @@ -4,6 +4,7 @@ use GuzzleHttp\Exception\GuzzleException; use LbilTech\TelegramGitNotifier\Constants\EventConstant; +use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException; use LbilTech\TelegramGitNotifier\Exceptions\SendNotificationException; use Symfony\Component\HttpFoundation\Request; @@ -30,11 +31,18 @@ public function accessDenied( public function setPayload(Request $request, string $event) { + $content = null; + if ($this->event->platform === 'gitlab') { - $this->payload = json_decode($request->getContent()); + $content = $request->getContent(); } elseif ($this->event->platform === EventConstant::DEFAULT_PLATFORM) { - $this->payload = json_decode($request->request->get('payload')); + $content = $request->request->get('payload'); + } + + if (is_string($content)) { + $this->payload = json_decode($content); } + $this->setMessage($event); return $this->payload; @@ -46,6 +54,7 @@ public function setPayload(Request $request, string $event) * @param string $typeEvent * * @return void + * @throws MessageIsEmptyException */ private function setMessage(string $typeEvent): void { @@ -56,10 +65,16 @@ private function setMessage(string $typeEvent): void ? "events.{$this->event->platform}.{$event}.default" : "events.{$this->event->platform}.{$event}.{$action}"; - $this->message = view($viewTemplate, [ + $viewResult = view($viewTemplate, [ 'payload' => $this->payload, 'event' => tgn_convert_event_name($typeEvent), ]); + + if ($viewResult === null) { + throw MessageIsEmptyException::create(); + } + + $this->message = $viewResult; } public function sendNotify(string $message = null, array $options = []): bool diff --git a/src/Trait/EventSettingTrait.php b/src/Trait/EventSettingTrait.php index 6f5d925..51f98ad 100644 --- a/src/Trait/EventSettingTrait.php +++ b/src/Trait/EventSettingTrait.php @@ -157,6 +157,10 @@ public function sendSettingEventMessage( public function getEventFromCallback(?string $callback): string { + if (!$callback) { + return ''; + } + return str_replace([ EventConstant::EVENT_PREFIX, EventConstant::GITHUB_EVENT_SEPARATOR, From 7561a62642325fcd80c2713e69cd8f4ba34321bf Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Mon, 30 Oct 2023 23:54:34 +0700 Subject: [PATCH 7/7] fix: remove redundant require before test --- composer.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/composer.json b/composer.json index 7f894cd..1577599 100644 --- a/composer.json +++ b/composer.json @@ -33,11 +33,6 @@ "common/helpers.php" ] }, - "autoload-dev": { - "psr-4": { - "LbilTech\\TelegramGitNotifier\\Tests\\": "tests" - } - }, "require": { "php": "^8.1", "eleirbag89/telegrambotphp": "^1.4",