Skip to content

Commit dece2c4

Browse files
tanhongitCopilot
andcommitted
refactor: replace empty() with explicit checks, fix null safety
- Replace all empty() calls with explicit === '' / !== null checks - Add is_array() guard in ConfigHelper::execConfig() traversal - Handle preg_replace() null return in helpers - Fix setCallbackContentMessage() using isset() instead of truthy check - Make ConfigHelper::config readonly and private - Add return type to tgn_view() Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5b0d6eb commit dece2c4

10 files changed

Lines changed: 28 additions & 30 deletions

File tree

common/helpers.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @return string|null
1212
*/
13-
function tgn_singularity(string $word): string|null
13+
function tgn_singularity(string $word): ?string
1414
{
1515
static $singular_rules = [
1616
'/(quiz)zes$/i' => '$1',
@@ -56,7 +56,7 @@ function tgn_singularity(string $word): string|null
5656
*/
5757
function tgn_snake_case(string $string): string
5858
{
59-
$string = preg_replace('/\s+/', '_', $string);
59+
$string = preg_replace('/\s+/', '_', $string) ?? $string;
6060

6161
return strtolower($string);
6262
}
@@ -100,7 +100,7 @@ function tgn_convert_event_name(string $event): string
100100
*/
101101
function tgn_convert_action_name(string $action): string
102102
{
103-
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $action));
103+
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $action) ?? $action);
104104
}
105105
}
106106

@@ -128,28 +128,26 @@ function config(string $string): mixed
128128
*
129129
* @return null|string
130130
*/
131-
function view(string $partialPath, array $data = []): null|string
131+
function view(string $partialPath, array $data = []): ?string
132132
{
133133
$content = (new ConfigHelper())->getTemplateData(
134134
$partialPath,
135135
$data
136136
);
137137

138-
return $content ?: null;
138+
return $content !== '' ? $content : null;
139139
}
140140
}
141141
}
142142

