Skip to content

Commit 64adf20

Browse files
committed
Add PHP code sniffer and linter
1 parent dc31e44 commit 64adf20

File tree

10 files changed

+152
-19
lines changed

10 files changed

+152
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
55

66
## [Unreleased]
77
### Added
8+
- Code checkers to ensure coding standard.
89
### Changed
910
- Bumped Manager to 1.5.
1011
- Logging is now decoupled with custom Monolog logger.

commands/HelpCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Longman\TelegramBot\Commands\Command;
1414
use Longman\TelegramBot\Commands\UserCommand;
1515
use Longman\TelegramBot\Entities\ServerResponse;
16+
use Longman\TelegramBot\Exception\TelegramException;
1617

1718
/**
1819
* User "/help" command
@@ -79,7 +80,7 @@ public function execute(): ServerResponse
7980
foreach ($user_commands as $user_command) {
8081
$text .= '/' . $user_command->getName() . ' - ' . $user_command->getDescription() . PHP_EOL;
8182
}
82-
if ($safe_to_show && \count($admin_commands) > 0) {
83+
if ($safe_to_show && count($admin_commands) > 0) {
8384
$text .= PHP_EOL . '*Admin Commands List*:' . PHP_EOL;
8485
foreach ($admin_commands as $admin_command) {
8586
$text .= '/' . $admin_command->getName() . ' - ' . $admin_command->getDescription() . PHP_EOL;
@@ -112,24 +113,25 @@ public function execute(): ServerResponse
112113
* Get all available User and Admin commands to display in the help list.
113114
*
114115
* @return Command[][]
116+
* @throws TelegramException
115117
*/
116118
protected function getUserAdminCommands(): array
117119
{
118120
// Only get enabled Admin and User commands that are allowed to be shown.
119121
/** @var Command[] $commands */
120-
$commands = array_filter($this->telegram->getCommandsList(), function ($command) {
122+
$commands = array_filter($this->telegram->getCommandsList(), static function ($command) {
121123
/** @var Command $command */
122124
return !$command->isSystemCommand() && $command->showInHelp() && $command->isEnabled();
123125
});
124126
ksort($commands);
125127

126-
$user_commands = array_filter($commands, function ($command) {
128+
$user_commands = array_filter($commands, static function ($command) {
127129
/** @var Command $command */
128130
return $command->isUserCommand();
129131
});
130132
ksort($user_commands);
131133

132-
$admin_commands = array_filter($commands, function ($command) {
134+
$admin_commands = array_filter($commands, static function ($command) {
133135
/** @var Command $command */
134136
return $command->isAdminCommand();
135137
});

commands/NewchatmembersCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Longman\TelegramBot\Entities\ChatMember;
1515
use Longman\TelegramBot\Entities\ServerResponse;
1616
use Longman\TelegramBot\Entities\User;
17+
use Longman\TelegramBot\Exception\TelegramException;
1718
use Longman\TelegramBot\Request;
1819
use TelegramBot\SupportBot\Helpers;
1920

@@ -54,7 +55,7 @@ class NewchatmembersCommand extends SystemCommand
5455

5556
/**
5657
* @inheritdoc
57-
* @throws \Longman\TelegramBot\Exception\TelegramException
58+
* @throws TelegramException
5859
*/
5960
public function execute(): ServerResponse
6061
{
@@ -78,15 +79,15 @@ public function execute(): ServerResponse
7879
* @param array $new_users
7980
*
8081
* @return ServerResponse
81-
* @throws \Longman\TelegramBot\Exception\TelegramException
82+
* @throws TelegramException
8283
*/
8384
private function refreshWelcomeMessage(array $new_users): ServerResponse
8485
{
8586
if (empty($new_users)) {
8687
return Request::emptyResponse();
8788
}
8889

89-
$new_users_text = implode(', ', array_map(function (User $new_user) {
90+
$new_users_text = implode(', ', array_map(static function (User $new_user) {
9091
return '<a href="tg://user?id=' . $new_user->getId() . '">' . filter_var($new_user->getFirstName(), FILTER_SANITIZE_SPECIAL_CHARS) . '</a>';
9192
}, $new_users));
9293

@@ -125,7 +126,7 @@ private function isUserAllowedToAddBot(): bool
125126
])->getResult();
126127

127128
if ($chat_member instanceof ChatMember) {
128-
return \in_array($chat_member->getStatus(), ['creator', 'administrator'], true);
129+
return in_array($chat_member->getStatus(), ['creator', 'administrator'], true);
129130
}
130131

131132
return false;

commands/StartCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Longman\TelegramBot\Commands\SystemCommand;
1414
use Longman\TelegramBot\Entities\ServerResponse;
15+
use Longman\TelegramBot\Exception\TelegramException;
1516

1617
/**
1718
* System "/start" command
@@ -45,7 +46,7 @@ class StartCommand extends SystemCommand
4546

4647
/**
4748
* @inheritdoc
48-
* @throws \Longman\TelegramBot\Exception\TelegramException
49+
* @throws TelegramException
4950
*/
5051
public function execute(): ServerResponse
5152
{

composer.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@
2727
"elvanto/litemoji": "^1.4",
2828
"monolog/monolog": "^1.24"
2929
},
30+
"require-dev": {
31+
"squizlabs/php_codesniffer": "^3.4",
32+
"jakub-onderka/php-parallel-lint": "^1.0"
33+
},
3034
"autoload": {
3135
"psr-4": {
3236
"TelegramBot\\SupportBot\\": "src"
3337
}
38+
},
39+
"scripts": {
40+
"check-code": [
41+
"vendor/bin/parallel-lint . --exclude vendor",
42+
"vendor/bin/phpcs"
43+
]
3444
}
3545
}

composer.lock

Lines changed: 102 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpcs.xml.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<arg value="snp"/>
4+
<arg name="colors"/>
5+
<arg name="extensions" value="php"/>
6+
7+
<file>.</file>
8+
<exclude-pattern>*/vendor/*</exclude-pattern>
9+
10+
<rule ref="PSR12"/>
11+
</ruleset>

public/manager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Monolog\Logger;
1919
use Psr\Log\NullLogger;
2020
use TelegramBot\TelegramBotManager\BotManager;
21+
use Throwable;
2122

2223
// Composer autoloader.
2324
require_once __DIR__ . '/../vendor/autoload.php';
@@ -58,15 +59,15 @@
5859
$bot->run();
5960
} catch (TelegramLogException $e) {
6061
// Silence... beautiful silence =)
61-
} catch (\Throwable $e) {
62+
} catch (Throwable $e) {
6263
TelegramLog::error($e->getMessage());
6364
}
6465

6566
/**
6667
* Initialise the logging.
6768
*
6869
*/
69-
function initLogging()
70+
function initLogging(): void
7071
{
7172
// Logging.
7273
$logging_paths = json_decode(getenv('TG_LOGGING'), true) ?? [];

public/webhooks/github.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Github\Client;
1515
use Longman\TelegramBot\Entities\ServerResponse;
1616
use Longman\TelegramBot\Exception\TelegramException;
17+
use Longman\TelegramBot\Request;
1718
use Longman\TelegramBot\Telegram;
1819
use Longman\TelegramBot\TelegramLog;
1920
use Monolog\Formatter\LineFormatter;
@@ -63,7 +64,8 @@
6364
*
6465
* @param array $data
6566
*/
66-
function handleRelease($data) {
67+
function handleRelease(array $data): void
68+
{
6769
$repo = $data['repository'];
6870
$release = $data['release'];
6971
$action = $data['action'];
@@ -96,7 +98,8 @@ function handleRelease($data) {
9698
*
9799
* @return string
98100
*/
99-
function parseReleaseBody($body, $user, $repo) {
101+
function parseReleaseBody($body, $user, $repo): string
102+
{
100103
// Replace headers with bold text.
101104
$body = preg_replace_callback('~### (?<header>.*)~', static function ($matches) {
102105
return "*{$matches['header']}*";
@@ -142,7 +145,8 @@ function parseReleaseBody($body, $user, $repo) {
142145
*
143146
* @return ServerResponse|null
144147
*/
145-
function sendTelegramMessage($chat_id, $text) {
148+
function sendTelegramMessage($chat_id, $text): ?ServerResponse
149+
{
146150
try {
147151
new Telegram(getenv('TG_API_KEY'));
148152

@@ -153,7 +157,7 @@ function sendTelegramMessage($chat_id, $text) {
153157

154158
$parse_mode = 'markdown';
155159

156-
return Longman\TelegramBot\Request::sendMessage(compact('chat_id', 'text', 'parse_mode'));
160+
return Request::sendMessage(compact('chat_id', 'text', 'parse_mode'));
157161
} catch (TelegramException $e) {
158162
TelegramLog::error($e->getMessage());
159163
} catch (Throwable $e) {

src/Webhooks/Utils.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
namespace TelegramBot\SupportBot\Webhooks;
1313

14-
class Utils {
14+
class Utils
15+
{
1516
/**
1617
* Log the incoming webhook data.
1718
*
1819
* @param string $path
1920
*/
20-
public static function logWebhookData($path): void {
21+
public static function logWebhookData($path): void
22+
{
2123
$f = fopen($path, 'ab+');
2224
fwrite($f, sprintf(
2325
"%s\ninput: %s\nGET: %s\nPOST: %s\nSERVER: %s\n\n",

0 commit comments

Comments
 (0)