Skip to content

Commit

Permalink
Misc cleanup (NamelessMC#2566)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadhgboyle authored Jan 15, 2022
1 parent e5b73fa commit ad17142
Show file tree
Hide file tree
Showing 141 changed files with 1,238 additions and 1,266 deletions.
4 changes: 2 additions & 2 deletions cli_install.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getEnvVar(string $name, bool $required = false) {
return $value ?: '';
}

if (php_sapi_name() != 'cli') {
if (PHP_SAPI != 'cli') {
die('This script must be run from the command line.');
}

Expand Down Expand Up @@ -71,7 +71,7 @@ function getEnvVar(string $name, bool $required = false) {
}
}

define('ROOT_PATH', dirname(__FILE__));
define('ROOT_PATH', __DIR__);

print('✍️ Creating new config.php file...' . PHP_EOL);
$conf = [
Expand Down
2 changes: 1 addition & 1 deletion core/avatar/face.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$size = isset($_GET['s']) ? max(8, min(250, $_GET['s'])) : 48;
$user = $_GET['u'] ?? '';
$view = isset($_GET['v']) ? substr($_GET['v'], 0, 1) : 'f';
$view = isset($_GET['v']) ? $_GET['v'][0] : 'f';
$view = in_array($view, ['f', 'l', 'r', 'b']) ? $view : 'f';

function get_skin($user, $cache) {
Expand Down
4 changes: 2 additions & 2 deletions core/classes/Avatars/AvatarSourceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function formatUrl(string $url_to_format, string $uuid, int $size): strin
*
* @return string URL with placeholders to format.
*/
abstract function getUrlToFormat(string $perspective): string;
abstract public function getUrlToFormat(string $perspective): string;

/**
* Translate NamelessMC perspective name to the relative name for this avatar source.
Expand All @@ -102,4 +102,4 @@ abstract function getUrlToFormat(string $perspective): string;
public function getRelativePerspective(string $perspective): string {
return $this->_perspectives_map[strtolower($perspective)];
}
}
}
2 changes: 1 addition & 1 deletion core/classes/Collections/CollectionItemBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public function isEnabled(): bool {
return $this->_enabled;
}

public abstract function getContent(): string;
abstract public function getContent(): string;
}
10 changes: 5 additions & 5 deletions core/classes/Core/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function create(int $user_id, string $type, array $text_short, arr
'content' => str_replace(($text['replace'] ?? ''), ($text['replace_with'] ?? ''), $language->get($text['file'], $text['term'])),
'created' => date('U')
])) {
throw new Exception('There was a problem creating an alert.');
throw new RuntimeException('There was a problem creating an alert.');
}
}
}
Expand Down Expand Up @@ -84,10 +84,10 @@ public static function getPMs(int $user_id, bool $all = false): array {

if (!count($pm_full)) {
continue;
} else {
$pm_full = $pm_full[0];
}

$pm_full = $pm_full[0];

$pms[] = [
'id' => $pm_full->id,
'title' => Output::getClean($pm_full->title),
Expand All @@ -110,10 +110,10 @@ public static function getPMs(int $user_id, bool $all = false): array {

if (!count($pm_full)) {
continue;
} else {
$pm_full = $pm_full[0];
}

$pm_full = $pm_full[0];

$unread[] = [
'id' => $pm_full->id,
'title' => Output::getClean($pm_full->title),
Expand Down
2 changes: 1 addition & 1 deletion core/classes/Core/Announcements.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getAvailable(?string $page = null, ?string $custom_page = null,
$pages = json_decode($announcement->pages, true);
$groups = json_decode($announcement->groups, true);

if (in_array($page, $pages) || $page == 'api' || in_array($custom_page, $pages)) {
if ($page === 'api' || in_array($page, $pages) || in_array($custom_page, $pages)) {
foreach ($user_groups as $group) {
if (in_array($group, $groups)) {
$announcements[] = $announcement;
Expand Down
22 changes: 12 additions & 10 deletions core/classes/Core/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public function isCached(string $key): bool {
$entry = $cachedData[$key];
if ($entry && $this->_checkExpired($entry['time'], $entry['expire'])) {
return false;
} else {
return isset($cachedData[$key]['data']);
}

return isset($cachedData[$key]['data']);
}
}

Expand All @@ -105,9 +105,9 @@ private function _loadCache() {
if (file_exists($this->getCacheDir())) {
$file = file_get_contents($this->getCacheDir());
return json_decode($file, true);
} else {
return false;
}

return false;
}

/**
Expand All @@ -132,10 +132,12 @@ public function getCacheDir(): string {
*/
private function _checkCacheDir(): bool {
if (!is_dir($this->getCachePath()) && !mkdir($this->getCachePath(), 0775, true)) {
throw new Exception('Unable to create cache directory ' . $this->getCachePath());
} else if (!is_readable($this->getCachePath()) || !is_writable($this->getCachePath())) {
throw new RuntimeException('Unable to create cache directory ' . $this->getCachePath());
}

if (!is_readable($this->getCachePath()) || !is_writable($this->getCachePath())) {
if (!chmod($this->getCachePath(), 0775)) {
throw new Exception('Your <b>' . $this->getCachePath() . '</b> directory must be readable and writeable. Check your file permissions.');
throw new RuntimeException('Your <b>' . $this->getCachePath() . '</b> directory must be readable and writeable. Check your file permissions.');
}
}
return true;
Expand Down Expand Up @@ -284,9 +286,9 @@ public function retrieveAll(bool $meta = false): array {
}
}
return $results;
} else {
return $this->_loadCache();
}

return $this->_loadCache();
}

/**
Expand All @@ -304,7 +306,7 @@ public function erase(string $key): Cache {
$cacheData = json_encode($cacheData);
file_put_contents($this->getCacheDir(), $cacheData);
} else {
throw new Exception("Error: erase() - Key '$key' not found.");
throw new RuntimeException("Error: erase() - Key '$key' not found.");
}
}
return $this;
Expand Down
2 changes: 1 addition & 1 deletion core/classes/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Config {
public static function get(string $path = null) {
if ($path) {
if (!isset($GLOBALS['config'])) {
throw new Exception('Config unavailable. Please refresh the page.');
throw new RuntimeException('Config unavailable. Please refresh the page.');
}

$config = $GLOBALS['config'];
Expand Down
14 changes: 7 additions & 7 deletions core/classes/Core/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function get(string $module, string $setting) {
$this->_cache->setCache($module . 'configuration');
if ($this->_cache->isCached($setting)) {
return $this->_cache->retrieve($setting);
} else {
$data = $this->_db->selectQuery('SELECT value FROM `nl2_' . Output::getClean($module) . 'settings` WHERE `name` = ?', [$setting]);
if ($data->count()) {
$results = $data->results();
$this->_cache->store($setting, $results[0]->value);
return $results[0]->value;
}
}

$data = $this->_db->selectQuery('SELECT value FROM `nl2_' . Output::getClean($module) . 'settings` WHERE `name` = ?', [$setting]);
if ($data->count()) {
$results = $data->results();
$this->_cache->store($setting, $results[0]->value);
return $results[0]->value;
}

return null;
Expand Down
6 changes: 4 additions & 2 deletions core/classes/Core/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Email class
*/

use PHPMailer\PHPMailer\PHPMailer;

class Email {

/**
Expand Down Expand Up @@ -67,7 +69,7 @@ private static function sendMailer(array $email) {
require(ROOT_PATH . '/core/email.php');

// Initialise PHPMailer
$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
$mail = new PHPMailer(true);

try {
// init
Expand Down Expand Up @@ -136,7 +138,7 @@ public static function formatEmail(string $email, Language $viewing_language): s
$viewing_language->get('emails', $email . '_message'),
$viewing_language->get('emails', 'thanks'),
],
file_get_contents(join(DIRECTORY_SEPARATOR, [ROOT_PATH, 'custom', 'templates', TEMPLATE, 'email', $email . '.html']))
file_get_contents(implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'custom', 'templates', TEMPLATE, 'email', $email . '.html']))
);
}
}
8 changes: 4 additions & 4 deletions core/classes/Core/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public static function exists(string $type = 'post'): bool {
public static function get(string $item) {
if (isset($_POST[$item])) {
return $_POST[$item];
} else {
if (isset($_GET[$item])) {
return $_GET[$item];
}
}

if (isset($_GET[$item])) {
return $_GET[$item];
}

return '';
Expand Down
2 changes: 1 addition & 1 deletion core/classes/Core/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(string $module = null, string $active_language = nul

// Require file
if (!$module || $module == 'core') {
$path = join(DIRECTORY_SEPARATOR, [ROOT_PATH, 'custom', 'languages', $this->_activeLanguage]);
$path = implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'custom', 'languages', $this->_activeLanguage]);
$this->_module = 'Core';
} else {
$path = str_replace('/', DIRECTORY_SEPARATOR, $module) . DIRECTORY_SEPARATOR . $this->_activeLanguage;
Expand Down
26 changes: 13 additions & 13 deletions core/classes/Core/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(Module $module, string $name, string $author, string
* @param Widgets $widgets Instance of widget class to pass.
* @param TemplateBase $template Template to pass.
*/
public static function loadPage(User $user, Pages $pages, Cache $cache, Smarty $smarty, iterable $navs, Widgets $widgets, TemplateBase $template) {
public static function loadPage(User $user, Pages $pages, Cache $cache, Smarty $smarty, iterable $navs, Widgets $widgets, TemplateBase $template): void {
foreach (self::getModules() as $module) {
$module->onPageLoad($user, $pages, $cache, $smarty, $navs, $widgets, $template);
}
Expand All @@ -58,7 +58,7 @@ public static function getModules(): iterable {
return self::$_modules;
}

abstract function onPageLoad(User $user, Pages $pages, Cache $cache, Smarty $smarty, iterable $navs, Widgets $widgets, ?TemplateBase $template);
abstract public function onPageLoad(User $user, Pages $pages, Cache $cache, Smarty $smarty, iterable $navs, Widgets $widgets, ?TemplateBase $template);

/**
* Determine loading arrangement of modules.
Expand All @@ -74,8 +74,8 @@ public static function determineModuleOrder(): array {
continue;
}

for ($n = 0; $n < count($module_order); $n++) {
$before_after = self::findBeforeAfter($module_order, $module_order[$n]);
foreach ($module_order as $n => $nValue) {
$before_after = self::findBeforeAfter($module_order, $nValue);

if (!array_diff($module->getLoadAfter(), $before_after[0]) && !array_diff($module->getLoadBefore(), $before_after[1])) {
array_splice($module_order, $n + 1, 0, $module->getName());
Expand All @@ -98,7 +98,7 @@ public function getName(): string {
*
* @param string $name New name.
*/
protected final function setName(string $name): void {
final protected function setName(string $name): void {
$this->_name = $name;
} // TODO: Implement

Expand Down Expand Up @@ -130,21 +130,21 @@ public function getLoadBefore(): array {
return $this->_load_before;
}

abstract function onInstall();
abstract public function onInstall();

abstract function onUninstall();
abstract public function onUninstall();

abstract function onEnable();
abstract public function onEnable();

abstract function onDisable();
abstract public function onDisable();

abstract function getDebugInfo(): array;
abstract public function getDebugInfo(): array;

public function getAuthor(): string {
return $this->_author;
}

protected final function setAuthor(string $author): void {
final protected function setAuthor(string $author): void {
$this->_author = $author;
}

Expand All @@ -157,7 +157,7 @@ public function getVersion(): string {
*
* @param string $version Version to set.
*/
protected final function setVersion(string $version): void {
final protected function setVersion(string $version): void {
$this->_version = $version;
}

Expand All @@ -170,7 +170,7 @@ public function getNamelessVersion(): string {
*
* @param string $nameless_version NamelessMC version to set.
*/
protected final function setNamelessVersion(string $nameless_version): void {
final protected function setNamelessVersion(string $nameless_version): void {
$this->_nameless_version = $nameless_version;
}
}
22 changes: 11 additions & 11 deletions core/classes/Core/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ public function returnNav(string $location = 'top'): array {
if (count($return[$key]['items'])) {
uasort(
$return[$key]['items'],
function ($a, $b) {
static function ($a, $b) {
if ($a['order'] > $b['order']) {
return 1;
} else {
if ($a['order'] < $b['order']) {
return -1;
}
}

if ($a['order'] < $b['order']) {
return -1;
}
return 0;
}
Expand All @@ -186,13 +186,13 @@ function ($a, $b) {
if (isset($return[$key]['items']) && count($return[$key]['items'])) {
uasort(
$return[$key]['items'],
function ($a, $b) {
static function ($a, $b) {
if ($a['order'] > $b['order']) {
return 1;
} else {
if ($a['order'] < $b['order']) {
return -1;
}
}

if ($a['order'] < $b['order']) {
return -1;
}
return 0;
}
Expand All @@ -202,7 +202,7 @@ function ($a, $b) {
}
}

uasort($return, function ($a, $b) {
uasort($return, static function ($a, $b) {
$result = 0;
if ($a['order'] > $b['order']) {
$result = 1;
Expand Down
2 changes: 1 addition & 1 deletion core/classes/Core/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function add(string $module, string $url, string $file, string $name = ''
* @param string $name Name of page.
* @param bool $widgets Can widgets be used on the page? Default false.
*/
public function addCustom(string $url, string $name, bool $widgets = false) {
public function addCustom(string $url, string $name, bool $widgets = false): void {
$this->_pages[$url] = [
'module' => 'Core',
'file' => 'custom.php',
Expand Down
2 changes: 1 addition & 1 deletion core/classes/Core/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Redirect {
*
* @param string $location Path or URL to redirect to
*/
public static function to(string $location) {
public static function to(string $location): void {
// Javascript redirect
// Tag attribute data-cfasync="false" fixes Cloudflare caching issues (credit @computerwizjared)
echo '<script data-cfasync="false">window.location.replace("' . str_replace('&amp;', '&', htmlspecialchars($location)) . '");</script>';
Expand Down
2 changes: 1 addition & 1 deletion core/classes/Core/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct() {
public function create(array $post = []): array {
// Insert into database
if (!$this->_db->insert('reports', $post)) {
throw new Exception('There was a problem creating the report.');
throw new RuntimeException('There was a problem creating the report.');
}

$id = $this->_db->lastId();
Expand Down
Loading

0 comments on commit ad17142

Please sign in to comment.