chore: code cleanup.

This commit is contained in:
James Cole
2023-05-29 13:56:55 +02:00
parent 7f7644c92f
commit 1b52147a05
295 changed files with 12418 additions and 12324 deletions

View File

@@ -32,6 +32,21 @@ use Illuminate\Support\Facades\Log;
*/
trait DepositValidation
{
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param array $array
*
@@ -71,7 +86,7 @@ trait DepositValidation
if (null !== $search) {
Log::debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name));
$this->setDestination($search);
$result = true;
$result = true;
}
}
Log::debug(sprintf('validateDepositDestination will return %s', var_export($result, true)));
@@ -79,21 +94,6 @@ trait DepositValidation
return $result;
}
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param array $array
*
@@ -132,7 +132,7 @@ trait DepositValidation
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
Log::debug('ID result is not null and seems valid, save as source account.');
$this->setSource($search);
$result = true;
$result = true;
}
}
@@ -146,7 +146,7 @@ trait DepositValidation
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
Log::debug('IBAN result is not null and seems valid, save as source account.');
$this->setSource($search);
$result = true;
$result = true;
}
}
@@ -162,7 +162,7 @@ trait DepositValidation
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
Log::debug('Number result is not null and seems valid, save as source account.');
$this->setSource($search);
$result = true;
$result = true;
}
}

View File

@@ -33,6 +33,13 @@ use Illuminate\Support\Facades\Log;
*/
trait OBValidation
{
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $array
*
@@ -71,7 +78,7 @@ trait OBValidation
if (null !== $search) {
Log::debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name));
$this->setDestination($search);
$result = true;
$result = true;
}
}
Log::debug(sprintf('validateOBDestination(%d, "%s") will return %s', $accountId, $accountName, var_export($result, true)));
@@ -79,13 +86,6 @@ trait OBValidation
return $result;
}
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* Source of an opening balance can either be an asset account
* or an "initial balance account". The latter can be created.
@@ -128,7 +128,7 @@ trait OBValidation
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
Log::debug(sprintf('Found account of correct type: #%d, "%s"', $search->id, $search->name));
$this->setSource($search);
$result = true;
$result = true;
}
}

View File

@@ -31,6 +31,21 @@ use Illuminate\Support\Facades\Log;
*/
trait TransferValidation
{
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param array $array
*
@@ -72,21 +87,6 @@ trait TransferValidation
return true;
}
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param array $array
*

View File

@@ -32,6 +32,21 @@ use Illuminate\Support\Facades\Log;
*/
trait WithdrawalValidation
{
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param array $array
*
@@ -67,21 +82,6 @@ trait WithdrawalValidation
return true;
}
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param array $array
*
@@ -128,10 +128,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_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;
Log::debug('Now in validateWithdrawalSource', $array);
// source can be any of the following types.

View File

@@ -82,6 +82,34 @@ class AccountValidator
return $this->source;
}
/**
* @param Account|null $account
*/
public function setSource(?Account $account): void
{
if (null === $account) {
Log::debug('AccountValidator source is set to NULL');
}
if (null !== $account) {
Log::debug(sprintf('AccountValidator source is set to #%d: "%s" (%s)', $account->id, $account->name, $account?->accountType->type));
}
$this->source = $account;
}
/**
* @param Account|null $account
*/
public function setDestination(?Account $account): void
{
if (null === $account) {
Log::debug('AccountValidator destination is set to NULL');
}
if (null !== $account) {
Log::debug(sprintf('AccountValidator destination is set to #%d: "%s" (%s)', $account->id, $account->name, $account?->accountType->type));
}
$this->destination = $account;
}
/**
* @param string $transactionType
*/
@@ -183,6 +211,21 @@ class AccountValidator
return $result;
}
/**
* @param string $accountType
*
* @return bool
*/
protected function canCreateType(string $accountType): bool
{
$canCreate = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE, AccountType::LIABILITY_CREDIT];
if (in_array($accountType, $canCreate, true)) {
return true;
}
return false;
}
/**
* @param array $accountTypes
*
@@ -204,21 +247,6 @@ class AccountValidator
return false;
}
/**
* @param string $accountType
*
* @return bool
*/
protected function canCreateType(string $accountType): bool
{
$canCreate = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE, AccountType::LIABILITY_CREDIT];
if (in_array($accountType, $canCreate, true)) {
return true;
}
return false;
}
/**
* @param array $validTypes
* @param array $data
@@ -264,32 +292,4 @@ class AccountValidator
return null;
}
/**
* @param Account|null $account
*/
public function setDestination(?Account $account): void
{
if (null === $account) {
Log::debug('AccountValidator destination is set to NULL');
}
if (null !== $account) {
Log::debug(sprintf('AccountValidator destination is set to #%d: "%s" (%s)', $account->id, $account->name, $account?->accountType->type));
}
$this->destination = $account;
}
/**
* @param Account|null $account
*/
public function setSource(?Account $account): void
{
if (null === $account) {
Log::debug('AccountValidator source is set to NULL');
}
if (null !== $account) {
Log::debug(sprintf('AccountValidator source is set to #%d: "%s" (%s)', $account->id, $account->name, $account?->accountType->type));
}
$this->source = $account;
}
}

