Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Dec 17, 2023
1 parent 5bbc2be commit 0c0a5f9
Show file tree
Hide file tree
Showing 217 changed files with 2,751 additions and 2,015 deletions.
2 changes: 1 addition & 1 deletion 403.php
Expand Up @@ -33,7 +33,7 @@
'HOME' => $language->get('errors', '403_home'),
'LOGIN' => $language->get('general', 'sign_in'),
'LOGIN_LINK' => URL::build('/login'),
'PATH' => (defined('CONFIG_PATH') ? CONFIG_PATH : '')
'PATH' => (defined('CONFIG_PATH') ? CONFIG_PATH : ''),
]
);

Expand Down
2 changes: 1 addition & 1 deletion 404.php
Expand Up @@ -31,7 +31,7 @@
'BACK' => $language->get('errors', '404_back'),
'HOME' => $language->get('errors', '404_home'),
'ERROR' => $language->get('errors', '404_error'),
'PATH' => (defined('CONFIG_PATH') ? CONFIG_PATH : '')
'PATH' => (defined('CONFIG_PATH') ? CONFIG_PATH : ''),
]
);

Expand Down
5 changes: 2 additions & 3 deletions core/avatar/face.php
Expand Up @@ -17,8 +17,8 @@
$view = isset($_GET['v']) ? $_GET['v'][0] : 'f';
$view = in_array($view, ['f', 'l', 'r', 'b']) ? $view : 'f';

