Skip to content

Commit 5a8695d

Browse files
committed
refactor: simplified code
1 parent e5b76d0 commit 5a8695d

File tree

5 files changed

+26
-33
lines changed

5 files changed

+26
-33
lines changed

phpmyfaq/src/phpMyFAQ/Category.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ class Category
3535
{
3636
/**
3737
* The categories as an array.
38-
* @deprecated Will be removed in a future version. Use query methods (e.g., getOrderedCategories) instead.
3938
* @var array<int, array<string, mixed>>
4039
*/
41-
public array $categories = [];
40+
private array $categories = [];
4241

4342
/**
4443
* The category names as an array indexed by category ID.
@@ -47,13 +46,6 @@ class Category
4746
*/
4847
public array $categoryNames = [];
4948

50-
/**
51-
* The image as an array.
52-
* @deprecated Will be removed in a future version.
53-
* @var array<int, string>
54-
*/
55-
public array $image = [];
56-
5749
/**
5850
* The tree with the tabs.
5951
* @deprecated Will be removed in a future version.
@@ -266,7 +258,7 @@ public function getOrderedCategories(bool $withPermission = true, bool $withInac
266258
/**
267259
* Get the level of the item id.
268260
*/
269-
private function getLevelOf(int $categoryId): int
261+
public function getLevelOf(int $categoryId): int
270262
{
271263
return $this->getTreeBuilder()->computeLevel($this->categoryNames, $categoryId);
272264
}
@@ -342,11 +334,11 @@ private function getSymbol(int $categoryId, int $parentId): string
342334
{
343335
$siblings = $this->children[$parentId] ?? [];
344336
$array = array_keys($siblings);
345-
return $categoryId == end($array) ? 'angle' : 'medium';
337+
return $categoryId === end($array) ? 'angle' : 'medium';
346338
}
347339

348340
/**
349-
* List in array the root, super-root, ... of the $id.
341+
* List in an array the root, super-root, ... of the $id.
350342
*/
351343
private function getNodes(int $categoryId): array
352344
{
@@ -547,10 +539,13 @@ public function getCategoryLanguagesTranslated(int $categoryId): array
547539
public function getCategoryLanguagesToTranslate(int $categoryId, string $selectedLanguage): string
548540
{
549541
$output = '';
550-
$existingLanguage = $this->configuration->getLanguage()->isLanguageAvailable($categoryId, 'faqcategories');
542+
$existingLanguage = $this->configuration->getLanguage()->isLanguageAvailable(
543+
identifier: $categoryId,
544+
table: 'faqcategories',
545+
);
551546

552547
foreach (LanguageHelper::getAvailableLanguages() as $lang => $langname) {
553-
if (!in_array(strtolower((string) $lang), $existingLanguage)) {
548+
if (!in_array(strtolower((string) $lang), $existingLanguage, strict: true)) {
554549
$output .= "\t<option value=\"" . strtolower((string) $lang) . '"';
555550
if ($lang === $selectedLanguage) {
556551
$output .= ' selected="selected"';
@@ -601,13 +596,13 @@ public function getCategoryTree(): array
601596
return $this->catTree;
602597
}
603598

604-
public function getCategoryName(int $categoryId): array
599+
public function getCategoryName(int $categoryId): string
605600
{
606-
return $this->categoryNames[$categoryId]['name'] ?? [];
601+
return $this->categoryNames[$categoryId]['name'] ?? '';
607602
}
608603

609-
public function getCategoryDescription(int $categoryId): array
604+
public function getCategoryDescription(int $categoryId): string
610605
{
611-
return $this->categoryNames[$categoryId]['description'] ?? [];
606+
return $this->categoryNames[$categoryId]['description'] ?? '';
612607
}
613608
}

phpmyfaq/src/phpMyFAQ/Controller/Administration/CategoryController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,7 @@ public function translate(Request $request): Response
518518
'langToTranslate' => $langOptions, // deprecated in the future; generated from data service now
519519
'categoryDescriptionLabel' => Translation::get('categoryDescriptionLabel'),
520520
'categoryOwnerLabel' => Translation::get('categoryOwnerLabel'),
521-
'userSelection' => $userHelper->getAllUsersForTemplate(
522-
(int) $category->categoryNames[$categoryId]['user_id'],
523-
),
521+
'userSelection' => $userHelper->getAllUsersForTemplate($category->getOwner($categoryId),),
524522
'ad_categ_transalready' => Translation::get('ad_categ_transalready'),
525523
'langTranslated' => $category->getCategoryLanguagesTranslated($categoryId),
526524
'ad_categ_translatecateg' => Translation::get('ad_categ_translatecateg'),

phpmyfaq/src/phpMyFAQ/Export/Pdf.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,22 @@ public function generate(int $categoryId = 0, bool $downwards = true, string $la
109109
$this->wrapper->AddPage();
110110

111111
// Bookmark for categories
112-
if ($currentCategory !== $this->category->categoryNames[$faq['category_id']]['id']) {
112+
if ($currentCategory !== $faq['category_id']) {
113113
$this->wrapper->Bookmark(
114114
html_entity_decode(
115-
(string) $this->category->categoryNames[$faq['category_id']]['name'],
115+
(string) $this->category->getCategoryName($faq['category_id']),
116116
ENT_QUOTES,
117117
'utf-8',
118118
),
119-
$this->category->categoryNames[$faq['category_id']]['level'] - 1,
119+
$this->category->getLevelOf($faq['category_id']) - 1,
120120
0,
121121
);
122122
}
123123

124124
// Bookmark for FAQs
125125
$this->wrapper->Bookmark(
126126
html_entity_decode((string) $faq['topic'], ENT_QUOTES, 'utf-8'),
127-
$this->category->categoryNames[$faq['category_id']]['level'],
127+
$this->category->getLevelOf($faq['category_id']),
128128
0,
129129
);
130130

@@ -133,7 +133,7 @@ public function generate(int $categoryId = 0, bool $downwards = true, string $la
133133
}
134134

135135
$this->wrapper->SetFont($this->wrapper->getCurrentFont(), 'b', 12);
136-
$this->wrapper->WriteHTML('<h1>' . $this->category->categoryNames[$faq['category_id']]['name'] . '</h1>');
136+
$this->wrapper->WriteHTML('<h1>' . $this->category->getCategoryName($faq['category_id']) . '</h1>');
137137
$this->wrapper->WriteHTML('<h2>' . $faq['topic'] . '</h2>');
138138
$this->wrapper->Ln(10);
139139

@@ -164,7 +164,7 @@ public function generate(int $categoryId = 0, bool $downwards = true, string $la
164164
Translation::get('msgLastUpdateArticle') . Date::createIsoDate($faq['lastmodified']),
165165
);
166166

167-
$currentCategory = $this->category->categoryNames[$faq['category_id']]['id'];
167+
$currentCategory = $faq['category_id'];
168168
}
169169

170170
// remove default header/footer

phpmyfaq/src/phpMyFAQ/Helper/QuestionHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function getOpenQuestions(): stdClass
104104
$question->email = $mail->safeEmail($row->email);
105105
$question->userName = $row->username;
106106
$question->categoryId = $row->category_id;
107-
$question->categoryName = $this->getCategory()->categoryNames[$row->category_id]['name'] ?? '';
107+
$question->categoryName = $this->getCategory()->getCategoryName($row->category_id);
108108
$question->question = $row->question;
109109
$question->answerId = $row->answer_id;
110110

phpmyfaq/src/phpMyFAQ/Language.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ public function __construct(
5555
* Returns an array of country codes for a specific FAQ record ID,
5656
* specific category ID or all languages used by FAQ records, categories.
5757
*
58-
* @param int $faqId ID
59-
* @param string $table Specifies table
58+
* @param int $identifier ID
59+
* @param string $table Specifies table
6060
* @return string[]
6161
*/
62-
public function isLanguageAvailable(int $faqId, string $table = 'faqdata'): array
62+
public function isLanguageAvailable(int $identifier, string $table = 'faqdata'): array
6363
{
6464
$output = [];
6565

66-
if ($faqId === 0) {
66+
if ($identifier === 0) {
6767
$distinct = ' DISTINCT ';
6868
$where = '';
6969
} else {
7070
$distinct = '';
71-
$where = ' WHERE id = ' . $faqId;
71+
$where = ' WHERE id = ' . $identifier;
7272
}
7373

7474
$query = sprintf('SELECT %s lang FROM %s%s %s', $distinct, Database::getTablePrefix(), $table, $where);

0 commit comments

Comments
 (0)