Skip to content

Commit

Permalink
refactor: some code improved found by Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Oct 19, 2023
1 parent a32a00a commit f4ce7a1
Show file tree
Hide file tree
Showing 33 changed files with 276 additions and 259 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This is a log of major user-visible changes in each phpMyFAQ release.

### phpMyFAQ v4.0.0-dev - 2023-

- changed PHP requirement to PHP 8.2.0 or later (Thorsten)
- changed rewrite rules for Apache and nginx as mandatory requirement (Thorsten)
- changed folder structure (Thorsten, Jan Harms)
- added experimental support for PHP 8.3 (Thorsten)
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/admin/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
'buttonCheckUpdates' => Translation::get('buttonCheckUpdates'),
'headerDownloadPackage' => Translation::get('headerDownloadPackage'),
'isOnNightlies' => $faqConfig->get('upgrade.releaseEnvironment') === ReleaseType::NIGHTLY->value,
'releaseEnvironment' => ucfirst($faqConfig->get('upgrade.releaseEnvironment')),
'releaseEnvironment' => ucfirst((string) $faqConfig->get('upgrade.releaseEnvironment')),
'dateLastChecked' => $faqConfig->get('upgrade.dateLastChecked')
];

Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@
$categoryId = $categoryIdFound;
}

if ($faq->hasTitleAHash($question) {
if ($faq->hasTitleAHash($question)) {
$response->setStatusCode(400);
$result = [
'stored' => false,
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/assets/src/api/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2014-2023 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2014-11-23
*/
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/assets/src/api/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2023 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-09-19
*/
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/assets/src/api/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @package phpMyFAQ
* @author Thorsten Rinne
* @copyright 2023 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-01-06
*/
Expand Down
5 changes: 3 additions & 2 deletions phpmyfaq/assets/src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './forms';
export * from './bookmarks';
export * from './autocomplete';
export * from './bookmarks';
export * from './forms';
export * from './setup';
16 changes: 16 additions & 0 deletions phpmyfaq/assets/src/api/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Setup API functionality
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2023 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-10-18
*/

export const handleSetup = () => {};
30 changes: 26 additions & 4 deletions phpmyfaq/setup/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
*/

use Composer\Autoload\ClassLoader;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Component\Alert;use phpMyFAQ\Core\Exception;
use phpMyFAQ\Language\LanguageCodes;
use phpMyFAQ\Setup\Installer;
use phpMyFAQ\Strings;
use phpMyFAQ\System;
use phpMyFAQ\System;use phpMyFAQ\Translation;

define('PMF_ROOT_DIR', dirname(__FILE__, 2));
const PMF_SRC_DIR = PMF_ROOT_DIR . '/src';
Expand Down Expand Up @@ -109,7 +109,8 @@ class="needs-validation" novalidate>
<h1 class="display-4 fw-bold">phpMyFAQ <?= System::getVersion() ?></h1>
<div class="col-lg-6 mx-auto">
<p class="lead mb-4">
Did you already read our <a target="_blank" href="https://www.phpmyfaq.de/docs/3.2">documentation</a>
Did you already read our
<a target="_blank" href="<?= System::getDocumentationUrl() ?>">documentation</a>
carefully before starting the phpMyFAQ setup?
</p>
</div>
Expand All @@ -127,9 +128,30 @@ class="needs-validation" novalidate>
// Initialize static string wrapper
//
Strings::init();

//
// Set translation class
//
try {
Translation::create()
->setLanguagesDir(PMF_LANGUAGE_DIR)
->setDefaultLanguage('en')
->setCurrentLanguage('en')
->setMultiByteLanguage();
} catch (Exception $e) {
echo '<strong>Error:</strong> ' . $e->getMessage();
}

$system = new System();
$installer = new Installer($system);
$installer->checkBasicStuff();

try {
$installer->checkBasicStuff();
} catch (Exception $e) {
echo Alert::danger('ad_entryins_fail', $e->getMessage());
System::renderFooter();
}

$installer->checkFilesystemPermissions();

// not yet POSTed
Expand Down
16 changes: 9 additions & 7 deletions phpmyfaq/setup/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@
$version = Filter::filterInput(INPUT_POST, 'version', FILTER_SANITIZE_SPECIAL_CHARS);
$query = [];

if (
!file_exists(PMF_ROOT_DIR . '/config/database.php') &&
!file_exists(PMF_ROOT_DIR . '/content/core/config/database.php')
) {
$update = new Update(new System(), Configuration::getConfigurationInstance());

if (!$update->checkDatabaseFile()) {
$redirect = new RedirectResponse('./index.php');
$redirect->send();
}
Expand Down Expand Up @@ -108,7 +107,7 @@
<h1 class="display-4 fw-bold">phpMyFAQ <?= System::getVersion() ?></h1>
<div class="col-lg-6 mx-auto">
<p class="lead mb-4">
Did you already read our <a target="_blank" href="https://www.phpmyfaq.de/docs/3.2">documentation</a>
Did you already read our <a target="_blank" href="<?= System::getDocumentationUrl() ?>">documentation</a>
carefully before starting the phpMyFAQ setup?
</p>
</div>
Expand All @@ -125,11 +124,14 @@
$faqConfig = Configuration::getConfigurationInstance();
$version = $faqConfig->getVersion();

$installer = new Installer($system);
$update = new Update($system, $faqConfig);
$update->setVersion(System::getVersion());

$installer->checkPreUpgrade($dbConfig->getType());
try {
$update->checkPreUpgrade($dbConfig->getType());
} catch (Exception $e) {
echo Alert::danger('ad_entryins_fail', $e->getMessage());
}

if ($update->isConfigTableAvailable($faqConfig->getDb())) {
echo Alert::danger('ad_entryins_fail');
Expand Down
17 changes: 4 additions & 13 deletions phpmyfaq/src/phpMyFAQ/Controller/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,20 @@

use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Filter;
use phpMyFAQ\Setup\Update;
use phpMyFAQ\System;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;

class SetupController
{
/**
* @throws \JsonException
*/
public function update(Request $request): StreamedResponse
public function update(): StreamedResponse
{
$update = new Update(new System(), Configuration::getConfigurationInstance());
$update->setVersion(System::getVersion());

//$postBody = json_decode($request->getContent(), false, 512, JSON_THROW_ON_ERROR);
//$step = Filter::filterVar($postBody->step, FILTER_VALIDATE_INT);
$step = 1;

return new StreamedResponse(function () use ($step, $update) {
return new StreamedResponse(function () use ($update) {
$progressCallback = function ($progress) {
echo json_encode(['progress' => $progress]) . "\n";
echo json_encode(['progress' => $progress], JSON_THROW_ON_ERROR) . "\n";
ob_flush();
flush();
};
Expand All @@ -51,7 +42,7 @@ public function update(Request $request): StreamedResponse
echo json_encode(['message' => '✅ Database successfully updated.']);
}
} catch (Exception $e) {
echo json_encode(['message' => 'Update database failed: ' . $e->getMessage()]);
echo json_encode(['message' => 'Update database failed: ' . $e->getMessage()], JSON_THROW_ON_ERROR);
}
});
}
Expand Down
18 changes: 6 additions & 12 deletions phpmyfaq/src/phpMyFAQ/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public static function factory(string $type): ?DatabaseDriver
self::$instance = new $class();

return self::$instance;
} else {
throw new Exception('Invalid Database Type: ' . $type);
}

throw new Exception('Invalid Database Type: ' . $type);
}

/**
Expand Down Expand Up @@ -96,19 +96,13 @@ public static function getType(): string
* Check if a table is filled with data.
*
* @param string $tableName Table name
* @return bool true, if table is empty, otherwise false
* @return bool true, if the table is empty, otherwise false
*/
public static function checkOnEmptyTable(string $tableName): bool
{
if (
self::$instance->numRows(
self::$instance->query('SELECT * FROM ' . self::getTablePrefix() . $tableName)
) < 1
) {
return true;
} else {
return false;
}
return self::$instance->numRows(
self::$instance->query('SELECT * FROM ' . self::getTablePrefix() . $tableName)
) < 1;
}

/**
Expand Down
12 changes: 4 additions & 8 deletions phpmyfaq/src/phpMyFAQ/Database/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ public function numRows(mixed $result): int
{
if ($result instanceof mysqli_result) {
return $result->num_rows;
} else {
return 0;
}

return 0;
}

/**
Expand Down Expand Up @@ -280,13 +280,9 @@ public function nextId(string $table, string $id): int
$table
);

$result = $this->query($select);
$mysqliresult = $this->query($select);

if ($result instanceof mysqli_result) {
$current = $result->fetch_row();
} else {
$current = [0];
}
$current = $mysqliresult instanceof mysqli_result ? $mysqliresult->fetch_row() : [0];

return $current[0] + 1;
}
Expand Down
14 changes: 3 additions & 11 deletions phpmyfaq/src/phpMyFAQ/Database/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function connect(

$this->conn = pg_connect($connectionString);

if (empty($database) || !$this->conn) {
if ($database === '' || !$this->conn) {
Database::errorPage(pg_last_error($this->conn));
die();
}
Expand Down Expand Up @@ -238,11 +238,7 @@ public function nextId(string $table, string $id): int
public function clientVersion(): string
{
$pg_version = pg_version($this->conn);
if (isset($pg_version['client'])) {
return $pg_version['client'];
} else {
return 'n/a';
}
return $pg_version['client'] ?? 'n/a';
}

/**
Expand All @@ -251,11 +247,7 @@ public function clientVersion(): string
public function serverVersion(): string
{
$pg_version = pg_version($this->conn);
if (isset($pg_version['server_version'])) {
return $pg_version['server_version'];
} else {
return 'n/a';
}
return $pg_version['server_version'] ?? 'n/a';
}

/**
Expand Down
5 changes: 4 additions & 1 deletion phpmyfaq/src/phpMyFAQ/Database/Sqlite3.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ public function fetchAssoc(mixed $result): array
*/
public function numRows(mixed $result): int
{
!isset($result->fetchedByPMF) || !$result->fetchedByPMF || die(self::ERROR_MESSAGE);
if (isset($result->fetchedByPMF) && $result->fetchedByPMF) {
die(self::ERROR_MESSAGE);
}

$numberOfRows = 0;
while ($result->fetchArray(SQLITE3_NUM)) {
++$numberOfRows;
Expand Down
20 changes: 9 additions & 11 deletions phpmyfaq/src/phpMyFAQ/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Date
/**
* Constructor.
*/
public function __construct(private readonly Configuration $config)
public function __construct(private readonly Configuration $configuration)
{
}

Expand Down Expand Up @@ -74,27 +74,25 @@ public static function getTrackingFileDate(string $file, bool $endOfDay = false)
$year = Strings::substr($file, 12, 4);

if (!$endOfDay) {
$time = mktime(0, 0, 0, (int) $month, (int) $day, (int) $year);
} else {
$time = mktime(23, 59, 59, (int) $month, (int) $day, (int) $year);
return mktime(0, 0, 0, (int)$month, (int)$day, (int)$year);
}

return $time;
} else {
return -1;
return mktime(23, 59, 59, (int) $month, (int) $day, (int) $year);
}

return -1;
}

/**
* Returns date formatted according to user defined format.
* Returns date formatted according to user-defined format.
*/
public function format(string $unformattedDate): string
{
try {
$date = new DateTime($unformattedDate);
return $date->format($this->config->get('main.dateFormat'));
$dateTime = new DateTime($unformattedDate);
return $dateTime->format($this->configuration->get('main.dateFormat'));
} catch (Exception $e) {
$this->config->getLogger()->error($e->getMessage());
$this->configuration->getLogger()->error($e->getMessage());
return '';
}
}
Expand Down
Loading

0 comments on commit f4ce7a1

Please sign in to comment.