function get_skin($user, $cache) {

function get_skin($user, $cache)
{
// Check cache
$cache->setCache('avatarCache_' . $user);
if ($cache->isCached($user)) {
Expand Down Expand Up @@ -49,7 +49,6 @@ function get_skin($user, $cache) {
$output .= 'Ne9AAAAAElFTkSuQmCC';
$output = base64_decode($output);
if ($user != '') {

$json = HttpClient::get('https://sessionserver.mojang.com/session/minecraft/profile/' . $user)->json();

if (isset($json->properties[0]->value)) {
Expand Down
57 changes: 36 additions & 21 deletions core/classes/Avatars/AvatarSource.php
Expand Up @@ -7,8 +7,8 @@
* @version 2.0.0-pr10
* @license MIT
*/
class AvatarSource {

class AvatarSource
{
protected static array $_sources = [];

protected static AvatarSourceBase $_active_source;
Expand All @@ -18,26 +18,28 @@ class AvatarSource {
* Uses active avatar source to get the URL of their Minecraft avatar.
*
* @param string $uuid UUID of avatar to get.
* @param int $size Size in pixels to render avatar at. Default 128
* @param int $size Size in pixels to render avatar at. Default 128
*
* @return string Compiled URL of avatar image.
*/
public static function getAvatarFromUUID(string $uuid, int $size = 128): string {
public static function getAvatarFromUUID(string $uuid, int $size = 128): string
{
return self::getActiveSource()->getAvatar($uuid, self::getDefaultPerspective(), $size);
}

/**
* Get a user's avatar from their raw data object.
* Used by the API for TinyMCE mention avatars to avoid reloading the user from the database.
*
* @param object $data User data to use
* @param bool $allow_gifs Whether to allow GIFs or not ()
* @param int $size Size in pixels to render avatar at. Default 128
* @param bool $full Whether to return the full URL or just the path
* @param object $data User data to use
* @param bool $allow_gifs Whether to allow GIFs or not ()
* @param int $size Size in pixels to render avatar at. Default 128
* @param bool $full Whether to return the full URL or just the path
*
* @return string Full URL of avatar image.
*/
public static function getAvatarFromUserData(object $data, bool $allow_gifs = false, int $size = 128, bool $full = false): string {
public static function getAvatarFromUserData(object $data, bool $allow_gifs = false, int $size = 128, bool $full = false): string
{
// If custom avatars are enabled, first check if they have gravatar enabled, and then fallback to normal image
if (defined('CUSTOM_AVATARS')) {
if ($data->gravatar) {
Expand Down Expand Up @@ -95,10 +97,11 @@ public static function getAvatarFromUserData(object $data, bool $allow_gifs = fa
/**
* Determine if a URL is a valid image URL for avatars.
*
* @param string $url URL to check
* @return bool Whether the URL is a valid image URL
* @param string $url URL to check
* @return bool Whether the URL is a valid image URL
*/
private static function validImageUrl(string $url): bool {
private static function validImageUrl(string $url): bool
{
$cache = new Cache(['name' => 'nameless', 'extension' => '.cache', 'path' => ROOT_PATH . '/cache/']);
$cache->setCache('avatar_validity');

Expand All @@ -107,6 +110,7 @@ private static function validImageUrl(string $url): bool {
}

$is_valid = false;

try {
$response = HttpClient::createClient()->head($url);
$headers = $response->getHeaders();
Expand All @@ -117,6 +121,7 @@ private static function validImageUrl(string $url): bool {
}

$cache->store($url, $is_valid, 3600);

return $is_valid;
}

Expand All @@ -125,7 +130,8 @@ private static function validImageUrl(string $url): bool {
*
* @return AvatarSourceBase The active source.
*/
public static function getActiveSource(): AvatarSourceBase {
public static function getActiveSource(): AvatarSourceBase
{
return self::$_active_source;
}

Expand All @@ -135,7 +141,8 @@ public static function getActiveSource(): AvatarSourceBase {
*
* @param string $name Name of source to set as active.
*/
public static function setActiveSource(string $name): void {
public static function setActiveSource(string $name): void
{
$source = self::getSourceByName($name);
if ($source === null) {
$source = self::getSourceByName('cravatar');
Expand All @@ -149,7 +156,8 @@ public static function setActiveSource(string $name): void {
*
* @return string Perspective.
*/
private static function getDefaultPerspective(): string {
private static function getDefaultPerspective(): string
{
if (defined('DEFAULT_AVATAR_PERSPECTIVE')) {
return DEFAULT_AVATAR_PERSPECTIVE;
}
Expand All @@ -162,7 +170,8 @@ private static function getDefaultPerspective(): string {
*
* @return AvatarSourceBase|null Instance if found, null if not found.
*/
public static function getSourceByName(string $name): ?AvatarSourceBase {
public static function getSourceByName(string $name): ?AvatarSourceBase
{
foreach (self::getAllSources() as $source) {
if (strtolower($source->getName()) == strtolower($name)) {
return $source;
Expand All @@ -177,7 +186,8 @@ public static function getSourceByName(string $name): ?AvatarSourceBase {
*
* @return AvatarSourceBase[]
*/
public static function getAllSources(): iterable {
public static function getAllSources(): iterable
{
return self::$_sources;
}

Expand All @@ -186,10 +196,12 @@ public static function getAllSources(): iterable {
*
* @return string URL with placeholders.
*/
public static function getUrlToFormat(): string {
public static function getUrlToFormat(): string
{
// Default to Cravatar
if (!isset(self::$_active_source)) {
require_once(ROOT_PATH . '/modules/Core/classes/Avatars/CravatarAvatarSource.php');

return (new CravatarAvatarSource())->getUrlToFormat(self::getDefaultPerspective());
}

Expand All @@ -201,7 +213,8 @@ public static function getUrlToFormat(): string {
*
* @param AvatarSourceBase $source Instance of avatar source to register.
*/
public static function registerSource(AvatarSourceBase $source): void {
public static function registerSource(AvatarSourceBase $source): void
{
self::$_sources[] = $source;
}

Expand All @@ -211,7 +224,8 @@ public static function registerSource(AvatarSourceBase $source): void {
*
* @return array<string, string> List of names.
*/
public static function getAllSourceNames(): array {
public static function getAllSourceNames(): array
{
$names = [];

foreach (self::getAllSources() as $source) {
Expand All @@ -227,7 +241,8 @@ public static function getAllSourceNames(): array {
*
* @return array<string, array<string>> Array of source => [] perspectives.
*/
public static function getAllPerspectives(): array {
public static function getAllPerspectives(): array
{
$perspectives = [];

foreach (self::getAllSources() as $source) {
Expand Down
37 changes: 22 additions & 15 deletions core/classes/Avatars/AvatarSourceBase.php
Expand Up @@ -7,8 +7,8 @@
* @version 2.0.0-pr10
* @license MIT
*/
abstract class AvatarSourceBase {

abstract class AvatarSourceBase
{
protected string $_name;

/**
Expand All @@ -27,7 +27,8 @@ abstract class AvatarSourceBase {
*
* @return string Name of this avatar source.
*/
public function getName(): string {
public function getName(): string
{
return $this->_name;
}

Expand All @@ -36,7 +37,8 @@ public function getName(): string {
*
* @return string Base url of this source.
*/
public function getBaseUrl(): string {
public function getBaseUrl(): string
{
return $this->_base_url;
}

Expand All @@ -45,33 +47,36 @@ public function getBaseUrl(): string {
*
* @return array Array of perspective names.
*/
public function getPerspectives(): array {
public function getPerspectives(): array
{
return array_keys($this->_perspectives_map);
}

/**
* Get the URL for this users avatar.
*
* @param string $uuid UUID of avatar to get.
* @param string $uuid UUID of avatar to get.
* @param string $perspective Perspective to render avatar with.
* @param int $size Size in pixels to render avatar at. Default 128
* @param int $size Size in pixels to render avatar at. Default 128
*
* @return string Compiled URL of avatar image.
*/
public function getAvatar(string $uuid, string $perspective, int $size = 128): string {
public function getAvatar(string $uuid, string $perspective, int $size = 128): string
{
return $this->formatUrl($this->getUrlToFormat($perspective), $uuid, $size);
}

/**
* Replace placeholders in raw url with uuid and size of requested avatar.
*
* @param string $url_to_format Raw url to replace placeholders in.
* @param string $uuid uuid (or username, yuck!) of avatar to get.
* @param int $size Size of avatar image in pixels to get.
* @param string $uuid uuid (or username, yuck!) of avatar to get.
* @param int $size Size of avatar image in pixels to get.
*
* @return string Formatted url.
*/
public function formatUrl(string $url_to_format, string $uuid, int $size): string {
public function formatUrl(string $url_to_format, string $uuid, int $size): string
{
return str_replace(
['{identifier}', '{size}'],
[$uuid, $size],
Expand All @@ -82,7 +87,7 @@ public function formatUrl(string $url_to_format, string $uuid, int $size): strin
/**
* Get raw URL with placeholders to format.
* - `{identifier} = UUID / username`
* - `{size} = size in pixels`
* - `{size} = size in pixels`.
*
* @param string $perspective Perspective to use in url.
*
Expand All @@ -93,17 +98,19 @@ abstract public function getUrlToFormat(string $perspective): string;
/**
* Translate NamelessMC perspective name to the relative name for this avatar source.
*
* @param string $perspective NamelessMC perspective name to translate.
* @return string Translated perspective name.
* @param string $perspective NamelessMC perspective name to translate.
* @throws InvalidArgumentException When an invalid perspective is passed.
* @return string Translated perspective name.
*/
public function getRelativePerspective(string $perspective): string {
public function getRelativePerspective(string $perspective): string
{
$perspective = strtolower($perspective);
if (isset($this->_perspectives_map[$perspective])) {
return $this->_perspectives_map[$perspective];
}

$class = static::class;

throw new InvalidArgumentException("Attempted to get invalid perspective of: {$perspective} on {$class}");
}
}
18 changes: 11 additions & 7 deletions core/classes/Collections/Collection.php
@@ -1,29 +1,32 @@
<?php
/**
* Base Collection class
* Base Collection class.
*
* @package NamelessMC\Collections
* @author Samerton
* @version 2.0.0-pr13
* @license MIT
*/
class Collection {

class Collection
{
/** @var CollectionItemBase[] */
private array $_items;

public function __construct() {
public function __construct()
{
$this->_items = [];
}

public function addItem(CollectionItemBase $item): void {
public function addItem(CollectionItemBase $item): void
{
$this->_items[] = $item;
}

/**
* @return CollectionItemBase[]
*/
public function getEnabledItems(): array {
public function getEnabledItems(): array
{
$items = [];

foreach ($this->_items as $item) {
Expand All @@ -42,7 +45,8 @@ public function getEnabledItems(): array {
/**
* @return CollectionItemBase[]
*/
public function getAllItems(): array {
public function getAllItems(): array
{
$items = $this->_items;
uasort($items, static function (CollectionItemBase $a, CollectionItemBase $b) {
return $a->getOrder() - $b->getOrder();
Expand Down
13 changes: 8 additions & 5 deletions core/classes/Collections/CollectionItemBase.php
Expand Up @@ -8,21 +8,24 @@
* @version 2.0.0-pr8
* @license MIT
*/
abstract class CollectionItemBase {

abstract class CollectionItemBase
{
private int $_order;
private bool $_enabled;

public function __construct(int $order, bool $enabled) {
public function __construct(int $order, bool $enabled)
{
$this->_order = $order;
$this->_enabled = $enabled;
}

public function getOrder(): int {
public function getOrder(): int
{
return $this->_order;
}

public function isEnabled(): bool {
public function isEnabled(): bool
{
return $this->_enabled;
}

Expand Down

0 comments on commit 0c0a5f9

Please sign in to comment.