mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-06 14:12:15 +00:00
Code cleanup.
This commit is contained in:
@@ -31,20 +31,40 @@ trait AppendsLocationData
|
||||
/**
|
||||
* Abstract method.
|
||||
*
|
||||
* @param $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function has($key);
|
||||
|
||||
/**
|
||||
* Abstract method.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function method();
|
||||
|
||||
/**
|
||||
* Abstract method.
|
||||
*
|
||||
* @param mixed ...$patterns
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function routeIs(...$patterns);
|
||||
|
||||
/**
|
||||
* Abstract method stolen from "InteractsWithInput".
|
||||
*
|
||||
* @param null $key
|
||||
* @param bool $default
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
|
||||
*/
|
||||
abstract public function boolean($key = null, $default = false);
|
||||
|
||||
/**
|
||||
* Read the submitted Request data and add new or updated Location data to the array.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @param string|null $prefix
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function appendLocationData(array $data, ?string $prefix): array
|
||||
{
|
||||
@@ -97,12 +117,6 @@ trait AppendsLocationData
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $prefix
|
||||
* @param string $key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getLocationKey(?string $prefix, string $key): string
|
||||
{
|
||||
if (null === $prefix) {
|
||||
@@ -112,11 +126,6 @@ trait AppendsLocationData
|
||||
return sprintf('%s_%s', $prefix, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $prefix
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isValidPost(?string $prefix): bool
|
||||
{
|
||||
app('log')->debug('Now in isValidPost()');
|
||||
@@ -155,38 +164,6 @@ trait AppendsLocationData
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function method();
|
||||
|
||||
/**
|
||||
* Abstract method.
|
||||
*
|
||||
* @param mixed ...$patterns
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function routeIs(...$patterns);
|
||||
|
||||
/**
|
||||
* Abstract method stolen from "InteractsWithInput".
|
||||
*
|
||||
* @param null $key
|
||||
* @param bool $default
|
||||
*
|
||||
* @return mixed
|
||||
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
|
||||
*/
|
||||
abstract public function boolean($key = null, $default = false);
|
||||
|
||||
/**
|
||||
* @param string|null $prefix
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isValidPUT(?string $prefix): bool
|
||||
{
|
||||
$longitudeKey = $this->getLocationKey($prefix, 'longitude');
|
||||
@@ -227,11 +204,6 @@ trait AppendsLocationData
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $prefix
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isValidEmptyPUT(?string $prefix): bool
|
||||
{
|
||||
$longitudeKey = $this->getLocationKey($prefix, 'longitude');
|
||||
@@ -239,9 +211,9 @@ trait AppendsLocationData
|
||||
$zoomLevelKey = $this->getLocationKey($prefix, 'zoom_level');
|
||||
|
||||
return (
|
||||
null === $this->get($longitudeKey)
|
||||
&& null === $this->get($latitudeKey)
|
||||
&& null === $this->get($zoomLevelKey))
|
||||
null === $this->get($longitudeKey)
|
||||
&& null === $this->get($latitudeKey)
|
||||
&& null === $this->get($zoomLevelKey))
|
||||
&& (
|
||||
'PUT' === $this->method()
|
||||
|| ('POST' === $this->method() && $this->routeIs('*.update'))
|
||||
|
||||
@@ -34,8 +34,6 @@ trait ChecksLogin
|
||||
{
|
||||
/**
|
||||
* Verify the request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
@@ -47,13 +45,16 @@ trait ChecksLogin
|
||||
}
|
||||
if (!property_exists($this, 'acceptedRoles')) { // @phpstan-ignore-line
|
||||
app('log')->debug('Request class has no acceptedRoles array');
|
||||
|
||||
return true; // check for false already took place.
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$userGroup = $this->getUserGroup();
|
||||
if (null === $userGroup) {
|
||||
app('log')->error('User has no valid user group submitted or otherwise.');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -65,21 +66,21 @@ trait ChecksLogin
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the user group or NULL if none is set.
|
||||
* Will throw exception if invalid.
|
||||
*
|
||||
* @return UserGroup|null
|
||||
*/
|
||||
public function getUserGroup(): ?UserGroup
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
app('log')->debug('Now in getUserGroup()');
|
||||
/** @var UserGroup|null $userGroup */
|
||||
|
||||
/** @var null|UserGroup $userGroup */
|
||||
$userGroup = $this->route()?->parameter('userGroup');
|
||||
if (null === $userGroup) {
|
||||
app('log')->debug('Request class has no userGroup parameter, but perhaps there is a parameter.');
|
||||
@@ -91,10 +92,12 @@ trait ChecksLogin
|
||||
$userGroup = UserGroup::find($userGroupId);
|
||||
if (null === $userGroup) {
|
||||
app('log')->error(sprintf('Request class has user_group_id (#%d), but group does not exist.', $userGroupId));
|
||||
|
||||
return null;
|
||||
}
|
||||
app('log')->debug('Request class has valid user_group_id.');
|
||||
}
|
||||
|
||||
return $userGroup;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,4 @@ namespace FireflyIII\Support\Request;
|
||||
/**
|
||||
* Trait ConvertAPIDataTypes
|
||||
*/
|
||||
trait ConvertAPIDataTypes
|
||||
{
|
||||
}
|
||||
trait ConvertAPIDataTypes {}
|
||||
|
||||
@@ -89,10 +89,6 @@ trait ConvertsDataTypes
|
||||
|
||||
/**
|
||||
* Return integer value.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function convertInteger(string $field): int
|
||||
{
|
||||
@@ -102,20 +98,11 @@ trait ConvertsDataTypes
|
||||
/**
|
||||
* Abstract method that always exists in the Request classes that use this
|
||||
* trait, OR a stub needs to be added by any other class that uses this train.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed|null $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function get(string $key, mixed $default = null): mixed;
|
||||
|
||||
/**
|
||||
* Return string value.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function convertString(string $field): string
|
||||
{
|
||||
@@ -123,14 +110,10 @@ trait ConvertsDataTypes
|
||||
if (!is_scalar($entry)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return (string)$this->clearString((string)$entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $string
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function clearString(?string $string): ?string
|
||||
{
|
||||
$string = $this->clearStringKeepNewlines($string);
|
||||
@@ -148,11 +131,6 @@ trait ConvertsDataTypes
|
||||
return trim($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $string
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function clearStringKeepNewlines(?string $string): ?string
|
||||
{
|
||||
if (null === $string) {
|
||||
@@ -173,8 +151,6 @@ trait ConvertsDataTypes
|
||||
* TODO duplicate, see SelectTransactionsRequest
|
||||
*
|
||||
* Validate list of accounts. This one is for V2 endpoints, so it searches for groups, not users.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAccountList(): Collection
|
||||
{
|
||||
@@ -207,10 +183,6 @@ trait ConvertsDataTypes
|
||||
|
||||
/**
|
||||
* Return string value with newlines.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function stringWithNewlines(string $field): string
|
||||
{
|
||||
@@ -218,9 +190,17 @@ trait ConvertsDataTypes
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $array
|
||||
* Abstract method that always exists in the Request classes that use this
|
||||
* trait, OR a stub needs to be added by any other class that uses this train.
|
||||
*
|
||||
* @return array|null
|
||||
* @param mixed $key
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function has($key);
|
||||
|
||||
/**
|
||||
* @param mixed $array
|
||||
*/
|
||||
protected function arrayFromValue($array): ?array
|
||||
{
|
||||
@@ -237,11 +217,6 @@ trait ConvertsDataTypes
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function convertBoolean(?string $value): bool
|
||||
{
|
||||
if (null === $value) {
|
||||
@@ -263,11 +238,6 @@ trait ConvertsDataTypes
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $string
|
||||
*
|
||||
* @return Carbon|null
|
||||
*/
|
||||
protected function convertDateTime(?string $string): ?Carbon
|
||||
{
|
||||
$value = $this->get((string)$string);
|
||||
@@ -283,6 +253,7 @@ trait ConvertsDataTypes
|
||||
$carbon = Carbon::createFromFormat('Y-m-d', $value);
|
||||
} catch (InvalidDateException $e) { // @phpstan-ignore-line
|
||||
app('log')->error(sprintf('[1] "%s" is not a valid date: %s', $value, $e->getMessage()));
|
||||
|
||||
return null;
|
||||
} catch (InvalidFormatException $e) { // @phpstan-ignore-line
|
||||
app('log')->error(sprintf('[2] "%s" is of an invalid format: %s', $value, $e->getMessage()));
|
||||
@@ -291,10 +262,13 @@ trait ConvertsDataTypes
|
||||
}
|
||||
if (false === $carbon) {
|
||||
app('log')->error(sprintf('[2] "%s" is of an invalid format.', $value));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $carbon;
|
||||
}
|
||||
|
||||
// is an atom string, I hope?
|
||||
try {
|
||||
$carbon = Carbon::parse($value);
|
||||
@@ -307,15 +281,12 @@ trait ConvertsDataTypes
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $carbon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return floating value.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return float|null
|
||||
*/
|
||||
protected function convertFloat(string $field): ?float
|
||||
{
|
||||
@@ -327,11 +298,6 @@ trait ConvertsDataTypes
|
||||
return (float)$res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $string
|
||||
*
|
||||
* @return Carbon|null
|
||||
*/
|
||||
protected function dateFromValue(?string $string): ?Carbon
|
||||
{
|
||||
if (null === $string) {
|
||||
@@ -341,6 +307,7 @@ trait ConvertsDataTypes
|
||||
return null;
|
||||
}
|
||||
$carbon = null;
|
||||
|
||||
try {
|
||||
$carbon = new Carbon($string, config('app.timezone'));
|
||||
} catch (InvalidFormatException $e) {
|
||||
@@ -359,10 +326,6 @@ trait ConvertsDataTypes
|
||||
/**
|
||||
* Returns all data in the request, or omits the field if not set,
|
||||
* according to the config from the request. This is the way.
|
||||
*
|
||||
* @param array $fields
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAllData(array $fields): array
|
||||
{
|
||||
@@ -370,33 +333,20 @@ trait ConvertsDataTypes
|
||||
foreach ($fields as $field => $info) {
|
||||
if (true === $this->has($info[0])) {
|
||||
$method = $info[1];
|
||||
$return[$field] = $this->$method($info[0]); // @phpstan-ignore-line
|
||||
$return[$field] = $this->{$method}($info[0]); // @phpstan-ignore-line
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method that always exists in the Request classes that use this
|
||||
* trait, OR a stub needs to be added by any other class that uses this train.
|
||||
*
|
||||
* @param mixed $key
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function has($key);
|
||||
|
||||
/**
|
||||
* Return date or NULL.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return Carbon|null
|
||||
*/
|
||||
protected function getCarbonDate(string $field): ?Carbon
|
||||
{
|
||||
$result = null;
|
||||
|
||||
try {
|
||||
$result = '' !== (string)$this->get($field) ? new Carbon((string)$this->get($field), config('app.timezone')) : null;
|
||||
} catch (InvalidFormatException $e) {
|
||||
@@ -411,10 +361,6 @@ trait ConvertsDataTypes
|
||||
|
||||
/**
|
||||
* Parse to integer
|
||||
*
|
||||
* @param string|null $string
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
protected function integerFromValue(?string $string): ?int
|
||||
{
|
||||
@@ -430,10 +376,6 @@ trait ConvertsDataTypes
|
||||
|
||||
/**
|
||||
* Return integer value, or NULL when it's not set.
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
protected function nullableInteger(string $field): ?int
|
||||
{
|
||||
|
||||
@@ -28,11 +28,6 @@ namespace FireflyIII\Support\Request;
|
||||
*/
|
||||
trait GetRecurrenceData
|
||||
{
|
||||
/**
|
||||
* @param array $transaction
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getSingleTransactionData(array $transaction): array
|
||||
{
|
||||
$return = [];
|
||||
|
||||
@@ -28,17 +28,11 @@ namespace FireflyIII\Support\Request;
|
||||
*/
|
||||
trait GetRuleConfiguration
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getTriggers(): array
|
||||
{
|
||||
return array_keys(config('search.operators'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getTriggersWithContext(): array
|
||||
{
|
||||
$list = config('search.operators');
|
||||
|
||||
Reference in New Issue
Block a user