Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -24,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\Bill;
@@ -39,14 +38,12 @@ use Illuminate\Contracts\Validation\ValidationRule;
*/
class BelongsUser implements ValidationRule
{
/**
* @inheritDoc
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$attribute = $this->parseAttribute($attribute);
if (!auth()->check()) {
$fail('validation.belongs_user')->translate();
return;
}
app('log')->debug(sprintf('Going to validate %s', $attribute));
@@ -68,11 +65,32 @@ class BelongsUser implements ValidationRule
}
}
/**
* @param string $attribute
*
* @return string
*/
protected function countField(string $class, string $field, string $value): int
{
$value = trim($value);
$objects = [];
// get all objects belonging to user:
if (PiggyBank::class === $class) {
$objects = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
->where('accounts.user_id', '=', auth()->user()->id)->get(['piggy_banks.*'])
;
}
if (PiggyBank::class !== $class) {
$objects = $class::where('user_id', '=', auth()->user()->id)->get();
}
$count = 0;
foreach ($objects as $object) {
$objectValue = trim((string)$object->{$field}); // @phpstan-ignore-line
app('log')->debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
if ($objectValue === $value) {
++$count;
app('log')->debug(sprintf('Hit! Count is now %d', $count));
}
}
return $count;
}
private function parseAttribute(string $attribute): string
{
$parts = explode('.', $attribute);
@@ -86,25 +104,16 @@ class BelongsUser implements ValidationRule
return $attribute;
}
/**
* @param int $value
*
* @return bool
*/
private function validatePiggyBankId(int $value): bool
{
$count = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
->where('piggy_banks.id', '=', $value)
->where('accounts.user_id', '=', auth()->user()->id)->count();
->where('piggy_banks.id', '=', $value)
->where('accounts.user_id', '=', auth()->user()->id)->count()
;
return 1 === $count;
}
/**
* @param string $value
*
* @return bool
*/
private function validatePiggyBankName(string $value): bool
{
$count = $this->countField(PiggyBank::class, 'name', $value);
@@ -112,44 +121,6 @@ class BelongsUser implements ValidationRule
return 1 === $count;
}
/**
* @param string $class
* @param string $field
* @param string $value
*
* @return int
*
*/
protected function countField(string $class, string $field, string $value): int
{
$value = trim($value);
$objects = [];
// get all objects belonging to user:
if (PiggyBank::class === $class) {
$objects = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
->where('accounts.user_id', '=', auth()->user()->id)->get(['piggy_banks.*']);
}
if (PiggyBank::class !== $class) {
$objects = $class::where('user_id', '=', auth()->user()->id)->get();
}
$count = 0;
foreach ($objects as $object) {
$objectValue = trim((string)$object->$field); // @phpstan-ignore-line
app('log')->debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
if ($objectValue === $value) {
$count++;
app('log')->debug(sprintf('Hit! Count is now %d', $count));
}
}
return $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateBillId(int $value): bool
{
if (0 === $value) {
@@ -160,11 +131,6 @@ class BelongsUser implements ValidationRule
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateJournalId(int $value): bool
{
if (0 === $value) {
@@ -175,11 +141,6 @@ class BelongsUser implements ValidationRule
return 1 === $count;
}
/**
* @param string $value
*
* @return bool
*/
private function validateBillName(string $value): bool
{
$count = $this->countField(Bill::class, 'name', $value);
@@ -188,11 +149,6 @@ class BelongsUser implements ValidationRule
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateBudgetId(int $value): bool
{
if (0 === $value) {
@@ -203,11 +159,6 @@ class BelongsUser implements ValidationRule
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateCategoryId(int $value): bool
{
$count = Category::where('id', '=', $value)->where('user_id', '=', auth()->user()->id)->count();
@@ -215,11 +166,6 @@ class BelongsUser implements ValidationRule
return 1 === $count;
}
/**
* @param string $value
*
* @return bool
*/
private function validateBudgetName(string $value): bool
{
$count = $this->countField(Budget::class, 'name', $value);
@@ -227,11 +173,6 @@ class BelongsUser implements ValidationRule
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateAccountId(int $value): bool
{
if (0 === $value) {

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\Bill;
@@ -46,22 +45,18 @@ class BelongsUserGroup implements ValidationRule
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct(UserGroup $userGroup)
{
$this->userGroup = $userGroup;
}
/**
* @inheritDoc
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$attribute = $this->parseAttribute($attribute);
if (!auth()->check()) {
$fail('validation.belongs_user_or_user_group')->translate();
return;
}
app('log')->debug(sprintf('Group: Going to validate "%s"', $attribute));
@@ -83,11 +78,32 @@ class BelongsUserGroup implements ValidationRule
}
}
/**
* @param string $attribute
*
* @return string
*/
protected function countField(string $class, string $field, string $value): int
{
$value = trim($value);
$objects = [];
// get all objects belonging to user:
if (PiggyBank::class === $class) {
$objects = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
->where('accounts.user_group_id', '=', $this->userGroup->id)->get(['piggy_banks.*'])
;
}
if (PiggyBank::class !== $class) {
$objects = $class::where('user_group_id', '=', $this->userGroup->id)->get();
}
$count = 0;
foreach ($objects as $object) {
$objectValue = trim((string)$object->{$field}); // @phpstan-ignore-line
app('log')->debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
if ($objectValue === $value) {
++$count;
app('log')->debug(sprintf('Hit! Count is now %d', $count));
}
}
return $count;
}
private function parseAttribute(string $attribute): string
{
$parts = explode('.', $attribute);
@@ -101,25 +117,16 @@ class BelongsUserGroup implements ValidationRule
return $attribute;
}
/**
* @param int $value
*
* @return bool
*/
private function validatePiggyBankId(int $value): bool
{
$count = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
->where('piggy_banks.id', '=', $value)
->where('accounts.user_group_id', '=', $this->userGroup->id)->count();
->where('piggy_banks.id', '=', $value)
->where('accounts.user_group_id', '=', $this->userGroup->id)->count()
;
return 1 === $count;
}
/**
* @param string $value
*
* @return bool
*/
private function validatePiggyBankName(string $value): bool
{
$count = $this->countField(PiggyBank::class, 'name', $value);
@@ -127,44 +134,6 @@ class BelongsUserGroup implements ValidationRule
return 1 === $count;
}
/**
* @param string $class
* @param string $field
* @param string $value
*
* @return int
*
*/
protected function countField(string $class, string $field, string $value): int
{
$value = trim($value);
$objects = [];
// get all objects belonging to user:
if (PiggyBank::class === $class) {
$objects = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')
->where('accounts.user_group_id', '=', $this->userGroup->id)->get(['piggy_banks.*']);
}
if (PiggyBank::class !== $class) {
$objects = $class::where('user_group_id', '=', $this->userGroup->id)->get();
}
$count = 0;
foreach ($objects as $object) {
$objectValue = trim((string)$object->$field); // @phpstan-ignore-line
app('log')->debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
if ($objectValue === $value) {
$count++;
app('log')->debug(sprintf('Hit! Count is now %d', $count));
}
}
return $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateBillId(int $value): bool
{
if (0 === $value) {
@@ -175,11 +144,6 @@ class BelongsUserGroup implements ValidationRule
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateJournalId(int $value): bool
{
if (0 === $value) {
@@ -190,11 +154,6 @@ class BelongsUserGroup implements ValidationRule
return 1 === $count;
}
/**
* @param string $value
*
* @return bool
*/
private function validateBillName(string $value): bool
{
$count = $this->countField(Bill::class, 'name', $value);
@@ -203,11 +162,6 @@ class BelongsUserGroup implements ValidationRule
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateBudgetId(int $value): bool
{
if (0 === $value) {
@@ -218,11 +172,6 @@ class BelongsUserGroup implements ValidationRule
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateCategoryId(int $value): bool
{
$count = Category::where('id', '=', $value)->where('user_group_id', '=', $this->userGroup->id)->count();
@@ -230,11 +179,6 @@ class BelongsUserGroup implements ValidationRule
return 1 === $count;
}
/**
* @param string $value
*
* @return bool
*/
private function validateBudgetName(string $value): bool
{
$count = $this->countField(Budget::class, 'name', $value);
@@ -242,11 +186,6 @@ class BelongsUserGroup implements ValidationRule
return 1 === $count;
}
/**
* @param int $value
*
* @return bool
*/
private function validateAccountId(int $value): bool
{
if (0 === $value) {

View File

@@ -23,36 +23,30 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Illuminate\Contracts\Validation\ValidationRule;
/**
*
* Class IsAssetAccountId
*/
class IsAssetAccountId implements ValidationRule
{
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$accountId = (int)$value;
/** @var Account|null $account */
/** @var null|Account $account */
$account = Account::with('accountType')->find($accountId);
if (null === $account) {
$fail('validation.no_asset_account')->translate();
return;
}
if ($account->accountType->type !== AccountType::ASSET && $account->accountType->type !== AccountType::DEFAULT) {
if (AccountType::ASSET !== $account->accountType->type && AccountType::DEFAULT !== $account->accountType->type) {
$fail('validation.no_asset_account')->translate();
}
}

View File

@@ -24,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
@@ -33,15 +32,9 @@ use Illuminate\Contracts\Validation\ValidationRule;
class IsBoolean implements ValidationRule
{
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
if (is_bool($value)) {
return;

View File

@@ -27,7 +27,6 @@ namespace FireflyIII\Rules;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
@@ -36,19 +35,14 @@ use Illuminate\Contracts\Validation\ValidationRule;
class IsDateOrTime implements ValidationRule
{
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$value = (string)$value;
if ('' === $value) {
$fail('validation.date_or_time')->translate();
return;
}
if (10 === strlen($value)) {
@@ -59,16 +53,19 @@ class IsDateOrTime implements ValidationRule
app('log')->error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
$fail('validation.date_or_time')->translate();
return;
} catch (InvalidFormatException $e) { // @phpstan-ignore-line
app('log')->error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage()));
$fail('validation.date_or_time')->translate();
return;
}
return;
}
// is an atom string, I hope?
try {
Carbon::parse($value);
@@ -76,11 +73,13 @@ class IsDateOrTime implements ValidationRule
app('log')->error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
$fail('validation.date_or_time')->translate();
return;
} catch (InvalidFormatException $e) {
app('log')->error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage()));
$fail('validation.date_or_time')->translate();
return;
}
}

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
@@ -35,10 +34,7 @@ class IsDuplicateTransaction implements ValidationRule
{
private string $value;
/**
* @inheritDoc
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$this->value = $value;

View File

@@ -24,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use FireflyIII\Models\TransactionType;
use FireflyIII\Validation\AccountValidator;
use Illuminate\Contracts\Validation\ValidationRule;
@@ -35,30 +34,25 @@ use Illuminate\Contracts\Validation\ValidationRule;
class IsTransferAccount implements ValidationRule
{
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
app('log')->debug(sprintf('Now in %s(%s)', __METHOD__, $value));
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator->setTransactionType(TransactionType::TRANSFER);
$validator->setUser(auth()->user());
$validAccount = $validator->validateSource(['name' => (string)$value,]);
$validAccount = $validator->validateSource(['name' => (string)$value]);
if (true === $validAccount) {
app('log')->debug('Found account based on name. Return true.');
// found by name, use repos to return.
return;
}
$validAccount = $validator->validateSource(['id' => (int)$value,]);
$validAccount = $validator->validateSource(['id' => (int)$value]);
app('log')->debug(sprintf('Search by id (%d), result is %s.', (int)$value, var_export($validAccount, true)));
if (false === $validAccount) {

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use FireflyIII\Models\Account;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget;
@@ -47,14 +46,10 @@ use Illuminate\Contracts\Validation\ValidationRule;
*/
class IsValidAttachmentModel implements ValidationRule
{
/** @var string */
private string $model;
/**
* IsValidAttachmentModel constructor.
*
*
* @param string $model
*/
public function __construct(string $model)
{
@@ -63,32 +58,13 @@ class IsValidAttachmentModel implements ValidationRule
}
/**
* @param string $model
*
* @return string
*/
private function normalizeModel(string $model): string
{
$search = ['FireflyIII\Models\\'];
$replace = '';
$model = str_replace($search, $replace, $model);
return sprintf('FireflyIII\Models\%s', $model);
}
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
if (!auth()->check()) {
$fail('validation.model_id_invalid')->translate();
return;
}
$result = match ($this->model) {
@@ -108,11 +84,15 @@ class IsValidAttachmentModel implements ValidationRule
}
}
/**
* @param int $value
*
* @return bool
*/
private function normalizeModel(string $model): string
{
$search = ['FireflyIII\Models\\'];
$replace = '';
$model = str_replace($search, $replace, $model);
return sprintf('FireflyIII\Models\%s', $model);
}
private function validateAccount(int $value): bool
{
/** @var AccountRepositoryInterface $repository */
@@ -122,11 +102,6 @@ class IsValidAttachmentModel implements ValidationRule
return null !== $repository->find($value);
}
/**
* @param int $value
*
* @return bool
*/
private function validateBill(int $value): bool
{
/** @var BillRepositoryInterface $repository */
@@ -136,11 +111,6 @@ class IsValidAttachmentModel implements ValidationRule
return null !== $repository->find($value);
}
/**
* @param int $value
*
* @return bool
*/
private function validateBudget(int $value): bool
{
/** @var BudgetRepositoryInterface $repository */
@@ -150,11 +120,6 @@ class IsValidAttachmentModel implements ValidationRule
return null !== $repository->find($value);
}
/**
* @param int $value
*
* @return bool
*/
private function validateCategory(int $value): bool
{
/** @var CategoryRepositoryInterface $repository */
@@ -164,11 +129,6 @@ class IsValidAttachmentModel implements ValidationRule
return null !== $repository->find($value);
}
/**
* @param int $value
*
* @return bool
*/
private function validatePiggyBank(int $value): bool
{
/** @var PiggyBankRepositoryInterface $repository */
@@ -178,11 +138,6 @@ class IsValidAttachmentModel implements ValidationRule
return null !== $repository->find($value);
}
/**
* @param int $value
*
* @return bool
*/
private function validateTag(int $value): bool
{
/** @var TagRepositoryInterface $repository */
@@ -192,11 +147,6 @@ class IsValidAttachmentModel implements ValidationRule
return null !== $repository->find($value);
}
/**
* @param int $value
*
* @return bool
*/
private function validateTransaction(int $value): bool
{
/** @var JournalAPIRepositoryInterface $repository */
@@ -206,11 +156,6 @@ class IsValidAttachmentModel implements ValidationRule
return null !== $repository->findTransaction($value);
}
/**
* @param int $value
*
* @return bool
*/
private function validateJournal(int $value): bool
{
$repository = app(JournalRepositoryInterface::class);

View File

@@ -24,10 +24,8 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Validator;
use JsonException;
/**
* Class IsValidBulkClause
@@ -37,33 +35,21 @@ class IsValidBulkClause implements ValidationRule
private string $error;
private array $rules;
/**
* @param string $type
*/
public function __construct(string $type)
{
$this->rules = config(sprintf('bulk.%s', $type));
$this->error = (string)trans('firefly.belongs_user');
}
/**
* @return string
*/
public function message(): string
{
return $this->error;
}
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$result = $this->basicValidation((string)$value);
if (false === $result) {
@@ -73,16 +59,12 @@ class IsValidBulkClause implements ValidationRule
/**
* Does basic rule based validation.
*
* @param string $value
*
* @return bool
*/
private function basicValidation(string $value): bool
{
try {
$array = json_decode($value, true, 8, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
} catch (\JsonException $e) {
$this->error = (string)trans('validation.json');
return false;
@@ -94,6 +76,7 @@ class IsValidBulkClause implements ValidationRule
return false;
}
/**
* @var string $arrayKey
* @var mixed $arrayValue
@@ -109,7 +92,7 @@ class IsValidBulkClause implements ValidationRule
'value' => $this->rules[$clause][$arrayKey],
]);
if ($validator->fails()) {
$this->error = sprintf('%s: %s: %s', $clause, $arrayKey, implode(', ', ($validator->errors()->get('value'))));
$this->error = sprintf('%s: %s: %s', $clause, $arrayKey, implode(', ', $validator->errors()->get('value')));
return false;
}

View File

@@ -24,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
@@ -34,8 +33,6 @@ class LessThanPiggyTarget implements ValidationRule
{
/**
* Get the validation error message.
*
* @return string
*/
public function message(): string
{
@@ -43,15 +40,9 @@ class LessThanPiggyTarget implements ValidationRule
}
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
// TODO not sure if this is still used.
}

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
@@ -39,10 +38,6 @@ class UniqueAccountNumber implements ValidationRule
/**
* Create a new rule instance.
*
*
* @param Account|null $account
* @param string|null $expectedType
*/
public function __construct(?Account $account, ?string $expectedType)
{
@@ -64,9 +59,6 @@ class UniqueAccountNumber implements ValidationRule
/**
* Get the validation error message.
*
*
* @return string
*/
public function message(): string
{
@@ -74,15 +66,9 @@ class UniqueAccountNumber implements ValidationRule
}
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
if (!auth()->check()) {
return;
@@ -107,16 +93,13 @@ class UniqueAccountNumber implements ValidationRule
);
$fail('validation.unique_account_number_for_user')->translate();
return;
}
}
app('log')->debug('Account number is valid.');
}
/**
* @return array
*
*/
private function getMaxOccurrences(): array
{
$maxCounts = [
@@ -139,20 +122,15 @@ class UniqueAccountNumber implements ValidationRule
return $maxCounts;
}
/**
* @param string $type
* @param string $accountNumber
*
* @return int
*/
private function countHits(string $type, string $accountNumber): int
{
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('accounts.user_id', auth()->user()->id)
->where('account_types.type', $type)
->where('account_meta.name', '=', 'account_number')
->where('account_meta.data', json_encode($accountNumber));
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('accounts.user_id', auth()->user()->id)
->where('account_types.type', $type)
->where('account_meta.name', '=', 'account_number')
->where('account_meta.data', json_encode($accountNumber))
;
if (null !== $this->account) {
$query->where('accounts.id', '!=', $this->account->id);

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use Illuminate\Contracts\Validation\ValidationRule;
@@ -38,10 +37,6 @@ class UniqueIban implements ValidationRule
/**
* Create a new rule instance.
*
*
* @param Account|null $account
* @param string|null $expectedType
*/
public function __construct(?Account $account, ?string $expectedType)
{
@@ -68,19 +63,13 @@ class UniqueIban implements ValidationRule
/**
* Get the validation error message.
*
*
* @return string
*/
public function message(): string
{
return (string)trans('validation.unique_iban_for_user');
}
/**
* @inheritDoc
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
if (!$this->passes($attribute, $value)) {
$fail((string)trans('validation.unique_iban_for_user'));
@@ -93,10 +82,7 @@ class UniqueIban implements ValidationRule
* @param string $attribute
* @param mixed $value
*
* @return bool
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
*/
public function passes($attribute, $value): bool
{
@@ -129,10 +115,6 @@ class UniqueIban implements ValidationRule
return true;
}
/**
* @return array
*
*/
private function getMaxOccurrences(): array
{
$maxCounts = [
@@ -156,12 +138,6 @@ class UniqueIban implements ValidationRule
return $maxCounts;
}
/**
* @param string $type
* @param string $iban
*
* @return int
*/
private function countHits(string $type, string $iban): int
{
$typesArray = [$type];
@@ -170,10 +146,11 @@ class UniqueIban implements ValidationRule
}
$query
= auth()->user()
->accounts()
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('accounts.iban', $iban)
->whereIn('account_types.type', $typesArray);
->accounts()
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('accounts.iban', $iban)
->whereIn('account_types.type', $typesArray)
;
if (null !== $this->account) {
$query->where('accounts.id', '!=', $this->account->id);

View File

@@ -24,27 +24,18 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class ValidJournals
*
*/
class ValidJournals implements ValidationRule
{
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
app('log')->debug('In ValidJournals::passes');
if (!is_array($value)) {
@@ -57,6 +48,7 @@ class ValidJournals implements ValidationRule
app('log')->debug(sprintf('Count for transaction #%d and user #%d is zero! Return FALSE', $journalId, $userId));
$fail('validation.invalid_selection')->translate();
return;
}
}

View File

@@ -23,33 +23,26 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class ValidRecurrenceRepetitionType
*
*/
class ValidRecurrenceRepetitionType implements ValidationRule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$value = (string)$value;
if ('daily' === $value) {
return;
}
//monthly,17
//ndom,3,7
// monthly,17
// ndom,3,7
if (in_array(substr($value, 0, 6), ['yearly', 'weekly'], true)) {
return;
}

View File

@@ -24,27 +24,17 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Carbon\Carbon;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use InvalidArgumentException;
/**
* Class ValidRecurrenceRepetitionValue
*
*/
class ValidRecurrenceRepetitionValue implements ValidationRule
{
/**
* @param string $attribute
* @param mixed $value
* @param Closure $fail
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
$value = (string)$value;
@@ -75,11 +65,6 @@ class ValidRecurrenceRepetitionValue implements ValidationRule
$fail('validation.valid_recurrence_rep_type')->translate();
}
/**
* @param string $value
*
* @return bool
*/
private function validateMonthly(string $value): bool
{
$dayOfMonth = (int)substr($value, 8);
@@ -87,12 +72,6 @@ class ValidRecurrenceRepetitionValue implements ValidationRule
return $dayOfMonth > 0 && $dayOfMonth < 32;
}
/**
* @param string $value
*
* @return bool
*
*/
private function validateNdom(string $value): bool
{
$parameters = explode(',', substr($value, 5));
@@ -108,11 +87,6 @@ class ValidRecurrenceRepetitionValue implements ValidationRule
return $dayOfWeek > 0 && $dayOfWeek < 8;
}
/**
* @param string $value
*
* @return bool
*/
private function validateWeekly(string $value): bool
{
$dayOfWeek = (int)substr($value, 7);
@@ -120,18 +94,14 @@ class ValidRecurrenceRepetitionValue implements ValidationRule
return $dayOfWeek > 0 && $dayOfWeek < 8;
}
/**
* @param string $value
*
* @return bool
*/
private function validateYearly(string $value): bool
{
// rest of the string must be valid date:
$dateString = substr($value, 7);
try {
Carbon::createFromFormat('Y-m-d', $dateString);
} catch (InvalidArgumentException $e) { // @phpstan-ignore-line
} catch (\InvalidArgumentException $e) { // @phpstan-ignore-line
app('log')->debug(sprintf('Could not parse date %s: %s', $dateString, $e->getMessage()));
return false;