143143
if (!function_exists('tgn_view')) {
144144
/**
145-
* Get view template
145+
* Get view template from the correct source (Laravel Blade or standalone PHP).
146146
*
147-
* @param string $partialPath
148-
* @param array $data
149-
*
150-
* @noinspection PhpMissingReturnTypeInspection
147+
* @param string $partialPath Dot-notation template path
148+
* @param array<string, mixed> $data Variables to pass to the template
151149
*/
152-
function tgn_view(string $partialPath, array $data = [])
150+
function tgn_view(string $partialPath, array $data = []): mixed
153151
{
154152
if (class_exists('Illuminate\Foundation\Application')) {
155153
$partialPath = config('telegram-git-notifier.view.namespace') . '::' . $partialPath;

src/Bot.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(
6161

6262
public function validateSettingFile(): void
6363
{
64-
if (empty($this->setting->getSettingFile())) {
64+
if ($this->setting->getSettingFile() === '') {
6565
throw ConfigFileException::settingFile($this->setting->getSettingFile());
6666
}
6767
}

src/Helpers/ConfigHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class ConfigHelper
1111
/**
1212
* @var array<string, mixed>
1313
*/
14-
public array $config;
14+
private readonly array $config;
1515

1616
public function __construct()
1717
{
@@ -27,7 +27,7 @@ public function execConfig(string $string): mixed
2727
$result = $this->config;
2828

2929
foreach ($keys as $key) {
30-
if (!isset($result[$key])) {
30+
if (!is_array($result) || !array_key_exists($key, $result)) {
3131
return '';
3232
}
3333

src/Models/Event.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function reloadEventConfig(): void
6464
*/
6565
public function updateEvent(string $event, ?string $action): void
6666
{
67-
if (!empty($action)) {
67+
if ($action !== null && $action !== '') {
6868
$this->eventConfig[$event][$action]
6969
= !$this->eventConfig[$event][$action];
7070
} else {
@@ -82,15 +82,15 @@ public function getPlatformEnum(): Platform
8282

8383
private function loadFromFile(): void
8484
{
85-
if (empty($this->platformFile) || !file_exists($this->platformFile)) {
85+
if ($this->platformFile === '' || !file_exists($this->platformFile)) {
8686
$this->eventConfig = [];
8787

8888
return;
8989
}
9090

9191
$json = file_get_contents($this->platformFile);
9292

93-
if (!empty($json)) {
93+
if ($json !== false && $json !== '') {
9494
$this->eventConfig = json_decode($json, true) ?? [];
9595
}
9696

src/Models/Setting.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public function reloadSettings(): void
5353

5454
public function isAllEventsNotification(): bool
5555
{
56-
return !empty($this->settings)
56+
return $this->settings !== []
5757
&& ($this->settings[SettingConstant::T_ALL_EVENTS_NOTIFICATION] ?? false) === true;
5858
}
5959

6060
public function isNotified(): bool
6161
{
62-
return !empty($this->settings)
62+
return $this->settings !== []
6363
&& ($this->settings[SettingConstant::T_IS_NOTIFIED] ?? false) === true;
6464
}
6565

@@ -98,15 +98,15 @@ public function updateSetting(
9898

9999
private function loadFromFile(): void
100100
{
101-
if (empty($this->settingFile) || !file_exists($this->settingFile)) {
101+
if ($this->settingFile === '' || !file_exists($this->settingFile)) {
102102
$this->settings = [];
103103

104104
return;
105105
}
106106

107107
$json = file_get_contents($this->settingFile);
108108

109-
if (!empty($json)) {
109+
if ($json !== false && $json !== '') {
110110
$this->settings = json_decode($json, true) ?? [];
111111
}
112112

src/Notifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function parseNotifyChatIds(?string $chatIds = null): array
5555
{
5656
$raw = $chatIds ?? config('telegram-git-notifier.bot.notify_chat_ids');
5757

58-
if (empty($raw)) {
58+
if ($raw === null || $raw === '') {
5959
return [];
6060
}
6161

src/Objects/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function isAccessEvent(
5353
$eventConfig = $this->event->getEventConfig()[tgn_convert_event_name($event)] ?? false;
5454
$action = $this->getActionOfEvent($payload);
5555

56-
if (!empty($action) && isset($eventConfig[$action])) {
56+
if ($action !== '' && isset($eventConfig[$action])) {
5757
$eventConfig = $eventConfig[$action];
5858
}
5959

src/Structures/App.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ private function createTelegramBaseContent(): array
3131

3232
public function sendMessage(?string $message = '', array $options = []): void
3333
{
34-
if (empty($message)) {
34+
if ($message === null || $message === '') {
3535
throw MessageIsEmptyException::create();
3636
}
3737

3838
$content = $this->createTelegramBaseContent();
3939
$content['text'] = $message;
4040

41-
if (!empty($options['reply_markup'])) {
41+
if (isset($options['reply_markup'])) {
4242
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard(
4343
$options['reply_markup']
4444
);
@@ -52,7 +52,7 @@ public function sendMessage(?string $message = '', array $options = []): void
5252

5353
public function sendPhoto(string $photo = '', array $options = []): void
5454
{
55-
if (empty($photo)) {
55+
if ($photo === '') {
5656
throw EntryNotFoundException::fileNotFound();
5757
}
5858

@@ -66,7 +66,7 @@ public function sendPhoto(string $photo = '', array $options = []): void
6666

6767
public function answerCallbackQuery(?string $text = null, array $options = []): void
6868
{
69-
if (empty($text)) {
69+
if ($text === null || $text === '') {
7070
throw MessageIsEmptyException::create();
7171
}
7272

@@ -122,7 +122,7 @@ public function setCallbackContentMessage(array $options = []): array
122122
'parse_mode' => 'HTML',
123123
];
124124

125-
$content['reply_markup'] = $options['reply_markup']
125+
$content['reply_markup'] = isset($options['reply_markup'])
126126
? $this->telegram->buildInlineKeyBoard($options['reply_markup'])
127127
: null;
128128

src/Structures/Notification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private function setMessage(string $typeEvent): void
9191
#[\NoDiscard('The return value indicates whether the notification was sent successfully')]
9292
public function sendNotify(?string $message = null, array $options = []): bool
9393
{
94-
if (!empty($message)) {
94+
if ($message !== null && $message !== '') {
9595
$this->message = $message;
9696
}
9797

src/Trait/ActionEventTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function getActionOfEvent(object $payload): string
1111
?? $payload->object_attributes?->noteable_type
1212
?? '';
1313

14-
if (!empty($action)) {
14+
if ($action !== '') {
1515
return tgn_convert_action_name($action);
1616
}
1717

0 commit comments

Comments
 (0)