Fix various code.

This commit is contained in:
James Cole
2025-05-27 17:06:15 +02:00
parent d8f512ca3a
commit 2cb14f6b72
123 changed files with 581 additions and 500 deletions

View File

@@ -79,10 +79,10 @@ class AccountValidator
public function setSource(?Account $account): void
{
if (null === $account) {
if (!$account instanceof Account) {
app('log')->debug('AccountValidator source is set to NULL');
}
if (null !== $account) {
if ($account instanceof Account) {
app('log')->debug(sprintf('AccountValidator source is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType?->type));
}
$this->source = $account;
@@ -90,10 +90,10 @@ class AccountValidator
public function setDestination(?Account $account): void
{
if (null === $account) {
if (!$account instanceof Account) {
app('log')->debug('AccountValidator destination is set to NULL');
}
if (null !== $account) {
if ($account instanceof Account) {
app('log')->debug(sprintf('AccountValidator destination is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType->type));
}
$this->destination = $account;
@@ -118,7 +118,7 @@ class AccountValidator
public function validateDestination(array $array): bool
{
app('log')->debug('Now in AccountValidator::validateDestination()', $array);
if (null === $this->source) {
if (!$this->source instanceof Account) {
app('log')->error('Source is NULL, always FALSE.');
$this->destError = 'No source account validation has taken place yet. Please do this first or overrule the object.';
@@ -260,10 +260,10 @@ class AccountValidator
// find by ID
if (null !== $accountId && $accountId > 0) {
$first = $this->accountRepository->find($accountId);
$accountType = null === $first ? 'invalid' : $first->accountType->type;
$accountType = $first instanceof Account ? $first->accountType->type : 'invalid';
$check = in_array($accountType, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if ((null !== $first) && $check) {
if (($first instanceof Account) && $check) {
app('log')->debug(sprintf('ID: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
@@ -273,10 +273,10 @@ class AccountValidator
// find by iban
if (null !== $accountIban && '' !== (string) $accountIban) {
$first = $this->accountRepository->findByIbanNull($accountIban, $validTypes);
$accountType = null === $first ? 'invalid' : $first->accountType->type;
$accountType = $first instanceof Account ? $first->accountType->type : 'invalid';
$check = in_array($accountType, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if ((null !== $first) && $check) {
if (($first instanceof Account) && $check) {
app('log')->debug(sprintf('Iban: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
@@ -286,10 +286,10 @@ class AccountValidator
// find by number
if (null !== $accountNumber && '' !== (string) $accountNumber) {
$first = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes);
$accountType = null === $first ? 'invalid' : $first->accountType->type;
$accountType = $first instanceof Account ? $first->accountType->type : 'invalid';
$check = in_array($accountType, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if ((null !== $first) && $check) {
if (($first instanceof Account) && $check) {
app('log')->debug(sprintf('Number: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
@@ -299,7 +299,7 @@ class AccountValidator
// find by name:
if ('' !== (string) $accountName) {
$first = $this->accountRepository->findByName($accountName, $validTypes);
if (null !== $first) {
if ($first instanceof Account) {
app('log')->debug(sprintf('Name: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;

View File

@@ -27,6 +27,8 @@ namespace FireflyIII\Validation\Api\Data\Bulk;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Validation\Validator;
use function Safe\json_decode;
trait ValidatesBulkTransactionQuery
{
protected function validateTransactionQuery(Validator $validator): void
@@ -34,7 +36,7 @@ trait ValidatesBulkTransactionQuery
$data = $validator->getData();
// assumption is all validation has already taken place and the query key exists.
$query = $data['query'] ?? '[]';
$json = \Safe\json_decode($query, true, 8, JSON_THROW_ON_ERROR);
$json = json_decode($query, true, 8, JSON_THROW_ON_ERROR);
if (
array_key_exists('where', $json)

View File

@@ -48,6 +48,10 @@ use PragmaRX\Google2FALaravel\Facade;
use Config;
use ValueError;
use function Safe\preg_match;
use function Safe\iconv;
use function Safe\json_encode;
/**
* Class FireflyValidator.
* TODO all of these validations must become separate classes.
@@ -113,7 +117,7 @@ class FireflyValidator extends Validator
return false;
}
$regex = '/^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z/i';
$result = \Safe\preg_match($regex, $value);
$result = preg_match($regex, $value);
if (false === $result || 0 === $result) {
return false;
}
@@ -203,7 +207,7 @@ class FireflyValidator extends Validator
$value = strtoupper($value);
// replace characters outside of ASCI range.
$value = (string) \Safe\iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
$value = (string) iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
$search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
$replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'];
@@ -637,7 +641,7 @@ class FireflyValidator extends Validator
->whereNull('accounts.deleted_at')
->where('accounts.user_id', auth()->user()->id)
->where('account_meta.name', 'account_number')
->where('account_meta.data', \Safe\json_encode($value))
->where('account_meta.data', json_encode($value))
;
if ($accountId > 0) {

View File

@@ -166,13 +166,13 @@ trait RecurrenceValidation
$reps = array_key_exists('nr_of_repetitions', $data) ? (int) $data['nr_of_repetitions'] : null;
$repeatUntil = array_key_exists('repeat_until', $data) ? new Carbon($data['repeat_until']) : null;
if (null === $reps && null === $repeatUntil) {
if (null === $reps && !$repeatUntil instanceof Carbon) {
$validator->errors()->add('nr_of_repetitions', trans('validation.require_repeat_until'));
$validator->errors()->add('repeat_until', trans('validation.require_repeat_until'));
return;
}
if ($reps > 0 && null !== $repeatUntil) {
if ($reps > 0 && $repeatUntil instanceof Carbon) {
$validator->errors()->add('nr_of_repetitions', trans('validation.require_repeat_until'));
$validator->errors()->add('repeat_until', trans('validation.require_repeat_until'));
}

View File

@@ -200,12 +200,12 @@ trait TransactionValidation
return;
}
if (null === $accountValidator->source) {
if (!$accountValidator->source instanceof Account) {
Log::debug('No source, return');
return;
}
if (null === $accountValidator->destination) {
if (!$accountValidator->destination instanceof Account) {
Log::debug('No destination, return');
return;
@@ -292,7 +292,11 @@ trait TransactionValidation
private function isLiabilityOrAsset(Account $account): bool
{
return $this->isLiability($account) || $this->isAsset($account);
if ($this->isLiability($account)) {
return true;
}
return (bool) $this->isAsset($account);
}
private function isLiability(Account $account): bool