View File

@@ -24,8 +24,8 @@ declare(strict_types=1);
namespace FireflyIII\Validation;
use Illuminate\Validation\Validator;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
/**
* Trait CurrencyValidation
@@ -36,6 +36,13 @@ trait CurrencyValidation
{
public const TEST = 'Test';
/**
* @param Validator $validator
*
* @return array
*/
abstract protected function getTransactionsArray(Validator $validator): array;
/**
* If the transactions contain foreign amounts, there must also be foreign currency information.
*
@@ -80,11 +87,4 @@ trait CurrencyValidation
}
}
}
/**
* @param Validator $validator
*
* @return array
*/
abstract protected function getTransactionsArray(Validator $validator): array;
}

View File

@@ -41,7 +41,6 @@ use FireflyIII\Support\ParseDateString;
use FireflyIII\TransactionRules\Triggers\TriggerInterface;
use FireflyIII\User;
use Google2FA;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
use PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException;
@@ -505,121 +504,6 @@ class FireflyValidator extends Validator
return $this->validateByAccountName($value);
}
/**
* @return bool
*/
private function validateAccountAnonymously(): bool
{
if (!array_key_exists('user_id', $this->data)) {
return false;
}
$user = User::find($this->data['user_id']);
$type = AccountType::find($this->data['account_type_id'])->first();
$value = $this->data['name'];
/** @var Account|null $result */
$result = $user->accounts()->where('account_type_id', $type->id)->where('name', $value)->first();
return null === $result;
}
/**
* @param string $value
* @param array $parameters
* @param string $type
*
* @return bool
*/
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
{
/** @var array|null $search */
$search = Config::get('firefly.accountTypeByIdentifier.'.$type);
if (null === $search) {
return false;
}
$accountTypes = AccountType::whereIn('type', $search)->get();
$ignore = (int)($parameters[0] ?? 0.0);
$accountTypeIds = $accountTypes->pluck('id')->toArray();
/** @var Account|null $result */
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
->where('name', $value)
->first();
return null === $result;
}
/**
* @param mixed $value
* @param mixed $parameters
*
* @return bool
*/
private function validateByAccountTypeId($value, $parameters): bool
{
$type = AccountType::find($this->data['account_type_id'])->first();
$ignore = (int)($parameters[0] ?? 0.0);
/** @var Account|null $result */
$result = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value)
->first();
return null === $result;
}
/**
* @param int $accountId
* @param mixed $value
*
* @return bool
*/
private function validateByParameterId(int $accountId, $value): bool
{
/** @var Account $existingAccount */
$existingAccount = Account::find($accountId);
$type = $existingAccount->accountType;
$ignore = $existingAccount->id;
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value)
->first();
return null === $entry;
}
/**
* @param mixed $value
*
* @return bool
*/
private function validateByAccountId($value): bool
{
/** @var Account $existingAccount */
$existingAccount = Account::find($this->data['id']);
$type = $existingAccount->accountType;
$ignore = $existingAccount->id;
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value)
->first();
return null === $entry;
}
/**
* @param string $value
*
* @return bool
*/
private function validateByAccountName(string $value): bool
{
return auth()->user()->accounts()->where('name', $value)->count() === 0;
}
/**
* @param mixed $attribute
* @param mixed $value
@@ -673,16 +557,6 @@ class FireflyValidator extends Validator
return false;
}
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrencyCode($attribute, $value): bool
{
return $this->validateUniqueCurrency('code', (string)$attribute, (string)$value);
}
/**
* @param string $field
* @param string $attribute
@@ -694,6 +568,16 @@ class FireflyValidator extends Validator
return 0 === DB::table('transaction_currencies')->where($field, $value)->whereNull('deleted_at')->count();
}
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrencyCode($attribute, $value): bool
{
return $this->validateUniqueCurrency('code', (string)$attribute, (string)$value);
}
/**
* @param $attribute
* @param $value
@@ -791,9 +675,9 @@ class FireflyValidator extends Validator
}
// get entries from table
$result = DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
->where('id', '!=', $exclude)
->where($field, $value)
->first([$field]);
->where('id', '!=', $exclude)
->where($field, $value)
->first([$field]);
if (null === $result) {
return true; // not found, so true.
}
@@ -871,4 +755,119 @@ class FireflyValidator extends Validator
return false;
}
/**
* @return bool
*/
private function validateAccountAnonymously(): bool
{
if (!array_key_exists('user_id', $this->data)) {
return false;
}
$user = User::find($this->data['user_id']);
$type = AccountType::find($this->data['account_type_id'])->first();
$value = $this->data['name'];
/** @var Account|null $result */
$result = $user->accounts()->where('account_type_id', $type->id)->where('name', $value)->first();
return null === $result;
}
/**
* @param mixed $value
*
* @return bool
*/
private function validateByAccountId($value): bool
{
/** @var Account $existingAccount */
$existingAccount = Account::find($this->data['id']);
$type = $existingAccount->accountType;
$ignore = $existingAccount->id;
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value)
->first();
return null === $entry;
}
/**
* @param string $value
*
* @return bool
*/
private function validateByAccountName(string $value): bool
{
return auth()->user()->accounts()->where('name', $value)->count() === 0;
}
/**
* @param mixed $value
* @param mixed $parameters
*
* @return bool
*/
private function validateByAccountTypeId($value, $parameters): bool
{
$type = AccountType::find($this->data['account_type_id'])->first();
$ignore = (int)($parameters[0] ?? 0.0);
/** @var Account|null $result */
$result = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value)
->first();
return null === $result;
}
/**
* @param string $value
* @param array $parameters
* @param string $type
*
* @return bool
*/
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
{
/** @var array|null $search */
$search = Config::get('firefly.accountTypeByIdentifier.'.$type);
if (null === $search) {
return false;
}
$accountTypes = AccountType::whereIn('type', $search)->get();
$ignore = (int)($parameters[0] ?? 0.0);
$accountTypeIds = $accountTypes->pluck('id')->toArray();
/** @var Account|null $result */
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
->where('name', $value)
->first();
return null === $result;
}
/**
* @param int $accountId
* @param mixed $value
*
* @return bool
*/
private function validateByParameterId(int $accountId, $value): bool
{
/** @var Account $existingAccount */
$existingAccount = Account::find($accountId);
$type = $existingAccount->accountType;
$ignore = $existingAccount->id;
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value)
->first();
return null === $entry;
}
}

