- added some documentation
- added type-hints
This commit is contained in:
Roland Häder 2022-06-20 02:45:53 +02:00
parent 97904ea7dd
commit 14bf72e4fe
Signed by: roland
GPG key ID: C82EDE5DDFA0BA77
6 changed files with 67 additions and 51 deletions

View file

@ -47,7 +47,7 @@ function fbrowser_content(App $a)
} }
// Needed to match the correct template in a module that uses a different theme than the user/site/default // Needed to match the correct template in a module that uses a different theme than the user/site/default
$theme = Strings::sanitizeFilePathItem($_GET['theme'] ?? null); $theme = Strings::sanitizeFilePathItem($_GET['theme'] ?? '');
if ($theme && is_file("view/theme/$theme/config.php")) { if ($theme && is_file("view/theme/$theme/config.php")) {
$a->setCurrentTheme($theme); $a->setCurrentTheme($theme);
} }

View file

@ -116,8 +116,7 @@ HELP;
/** /**
* Lists plugins * Lists plugins
* *
* @return int Return code of this command * @return int|bool Return code of this command, false on error (?)
*
* @throws \Exception * @throws \Exception
*/ */
private function list() private function list()
@ -165,10 +164,9 @@ HELP;
* Enables an addon * Enables an addon
* *
* @return int Return code of this command * @return int Return code of this command
*
* @throws \Exception * @throws \Exception
*/ */
private function enable() private function enable(): int
{ {
$addonname = $this->getArgument(1); $addonname = $this->getArgument(1);
@ -190,10 +188,9 @@ HELP;
* Disables an addon * Disables an addon
* *
* @return int Return code of this command * @return int Return code of this command
*
* @throws \Exception * @throws \Exception
*/ */
private function disable() private function disable(): int
{ {
$addonname = $this->getArgument(1); $addonname = $this->getArgument(1);

View file

@ -124,7 +124,7 @@ class Addon
* @return void * @return void
* @throws \Exception * @throws \Exception
*/ */
public static function uninstall($addon) public static function uninstall(string $addon)
{ {
$addon = Strings::sanitizeFilePathItem($addon); $addon = Strings::sanitizeFilePathItem($addon);
@ -149,7 +149,7 @@ class Addon
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
public static function install($addon) public static function install(string $addon): bool
{ {
$addon = Strings::sanitizeFilePathItem($addon); $addon = Strings::sanitizeFilePathItem($addon);
@ -185,6 +185,8 @@ class Addon
/** /**
* reload all updated addons * reload all updated addons
*
* @return void
*/ */
public static function reload() public static function reload()
{ {
@ -222,7 +224,7 @@ class Addon
* @return array with the addon information * @return array with the addon information
* @throws \Exception * @throws \Exception
*/ */
public static function getInfo($addon) public static function getInfo(string $addon): array
{ {
$addon = Strings::sanitizeFilePathItem($addon); $addon = Strings::sanitizeFilePathItem($addon);
@ -287,7 +289,7 @@ class Addon
* @param string $addon * @param string $addon
* @return boolean * @return boolean
*/ */
public static function isEnabled($addon) public static function isEnabled(string $addon): bool
{ {
return in_array($addon, self::$addons); return in_array($addon, self::$addons);
} }
@ -297,7 +299,7 @@ class Addon
* *
* @return array * @return array
*/ */
public static function getEnabledList() public static function getEnabledList(): array
{ {
return self::$addons; return self::$addons;
} }
@ -308,7 +310,7 @@ class Addon
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function getVisibleList() public static function getVisibleList(): array
{ {
$visible_addons = []; $visible_addons = [];
$stmt = DBA::select('addon', ['name'], ['hidden' => false, 'installed' => true]); $stmt = DBA::select('addon', ['name'], ['hidden' => false, 'installed' => true]);

View file

@ -49,6 +49,8 @@ class Hook
/** /**
* Load hooks * Load hooks
*
* @return void
*/ */
public static function loadHooks() public static function loadHooks()
{ {
@ -69,8 +71,9 @@ class Hook
* @param string $hook * @param string $hook
* @param string $file * @param string $file
* @param string $function * @param string $function
* @return void
*/ */
public static function add($hook, $file, $function) public static function add(string $hook, string $file, string $function)
{ {
if (!array_key_exists($hook, self::$hooks)) { if (!array_key_exists($hook, self::$hooks)) {
self::$hooks[$hook] = []; self::$hooks[$hook] = [];
@ -90,7 +93,7 @@ class Hook
* @return mixed|bool * @return mixed|bool
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function register($hook, $file, $function, $priority = 0) public static function register(string $hook, string $file, string $function, int $priority = 0)
{ {
$file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file); $file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
@ -111,7 +114,7 @@ class Hook
* @return boolean * @return boolean
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function unregister($hook, $file, $function) public static function unregister(string $hook, string $file, string $function): bool
{ {
$relative_file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file); $relative_file = str_replace(DI::app()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
@ -120,8 +123,8 @@ class Hook
self::delete($condition); self::delete($condition);
$condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function]; $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
$result = self::delete($condition);
return $result; return self::delete($condition);
} }
/** /**
@ -130,7 +133,7 @@ class Hook
* @param string $name Name of the hook * @param string $name Name of the hook
* @return array * @return array
*/ */
public static function getByName($name) public static function getByName(string $name): array
{ {
$return = []; $return = [];
@ -149,9 +152,10 @@ class Hook
* @param integer $priority of the hook * @param integer $priority of the hook
* @param string $name of the hook to call * @param string $name of the hook to call
* @param mixed $data to transmit to the callback handler * @param mixed $data to transmit to the callback handler
* @return void
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function fork($priority, $name, $data = null) public static function fork(int $priority, string $name, $data = null)
{ {
if (array_key_exists($name, self::$hooks)) { if (array_key_exists($name, self::$hooks)) {
foreach (self::$hooks[$name] as $hook) { foreach (self::$hooks[$name] as $hook) {
@ -184,9 +188,10 @@ class Hook
* *
* @param string $name of the hook to call * @param string $name of the hook to call
* @param string|array &$data to transmit to the callback handler * @param string|array &$data to transmit to the callback handler
* @return void
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function callAll($name, &$data = null) public static function callAll(string $name, &$data = null)
{ {
if (array_key_exists($name, self::$hooks)) { if (array_key_exists($name, self::$hooks)) {
foreach (self::$hooks[$name] as $hook) { foreach (self::$hooks[$name] as $hook) {
@ -202,9 +207,10 @@ class Hook
* @param string $name of the hook to call * @param string $name of the hook to call
* @param array $hook Hook data * @param array $hook Hook data
* @param string|array &$data to transmit to the callback handler * @param string|array &$data to transmit to the callback handler
* @return void
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function callSingle(App $a, $name, $hook, &$data = null) public static function callSingle(App $a, string $name, array $hook, &$data = null)
{ {
// Don't run a theme's hook if the user isn't using the theme // Don't run a theme's hook if the user isn't using the theme
if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) { if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
@ -229,7 +235,7 @@ class Hook
* @param string $name Name of the addon * @param string $name Name of the addon
* @return boolean * @return boolean
*/ */
public static function isAddonApp($name) public static function isAddonApp(string $name): bool
{ {
$name = Strings::sanitizeFilePathItem($name); $name = Strings::sanitizeFilePathItem($name);
@ -253,7 +259,7 @@ class Hook
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
public static function delete(array $condition) public static function delete(array $condition): bool
{ {
$result = DBA::delete('hook', $condition); $result = DBA::delete('hook', $condition);
@ -273,7 +279,7 @@ class Hook
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
private static function insert(array $condition) private static function insert(array $condition): bool
{ {
$result = DBA::insert('hook', $condition); $result = DBA::insert('hook', $condition);

View file

@ -128,7 +128,7 @@ class L10n
private function setLangFromSession(IHandleSessions $session) private function setLangFromSession(IHandleSessions $session)
{ {
if ($session->get('language') !== $this->lang) { if ($session->get('language') !== $this->lang) {
$this->loadTranslationTable($session->get('language')); $this->loadTranslationTable($session->get('language') ?? $this->lang);
} }
} }
@ -140,10 +140,10 @@ class L10n
* Uses an App object shim since all the strings files refer to $a->strings * Uses an App object shim since all the strings files refer to $a->strings
* *
* @param string $lang language code to load * @param string $lang language code to load
* * @return void
* @throws \Exception * @throws \Exception
*/ */
private function loadTranslationTable($lang) private function loadTranslationTable(string $lang)
{ {
$lang = Strings::sanitizeFilePathItem($lang); $lang = Strings::sanitizeFilePathItem($lang);
@ -183,7 +183,7 @@ class L10n
* *
* @return string The two-letter language code * @return string The two-letter language code
*/ */
public static function detectLanguage(array $server, array $get, string $sysLang = self::DEFAULT) public static function detectLanguage(array $server, array $get, string $sysLang = self::DEFAULT): string
{ {
$lang_variable = $server['HTTP_ACCEPT_LANGUAGE'] ?? null; $lang_variable = $server['HTTP_ACCEPT_LANGUAGE'] ?? null;
@ -269,7 +269,7 @@ class L10n
* *
* @return string * @return string
*/ */
public function t($s, ...$vars) public function t(array $s, ...$vars): string
{ {
if (empty($s)) { if (empty($s)) {
return ''; return '';
@ -307,7 +307,7 @@ class L10n
* @return string * @return string
* @throws \Exception * @throws \Exception
*/ */
public function tt(string $singular, string $plural, int $count) public function tt(string $singular, string $plural, int $count): string
{ {
$s = null; $s = null;
@ -352,7 +352,7 @@ class L10n
* *
* @return bool * @return bool
*/ */
private function stringPluralSelectDefault($n) private function stringPluralSelectDefault(int $n): bool
{ {
return $n != 1; return $n != 1;
} }
@ -369,7 +369,7 @@ class L10n
* *
* @return array * @return array
*/ */
public static function getAvailableLanguages() public static function getAvailableLanguages(): array
{ {
$langs = []; $langs = [];
$strings_file_paths = glob('view/lang/*/strings.php'); $strings_file_paths = glob('view/lang/*/strings.php');
@ -391,10 +391,9 @@ class L10n
* Translate days and months names. * Translate days and months names.
* *
* @param string $s String with day or month name. * @param string $s String with day or month name.
*
* @return string Translated string. * @return string Translated string.
*/ */
public function getDay($s) public function getDay(string $s): string
{ {
$ret = str_replace(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], $ret = str_replace(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
[$this->t('Monday'), $this->t('Tuesday'), $this->t('Wednesday'), $this->t('Thursday'), $this->t('Friday'), $this->t('Saturday'), $this->t('Sunday')], [$this->t('Monday'), $this->t('Tuesday'), $this->t('Wednesday'), $this->t('Thursday'), $this->t('Friday'), $this->t('Saturday'), $this->t('Sunday')],
@ -411,10 +410,9 @@ class L10n
* Translate short days and months names. * Translate short days and months names.
* *
* @param string $s String with short day or month name. * @param string $s String with short day or month name.
*
* @return string Translated string. * @return string Translated string.
*/ */
public function getDayShort($s) public function getDayShort(string $s): string
{ {
$ret = str_replace(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], $ret = str_replace(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
[$this->t('Mon'), $this->t('Tue'), $this->t('Wed'), $this->t('Thu'), $this->t('Fri'), $this->t('Sat'), $this->t('Sun')], [$this->t('Mon'), $this->t('Tue'), $this->t('Wed'), $this->t('Thu'), $this->t('Fri'), $this->t('Sat'), $this->t('Sun')],
@ -435,7 +433,7 @@ class L10n
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @hook poke_verbs pokes array * @hook poke_verbs pokes array
*/ */
public function getPokeVerbs() public function getPokeVerbs(): array
{ {
// index is present tense verb // index is present tense verb
// value is array containing past tense verb, translation of present, translation of past // value is array containing past tense verb, translation of present, translation of past
@ -461,7 +459,7 @@ class L10n
* @return static A new L10n instance * @return static A new L10n instance
* @throws \Exception * @throws \Exception
*/ */
public function withLang(string $lang) public function withLang(string $lang): L10n
{ {
// Don't create a new instance for same language // Don't create a new instance for same language
if ($lang === $this->lang) { if ($lang === $this->lang) {

View file

@ -32,7 +32,7 @@ require_once 'boot.php';
*/ */
class Theme class Theme
{ {
public static function getAllowedList() public static function getAllowedList(): array
{ {
$allowed_themes_str = DI::config()->get('system', 'allowed_themes'); $allowed_themes_str = DI::config()->get('system', 'allowed_themes');
$allowed_themes_raw = explode(',', str_replace(' ', '', $allowed_themes_str)); $allowed_themes_raw = explode(',', str_replace(' ', '', $allowed_themes_str));
@ -69,7 +69,7 @@ class Theme
* @param string $theme the name of the theme * @param string $theme the name of the theme
* @return array * @return array
*/ */
public static function getInfo($theme) public static function getInfo(string $theme): array
{ {
$theme = Strings::sanitizeFilePathItem($theme); $theme = Strings::sanitizeFilePathItem($theme);
@ -133,7 +133,7 @@ class Theme
* @return string * @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function getScreenshot($theme) public static function getScreenshot(string $theme): string
{ {
$theme = Strings::sanitizeFilePathItem($theme); $theme = Strings::sanitizeFilePathItem($theme);
@ -146,7 +146,13 @@ class Theme
return DI::baseUrl() . '/images/blank.png'; return DI::baseUrl() . '/images/blank.png';
} }
public static function uninstall($theme) /**
* Uninstalls given theme name
*
* @param string $theme Name of theme
* @return bool true on success
*/
public static function uninstall(string $theme)
{ {
$theme = Strings::sanitizeFilePathItem($theme); $theme = Strings::sanitizeFilePathItem($theme);
@ -167,10 +173,18 @@ class Theme
if ($key !== false) { if ($key !== false) {
unset($allowed_themes[$key]); unset($allowed_themes[$key]);
Theme::setAllowedList($allowed_themes); Theme::setAllowedList($allowed_themes);
return true;
} }
return false;
} }
public static function install($theme) /**
* Installs given theme name
*
* @param string $theme Name of theme
* @return bool true on success
*/
public static function install(string $theme): bool
{ {
$theme = Strings::sanitizeFilePathItem($theme); $theme = Strings::sanitizeFilePathItem($theme);
@ -208,7 +222,7 @@ class Theme
* @return string Path to the file or empty string if the file isn't found * @return string Path to the file or empty string if the file isn't found
* @throws \Exception * @throws \Exception
*/ */
public static function getPathForFile($file) public static function getPathForFile(string $file): string
{ {
$a = DI::app(); $a = DI::app();
@ -237,10 +251,9 @@ class Theme
* Provide a sane default if nothing is chosen or the specified theme does not exist. * Provide a sane default if nothing is chosen or the specified theme does not exist.
* *
* @param string $theme Theme name * @param string $theme Theme name
*
* @return string * @return string
*/ */
public static function getStylesheetPath($theme) public static function getStylesheetPath(string $theme): string
{ {
$theme = Strings::sanitizeFilePathItem($theme); $theme = Strings::sanitizeFilePathItem($theme);
@ -263,10 +276,10 @@ class Theme
/** /**
* Returns the path of the provided theme * Returns the path of the provided theme
* *
* @param $theme * @param string $theme Theme name
* @return string|null * @return string|null
*/ */
public static function getConfigFile($theme) public static function getConfigFile(string $theme)
{ {
$theme = Strings::sanitizeFilePathItem($theme); $theme = Strings::sanitizeFilePathItem($theme);
@ -285,11 +298,11 @@ class Theme
/** /**
* Returns the background color of the provided theme if available. * Returns the background color of the provided theme if available.
* *
* @param string $theme * @param string $theme Theme name
* @param int|null $uid Current logged-in user id * @param int|null $uid Current logged-in user id
* @return string|null * @return string|null
*/ */
public static function getBackgroundColor(string $theme, $uid = null) public static function getBackgroundColor(string $theme, int $uid = null)
{ {
$theme = Strings::sanitizeFilePathItem($theme); $theme = Strings::sanitizeFilePathItem($theme);