Code cleanup

This commit is contained in:
James Cole
2021-03-21 09:15:40 +01:00
parent da1751940e
commit 206845575c
317 changed files with 7418 additions and 7362 deletions

View File

@@ -24,10 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Validation\Account;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
/**
* I have no idea what made me do this. I'll reverse it some day.
*

View File

@@ -32,22 +32,6 @@ use Log;
*/
trait DepositValidation
{
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param int $accountId
* @param string $accountName
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account;
/**
* @param int|null $accountId
* @param $accountName
@@ -64,7 +48,7 @@ trait DepositValidation
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL we return false,
// because the destination of a deposit can't be created.
$this->destError = (string) trans('validation.deposit_dest_need_data');
$this->destError = (string)trans('validation.deposit_dest_need_data');
Log::error('Both values are NULL, cant create deposit destination.');
$result = false;
}
@@ -76,10 +60,10 @@ trait DepositValidation
if (null === $result) {
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int) $accountId, (string) $accountName);
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
if (null === $search) {
Log::debug('findExistingAccount() returned NULL, so the result is false.');
$this->destError = (string) trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
$this->destError = (string)trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
$result = false;
}
if (null !== $search) {
@@ -94,6 +78,22 @@ trait DepositValidation
return $result;
}
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param int $accountId
* @param string $accountName
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account;
/**
* @param int|null $accountId
* @param string|null $accountName
@@ -110,7 +110,7 @@ trait DepositValidation
// if both values are NULL return false,
// because the source of a deposit can't be created.
// (this never happens).
$this->sourceError = (string) trans('validation.deposit_source_need_data');
$this->sourceError = (string)trans('validation.deposit_source_need_data');
$result = false;
}
@@ -134,6 +134,7 @@ trait DepositValidation
$account->accountType = $accountType;
$this->source = $account;
}
return $result ?? false;
}
}

View File

@@ -33,13 +33,6 @@ use Log;
*/
trait OBValidation
{
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param int|null $accountId
* @param $accountName
@@ -56,7 +49,7 @@ trait OBValidation
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL we return false,
// because the destination of a deposit can't be created.
$this->destError = (string) trans('validation.ob_dest_need_data');
$this->destError = (string)trans('validation.ob_dest_need_data');
Log::error('Both values are NULL, cant create OB destination.');
$result = false;
}
@@ -68,10 +61,10 @@ trait OBValidation
if (null === $result) {
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int) $accountId, (string) $accountName);
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
if (null === $search) {
Log::debug('findExistingAccount() returned NULL, so the result is false.', $validTypes);
$this->destError = (string) trans('validation.ob_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
$this->destError = (string)trans('validation.ob_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
$result = false;
}
if (null !== $search) {
@@ -86,6 +79,13 @@ 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.
@@ -107,7 +107,7 @@ trait OBValidation
// if both values are NULL return false,
// because the source of a deposit can't be created.
// (this never happens).
$this->sourceError = (string) trans('validation.ob_source_need_data');
$this->sourceError = (string)trans('validation.ob_source_need_data');
$result = false;
}
@@ -143,6 +143,7 @@ trait OBValidation
$account->accountType = $accountType;
$this->source = $account;
}
return $result ?? false;
}
}

View File

