Added more type-hints and documented a few methods

This commit is contained in:
Roland Häder 2022-06-16 16:35:39 +02:00
parent 97e27cb523
commit aa5f0d5ec1
5 changed files with 26 additions and 17 deletions

View file

@ -70,9 +70,11 @@ class BaseCollection extends \ArrayIterator
}
/**
* @return int
* Getter for total count
*
* @return int Total count
*/
public function getTotalCount()
public function getTotalCount(): int
{
return $this->totalCount;
}
@ -85,7 +87,7 @@ class BaseCollection extends \ArrayIterator
* @return array
* @see array_column()
*/
public function column($column, $index_key = null)
public function column(string $column, $index_key = null): array
{
return array_column($this->getArrayCopy(true), $column, $index_key);
}
@ -97,7 +99,7 @@ class BaseCollection extends \ArrayIterator
* @return BaseCollection
* @see array_map()
*/
public function map(callable $callback)
public function map(callable $callback): BaseCollection
{
return new static(array_map($callback, $this->getArrayCopy()), $this->getTotalCount());
}
@ -110,7 +112,7 @@ class BaseCollection extends \ArrayIterator
* @return BaseCollection
* @see array_filter()
*/
public function filter(callable $callback = null, int $flag = 0)
public function filter(callable $callback = null, int $flag = 0): BaseCollection
{
return new static(array_filter($this->getArrayCopy(), $callback, $flag));
}

View file

@ -55,14 +55,14 @@ abstract class BaseEntity extends BaseDataTransferObject
}
/**
* @param $name
* @param mixed $name
* @return bool
* @throws HTTPException\InternalServerErrorException
*/
public function __isset($name)
public function __isset($name): bool
{
if (!property_exists($this, $name)) {
throw new HTTPException\InternalServerErrorException('Unknown property ' . $name . ' in Entity ' . static::class);
throw new HTTPException\InternalServerErrorException('Unknown property ' . $name . ' of type ' . gettype($name) . ' in Entity ' . static::class);
}
return !empty($this->$name);

View file

@ -110,11 +110,11 @@ abstract class BaseModel extends BaseDataTransferObject
* - $model->field (outside of class)
* - $this->field (inside of class)
*
* @param $name
* @param string $name Name of data to fetch
* @return mixed
* @throws HTTPException\InternalServerErrorException
*/
public function __get($name)
public function __get(string $name)
{
$this->checkValid();

View file

@ -331,7 +331,7 @@ abstract class BaseModule implements ICanHandleRequests
* Actually, important actions should not be triggered by Links / GET-Requests at all, but sometimes they still are,
* so this mechanism brings in some damage control (the attacker would be able to forge a request to a form of this type, but not to forms of other types).
*/
public static function getFormSecurityToken($typename = '')
public static function getFormSecurityToken(string $typename = '')
{
$user = User::getById(DI::app()->getLoggedInUserId(), ['guid', 'prvkey']);
$timestamp = time();
@ -340,7 +340,14 @@ abstract class BaseModule implements ICanHandleRequests
return $timestamp . '.' . $sec_hash;
}
public static function checkFormSecurityToken($typename = '', $formname = 'form_security_token')
/**
* Checks if form's security (CSRF) token is valid.
*
* @param string $typename ???
* @param string $formname Name of form/field (???)
* @return bool Whether it is valid
*/
public static function checkFormSecurityToken(string $typename = '', string $formname = 'form_security_token'): bool
{
$hash = null;
@ -372,12 +379,12 @@ abstract class BaseModule implements ICanHandleRequests
return ($sec_hash == $x[1]);
}
public static function getFormSecurityStandardErrorMessage()
public static function getFormSecurityStandardErrorMessage(): string
{
return DI::l10n()->t("The form security token was not correct. This probably happened because the form has been opened for too long \x28>3 hours\x29 before submitting it.") . EOL;
}
public static function checkFormSecurityTokenRedirectOnError($err_redirect, $typename = '', $formname = 'form_security_token')
public static function checkFormSecurityTokenRedirectOnError(string $err_redirect, string $typename = '', string $formname = 'form_security_token')
{
if (!self::checkFormSecurityToken($typename, $formname)) {
Logger::notice('checkFormSecurityToken failed: user ' . DI::app()->getLoggedInUserNickname() . ' - form element ' . $typename);
@ -387,7 +394,7 @@ abstract class BaseModule implements ICanHandleRequests
}
}
public static function checkFormSecurityTokenForbiddenOnError($typename = '', $formname = 'form_security_token')
public static function checkFormSecurityTokenForbiddenOnError(string $typename = '', string $formname = 'form_security_token')
{
if (!self::checkFormSecurityToken($typename, $formname)) {
Logger::notice('checkFormSecurityToken failed: user ' . DI::app()->getLoggedInUserNickname() . ' - form element ' . $typename);

View file

@ -57,7 +57,7 @@ class LegacyModule extends BaseModule
* @param string $file_path
* @throws \Exception
*/
private function setModuleFile($file_path)
private function setModuleFile(string $file_path)
{
if (!is_readable($file_path)) {
throw new \Exception(DI::l10n()->t('Legacy module file not found: %s', $file_path));
@ -87,7 +87,7 @@ class LegacyModule extends BaseModule
* @return string
* @throws \Exception
*/
private function runModuleFunction(string $function_suffix)
private function runModuleFunction(string $function_suffix): string
{
$function_name = $this->moduleName . '_' . $function_suffix;