Skip to content

Commit 40617d0

Browse files
committed
refactor: improved code
1 parent 1e1cfae commit 40617d0

File tree

9 files changed

+31
-27
lines changed

9 files changed

+31
-27
lines changed

mago.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ null-type-hint = "question"
1616

1717
[linter]
1818
integrations = ["symfony", "php-unit"]
19+
20+
[linter.rules]
21+
cyclomatic-complexity = { threshold = 50 }
22+
halstead = { volume-threshold = 3000, effort-threshold = 20000 }
23+
kan-defect = { threshold = 3.0 }
24+
too-many-enum-cases = { threshold = 50 }
25+
too-many-methods = { threshold = 20 }
26+
too-many-properties = { threshold = 15 }

phpmyfaq/src/phpMyFAQ/Attachment/Filesystem/AbstractFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(
5555
) {
5656
$this->path = $filepath;
5757

58-
$this->handle = @fopen($this->path, $this->mode);
58+
$this->handle = fopen($this->path, $this->mode);
5959

6060
if (!is_resource($this->handle)) {
6161
throw new FileException('Could not open file: ' . $this->path);

phpmyfaq/src/phpMyFAQ/Auth/AuthLdap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function assignUserToGroups(string $login, int $userId): void
126126
$groupName = $this->extractGroupNameFromDn($userGroup);
127127

128128
// Check if there's a specific mapping for this AD group
129-
if (!empty($groupMapping) && isset($groupMapping[$groupName])) {
129+
if (count($groupMapping) > 0 && isset($groupMapping[$groupName])) {
130130
$faqGroupName = $groupMapping[$groupName];
131131
} else {
132132
// Default: use the AD group name
@@ -232,7 +232,7 @@ public function checkCredentials(
232232
}
233233

234234
$allowedGroups = $ldapGroupConfig['allowed_groups'];
235-
if (!empty($allowedGroups)) {
235+
if (count($allowedGroups) > 0) {
236236
$hasAllowedGroup = false;
237237
foreach ($userGroups as $userGroup) {
238238
foreach ($allowedGroups as $allowedGroup) {

phpmyfaq/src/phpMyFAQ/Helper/StatisticsHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ public function getLastTrackingDate(int $lastDate): string
9595
if (is_file(PMF_ROOT_DIR . '/content/core/data/tracking' . date(format: 'dmY', timestamp: $lastDate))) {
9696
$fp = fopen(PMF_ROOT_DIR . '/content/core/data/tracking' . date(format: 'dmY', timestamp: $lastDate), 'r');
9797

98+
$date = null;
9899
while (($data = fgetcsv($fp, length: 1024, separator: ';', enclosure: '"', escape: '\\')) !== false) {
99100
$date = isset($data[7]) && 10 === strlen($data[7]) ? $data[7] : $requestTime;
100101
}
101102

102103
fclose($fp);
103104

104-
if (empty($date)) {
105+
if ($date === null || $date === 0) {
105106
$date = $request->server->get('REQUEST_TIME');
106107
}
107108

phpmyfaq/src/phpMyFAQ/Mail/Builtin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function send(string $recipients, array $headers, string $body): int
5757
}
5858

5959
// Send the email
60-
if (empty($sender)) {
60+
if ($sender === '') {
6161
return (int) mail($recipients, $subject, $body, $mailHeaders);
6262
}
6363

phpmyfaq/src/phpMyFAQ/Permission/MediumPermission.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function checkUserGroupRight(int $userId, int $rightId): bool
124124
public function grantGroupRight(int $groupId, int $rightId): bool
125125
{
126126
$right_data = $this->getRightData($rightId);
127-
if (empty($right_data) || !($right_data['for_groups'] ?? false)) {
127+
if (count($right_data) === 0 || !($right_data['for_groups'] ?? false)) {
128128
return false;
129129
}
130130

phpmyfaq/src/phpMyFAQ/Search/SearchDatabase.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,7 @@ public function search(string $searchTerm): mixed
104104
*/
105105
public function getResultColumns(): string
106106
{
107-
$resultColumns = '';
108-
109-
foreach ($this->resultColumns as $resultColumn) {
110-
if (empty($resultColumns)) {
111-
$resultColumns = $resultColumn;
112-
} else {
113-
$resultColumns .= ', ' . $resultColumn;
114-
}
115-
}
116-
117-
return $resultColumns;
107+
return implode(', ', $this->resultColumns);
118108
}
119109

120110
/**
@@ -262,7 +252,7 @@ public function getMatchClause(string $searchTerm = ''): string
262252
$where = '';
263253

264254
for ($i = 0; $i < $numKeys; ++$i) {
265-
if (strlen($where) != 0) {
255+
if ($where !== '') {
266256
$where .= ' OR';
267257
}
268258

phpmyfaq/src/phpMyFAQ/Session/Token.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ private function getSession(string $page): ?Token
176176

177177
private function getCookie(string $page): string
178178
{
179-
$cookieValue = Request::createFromGlobals()->cookies->get($this->getCookieName($page), '');
180-
181-
return empty($cookieValue) ? '' : $cookieValue;
179+
return Request::createFromGlobals()->cookies->get($this->getCookieName($page), '');
182180
}
183181

184182
/**

phpmyfaq/src/phpMyFAQ/User/Tracking.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,14 @@ public function getRemoteAddress(): string
123123
$remoteAddress = $this->request->getClientIp();
124124
$localAddresses = ['127.0.0.1', '::1'];
125125

126-
if (in_array($remoteAddress, $localAddresses) && $this->getRequestHeaders()->has('X-Forwarded-For')) {
126+
if (
127+
in_array($remoteAddress, $localAddresses, strict: true)
128+
&& $this->getRequestHeaders()->has('X-Forwarded-For')
129+
) {
127130
$remoteAddress = $this->getRequestHeaders()->get('X-Forwarded-For');
128131
}
129132

130-
return preg_replace('([^0-9a-z:.]+)i', '', (string) $remoteAddress);
133+
return preg_replace(pattern: '([^0-9a-z:.]+)i', replacement: '', subject: (string) $remoteAddress);
131134
}
132135

133136
private function isBanned(string $remoteAddress): bool
@@ -147,7 +150,7 @@ private function handleSession(?int $cookieId, string $remoteAddress, string $ac
147150
'sid',
148151
);
149152

150-
if (!is_null($cookieId) && !$cookieId != $this->userSession->getCurrentSessionId()) {
153+
if (!is_null($cookieId) && !$cookieId !== $this->userSession->getCurrentSessionId()) {
151154
$this->userSession->setCookie(
152155
UserSession::COOKIE_NAME_SESSION_ID,
153156
$this->userSession->getCurrentSessionId(),
@@ -180,11 +183,15 @@ private function writeTrackingData(string $action, int|string|null $data, string
180183
. ';'
181184
. $remoteAddress
182185
. ';'
183-
. str_replace(';', ',', $this->request->server->get('QUERY_STRING') ?? '')
186+
. str_replace(search: ';', replace: ',', subject: $this->request->server->get('QUERY_STRING') ?? '')
184187
. ';'
185-
. str_replace(';', ',', $this->request->server->get('HTTP_REFERER') ?? '')
188+
. str_replace(search: ';', replace: ',', subject: $this->request->server->get('HTTP_REFERER') ?? '')
186189
. ';'
187-
. str_replace(';', ',', urldecode((string) $this->request->server->get('HTTP_USER_AGENT')))
190+
. str_replace(
191+
search: ';',
192+
replace: ',',
193+
subject: urldecode((string) $this->request->server->get('HTTP_USER_AGENT')),
194+
)
188195
. ';'
189196
. $this->request->server->get('REQUEST_TIME')
190197
. ";\n";

0 commit comments

Comments
 (0)