mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
Various code cleanup.
This commit is contained in:
@@ -35,7 +35,7 @@ trait OBValidation
|
||||
{
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param $accountName
|
||||
* @param mixed $accountName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
@@ -33,7 +33,7 @@ trait TransferValidation
|
||||
{
|
||||
/**
|
||||
* @param int|null $accountId
|
||||
* @param $accountName
|
||||
* @param mixed $accountName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
@@ -58,14 +58,12 @@ class AccountValidator
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->createMode = false;
|
||||
$this->destError = 'No error yet.';
|
||||
$this->sourceError = 'No error yet.';
|
||||
$this->combinations = config('firefly.source_dests');
|
||||
$this->source = null;
|
||||
$this->destination = null;
|
||||
|
||||
/** @var AccountRepositoryInterface accountRepository */
|
||||
$this->createMode = false;
|
||||
$this->destError = 'No error yet.';
|
||||
$this->sourceError = 'No error yet.';
|
||||
$this->combinations = config('firefly.source_dests');
|
||||
$this->source = null;
|
||||
$this->destination = null;
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
}
|
||||
|
||||
|
@@ -57,7 +57,7 @@ trait ValidatesAutoBudgetRequest
|
||||
if ('' === $period) {
|
||||
$validator->errors()->add('auto_budget_period', (string)trans('validation.auto_budget_period_mandatory'));
|
||||
}
|
||||
if (null !== $amount && null !== $currencyId && null !== $currencyCode && '' === $currencyCode && '' === $currencyId) {
|
||||
if (null !== $amount && null !== $currencyId && null !== $currencyCode && '' === $currencyCode && 0 === $currencyId) {
|
||||
$validator->errors()->add('auto_budget_amount', (string)trans('validation.require_currency_info'));
|
||||
}
|
||||
}
|
||||
|
@@ -23,6 +23,7 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Validation;
|
||||
|
||||
use Illuminate\Validation\Validator;
|
||||
use Log;
|
||||
|
||||
@@ -45,7 +46,11 @@ trait CurrencyValidation
|
||||
|
||||
foreach ($transactions as $index => $transaction) {
|
||||
// if foreign amount is present, then the currency must be as well.
|
||||
if (isset($transaction['foreign_amount']) && !(isset($transaction['foreign_currency_id']) || isset($transaction['foreign_currency_code']))
|
||||
if (array_key_exists('foreign_amount', $transaction)
|
||||
&& !(array_key_exists('foreign_currency_id', $transaction)
|
||||
|| array_key_exists(
|
||||
'foreign_currency_code', $transaction
|
||||
))
|
||||
&& 0 !== bccomp('0', $transaction['foreign_amount'])
|
||||
) {
|
||||
$validator->errors()->add(
|
||||
@@ -54,7 +59,10 @@ trait CurrencyValidation
|
||||
);
|
||||
}
|
||||
// if the currency is present, then the amount must be present as well.
|
||||
if ((isset($transaction['foreign_currency_id']) || isset($transaction['foreign_currency_code'])) && !isset($transaction['foreign_amount'])) {
|
||||
if ((array_key_exists('foreign_currency_id', $transaction) || array_key_exists('foreign_currency_code', $transaction))
|
||||
&& !array_key_exists(
|
||||
'foreign_amount', $transaction
|
||||
)) {
|
||||
$validator->errors()->add(
|
||||
'transactions.' . $index . '.foreign_amount',
|
||||
(string)trans('validation.require_currency_amount')
|
||||
|
@@ -52,14 +52,14 @@ use function is_string;
|
||||
class FireflyValidator extends Validator
|
||||
{
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validate2faCode($attribute, $value): bool
|
||||
{
|
||||
if (!is_string($value) || null === $value || 6 !== strlen($value)) {
|
||||
if (null === $value || !is_string($value) || 6 !== strlen($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -69,9 +69,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -88,8 +88,8 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -108,14 +108,14 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateIban($attribute, $value): bool
|
||||
{
|
||||
if (!is_string($value) || null === $value || strlen($value) < 6) {
|
||||
if (null === $value || !is_string($value) || strlen($value) < 6) {
|
||||
return false;
|
||||
}
|
||||
// strip spaces
|
||||
@@ -186,9 +186,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -201,9 +201,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -216,9 +216,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -235,9 +235,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param string $attribute
|
||||
*
|
||||
* @param string $value
|
||||
* @param string|null $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -309,8 +309,8 @@ class FireflyValidator extends Validator
|
||||
/**
|
||||
* $attribute has the format triggers.%d.value.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param string $attribute
|
||||
* @param string|null $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -375,15 +375,15 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateSecurePassword($attribute, $value): bool
|
||||
{
|
||||
$verify = false;
|
||||
if (isset($this->data['verify_password'])) {
|
||||
if (array_key_exists('verify_password', $this->data)) {
|
||||
$verify = 1 === (int)$this->data['verify_password'];
|
||||
}
|
||||
if ($verify) {
|
||||
@@ -397,9 +397,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -410,21 +410,20 @@ class FireflyValidator extends Validator
|
||||
if (!auth()->check()) {
|
||||
return $this->validateAccountAnonymously();
|
||||
}
|
||||
if (isset($this->data['objectType'])) {
|
||||
|
||||
if (array_key_exists('objectType', $this->data)) {
|
||||
return $this->validateByAccountTypeString($value, $parameters, $this->data['objectType']);
|
||||
}
|
||||
if (isset($this->data['type'])) {
|
||||
if (array_key_exists('type', $this->data)) {
|
||||
return $this->validateByAccountTypeString($value, $parameters, $this->data['type']);
|
||||
}
|
||||
if (isset($this->data['account_type_id'])) {
|
||||
if (array_key_exists('account_type_id', $this->data)) {
|
||||
return $this->validateByAccountTypeId($value, $parameters);
|
||||
}
|
||||
$parameterId = $parameters[0] ?? null;
|
||||
if (null !== $parameterId) {
|
||||
return $this->validateByParameterId((int)$parameterId, $value);
|
||||
}
|
||||
if (isset($this->data['id'])) {
|
||||
if (array_key_exists('id', $this->data)) {
|
||||
return $this->validateByAccountId($value);
|
||||
}
|
||||
|
||||
@@ -437,7 +436,7 @@ class FireflyValidator extends Validator
|
||||
*/
|
||||
private function validateAccountAnonymously(): bool
|
||||
{
|
||||
if (!isset($this->data['user_id'])) {
|
||||
if (!array_key_exists('user_id',$this->data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -466,7 +465,7 @@ class FireflyValidator extends Validator
|
||||
*/
|
||||
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
|
||||
{
|
||||
/** @var array $search */
|
||||
/** @var array|null $search */
|
||||
$search = Config::get('firefly.accountTypeByIdentifier.' . $type);
|
||||
|
||||
if (null === $search) {
|
||||
@@ -491,8 +490,8 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -516,7 +515,8 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @param int $accountId
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -528,7 +528,6 @@ class FireflyValidator extends Validator
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
|
||||
/** @var Collection $set */
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first();
|
||||
@@ -537,7 +536,7 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -549,7 +548,6 @@ class FireflyValidator extends Validator
|
||||
$type = $existingAccount->accountType;
|
||||
$ignore = $existingAccount->id;
|
||||
|
||||
/** @var Collection $set */
|
||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||
->where('name', $value)
|
||||
->first();
|
||||
@@ -568,9 +566,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -602,6 +600,13 @@ class FireflyValidator extends Validator
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $something
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateUniqueExistingWebhook($value, $parameters, $something): bool
|
||||
{
|
||||
$existingId = (int)($something[0] ?? 0);
|
||||
@@ -614,7 +619,7 @@ class FireflyValidator extends Validator
|
||||
if (auth()->check()) {
|
||||
// get existing webhook value:
|
||||
if (0 !== $existingId) {
|
||||
/** @var Webhook $webhook */
|
||||
/** @var Webhook|null $webhook */
|
||||
$webhook = auth()->user()->webhooks()->find($existingId);
|
||||
if (null === $webhook) {
|
||||
return false;
|
||||
@@ -645,15 +650,15 @@ class FireflyValidator extends Validator
|
||||
|
||||
/**
|
||||
*
|
||||
* Validate an object and its unicity. Checks for encryption / encrypted values as well.
|
||||
* Validate an object and its uniqueness. Checks for encryption / encrypted values as well.
|
||||
*
|
||||
* parameter 0: the table
|
||||
* parameter 1: the field
|
||||
* parameter 2: an id to ignore (when editing)
|
||||
*
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -667,7 +672,7 @@ class FireflyValidator extends Validator
|
||||
* ID field, set that field to be the $exclude.
|
||||
*/
|
||||
$data = $this->getData();
|
||||
if (!isset($parameters[2]) && isset($data['id']) && (int)$data['id'] > 0) {
|
||||
if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int)$data['id'] > 0) {
|
||||
$exclude = (int)$data['id'];
|
||||
}
|
||||
// get entries from table
|
||||
@@ -686,9 +691,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -707,11 +712,11 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* TODO this method does not need a for loop
|
||||
* TODO this method does not need a for loop
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -738,8 +743,8 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @param $parameters
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
@@ -59,7 +59,7 @@ trait RecurrenceValidation
|
||||
if (null !== $recurrence) {
|
||||
Log::debug('There is a recurrence in the route.');
|
||||
// ok so we have a recurrence should be able to extract type somehow.
|
||||
/** @var RecurrenceTransaction $first */
|
||||
/** @var RecurrenceTransaction|null $first */
|
||||
$first = $recurrence->recurrenceTransactions()->first();
|
||||
if (null !== $first) {
|
||||
$transactionType = $first->transactionType ? $first->transactionType->type : 'withdrawal';
|
||||
@@ -91,7 +91,7 @@ trait RecurrenceValidation
|
||||
continue;
|
||||
}
|
||||
// validate source account.
|
||||
$sourceId = isset($transaction['source_id']) ? (int)$transaction['source_id'] : null;
|
||||
$sourceId = array_key_exists('source_id', $transaction) ? (int)$transaction['source_id'] : null;
|
||||
$sourceName = $transaction['source_name'] ?? null;
|
||||
$validSource = $accountValidator->validateSource($sourceId, $sourceName, null);
|
||||
|
||||
@@ -103,7 +103,7 @@ trait RecurrenceValidation
|
||||
return;
|
||||
}
|
||||
// validate destination account
|
||||
$destinationId = isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null;
|
||||
$destinationId = array_key_exists('destination_id', $transaction) ? (int)$transaction['destination_id'] : null;
|
||||
$destinationName = $transaction['destination_name'] ?? null;
|
||||
$validDestination = $accountValidator->validateDestination($destinationId, $destinationName, null);
|
||||
// do something with result:
|
||||
|
@@ -100,9 +100,9 @@ trait TransactionValidation
|
||||
$accountValidator->setTransactionType($transactionType);
|
||||
|
||||
// validate source account.
|
||||
$sourceId = isset($transaction['source_id']) ? (int)$transaction['source_id'] : null;
|
||||
$sourceName = isset($transaction['source_name']) ? (string)$transaction['source_name'] : null;
|
||||
$sourceIban = isset($transaction['source_iban']) ? (string)$transaction['source_iban'] : null;
|
||||
$sourceId = array_key_exists('source_id', $transaction) ? (int)$transaction['source_id'] : null;
|
||||
$sourceName = array_key_exists('source_name', $transaction) ? (string)$transaction['source_name'] : null;
|
||||
$sourceIban = array_key_exists('source_iban', $transaction) ? (string)$transaction['source_iban'] : null;
|
||||
$validSource = $accountValidator->validateSource($sourceId, $sourceName, $sourceIban);
|
||||
|
||||
// do something with result:
|
||||
@@ -113,9 +113,9 @@ trait TransactionValidation
|
||||
return;
|
||||
}
|
||||
// validate destination account
|
||||
$destinationId = isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : null;
|
||||
$destinationName = isset($transaction['destination_name']) ? (string)$transaction['destination_name'] : null;
|
||||
$destinationIban = isset($transaction['destination_iban']) ? (string)$transaction['destination_iban'] : null;
|
||||
$destinationId = array_key_exists('destination_id', $transaction) ? (int)$transaction['destination_id'] : null;
|
||||
$destinationName = array_key_exists('destination_name', $transaction) ? (string)$transaction['destination_name'] : null;
|
||||
$destinationIban = array_key_exists('destination_iban', $transaction) ? (string)$transaction['destination_iban'] : null;
|
||||
$validDestination = $accountValidator->validateDestination($destinationId, $destinationName, $destinationIban);
|
||||
// do something with result:
|
||||
if (false === $validDestination) {
|
||||
@@ -155,10 +155,11 @@ trait TransactionValidation
|
||||
Log::debug('Now validating single account update in validateSingleUpdate()');
|
||||
|
||||
// if no account types are given, just skip the check.
|
||||
if (!isset($transaction['source_id'])
|
||||
&& !isset($transaction['source_name'])
|
||||
&& !isset($transaction['destination_id'])
|
||||
&& !isset($transaction['destination_name'])) {
|
||||
if (
|
||||
!array_key_exists('source_id', $transaction)
|
||||
&& !array_key_exists('source_name', $transaction)
|
||||
&& !array_key_exists('destination_id', $transaction)
|
||||
&& !array_key_exists('destination_name', $transaction)) {
|
||||
Log::debug('No account data has been submitted so will not validating account info.');
|
||||
|
||||
return;
|
||||
@@ -362,7 +363,7 @@ trait TransactionValidation
|
||||
if (0 === $journalId) {
|
||||
return 'invalid';
|
||||
}
|
||||
/** @var TransactionJournal $journal */
|
||||
/** @var TransactionJournal|null $journal */
|
||||
$journal = TransactionJournal::with(['transactionType'])->find($journalId);
|
||||
if (null !== $journal) {
|
||||
return strtolower($journal->transactionType->type);
|
||||
|
Reference in New Issue
Block a user