@@ -24,8 +24,8 @@ declare(strict_types=1);
namespace FireflyIII\Validation\Account;
use Log;
use FireflyIII\Models\AccountType;
use Log;
/**
* Trait ReconciliationValidation
@@ -89,20 +89,24 @@ trait ReconciliationValidation
Log::debug('In validateReconciliationSource');
if (null === $accountId) {
Log::debug('Return FALSE');
return false;
}
$result = $this->accountRepository->findNull($accountId);
$types = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE, AccountType::RECONCILIATION];
if (null === $result) {
Log::debug('Return FALSE');
return false;
}
if (in_array($result->accountType->type, $types, true)) {
$this->source = $result;
Log::debug('Return TRUE');
return true;
}
Log::debug('Return FALSE');
return false;
}

View File

@@ -31,22 +31,6 @@ use Log;
*/
trait TransferValidation
{
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param int $accountId
* @param string $accountName
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account;
/**
* @param int|null $accountId
* @param $accountName
@@ -61,16 +45,16 @@ trait TransferValidation
if (null === $accountId && null === $accountName && 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');
$this->destError = (string)trans('validation.transfer_dest_need_data');
Log::error('Both values are NULL, cant create transfer destination.');
return false;
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int) $accountId, (string) $accountName);
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
if (null === $search) {
$this->destError = (string) trans('validation.transfer_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
$this->destError = (string)trans('validation.transfer_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
return false;
}
@@ -87,6 +71,22 @@ trait TransferValidation
return true;
}
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param int $accountId
* @param string $accountName
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account;
/**
* @param int|null $accountId
* @param string|null $accountName
@@ -101,16 +101,16 @@ trait TransferValidation
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
// 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');
$this->sourceError = (string)trans('validation.transfer_source_need_data');
Log::warning('Not a valid source, need more data.');
return false;
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int) $accountId, (string) $accountName);
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
if (null === $search) {
$this->sourceError = (string) trans('validation.transfer_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
$this->sourceError = (string)trans('validation.transfer_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
Log::warning('Not a valid source, cant find it.', $validTypes);
return false;

View File

@@ -33,20 +33,38 @@ use Log;
trait WithdrawalValidation
{
/**
* @param array $accountTypes
* @param int|null $accountId
* @param string|null $accountName
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
protected function validateGenericSource(?int $accountId, ?string $accountName): bool
{
Log::debug(sprintf('Now in validateGenericSource(%d, "%s")', $accountId, $accountName));
// source can be any of the following types.
$validTypes = [AccountType::ASSET, AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL we return TRUE
// because we assume the user doesnt want to submit / change anything.
$this->sourceError = (string)trans('validation.withdrawal_source_need_data');
Log::warning('Not a valid source. Need more data.');
/**
* @param array $validTypes
* @param int $accountId
* @param string $accountName
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account;
return false;
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
if (null === $search) {
$this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
Log::warning('Not a valid source. Cant find it.', $validTypes);
return false;
}
$this->source = $search;
Log::debug('Valid source account!');
return true;
}
/**
* @param int|null $accountId
@@ -62,7 +80,7 @@ trait WithdrawalValidation
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL return false,
// because the destination of a withdrawal can never be created automatically.
$this->destError = (string) trans('validation.withdrawal_dest_need_data');
$this->destError = (string)trans('validation.withdrawal_dest_need_data');
return false;
}
@@ -75,7 +93,7 @@ trait WithdrawalValidation
if (in_array($type, $validTypes, true)) {
return true;
}
$this->destError = (string) trans('validation.withdrawal_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
$this->destError = (string)trans('validation.withdrawal_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
return false;
}
@@ -85,6 +103,13 @@ trait WithdrawalValidation
return true === $this->canCreateTypes($validTypes);
}
/**
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param int|null $accountId
* @param string|null $accountName
@@ -99,16 +124,16 @@ trait WithdrawalValidation
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL we return false,
// because the source of a withdrawal can't be created.
$this->sourceError = (string) trans('validation.withdrawal_source_need_data');
$this->sourceError = (string)trans('validation.withdrawal_source_need_data');
Log::warning('Not a valid source. Need more data.');
return false;
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int) $accountId, (string) $accountName);
$search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName);
if (null === $search) {
$this->sourceError = (string) trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
$this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
Log::warning('Not a valid source. Cant find it.', $validTypes);
return false;
@@ -120,36 +145,11 @@ trait WithdrawalValidation
}
/**
* @param int|null $accountId
* @param string|null $accountName
* @param array $validTypes
* @param int $accountId
* @param string $accountName
*
* @return bool
* @return Account|null
*/
protected function validateGenericSource(?int $accountId, ?string $accountName): bool
{
Log::debug(sprintf('Now in validateGenericSource(%d, "%s")', $accountId, $accountName));
// source can be any of the following types.
$validTypes = [AccountType::ASSET, AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL we return TRUE
// because we assume the user doesnt want to submit / change anything.
$this->sourceError = (string) trans('validation.withdrawal_source_need_data');
Log::warning('Not a valid source. Need more data.');
return false;
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, (int) $accountId, (string) $accountName);
if (null === $search) {
$this->sourceError = (string) trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
Log::warning('Not a valid source. Cant find it.', $validTypes);
return false;
}
$this->source = $search;
Log::debug('Valid source account!');
return true;
}
abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account;
}