Fix code quality with rector [skip ci]

This commit is contained in:
James Cole
2025-11-09 09:08:03 +01:00
parent d2610be790
commit 68183a0a0e
209 changed files with 1021 additions and 1248 deletions

View File

@@ -37,9 +37,9 @@ trait DepositValidation
protected function validateDepositDestination(array $array): bool
{
$result = null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
$accountIban = $array['iban'] ?? null;
Log::debug('Now in validateDepositDestination', $array);
@@ -89,10 +89,10 @@ trait DepositValidation
*/
protected function validateDepositSource(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
$accountIban = $array['iban'] ?? null;
$accountNumber = $array['number'] ?? null;
Log::debug('Now in validateDepositSource', $array);
// null = we found nothing at all or didn't even search

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation\Account;
use Illuminate\Support\Facades\Log;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
@@ -35,23 +36,23 @@ trait LiabilityValidation
{
protected function validateLCDestination(array $array): bool
{
app('log')->debug('Now in validateLCDestination', $array);
Log::debug('Now in validateLCDestination', $array);
$result = null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
$validTypes = config('firefly.valid_liabilities');
// if the ID is not null the source account should be a dummy account of the type liability credit.
// the ID of the destination must belong to a liability.
if (null !== $accountId) {
if (AccountTypeEnum::LIABILITY_CREDIT->value !== $this->source?->accountType?->type) {
app('log')->error('Source account is not a liability.');
Log::error('Source account is not a liability.');
return false;
}
$result = $this->findExistingAccount($validTypes, $array);
if (null === $result) {
app('log')->error('Destination account is not a liability.');
Log::error('Destination account is not a liability.');
return false;
}
@@ -60,11 +61,11 @@ trait LiabilityValidation
}
if (null !== $accountName && '' !== $accountName) {
app('log')->debug('Destination ID is null, now we can assume the destination is a (new) liability credit account.');
Log::debug('Destination ID is null, now we can assume the destination is a (new) liability credit account.');
return true;
}
app('log')->error('Destination ID is null, but destination name is also NULL.');
Log::error('Destination ID is null, but destination name is also NULL.');
return false;
}
@@ -74,35 +75,35 @@ trait LiabilityValidation
*/
protected function validateLCSource(array $array): bool
{
app('log')->debug('Now in validateLCSource', $array);
Log::debug('Now in validateLCSource', $array);
// if the array has an ID and ID is not null, try to find it and check type.
// this account must be a liability
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountId = $array['id'] ?? null;
if (null !== $accountId) {
app('log')->debug('Source ID is not null, assume were looking for a liability.');
Log::debug('Source ID is not null, assume were looking for a liability.');
// find liability credit:
$result = $this->findExistingAccount(config('firefly.valid_liabilities'), $array);
if (null === $result) {
app('log')->error('Did not find a liability account, return false.');
Log::error('Did not find a liability account, return false.');
return false;
}
app('log')->debug(sprintf('Return true, found #%d ("%s")', $result->id, $result->name));
Log::debug(sprintf('Return true, found #%d ("%s")', $result->id, $result->name));
$this->setSource($result);
return true;
}
// if array has name and is not null, return true.
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountName = $array['name'] ?? null;
$result = true;
if ('' === $accountName || null === $accountName) {
app('log')->error('Array must have a name, is not the case, return false.');
Log::error('Array must have a name, is not the case, return false.');
$result = false;
}
if (true === $result) {
app('log')->error('Array has a name, return true.');
if ($result) {
Log::error('Array has a name, return true.');
// set the source to be a (dummy) revenue account.
$account = new Account();
$accountType = AccountType::whereType(AccountTypeEnum::LIABILITY_CREDIT->value)->first();

View File

@@ -37,8 +37,8 @@ trait OBValidation
protected function validateOBDestination(array $array): bool
{
$result = null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
Log::debug('Now in validateOBDestination', $array);
// source can be any of the following types.
@@ -83,8 +83,8 @@ trait OBValidation
*/
protected function validateOBSource(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
Log::debug('Now in validateOBSource', $array);
$result = null;
// source can be any of the following types.

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation\Account;
use Illuminate\Support\Facades\Log;
use FireflyIII\Models\Account;
/**
@@ -36,8 +37,8 @@ trait ReconciliationValidation
protected function validateReconciliationDestination(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
// if both are NULL, the destination is valid because the reconciliation
// is expected to be "negative", i.e. the money flows towards the
// destination to the asset account which is the source.
@@ -47,19 +48,19 @@ trait ReconciliationValidation
}
// after that, search for it expecting an asset account or a liability.
app('log')->debug('Now in validateReconciliationDestination', $array);
Log::debug('Now in validateReconciliationDestination', $array);
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
$this->sourceError = (string) trans('validation.reconciliation_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
Log::warning('Not a valid source. Cant find it.', $validTypes);
return false;
}
$this->setSource($search);
app('log')->debug('Valid source account!');
Log::debug('Valid source account!');
return true;
}
@@ -69,32 +70,32 @@ trait ReconciliationValidation
*/
protected function validateReconciliationSource(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
// if both are NULL, the source is valid because the reconciliation
// is expected to be "positive", i.e. the money flows from the
// source to the asset account that is the destination.
if (null === $accountId && null === $accountName) {
app('log')->debug('The source is valid because ID and name are NULL.');
Log::debug('The source is valid because ID and name are NULL.');
$this->setSource(new Account());
return true;
}
// after that, search for it expecting an asset account or a liability.
app('log')->debug('Now in validateReconciliationSource', $array);
Log::debug('Now in validateReconciliationSource', $array);
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
$this->sourceError = (string) trans('validation.reconciliation_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
Log::warning('Not a valid source. Cant find it.', $validTypes);
return false;
}
$this->setSource($search);
app('log')->debug('Valid source account!');
Log::debug('Valid source account!');
return true;
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation\Account;
use Illuminate\Support\Facades\Log;
use FireflyIII\Models\Account;
/**
@@ -33,17 +34,17 @@ trait TransferValidation
{
protected function validateTransferDestination(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
app('log')->debug('Now in validateTransferDestination', $array);
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
$accountIban = $array['iban'] ?? null;
Log::debug('Now in validateTransferDestination', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
if (null === $accountId && null === $accountName && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL we return false,
// because the destination of a transfer can't be created.
$this->destError = (string) trans('validation.transfer_dest_need_data');
app('log')->error('Both values are NULL, cant create transfer destination.');
Log::error('Both values are NULL, cant create transfer destination.');
return false;
}
@@ -74,11 +75,11 @@ trait TransferValidation
protected function validateTransferSource(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
app('log')->debug('Now in validateTransferSource', $array);
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
$accountIban = $array['iban'] ?? null;
$accountNumber = $array['number'] ?? null;
Log::debug('Now in validateTransferSource', $array);
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
if (null === $accountId && null === $accountName
@@ -87,7 +88,7 @@ trait TransferValidation
// if both values are NULL we return false,
// because the source of a withdrawal can't be created.
$this->sourceError = (string) trans('validation.transfer_source_need_data');
app('log')->warning('Not a valid source, need more data.');
Log::warning('Not a valid source, need more data.');
return false;
}
@@ -96,12 +97,12 @@ trait TransferValidation
$search = $this->findExistingAccount($validTypes, $array);
if (null === $search) {
$this->sourceError = (string) trans('validation.transfer_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
app('log')->warning('Not a valid source, cant find it.', $validTypes);
Log::warning('Not a valid source, cant find it.', $validTypes);
return false;
}
$this->setSource($search);
app('log')->debug('Valid source!');
Log::debug('Valid source!');
return true;
}

View File

@@ -35,9 +35,9 @@ trait WithdrawalValidation
{
protected function validateGenericSource(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
$accountIban = $array['iban'] ?? null;
Log::debug('Now in validateGenericSource', $array);
// source can be any of the following types.
$validTypes = [AccountTypeEnum::ASSET->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
@@ -70,10 +70,10 @@ trait WithdrawalValidation
protected function validateWithdrawalDestination(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
$accountIban = $array['iban'] ?? null;
$accountNumber = $array['number'] ?? null;
Log::debug('Now in validateWithdrawalDestination()', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
@@ -121,10 +121,10 @@ trait WithdrawalValidation
protected function validateWithdrawalSource(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
$accountId = $array['id'] ?? null;
$accountName = $array['name'] ?? null;
$accountIban = $array['iban'] ?? null;
$accountNumber = $array['number'] ?? null;
Log::debug('Now in validateWithdrawalSource', $array);
// source can be any of the following types.

View File

@@ -50,11 +50,11 @@ class AccountValidator
use TransferValidation;
use WithdrawalValidation;
public bool $createMode;
public string $destError;
public ?Account $destination;
public ?Account $source;
public string $sourceError;
public bool $createMode = false;
public string $destError = 'No error yet.';
public ?Account $destination = null;
public ?Account $source = null;
public string $sourceError = 'No error yet.';
private AccountRepositoryInterface $accountRepository;
private array $combinations;
private string $transactionType;
@@ -64,12 +64,7 @@ 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;
$this->accountRepository = app(AccountRepositoryInterface::class);
}
@@ -235,11 +230,7 @@ class AccountValidator
protected function canCreateType(string $accountType): bool
{
$canCreate = [AccountTypeEnum::EXPENSE->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::INITIAL_BALANCE->value, AccountTypeEnum::LIABILITY_CREDIT->value];
if (in_array($accountType, $canCreate, true)) {
return true;
}
return false;
return in_array($accountType, $canCreate, true);
}
/**
@@ -253,10 +244,10 @@ class AccountValidator
{
Log::debug('Now in findExistingAccount', [$validTypes, $data]);
Log::debug('The search will be reversed!');
$accountId = array_key_exists('id', $data) ? $data['id'] : null;
$accountIban = array_key_exists('iban', $data) ? $data['iban'] : null;
$accountNumber = array_key_exists('number', $data) ? $data['number'] : null;
$accountName = array_key_exists('name', $data) ? $data['name'] : null;
$accountId = $data['id'] ?? null;
$accountIban = $data['iban'] ?? null;
$accountNumber = $data['number'] ?? null;
$accountName = $data['name'] ?? null;
// find by ID
if (null !== $accountId && $accountId > 0) {

View File

@@ -24,8 +24,8 @@ declare(strict_types=1);
namespace FireflyIII\Validation\Api\Data\Bulk;
use Illuminate\Contracts\Validation\Validator;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Validation\Validator;
use function Safe\json_decode;

View File

@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation\AutoBudget;
use Illuminate\Validation\Validator;
use Illuminate\Contracts\Validation\Validator;
/**
* Trait ValidatesAutoBudgetRequest
@@ -40,10 +40,10 @@ trait ValidatesAutoBudgetRequest
$type = $data['auto_budget_type'] ?? '';
/** @var null|float|int|string $amount */
$amount = array_key_exists('auto_budget_amount', $data) ? $data['auto_budget_amount'] : null;
$period = array_key_exists('auto_budget_period', $data) ? $data['auto_budget_period'] : null;
$amount = $data['auto_budget_amount'] ?? null;
$period = $data['auto_budget_period'] ?? null;
$currencyId = array_key_exists('auto_budget_currency_id', $data) ? (int) $data['auto_budget_currency_id'] : null;
$currencyCode = array_key_exists('auto_budget_currency_code', $data) ? $data['auto_budget_currency_code'] : null;
$currencyCode = $data['auto_budget_currency_code'] ?? null;
if (is_numeric($type)) {
$type = (int) $type;
}

View File

@@ -24,8 +24,8 @@ declare(strict_types=1);
namespace FireflyIII\Validation;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
/**
* Trait CurrencyValidation
@@ -44,7 +44,7 @@ trait CurrencyValidation
if ($validator->errors()->count() > 0) {
return;
}
app('log')->debug('Now in validateForeignCurrencyInformation()');
Log::debug('Now in validateForeignCurrencyInformation()');
$transactions = $this->getTransactionsArray($validator);
foreach ($transactions as $index => $transaction) {

View File

@@ -123,11 +123,7 @@ class FireflyValidator extends Validator
}
$regex = '/^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z/i';
$result = preg_match($regex, $value);
if (0 === $result) {
return false;
}
return true;
return 0 !== $result;
}
public function validateExistingMfaCode(mixed $attribute, mixed $value): bool
@@ -459,7 +455,7 @@ class FireflyValidator extends Validator
*
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
*/
public function validateSecurePassword($attribute, $value): bool
public function validateSecurePassword($attribute, string $value): bool
{
$verify = false;
if (array_key_exists('verify_password', $this->data)) {

View File

@@ -24,10 +24,11 @@ declare(strict_types=1);
namespace FireflyIII\Validation;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Validation\Validator;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
use Illuminate\Validation\Validator;
/**
* Trait GroupValidation.
@@ -89,7 +90,7 @@ trait GroupValidation
protected function preventUpdateReconciled(Validator $validator, TransactionGroup $transactionGroup): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
Log::debug(sprintf('Now in %s', __METHOD__));
$count = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', 'transactions.transaction_journal_id')
->leftJoin('transaction_groups', 'transaction_groups.id', 'transaction_journals.transaction_group_id')
@@ -97,7 +98,7 @@ trait GroupValidation
->where('transactions.reconciled', 1)->where('transactions.amount', '<', 0)->count('transactions.id')
;
if (0 === $count) {
app('log')->debug(sprintf('Transaction is not reconciled, done with %s', __METHOD__));
Log::debug(sprintf('Transaction is not reconciled, done with %s', __METHOD__));
return;
}
@@ -123,7 +124,7 @@ trait GroupValidation
}
}
app('log')->debug(sprintf('Done with %s', __METHOD__));
Log::debug(sprintf('Done with %s', __METHOD__));
}
/**
@@ -135,7 +136,7 @@ trait GroupValidation
if ($validator->errors()->count() > 0) {
return;
}
app('log')->debug('Now in GroupValidation::validateDescriptions()');
Log::debug('Now in GroupValidation::validateDescriptions()');
$transactions = $this->getTransactionsArray($validator);
$validDescriptions = 0;
foreach ($transactions as $transaction) {
@@ -158,7 +159,7 @@ trait GroupValidation
if ($validator->errors()->count() > 0) {
return;
}
app('log')->debug('Now in validateGroupDescription()');
Log::debug('Now in validateGroupDescription()');
$data = $validator->getData();
$transactions = $this->getTransactionsArray($validator);
@@ -175,12 +176,12 @@ trait GroupValidation
*/
protected function validateJournalIds(Validator $validator, TransactionGroup $transactionGroup): void
{
app('log')->debug(sprintf('Now in GroupValidation::validateJournalIds(%d)', $transactionGroup->id));
Log::debug(sprintf('Now in GroupValidation::validateJournalIds(%d)', $transactionGroup->id));
$transactions = $this->getTransactionsArray($validator);
if (count($transactions) < 2) {
// no need for validation.
app('log')->debug(sprintf('%d transaction(s) in submission, can skip this check.', count($transactions)));
Log::debug(sprintf('%d transaction(s) in submission, can skip this check.', count($transactions)));
return;
}
@@ -204,17 +205,17 @@ trait GroupValidation
if (array_key_exists('transaction_journal_id', $transaction)) {
$journalId = $transaction['transaction_journal_id'];
}
app('log')->debug(sprintf('Now in validateJournalId(%d, %d)', $index, $journalId));
Log::debug(sprintf('Now in validateJournalId(%d, %d)', $index, $journalId));
if (0 === $journalId || '' === $journalId || '0' === $journalId) {
app('log')->debug('Submitted 0, will accept to be used in a new transaction.');
Log::debug('Submitted 0, will accept to be used in a new transaction.');
return;
}
$journalId = (int) $journalId;
$count = $transactionGroup->transactionJournals()->where('transaction_journals.id', $journalId)->count();
if (0 === $journalId || 0 === $count) {
app('log')->warning(sprintf('Transaction group #%d has %d journals with ID %d', $transactionGroup->id, $count, $journalId));
app('log')->warning('Invalid submission: Each split must have transaction_journal_id (either valid ID or 0).');
Log::warning(sprintf('Transaction group #%d has %d journals with ID %d', $transactionGroup->id, $count, $journalId));
Log::warning('Invalid submission: Each split must have transaction_journal_id (either valid ID or 0).');
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), (string) trans('validation.need_id_in_edit'));
}
}

View File

@@ -24,10 +24,11 @@ declare(strict_types=1);
namespace FireflyIII\Validation;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Validation\Validator;
use Carbon\Carbon;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceTransaction;
use Illuminate\Validation\Validator;
use InvalidArgumentException;
/**
@@ -50,22 +51,22 @@ trait RecurrenceValidation
// grab model from parameter and try to set the transaction type from it
if ('invalid' === $transactionType) {
app('log')->debug('Type is invalid but we will search for it.');
Log::debug('Type is invalid but we will search for it.');
/** @var null|Recurrence $recurrence */
$recurrence = $this->route()?->parameter('recurrence');
if (null !== $recurrence) {
app('log')->debug('There is a recurrence in the route.');
Log::debug('There is a recurrence in the route.');
// ok so we have a recurrence should be able to extract type somehow.
/** @var null|RecurrenceTransaction $first */
$first = $recurrence->recurrenceTransactions()->first();
if (null !== $first) {
$transactionType = null !== $first->transactionType ? $first->transactionType->type : 'withdrawal';
app('log')->debug(sprintf('Determined type to be %s.', $transactionType));
Log::debug(sprintf('Determined type to be %s.', $transactionType));
}
if (null === $first) {
app('log')->warning('Just going to assume type is a withdrawal.');
Log::warning('Just going to assume type is a withdrawal.');
$transactionType = 'withdrawal';
}
}
@@ -76,7 +77,7 @@ trait RecurrenceValidation
/** @var AccountValidator $accountValidator */
$accountValidator = app(AccountValidator::class);
app('log')->debug(sprintf('Going to loop %d transaction(s)', count($transactions)));
Log::debug(sprintf('Going to loop %d transaction(s)', count($transactions)));
foreach ($transactions as $index => $transaction) {
$transactionType = $transaction['type'] ?? $transactionType;
$accountValidator->setTransactionType($transactionType);
@@ -296,7 +297,7 @@ trait RecurrenceValidation
try {
Carbon::createFromFormat('Y-m-d', $moment);
} catch (InvalidArgumentException $e) { // @phpstan-ignore-line
app('log')->debug(sprintf('Invalid argument for Carbon: %s', $e->getMessage()));
Log::debug(sprintf('Invalid argument for Carbon: %s', $e->getMessage()));
$validator->errors()->add(sprintf('repetitions.%d.moment', $index), (string) trans('validation.valid_recurrence_rep_moment'));
}
}
@@ -306,12 +307,12 @@ trait RecurrenceValidation
*/
protected function validateTransactionId(Recurrence $recurrence, Validator $validator): void
{
app('log')->debug('Now in validateTransactionId');
Log::debug('Now in validateTransactionId');
$transactions = $this->getTransactionData();
$submittedTrCount = count($transactions);
if (0 === $submittedTrCount) {
app('log')->warning('[b] User submitted no transactions.');
Log::warning('[b] User submitted no transactions.');
$validator->errors()->add('transactions', (string) trans('validation.at_least_one_transaction'));
return;
@@ -320,31 +321,31 @@ trait RecurrenceValidation
if (1 === $submittedTrCount && 1 === $originalTrCount) {
$first = $transactions[0]; // can safely assume index 0.
if (!array_key_exists('id', $first)) {
app('log')->debug('Single count and no ID, done.');
Log::debug('Single count and no ID, done.');
return; // home safe!
}
$id = $first['id'];
if ('' === (string) $id) {
app('log')->debug('Single count and empty ID, done.');
Log::debug('Single count and empty ID, done.');
return; // home safe!
}
$integer = (int) $id;
$secondCount = $recurrence->recurrenceTransactions()->where('recurrences_transactions.id', $integer)->count();
app('log')->debug(sprintf('Result of ID count: %d', $secondCount));
Log::debug(sprintf('Result of ID count: %d', $secondCount));
if (0 === $secondCount) {
$validator->errors()->add('transactions.0.id', (string) trans('validation.id_does_not_match', ['id' => $integer]));
}
app('log')->debug('Single ID validation done.');
Log::debug('Single ID validation done.');
return;
}
app('log')->debug('Multi ID validation.');
Log::debug('Multi ID validation.');
$idsMandatory = false;
if ($submittedTrCount < $originalTrCount) {
app('log')->debug(sprintf('User submits %d transaction, recurrence has %d transactions. All entries must have ID.', $submittedTrCount, $originalTrCount));
Log::debug(sprintf('User submits %d transaction, recurrence has %d transactions. All entries must have ID.', $submittedTrCount, $originalTrCount));
$idsMandatory = true;
}
@@ -362,41 +363,41 @@ trait RecurrenceValidation
$unmatchedIds = 0;
foreach ($transactions as $index => $transaction) {
app('log')->debug(sprintf('Now at %d/%d', $index + 1, $submittedTrCount));
Log::debug(sprintf('Now at %d/%d', $index + 1, $submittedTrCount));
if (!is_array($transaction)) {
app('log')->warning('Not an array. Give error.');
Log::warning('Not an array. Give error.');
$validator->errors()->add(sprintf('transactions.%d.id', $index), (string) trans('validation.at_least_one_transaction'));
return;
}
if (!array_key_exists('id', $transaction) && $idsMandatory) {
app('log')->warning('ID is mandatory but array has no ID.');
Log::warning('ID is mandatory but array has no ID.');
$validator->errors()->add(sprintf('transactions.%d.id', $index), (string) trans('validation.need_id_to_match'));
return;
}
if (array_key_exists('id', $transaction)) { // don't matter if $idsMandatory
app('log')->debug('Array has ID.');
Log::debug('Array has ID.');
$idCount = $recurrence->recurrenceTransactions()->where('recurrences_transactions.id', (int) $transaction['id'])->count();
if (0 === $idCount) {
app('log')->debug('ID does not exist or no match. Count another unmatched ID.');
Log::debug('ID does not exist or no match. Count another unmatched ID.');
++$unmatchedIds;
}
}
if (!array_key_exists('id', $transaction) && !$idsMandatory) {
app('log')->debug('Array has no ID but was not mandatory at this point.');
Log::debug('Array has no ID but was not mandatory at this point.');
++$unmatchedIds;
}
}
// if too many don't match, but you haven't submitted more than already present:
$maxUnmatched = max(1, $submittedTrCount - $originalTrCount);
app('log')->debug(sprintf('Submitted: %d. Original: %d. User can submit %d unmatched transactions.', $submittedTrCount, $originalTrCount, $maxUnmatched));
Log::debug(sprintf('Submitted: %d. Original: %d. User can submit %d unmatched transactions.', $submittedTrCount, $originalTrCount, $maxUnmatched));
if ($unmatchedIds > $maxUnmatched) {
app('log')->warning(sprintf('Too many unmatched transactions (%d).', $unmatchedIds));
Log::warning(sprintf('Too many unmatched transactions (%d).', $unmatchedIds));
$validator->errors()->add('transactions.0.id', (string) trans('validation.too_many_unmatched'));
return;
}
app('log')->debug('Done with ID validation.');
Log::debug('Done with ID validation.');
}
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation;
use Illuminate\Contracts\Validation\Validator;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Enums\TransactionTypeEnum;
use FireflyIII\Exceptions\FireflyException;
@@ -36,7 +37,6 @@ use FireflyIII\Repositories\Account\AccountRepository;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
/**
* Trait TransactionValidation
@@ -302,11 +302,7 @@ trait TransactionValidation
private function isLiability(Account $account): bool
{
$type = $account->accountType->type;
if (in_array($type, config('firefly.valid_liabilities'), true)) {
return true;
}
return false;
return in_array($type, config('firefly.valid_liabilities'), true);
}
private function isAsset(Account $account): bool
@@ -327,11 +323,7 @@ trait TransactionValidation
if ('' === $transaction['foreign_amount']) {
return false;
}
if (0 === bccomp('0', (string) $transaction['foreign_amount'])) {
return false;
}
return true;
return 0 !== bccomp('0', (string) $transaction['foreign_amount']);
}
/**
@@ -758,12 +750,8 @@ trait TransactionValidation
// source ID's are equal, return void.
return true;
}
if ($this->arrayEqual($comparison['source_name'])) {
// source names are equal, return void.
return true;
}
return false;
// source names are equal, return void.
return (bool) $this->arrayEqual($comparison['source_name']);
}
private function arrayEqual(array $array): bool
@@ -777,12 +765,8 @@ trait TransactionValidation
// destination ID's are equal, return void.
return true;
}
if ($this->arrayEqual($comparison['destination_name'])) {
// destination names are equal, return void.
return true;
}
return false;
// destination names are equal, return void.
return (bool) $this->arrayEqual($comparison['destination_name']);
}
private function compareAccountDataTransfer(array $comparison): bool
@@ -799,11 +783,7 @@ trait TransactionValidation
// destination ID's are equal, return void.
return true;
}
if ($this->arrayEqual($comparison['destination_name'])) {
// destination names are equal, return void.
return true;
}
return false;
// destination names are equal, return void.
return (bool) $this->arrayEqual($comparison['destination_name']);
}
}