View File

@@ -26,8 +26,8 @@ namespace FireflyIII\Validation;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionGroup;
use Illuminate\Validation\Validator;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
/**
* Trait GroupValidation.
@@ -36,6 +36,13 @@ use Illuminate\Support\Facades\Log;
*/
trait GroupValidation
{
/**
* @param Validator $validator
*
* @return array
*/
abstract protected function getTransactionsArray(Validator $validator): array;
/**
* @param Validator $validator
*/
@@ -54,7 +61,7 @@ trait GroupValidation
];
/** @var array $transaction */
foreach ($transactions as $index => $transaction) {
if(!is_array($transaction)) {
if (!is_array($transaction)) {
throw new FireflyException('Invalid data submitted: transaction is not array.');
}
$hasAccountInfo = false;
@@ -79,13 +86,6 @@ trait GroupValidation
// only an issue if there is no transaction_journal_id
}
/**
* @param Validator $validator
*
* @return array
*/
abstract protected function getTransactionsArray(Validator $validator): array;
/**
* Adds an error to the "description" field when the user has submitted no descriptions and no
* journal description.

View File

@@ -26,9 +26,9 @@ namespace FireflyIII\Validation;
use Carbon\Carbon;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceTransaction;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
use InvalidArgumentException;
use Illuminate\Support\Facades\Log;
/**
* Trait RecurrenceValidation

File diff suppressed because it is too large Load Diff