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

@@ -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.');
}
}