Skip to content

Firefly III: a personal finances manager

Refactor phpstan errors.

James Cole12675dd9505a
diff --git a/.ci/phpstan.neon b/.ci/phpstan.neon
index 13505b56ef..b9701c47a8 100644
--- a/.ci/phpstan.neon
+++ b/.ci/phpstan.neon
@@ -5,14 +5,21 @@ includes:
- ../vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
parameters:
+ universalObjectCratesClasses:
+ - Illuminate\Database\Eloquent\Model
+ reportUnmatchedIgnoredErrors: false
ignoreErrors:
- '#is not allowed to extend#'
- '#is neither abstract nor final#'
- '#has a nullable return type declaration#'
- '#with a nullable type declaration#'
- '#with null as default value#'
- - 'No error to ignore is reported on#'
- '#is not covariant with PHPDoc type array#'
+ -
+ message: '#Constructor in [a-zA-Z0-9\\_]+ has parameter \$[a-zA-Z0-9\\_]+ with default value#'
+ paths:
+ - ../app/Exceptions/IntervalException.php
+ - ../app/Support/Navigation.php
-
message: '#but containers should not be injected#'
paths:
@@ -67,5 +74,5 @@ parameters:
- ../bootstrap/app.php
# The level 8 is the highest level. original was 5
- level: 1
+ level: 2
diff --git a/app/Api/V2/Controllers/Model/Budget/ShowController.php b/app/Api/V2/Controllers/Model/Budget/ShowController.php
index b3e1a3805b..fc43498b0b 100644
--- a/app/Api/V2/Controllers/Model/Budget/ShowController.php
+++ b/app/Api/V2/Controllers/Model/Budget/ShowController.php
@@ -38,8 +38,6 @@ use Illuminate\Http\JsonResponse;
*/
class ShowController extends Controller
{
- use ConvertsExchangeRates;
-
private BudgetRepositoryInterface $repository;
/**
@@ -69,7 +67,6 @@ class ShowController extends Controller
{
$data = $request->getAll();
$result = $this->repository->budgetedInPeriodForBudget($budget, $data['start'], $data['end']);
- //$converted = $this->cerSum(array_values($result));
return response()->json($result);
}
@@ -83,7 +80,6 @@ class ShowController extends Controller
{
$data = $request->getAll();
$result = $this->repository->spentInPeriodForBudget($budget, $data['start'], $data['end']);
- // $converted = $this->cerSum(array_values($result));
return response()->json($result);
}
diff --git a/app/Api/V2/Controllers/Model/Budget/SumController.php b/app/Api/V2/Controllers/Model/Budget/SumController.php
index 9f5c8f1ff6..21a3ffa6bc 100644
--- a/app/Api/V2/Controllers/Model/Budget/SumController.php
+++ b/app/Api/V2/Controllers/Model/Budget/SumController.php
@@ -36,8 +36,6 @@ use Illuminate\Http\JsonResponse;
*/
class SumController extends Controller
{
- use ConvertsExchangeRates;
-
private BudgetRepositoryInterface $repository;
/**
@@ -65,12 +63,10 @@ class SumController extends Controller
*/
public function budgeted(DateRequest $request): JsonResponse
{
- throw new FireflyException('Needs refactoring, uses deprecated method.');
$data = $request->getAll();
$result = $this->repository->budgetedInPeriod($data['start'], $data['end']);
- $converted = $this->cerSum(array_values($result));
- return response()->json($converted);
+ return response()->json($result);
}
/**
@@ -83,11 +79,9 @@ class SumController extends Controller
*/
public function spent(DateRequest $request): JsonResponse
{
- throw new FireflyException('Needs refactoring, uses deprecated method.');
$data = $request->getAll();
$result = $this->repository->spentInPeriod($data['start'], $data['end']);
- $converted = $this->cerSum(array_values($result));
- return response()->json($converted);
+ return response()->json($result);
}
}
diff --git a/app/Api/V2/Controllers/Summary/NetWorthController.php b/app/Api/V2/Controllers/Summary/NetWorthController.php
index 6cb10ff38b..1519926d1d 100644
--- a/app/Api/V2/Controllers/Summary/NetWorthController.php
+++ b/app/Api/V2/Controllers/Summary/NetWorthController.php
@@ -40,7 +40,6 @@ use Illuminate\Http\JsonResponse;
class NetWorthController extends Controller
{
use ValidatesUserGroupTrait;
- use ConvertsExchangeRates;
private NetWorthInterface $netWorth;
private AccountRepositoryInterface $repository;
diff --git a/app/Console/Commands/Correction/CorrectAmounts.php b/app/Console/Commands/Correction/CorrectAmounts.php
index a35b941aaa..e1055e7f2b 100644
--- a/app/Console/Commands/Correction/CorrectAmounts.php
+++ b/app/Console/Commands/Correction/CorrectAmounts.php
@@ -167,7 +167,7 @@ class CorrectAmounts extends Command
return;
}
- /** @var BudgetLimit $item */
+ /** @var CurrencyExchangeRate $item */
foreach ($set as $item) {
$item->rate = app('steam')->positive((string)$item->rate);
$item->save();
@@ -207,7 +207,7 @@ class CorrectAmounts extends Command
return;
}
- /** @var PiggyBankRepetition $item */
+ /** @var PiggyBank $item */
foreach ($set as $item) {
$item->targetamount = app('steam')->positive((string)$item->targetamount);
$item->save();
@@ -229,7 +229,7 @@ class CorrectAmounts extends Command
return;
}
- /** @var PiggyBankRepetition $item */
+ /** @var RecurrenceTransaction $item */
foreach ($set as $item) {
$item->amount = app('steam')->positive((string)$item->amount);
$item->foreign_amount = app('steam')->positive((string)$item->foreign_amount);
diff --git a/app/Console/Commands/System/ForceDecimalSize.php b/app/Console/Commands/System/ForceDecimalSize.php
index e1f8237a33..590cb05cb3 100644
--- a/app/Console/Commands/System/ForceDecimalSize.php
+++ b/app/Console/Commands/System/ForceDecimalSize.php
@@ -506,8 +506,8 @@ class ForceDecimalSize extends Command
continue;
}
// fix $field by rounding it down correctly.
- $pow = pow(10, (int)$currency->decimal_places);
- $correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
+ $pow = (float)pow(10, (int)$currency->decimal_places);
+ $correct = bcdiv((string)round((float)$value * $pow), (string)$pow, 12);
$this->friendlyWarning(sprintf('Transaction #%d has amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct));
Transaction::find($item->id)->update(['amount' => $correct]);
}
@@ -533,8 +533,8 @@ class ForceDecimalSize extends Command
continue;
}
// fix $field by rounding it down correctly.
- $pow = pow(10, (int)$currency->decimal_places);
- $correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
+ $pow = (float)pow(10, (int)$currency->decimal_places);
+ $correct = bcdiv((string)round((float)$value * $pow), (string)$pow, 12);
$this->friendlyWarning(
sprintf('Transaction #%d has foreign amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct)
);
diff --git a/app/Console/Commands/System/VerifySecurityAlerts.php b/app/Console/Commands/System/VerifySecurityAlerts.php
index 0b08025a60..a181e3db77 100644
--- a/app/Console/Commands/System/VerifySecurityAlerts.php
+++ b/app/Console/Commands/System/VerifySecurityAlerts.php
@@ -27,7 +27,9 @@ namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
+use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\FilesystemException;
+use League\Flysystem\FilesystemReader;
use Storage;
/**
diff --git a/app/Support/Calendar/Exceptions/IntervalException.php b/app/Exceptions/IntervalException.php
similarity index 79%
rename from app/Support/Calendar/Exceptions/IntervalException.php
rename to app/Exceptions/IntervalException.php
index 6192dcc51e..f2307c9783 100644
--- a/app/Support/Calendar/Exceptions/IntervalException.php
+++ b/app/Exceptions/IntervalException.php
@@ -3,7 +3,7 @@
/*
* IntervalException.php
- * Copyright (c) 2023 Antonio Spinelli https://github.com/tonicospinelli
+ * Copyright (c) 2023 [email protected]
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
@@ -23,7 +23,7 @@
declare(strict_types=1);
-namespace FireflyIII\Support\Calendar\Exceptions;
+namespace FireflyIII\Exceptions;
use Exception;
use FireflyIII\Support\Calendar\Periodicity;
@@ -34,9 +34,17 @@ use Throwable;
*/
final class IntervalException extends Exception
{
+
public readonly array $availableIntervals;
public readonly Periodicity $periodicity;
- protected $message = 'The periodicity %s is unknown. Choose one of available periodicity: %s';
+ protected $message = 'The periodicity %s is unknown. Choose one of available periodicity: %s';
+
+ public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
+ {
+ parent::__construct($message, $code, $previous);
+ $this->availableIntervals = [];
+ $this->periodicity = Periodicity::Monthly;
+ }
/**
* @param Periodicity $periodicity
@@ -51,7 +59,8 @@ final class IntervalException extends Exception
array $intervals,
int $code = 0,
?Throwable $previous = null
- ): IntervalException {
+ ): IntervalException
+ {
$message = sprintf(
'The periodicity %s is unknown. Choose one of available periodicity: %s',
$periodicity->name,
diff --git a/app/Helpers/Collector/GroupCollector.php b/app/Helpers/Collector/GroupCollector.php
index 04c55bed2e..5236559e00 100644
--- a/app/Helpers/Collector/GroupCollector.php
+++ b/app/Helpers/Collector/GroupCollector.php
@@ -1107,7 +1107,7 @@ class GroupCollector implements GroupCollectorInterface
/**
* Set the user object and start the query.
*
- * @param User $user
+ * @param UserGroup $userGroup
*
* @return GroupCollectorInterface
*/
diff --git a/app/Http/Middleware/InterestingMessage.php b/app/Http/Middleware/InterestingMessage.php
index da8e0203c9..ba3d5553ee 100644
--- a/app/Http/Middleware/InterestingMessage.php
+++ b/app/Http/Middleware/InterestingMessage.php
@@ -271,6 +271,11 @@ class InterestingMessage
return null !== $code && null !== $message;
}
+ /**
+ * @param Request $request
+ *
+ * @return void
+ */
private function handleCurrencyMessage(Request $request): void
{
// params:
@@ -278,7 +283,7 @@ class InterestingMessage
$code = $request->get('code');
$message = $request->get('message');
- /** @var TransactionCurrency $webhook */
+ /** @var TransactionCurrency $currency */
$currency = TransactionCurrency::whereCode($code)->first();
if (null === $currency) {
diff --git a/app/Jobs/CreateAutoBudgetLimits.php b/app/Jobs/CreateAutoBudgetLimits.php
index 3d36040539..72601de596 100644
--- a/app/Jobs/CreateAutoBudgetLimits.php
+++ b/app/Jobs/CreateAutoBudgetLimits.php
@@ -227,7 +227,7 @@ class CreateAutoBudgetLimits implements ShouldQueue
* @param Carbon $end
* @param string|null $amount
*/
- private function createBudgetLimit(AutoBudget $autoBudget, Carbon $start, Carbon $end, ?string $amount = null)
+ private function createBudgetLimit(AutoBudget $autoBudget, Carbon $start, Carbon $end, ?string $amount = null): void
{
app('log')->debug(sprintf('No budget limit exist. Must create one for auto-budget #%d', $autoBudget->id));
if (null !== $amount) {
diff --git a/app/Models/AccountMeta.php b/app/Models/AccountMeta.php
index 6d7a31aaa2..2cf48336f8 100644
--- a/app/Models/AccountMeta.php
+++ b/app/Models/AccountMeta.php
@@ -78,19 +78,17 @@ class AccountMeta extends Model
/**
* @param mixed $value
*
- * @return mixed
+ * @return string
*/
- public function getDataAttribute($value): string
+ public function getDataAttribute(mixed $value): string
{
return (string)json_decode($value, true);
}
/**
* @param mixed $value
- *
-
*/
- public function setDataAttribute($value): void
+ public function setDataAttribute(mixed $value): void
{
$this->attributes['data'] = json_encode($value);
}
diff --git a/app/Models/PiggyBank.php b/app/Models/PiggyBank.php
index 47c5aa6de0..ced5b6876a 100644
--- a/app/Models/PiggyBank.php
+++ b/app/Models/PiggyBank.php
@@ -30,6 +30,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
+use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
@@ -145,7 +146,7 @@ class PiggyBank extends Model
}
/**
- * Get all of the piggy bank's notes.
+ * Get all the piggy bank's notes.
*/
public function notes(): MorphMany
{
@@ -155,7 +156,7 @@ class PiggyBank extends Model
/**
* Get all the tags for the post.
*/
- public function objectGroups()
+ public function objectGroups(): MorphToMany
{
return $this->morphToMany(ObjectGroup::class, 'object_groupable');
}
diff --git a/app/Repositories/Account/AccountTasker.php b/app/Repositories/Account/AccountTasker.php
index 5bdce52eee..692a89a397 100644
--- a/app/Repositories/Account/AccountTasker.php
+++ b/app/Repositories/Account/AccountTasker.php
@@ -60,7 +60,7 @@ class AccountTasker implements AccountTaskerInterface
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
- $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
+ $defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
$return = [
'accounts' => [],
@@ -160,7 +160,7 @@ class AccountTasker implements AccountTaskerInterface
*/
private function groupExpenseByDestination(array $array): array
{
- $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
+ $defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$currencies = [$defaultCurrency->id => $defaultCurrency,];
@@ -257,7 +257,7 @@ class AccountTasker implements AccountTaskerInterface
*/
private function groupIncomeBySource(array $array): array
{
- $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
+ $defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$currencies = [$defaultCurrency->id => $defaultCurrency,];
diff --git a/app/Repositories/Budget/BudgetLimitRepository.php b/app/Repositories/Budget/BudgetLimitRepository.php
index 06449acf89..09869e9dad 100644
--- a/app/Repositories/Budget/BudgetLimitRepository.php
+++ b/app/Repositories/Budget/BudgetLimitRepository.php
@@ -294,7 +294,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
$factory = app(TransactionCurrencyFactory::class);
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
if (null === $currency) {
- $currency = app('amount')->getDefaultCurrencyByUser($this->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
}
$currency->enabled = true;
$currency->save();
@@ -371,7 +371,7 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
}
// catch unexpected null:
if (null === $currency) {
- $currency = $budgetLimit->transactionCurrency ?? app('amount')->getDefaultCurrencyByUser($this->user);
+ $currency = $budgetLimit->transactionCurrency ?? app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
}
$currency->enabled = true;
$currency->save();
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index 870cb03747..caceb91a35 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -416,7 +416,7 @@ class BudgetRepository implements BudgetRepositoryInterface
$autoBudget = $this->getAutoBudget($budget);
// grab default currency:
- $currency = app('amount')->getDefaultCurrencyByUser($this->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
if (null === $autoBudget) {
// at this point it's a blind assumption auto_budget_type is 1 or 2.
@@ -847,7 +847,7 @@ class BudgetRepository implements BudgetRepositoryInterface
$currency = $repos->findByCode((string)$data['currency_code']);
}
if (null === $currency) {
- $currency = app('amount')->getDefaultCurrencyByUser($this->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
}
$autoBudget = new AutoBudget();
diff --git a/app/Repositories/Budget/OperationsRepository.php b/app/Repositories/Budget/OperationsRepository.php
index 1ae40881fe..61b646f9f0 100644
--- a/app/Repositories/Budget/OperationsRepository.php
+++ b/app/Repositories/Budget/OperationsRepository.php
@@ -220,67 +220,6 @@ class OperationsRepository implements OperationsRepositoryInterface
return $repos->getActiveBudgets();
}
- /**
- * @param Collection $budgets
- * @param Collection $accounts
- * @param Carbon $start
- * @param Carbon $end
- *
- * @return array
- * @deprecated
- */
- public function spentInPeriodMc(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array
- {
- /** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
- $collector->setUser($this->user);
- $collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation();
-
- if ($accounts->count() > 0) {
- $collector->setAccounts($accounts);
- }
- // TODO possible candidate for "get extracted groups" method.
- $set = $collector->getGroups();
- $return = [];
- $total = [];
- $currencies = [];
- /** @var array $group */
- foreach ($set as $group) {
- /** @var array $transaction */
- foreach ($group['transactions'] as $transaction) {
- $code = $transaction['currency_code'];
- if (!array_key_exists($code, $currencies)) {
- $currencies[$code] = [
- 'id' => $transaction['currency_id'],
- 'decimal_places' => $transaction['currency_decimal_places'],
- 'code' => $transaction['currency_code'],
- 'name' => $transaction['currency_name'],
- 'symbol' => $transaction['currency_symbol'],
- ];
- }
- $total[$code] = array_key_exists($code, $total) ? bcadd($total[$code], $transaction['amount']) : $transaction['amount'];
- }
- }
- /**
- * @var string $code
- * @var string $spent
- */
- foreach ($total as $code => $spent) {
- /** @var TransactionCurrency $currency */
- $currency = $currencies[$code];
- $return[] = [
- 'currency_id' => (string)$currency['id'],
- 'currency_code' => $code,
- 'currency_name' => $currency['name'],
- 'currency_symbol' => $currency['symbol'],
- 'currency_decimal_places' => $currency['decimal_places'],
- 'amount' => app('steam')->bcround($spent, $currency['decimal_places']),
- ];
- }
-
- return $return;
- }
-
/**
* @param Carbon $start
* @param Carbon $end
@@ -289,7 +228,6 @@ class OperationsRepository implements OperationsRepositoryInterface
* @param TransactionCurrency|null $currency
*
* @return array
- * @deprecated
*/
public function sumExpenses(
Carbon $start,
diff --git a/app/Repositories/Budget/OperationsRepositoryInterface.php b/app/Repositories/Budget/OperationsRepositoryInterface.php
index 1e2ab042f8..40348997ec 100644
--- a/app/Repositories/Budget/OperationsRepositoryInterface.php
+++ b/app/Repositories/Budget/OperationsRepositoryInterface.php
@@ -75,29 +75,12 @@ interface OperationsRepositoryInterface
*/
public function setUser(User | Authenticatable | null $user): void;
-
- /**
- * Return multi-currency spent information.
- *
- * @param Collection $budgets
- * @param Collection $accounts
- * @param Carbon $start
- * @param Carbon $end
- *
- * @return array
- * @deprecated
- */
- public function spentInPeriodMc(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array;
-
/**
- * TODO this method was marked as deprecated but I'm not sure why.
- *
* @param Carbon $start
* @param Carbon $end
* @param Collection|null $accounts
* @param Collection|null $budgets
* @param TransactionCurrency|null $currency
- *
* @return array
*
*/
diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php
index 5208191d65..fbda675b2d 100644
--- a/app/Repositories/PiggyBank/PiggyBankRepository.php
+++ b/app/Repositories/PiggyBank/PiggyBankRepository.php
@@ -194,7 +194,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
$accountRepos = app(AccountRepositoryInterface::class);
$accountRepos->setUser($this->user);
- $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
+ $defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
$piggyBankCurrency = $accountRepos->getAccountCurrency($piggyBank->account) ?? $defaultCurrency;
app('log')->debug(sprintf('Piggy bank #%d currency is %s', $piggyBank->id, $piggyBankCurrency->code));
diff --git a/app/Repositories/TransactionGroup/TransactionGroupRepository.php b/app/Repositories/TransactionGroup/TransactionGroupRepository.php
index 46606143ea..4b33a4935c 100644
--- a/app/Repositories/TransactionGroup/TransactionGroupRepository.php
+++ b/app/Repositories/TransactionGroup/TransactionGroupRepository.php
@@ -401,7 +401,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
{
$return = [];
$journals = $group->transactionJournals->pluck('id')->toArray();
- $currency = app('amount')->getDefaultCurrencyByUser($this->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
$data = PiggyBankEvent::whereIn('transaction_journal_id', $journals)
->with('piggyBank', 'piggyBank.account')
->get(['piggy_bank_events.*']);
diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php
index 955869f98f..60242e6edf 100644
--- a/app/Repositories/User/UserRepository.php
+++ b/app/Repositories/User/UserRepository.php
@@ -299,10 +299,12 @@ class UserRepository implements UserRepositoryInterface
if (null === $user) {
return false;
}
- /** @var Role $userRole */
- foreach ($user->roles as $userRole) {
- if ($userRole->name === $role) {
- return true;
+ if ($user instanceof User) {
+ /** @var Role $userRole */
+ foreach ($user->roles as $userRole) {
+ if ($userRole->name === $role) {
+ return true;
+ }
}
}
diff --git a/app/Repositories/UserGroup/UserGroupRepository.php b/app/Repositories/UserGroup/UserGroupRepository.php
index 7be53c78f8..4156df6cc3 100644
--- a/app/Repositories/UserGroup/UserGroupRepository.php
+++ b/app/Repositories/UserGroup/UserGroupRepository.php
@@ -220,7 +220,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface
return $userGroup;
}
// count the number of members in the group right now:
- $membershipCount = $userGroup->groupMemberships()->distinct()->get(['group_memberships.user_id'])->count();
+ $membershipCount = $userGroup->groupMemberships()->distinct()->count(['group_memberships.user_id']);
// if it's 1:
if (1 === $membershipCount) {
diff --git a/app/Repositories/UserGroups/Account/AccountRepositoryInterface.php b/app/Repositories/UserGroups/Account/AccountRepositoryInterface.php
index 379615acbe..600419b790 100644
--- a/app/Repositories/UserGroups/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/UserGroups/Account/AccountRepositoryInterface.php
@@ -27,6 +27,7 @@ namespace FireflyIII\Repositories\UserGroups\Account;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionCurrency;
+use FireflyIII\User;
use Illuminate\Support\Collection;
/**
@@ -34,6 +35,14 @@ use Illuminate\Support\Collection;
*/
interface AccountRepositoryInterface
{
+
+ /**
+ * @param User $user
+ *
+ * @return void
+ */
+ public function setUser(User $user): void;
+
/**
* @param int $accountId
*
diff --git a/app/Repositories/UserGroups/Bill/BillRepositoryInterface.php b/app/Repositories/UserGroups/Bill/BillRepositoryInterface.php
index ba68704938..850aced525 100644
--- a/app/Repositories/UserGroups/Bill/BillRepositoryInterface.php
+++ b/app/Repositories/UserGroups/Bill/BillRepositoryInterface.php
@@ -27,6 +27,7 @@ namespace FireflyIII\Repositories\UserGroups\Bill;
use Carbon\Carbon;
use FireflyIII\Models\Bill;
+use FireflyIII\User;
use Illuminate\Support\Collection;
/**
@@ -34,6 +35,13 @@ use Illuminate\Support\Collection;
*/
interface BillRepositoryInterface
{
+ /**
+ * @param User $user
+ *
+ * @return void
+ */
+ public function setUser(User $user): void;
+
/**
* TODO duplicate of other repos
* Add correct order to bills.
diff --git a/app/Repositories/UserGroups/Budget/AvailableBudgetRepositoryInterface.php b/app/Repositories/UserGroups/Budget/AvailableBudgetRepositoryInterface.php
index 6a24ad24da..9251e44a64 100644
--- a/app/Repositories/UserGroups/Budget/AvailableBudgetRepositoryInterface.php
+++ b/app/Repositories/UserGroups/Budget/AvailableBudgetRepositoryInterface.php
@@ -26,12 +26,19 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroups\Budget;
use Carbon\Carbon;
+use FireflyIII\User;
/**
* Interface AvailableBudgetRepositoryInterface
*/
interface AvailableBudgetRepositoryInterface
{
+ /**
+ * @param User $user
+ *
+ * @return void
+ */
+ public function setUser(User $user): void;
/**
* @param Carbon $start
* @param Carbon $end
diff --git a/app/Repositories/UserGroups/Budget/BudgetRepositoryInterface.php b/app/Repositories/UserGroups/Budget/BudgetRepositoryInterface.php
index 6d9ef85332..7f65743397 100644
--- a/app/Repositories/UserGroups/Budget/BudgetRepositoryInterface.php
+++ b/app/Repositories/UserGroups/Budget/BudgetRepositoryInterface.php
@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroups\Budget;
+use FireflyIII\User;
use Illuminate\Support\Collection;
/**
@@ -32,6 +33,12 @@ use Illuminate\Support\Collection;
*/
interface BudgetRepositoryInterface
{
+ /**
+ * @param User $user
+ *
+ * @return void
+ */
+ public function setUser(User $user): void;
/**
* @return Collection
*/
diff --git a/app/Repositories/UserGroups/Budget/OperationsRepositoryInterface.php b/app/Repositories/UserGroups/Budget/OperationsRepositoryInterface.php
index d6313824dc..32cd90f0cf 100644
--- a/app/Repositories/UserGroups/Budget/OperationsRepositoryInterface.php
+++ b/app/Repositories/UserGroups/Budget/OperationsRepositoryInterface.php
@@ -26,6 +26,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroups\Budget;
use Carbon\Carbon;
+use FireflyIII\User;
use Illuminate\Support\Collection;
/**
@@ -33,6 +34,13 @@ use Illuminate\Support\Collection;
*/
interface OperationsRepositoryInterface
{
+ /**
+ * @param User $user
+ *
+ * @return void
+ */
+ public function setUser(User $user): void;
+
/**
* This method returns a list of all the withdrawal transaction journals (as arrays) set in that period
* which have the specified budget set to them. It's grouped per currency, with as few details in the array
diff --git a/app/Repositories/UserGroups/Currency/CurrencyRepository.php b/app/Repositories/UserGroups/Currency/CurrencyRepository.php
index 355276b13f..e4a84b94e9 100644
--- a/app/Repositories/UserGroups/Currency/CurrencyRepository.php
+++ b/app/Repositories/UserGroups/Currency/CurrencyRepository.php
@@ -262,7 +262,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if (null === $result) {
app('log')->debug('Grabbing default currency for this user...');
- $result = app('amount')->getDefaultCurrencyByUser($this->user);
+ $result = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
}
if (null === $result) {
diff --git a/app/Repositories/UserGroups/Currency/CurrencyRepositoryInterface.php b/app/Repositories/UserGroups/Currency/CurrencyRepositoryInterface.php
index 1ba5fe9667..d6fde4767e 100644
--- a/app/Repositories/UserGroups/Currency/CurrencyRepositoryInterface.php
+++ b/app/Repositories/UserGroups/Currency/CurrencyRepositoryInterface.php
@@ -27,10 +27,17 @@ namespace FireflyIII\Repositories\UserGroups\Currency;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
+use FireflyIII\User;
use Illuminate\Support\Collection;
interface CurrencyRepositoryInterface
{
+ /**
+ * @param User $user
+ *
+ * @return void
+ */
+ public function setUser(User $user): void;
/**
* @param TransactionCurrency $currency
*
diff --git a/app/Repositories/UserGroups/Journal/JournalRepositoryInterface.php b/app/Repositories/UserGroups/Journal/JournalRepositoryInterface.php
index 5a2cd1b938..9396610b22 100644
--- a/app/Repositories/UserGroups/Journal/JournalRepositoryInterface.php
+++ b/app/Repositories/UserGroups/Journal/JournalRepositoryInterface.php
@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroups\Journal;
+use FireflyIII\User;
use Illuminate\Support\Collection;
/**
@@ -30,6 +31,13 @@ use Illuminate\Support\Collection;
*/
interface JournalRepositoryInterface
{
+ /**
+ * @param User $user
+ *
+ * @return void
+ */
+ public function setUser(User $user): void;
+
/**
* Search in journal descriptions.
*
diff --git a/app/Rules/IsAssetAccountId.php b/app/Rules/IsAssetAccountId.php
index 46d2e04e42..1f3104a01f 100644
--- a/app/Rules/IsAssetAccountId.php
+++ b/app/Rules/IsAssetAccountId.php
@@ -23,45 +23,35 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
+use Closure;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
-use Illuminate\Contracts\Validation\Rule;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
*
* Class IsAssetAccountId
*/
-class IsAssetAccountId implements Rule
+class IsAssetAccountId implements ValidationRule
{
/**
- * Get the validation error message. This is not translated because only the API uses it.
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
- * @return string
+ * @return void
*/
- public function message(): string
- {
- return 'This is not an asset account.';
- }
-
- /**
- * Determine if the validation rule passes.
- *
- * @param string $attribute
- * @param mixed $value
- *
- * @return bool
- */
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
$accountId = (int)$value;
- $account = Account::with('accountType')->find($accountId);
+ /** @var Account|null $account */
+ $account = Account::with('accountType')->find($accountId);
if (null === $account) {
- return false;
+ $fail('validation.no_asset_account')->translate();
+ return;
}
if ($account->accountType->type !== AccountType::ASSET && $account->accountType->type !== AccountType::DEFAULT) {
- return false;
+ $fail('validation.no_asset_account')->translate();
}
-
- return true;
}
}
diff --git a/app/Rules/IsBoolean.php b/app/Rules/IsBoolean.php
index 3019b38ad5..22c4cda2b7 100644
--- a/app/Rules/IsBoolean.php
+++ b/app/Rules/IsBoolean.php
@@ -24,46 +24,36 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
-use Illuminate\Contracts\Validation\Rule;
+use Closure;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class IsBoolean
*/
-class IsBoolean implements Rule
+class IsBoolean implements ValidationRule
{
- /**
- * Get the validation error message.
- *
- * @return string
- */
- public function message(): string
- {
- return (string)trans('validation.boolean');
- }
/**
- * Determine if the validation rule passes.
- *
- * @param string $attribute
- * @param mixed $value
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
- * @return bool
+ * @return void
*/
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (is_bool($value)) {
- return true;
+ return;
}
- if (is_int($value) && 0 === $value) {
- return true;
+ if (0 === $value) {
+ return;
}
- if (is_int($value) && 1 === $value) {
- return true;
+ if (1 === $value) {
+ return;
}
- if (is_string($value) && in_array($value, ['0', '1', 'true', 'false', 'on', 'off', 'yes', 'no', 'y', 'n'], true)) {
- return true;
+ if (in_array($value, ['0', '1', 'true', 'false', 'on', 'off', 'yes', 'no', 'y', 'n'], true)) {
+ return;
}
-
- return false;
+ $fail('validation.boolean')->translate();
}
}
diff --git a/app/Rules/IsDateOrTime.php b/app/Rules/IsDateOrTime.php
index 4e78b6a66a..b595cc1005 100644
--- a/app/Rules/IsDateOrTime.php
+++ b/app/Rules/IsDateOrTime.php
@@ -27,36 +27,28 @@ namespace FireflyIII\Rules;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException;
-use Illuminate\Contracts\Validation\Rule;
+use Closure;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class IsDateOrTime
*/
-class IsDateOrTime implements Rule
+class IsDateOrTime implements ValidationRule
{
- /**
- * Get the validation error message.
- *
- * @return string
- */
- public function message()
- {
- return (string)trans('validation.date_or_time');
- }
/**
- * Determine if the validation rule passes.
- *
- * @param string $attribute
- * @param mixed $value
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
- * @return bool
+ * @return void
*/
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
$value = (string)$value;
if ('' === $value) {
- return false;
+ $fail('validation.date_or_time')->translate();;
+ return;
}
if (10 === strlen($value)) {
// probably a date format.
@@ -65,14 +57,16 @@ class IsDateOrTime implements Rule
} catch (InvalidDateException $e) {
app('log')->error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
- return false;
+ $fail('validation.date_or_time')->translate();;
+ return;
} catch (InvalidFormatException $e) {
app('log')->error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage()));
- return false;
+ $fail('validation.date_or_time')->translate();;
+ return;
}
- return true;
+ return;
}
// is an atom string, I hope?
try {
@@ -80,13 +74,13 @@ class IsDateOrTime implements Rule
} catch (InvalidDateException $e) {
app('log')->error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
- return false;
+ $fail('validation.date_or_time')->translate();;
+ return;
} catch (InvalidFormatException $e) {
app('log')->error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage()));
- return false;
+ $fail('validation.date_or_time')->translate();;
+ return;
}
-
- return true;
}
}
diff --git a/app/Rules/IsDuplicateTransaction.php b/app/Rules/IsDuplicateTransaction.php
index 2bfb59089d..4f61474828 100644
--- a/app/Rules/IsDuplicateTransaction.php
+++ b/app/Rules/IsDuplicateTransaction.php
@@ -23,30 +23,25 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
-use Illuminate\Contracts\Validation\Rule;
+use Closure;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
+ * TODO not sure where this is used.
+ *
* Class IsDuplicateTransaction
*/
-class IsDuplicateTransaction implements Rule
+class IsDuplicateTransaction implements ValidationRule
{
private string $value;
/**
* @inheritDoc
*/
- public function message()
- {
- return $this->value;
- }
-
- /**
- * @inheritDoc
- */
- public function passes($attribute, $value)
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
$this->value = $value;
- return false;
+ $fail($this->value);
}
}
diff --git a/app/Rules/IsTransferAccount.php b/app/Rules/IsTransferAccount.php
index dab2a89873..34688b262f 100644
--- a/app/Rules/IsTransferAccount.php
+++ b/app/Rules/IsTransferAccount.php
@@ -24,34 +24,25 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
+use Closure;
use FireflyIII\Models\TransactionType;
use FireflyIII\Validation\AccountValidator;
-use Illuminate\Contracts\Validation\Rule;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class IsTransferAccount
*/
-class IsTransferAccount implements Rule
+class IsTransferAccount implements ValidationRule
{
- /**
- * Get the validation error message.
- *
- * @return string
- */
- public function message(): string
- {
- return (string)trans('validation.not_transfer_account');
- }
/**
- * Determine if the validation rule passes.
- *
- * @param string $attribute
- * @param mixed $value
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
- * @return bool
+ * @return void
*/
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
app('log')->debug(sprintf('Now in %s(%s)', __METHOD__, $value));
/** @var AccountValidator $validator */
@@ -64,11 +55,13 @@ class IsTransferAccount implements Rule
app('log')->debug('Found account based on name. Return true.');
// found by name, use repos to return.
- return true;
+ return;
}
$validAccount = $validator->validateSource(['id' => (int)$value,]);
app('log')->debug(sprintf('Search by id (%d), result is %s.', (int)$value, var_export($validAccount, true)));
- return false !== $validAccount;
+ if(false === $validAccount) {
+ $fail('validation.not_transfer_account')->translate();
+ }
}
}
diff --git a/app/Rules/IsValidAttachmentModel.php b/app/Rules/IsValidAttachmentModel.php
index a5c2aa9add..b5ca4bcdbd 100644
--- a/app/Rules/IsValidAttachmentModel.php
+++ b/app/Rules/IsValidAttachmentModel.php
@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
+use Closure;
use FireflyIII\Models\Account;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget;
@@ -39,15 +40,15 @@ use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
-use Illuminate\Contracts\Validation\Rule;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class IsValidAttachmentModel
*/
-class IsValidAttachmentModel implements Rule
+class IsValidAttachmentModel implements ValidationRule
{
/** @var string */
- private $model;
+ private string $model;
/**
* IsValidAttachmentModel constructor.
@@ -76,27 +77,17 @@ class IsValidAttachmentModel implements Rule
}
/**
- * Get the validation error message.
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
- * @return string
- */
- public function message(): string
- {
- return (string)trans('validation.model_id_invalid');
- }
-
- /**
- * Determine if the validation rule passes.
- *
- * @param string $attribute
- * @param mixed $value
- *
- * @return bool
+ * @return void
*/
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!auth()->check()) {
- return false;
+ $fail('validation.model_id_invalid')->translate();
+ return;
}
$methods = [
Account::class => 'validateAccount',
@@ -111,11 +102,15 @@ class IsValidAttachmentModel implements Rule
if (!array_key_exists($this->model, $methods)) {
app('log')->error(sprintf('Cannot validate model "%s" in %s.', substr($this->model, 0, 20), __METHOD__));
- return false;
+ $fail('validation.model_id_invalid')->translate();
+ return;
}
$method = $methods[$this->model];
- return $this->$method((int)$value);
+ $result = $this->$method((int)$value);
+ if(false === $result) {
+ $fail('validation.model_id_invalid')->translate();
+ }
}
/**
diff --git a/app/Rules/IsValidBulkClause.php b/app/Rules/IsValidBulkClause.php
index 7a8bda7692..83c7479d37 100644
--- a/app/Rules/IsValidBulkClause.php
+++ b/app/Rules/IsValidBulkClause.php
@@ -24,14 +24,15 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
-use Illuminate\Contracts\Validation\Rule;
+use Closure;
+use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Validator;
use JsonException;
/**
* Class IsValidBulkClause
*/
-class IsValidBulkClause implements Rule
+class IsValidBulkClause implements ValidationRule
{
private string $error;
private array $rules;
@@ -54,19 +55,18 @@ class IsValidBulkClause implements Rule
}
/**
- * @param string $attribute
- * @param mixed $value
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
- * @return bool
+ * @return void
*/
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
$result = $this->basicValidation((string)$value);
if (false === $result) {
- return false;
+ $fail($this->error);
}
-
- return true;
}
/**
diff --git a/app/Rules/LessThanPiggyTarget.php b/app/Rules/LessThanPiggyTarget.php
index e19589ea74..21f1ecc7e6 100644
--- a/app/Rules/LessThanPiggyTarget.php
+++ b/app/Rules/LessThanPiggyTarget.php
@@ -24,12 +24,13 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
-use Illuminate\Contracts\Validation\Rule;
+use Closure;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class LessThanPiggyTarget
*/
-class LessThanPiggyTarget implements Rule
+class LessThanPiggyTarget implements ValidationRule
{
/**
* Get the validation error message.
@@ -42,15 +43,14 @@ class LessThanPiggyTarget implements Rule
}
/**
- * Determine if the validation rule passes.
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
- * @param string $attribute
- * @param mixed $value
- *
- * @return bool
+ * @return void
*/
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
- return true;
+ // TODO not sure if this is still used.
}
}
diff --git a/app/Rules/UniqueAccountNumber.php b/app/Rules/UniqueAccountNumber.php
index fa5323d024..b63022df53 100644
--- a/app/Rules/UniqueAccountNumber.php
+++ b/app/Rules/UniqueAccountNumber.php
@@ -23,15 +23,16 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
+use Closure;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
-use Illuminate\Contracts\Validation\Rule;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class UniqueAccountNumber
*/
-class UniqueAccountNumber implements Rule
+class UniqueAccountNumber implements ValidationRule
{
private ?Account $account;
private ?string $expectedType;
@@ -73,21 +74,19 @@ class UniqueAccountNumber implements Rule
}
/**
- * Determine if the validation rule passes.
- *
- * @param string $attribute
- * @param mixed $value
- *
- * @return bool
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
+ * @return void
*/
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!auth()->check()) {
- return true;
+ return;
}
if (null === $this->expectedType) {
- return true;
+ return;
}
$maxCounts = $this->getMaxOccurrences();
@@ -105,12 +104,11 @@ class UniqueAccountNumber implements Rule
)
);
- return false;
+ $fail('validation.unique_account_number_for_user')->translate();
+ return;
}
}
app('log')->debug('Account number is valid.');
-
- return true;
}
/**
diff --git a/app/Rules/ValidJournals.php b/app/Rules/ValidJournals.php
index 949db4cd16..d1d4a02b23 100644
--- a/app/Rules/ValidJournals.php
+++ b/app/Rules/ValidJournals.php
@@ -24,40 +24,30 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
+use Closure;
use FireflyIII\Models\TransactionJournal;
-use Illuminate\Contracts\Validation\Rule;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class ValidJournals
*
*/
-class ValidJournals implements Rule
+class ValidJournals implements ValidationRule
{
- /**
- * Get the validation error message.
- *
- * @return string
- */
- public function message(): string
- {
- return (string)trans('validation.invalid_selection');
- }
/**
- * Determine if the validation rule passes.
- *
- * @param string $attribute
- * @param mixed $value
- *
- * @return bool
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
+ * @return void
*/
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
app('log')->debug('In ValidJournals::passes');
if (!is_array($value)) {
- return true;
+ return;
}
$userId = auth()->user()->id;
foreach ($value as $journalId) {
@@ -65,11 +55,10 @@ class ValidJournals implements Rule
if (0 === $count) {
app('log')->debug(sprintf('Count for transaction #%d and user #%d is zero! Return FALSE', $journalId, $userId));
- return false;
+ $fail('validation.invalid_selection')->translate();
+ return;
}
}
app('log')->debug('Return true!');
-
- return true;
}
}
diff --git a/app/Rules/ValidRecurrenceRepetitionType.php b/app/Rules/ValidRecurrenceRepetitionType.php
index 508835a5fb..50505a64e7 100644
--- a/app/Rules/ValidRecurrenceRepetitionType.php
+++ b/app/Rules/ValidRecurrenceRepetitionType.php
@@ -23,25 +23,16 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
-use Illuminate\Contracts\Validation\Rule;
+use Closure;
+use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class ValidRecurrenceRepetitionType
*
*/
-class ValidRecurrenceRepetitionType implements Rule
+class ValidRecurrenceRepetitionType implements ValidationRule
{
- /**
- * Get the validation error message.
- *
- * @return string
- */
- public function message(): string
- {
- return (string)trans('validation.valid_recurrence_rep_type');
- }
-
/**
* Determine if the validation rule passes.
*
@@ -51,24 +42,24 @@ class ValidRecurrenceRepetitionType implements Rule
* @return bool
*
*/
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
$value = (string)$value;
if ('daily' === $value) {
- return true;
+ return;
}
//monthly,17
//ndom,3,7
if (in_array(substr($value, 0, 6), ['yearly', 'weekly'], true)) {
- return true;
+ return;
}
if (str_starts_with($value, 'monthly')) {
- return true;
+ return;
}
if (str_starts_with($value, 'ndom')) {
- return true;
+ return;
}
- return false;
+ $fail('validation.valid_recurrence_rep_type')->translate();
}
}
diff --git a/app/Rules/ValidRecurrenceRepetitionValue.php b/app/Rules/ValidRecurrenceRepetitionValue.php
index bbb0e87bea..32566ba3dc 100644
--- a/app/Rules/ValidRecurrenceRepetitionValue.php
+++ b/app/Rules/ValidRecurrenceRepetitionValue.php
@@ -24,7 +24,8 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Carbon\Carbon;
-use Illuminate\Contracts\Validation\Rule;
+use Closure;
+use Illuminate\Contracts\Validation\ValidationRule;
use InvalidArgumentException;
/**
@@ -32,56 +33,44 @@ use InvalidArgumentException;
*
*/
-class ValidRecurrenceRepetitionValue implements Rule
+class ValidRecurrenceRepetitionValue implements ValidationRule
{
/**
- * Get the validation error message.
+ * @param string $attribute
+ * @param mixed $value
+ * @param Closure $fail
*
- * @return string
+ * @return void
*/
- public function message(): string
- {
- return (string)trans('validation.valid_recurrence_rep_type');
- }
-
- /**
- * Determine if the validation rule passes.
- *
- * @param string $attribute
- * @param mixed $value
- *
- * @return bool
- *
- */
- public function passes($attribute, $value): bool
+ public function validate(string $attribute, mixed $value, Closure $fail): void
{
$value = (string)$value;
if ('daily' === $value) {
- return true;
+ return;
}
- if (str_starts_with($value, 'monthly')) {
- return $this->validateMonthly($value);
+ if (str_starts_with($value, 'monthly') && $this->validateMonthly($value)) {
+ return;
}
// Value is like: ndom,3,7
// nth x-day of the month.
- if (str_starts_with($value, 'ndom')) {
- return $this->validateNdom($value);
+ if (str_starts_with($value, 'ndom') && $this->validateNdom($value)) {
+ return;
}
// Value is like: weekly,7
- if (str_starts_with($value, 'weekly')) {
- return $this->validateWeekly($value);
+ if (str_starts_with($value, 'weekly') && $this->validateWeekly($value)) {
+ return ;
}
// Value is like: yearly,2018-01-01
- if (str_starts_with($value, 'yearly')) {
- return $this->validateYearly($value);
+ if (str_starts_with($value, 'yearly') && $this->validateYearly($value)) {
+ return ;
}
- return false;
+ $fail('validation.valid_recurrence_rep_type')->translate();
}
/**
diff --git a/app/Services/Internal/Support/AccountServiceTrait.php b/app/Services/Internal/Support/AccountServiceTrait.php
index ecf92edde0..f56b263cb7 100644
--- a/app/Services/Internal/Support/AccountServiceTrait.php
+++ b/app/Services/Internal/Support/AccountServiceTrait.php
@@ -255,7 +255,7 @@ trait AccountServiceTrait
// get or grab currency:
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
- $currency = app('default')->getDefaultCurrencyByUser($account->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
// submit to factory:
@@ -386,7 +386,7 @@ trait AccountServiceTrait
if (null === $currency) {
// use default currency:
- $currency = app('amount')->getDefaultCurrencyByUser($this->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
}
$currency->enabled = true;
$currency->save();
@@ -433,7 +433,7 @@ trait AccountServiceTrait
// if exists, update:
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
- $currency = app('default')->getDefaultCurrencyByUser($account->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($account->user);
}
// simply grab the first journal and change it:
@@ -500,7 +500,7 @@ trait AccountServiceTrait
// get or grab currency:
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
- $currency = app('default')->getDefaultCurrencyByUser($account->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
// submit to factory:
@@ -632,7 +632,7 @@ trait AccountServiceTrait
// if exists, update:
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
- $currency = app('default')->getDefaultCurrencyByUser($account->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
// simply grab the first journal and change it:
@@ -715,7 +715,7 @@ trait AccountServiceTrait
// get or grab currency:
$currency = $this->accountRepository->getAccountCurrency($account);
if (null === $currency) {
- $currency = app('default')->getDefaultCurrencyByUser($account->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
// submit to factory:
diff --git a/app/Services/Internal/Support/RecurringTransactionTrait.php b/app/Services/Internal/Support/RecurringTransactionTrait.php
index 7f39cb31e6..02c04d9494 100644
--- a/app/Services/Internal/Support/RecurringTransactionTrait.php
+++ b/app/Services/Internal/Support/RecurringTransactionTrait.php
@@ -119,7 +119,7 @@ trait RecurringTransactionTrait
$currency = $factory->find($array['currency_id'] ?? null, $array['currency_code'] ?? null);
$foreignCurrency = $factory->find($array['foreign_currency_id'] ?? null, $array['foreign_currency_code'] ?? null);
if (null === $currency) {
- $currency = app('amount')->getDefaultCurrencyByUser($recurrence->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($recurrence->user->userGroup);
}
app('log')->debug(
diff --git a/app/Services/Internal/Update/BillUpdateService.php b/app/Services/Internal/Update/BillUpdateService.php
index 51af4f9d62..d948548734 100644
--- a/app/Services/Internal/Update/BillUpdateService.php
+++ b/app/Services/Internal/Update/BillUpdateService.php
@@ -60,7 +60,7 @@ class BillUpdateService
if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {
$factory = app(TransactionCurrencyFactory::class);
$currency = $factory->find((int)($data['currency_id'] ?? null), $data['currency_code'] ?? null) ??
- app('amount')->getDefaultCurrencyByUser($bill->user);
+ app('amount')->getDefaultCurrencyByUserGroup($bill->user->userGroup);
// enable the currency if it isn't.
$currency->enabled = true;
diff --git a/app/Support/Amount.php b/app/Support/Amount.php
index 7db3332c3c..8352d1e077 100644
--- a/app/Support/Amount.php
+++ b/app/Support/Amount.php
@@ -121,7 +121,7 @@ class Amount
/** @var User $user */
$user = auth()->user();
- return $this->getDefaultCurrencyByUser($user);
+ return $this->getDefaultCurrencyByUserGroup($user->userGroup);
}
/**
diff --git a/app/Support/Calendar/Calculator.php b/app/Support/Calendar/Calculator.php
index b09ba58c59..7cbebb670f 100644
--- a/app/Support/Calendar/Calculator.php
+++ b/app/Support/Calendar/Calculator.php
@@ -26,7 +26,7 @@ declare(strict_types=1);
namespace FireflyIII\Support\Calendar;
use Carbon\Carbon;
-use FireflyIII\Support\Calendar\Exceptions\IntervalException;
+use FireflyIII\Exceptions\IntervalException;
use SplObjectStorage;
/**
diff --git a/app/Support/Calendar/Periodicity/Interval.php b/app/Support/Calendar/Periodicity/Interval.php
index c3a3a06805..2cc326d074 100644
--- a/app/Support/Calendar/Periodicity/Interval.php
+++ b/app/Support/Calendar/Periodicity/Interval.php
@@ -37,7 +37,7 @@ abstract class Interval implements Interspacable
*
* @return int
*/
- public function skip(int $skip): int
+ final public function skip(int $skip): int
{
return static::INTERVAL * $skip;
}
diff --git a/app/Support/Form/CurrencyForm.php b/app/Support/Form/CurrencyForm.php
index d78ddacc10..376dbf1bfa 100644
--- a/app/Support/Form/CurrencyForm.php
+++ b/app/Support/Form/CurrencyForm.php
@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Support\Form;
-use Amount as Amt;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
@@ -68,11 +67,10 @@ class CurrencyForm
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
- $defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency();
+ $defaultCurrency = $options['currency'] ?? app('amount')->getDefaultCurrency();
/** @var Collection $currencies */
$currencies = app('amount')->getCurrencies();
unset($options['currency'], $options['placeholder']);
-
// perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount)
$preFilled = session('preFilled');
if (!is_array($preFilled)) {
@@ -140,7 +138,7 @@ class CurrencyForm
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
- $defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency();
+ $defaultCurrency = $options['currency'] ?? app('amount')->getDefaultCurrency();
/** @var Collection $currencies */
$currencies = app('amount')->getAllCurrencies();
unset($options['currency'], $options['placeholder']);
diff --git a/app/Support/Navigation.php b/app/Support/Navigation.php
index 5484868a5e..245ab5eb67 100644
--- a/app/Support/Navigation.php
+++ b/app/Support/Navigation.php
@@ -25,9 +25,9 @@ namespace FireflyIII\Support;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
+use FireflyIII\Exceptions\IntervalException;
use FireflyIII\Helpers\Fiscal\FiscalHelperInterface;
use FireflyIII\Support\Calendar\Calculator;
-use FireflyIII\Support\Calendar\Exceptions\IntervalException;
use FireflyIII\Support\Calendar\Periodicity;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -111,7 +111,7 @@ class Navigation
return $this->calculator->nextDateByInterval($epoch, $periodicity, $skipInterval);
} catch (IntervalException $exception) {
app('log')->warning($exception->getMessage(), ['exception' => $exception]);
- } catch (Throwable $exception) {
+ } catch (Throwable $exception) { // @phpstan-ignore-line
app('log')->error($exception->getMessage(), ['exception' => $exception]);
}
@@ -144,8 +144,8 @@ class Navigation
$workEnd = clone $loopDate;
while ($loopCount < 13) {
// make range:
- $workStart = \Navigation::startOfPeriod($workStart, $range);
- $workEnd = \Navigation::endOfPeriod($workStart, $range);
+ $workStart = $this->startOfPeriod($workStart, $range);
+ $workEnd = $this->endOfPeriod($workStart, $range);
// make sure we don't go overboard
if ($workEnd->gt($start)) {
diff --git a/app/Support/Report/Budget/BudgetReportGenerator.php b/app/Support/Report/Budget/BudgetReportGenerator.php
index 739a0c3a25..67d8b0d920 100644
--- a/app/Support/Report/Budget/BudgetReportGenerator.php
+++ b/app/Support/Report/Budget/BudgetReportGenerator.php
@@ -373,6 +373,6 @@ class BudgetReportGenerator
$this->blRepository->setUser($user);
$this->opsRepository->setUser($user);
$this->nbRepository->setUser($user);
- $this->currency = app('amount')->getDefaultCurrencyByUser($user);
+ $this->currency = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
}
}
diff --git a/app/Support/Steam.php b/app/Support/Steam.php
index 5e9fba34a7..942315c691 100644
--- a/app/Support/Steam.php
+++ b/app/Support/Steam.php
@@ -134,7 +134,7 @@ class Steam
if (null === $currency) {
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($account->user);
- $currency = $repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
+ $currency = $repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
$currencyId = (int)$currency->id;
@@ -211,7 +211,7 @@ class Steam
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
if (null === $currency) {
- $currency = $repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
+ $currency = $repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
// first part: get all balances in own currency:
$transactions = $account->transactions()
diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php
index eaa82dca79..4a7158a124 100644
--- a/app/Transformers/AccountTransformer.php
+++ b/app/Transformers/AccountTransformer.php
@@ -184,7 +184,7 @@ class AccountTransformer extends AbstractTransformer
// only grab default when result is null:
if (null === $currency) {
- $currency = app('amount')->getDefaultCurrencyByUser($account->user);
+ $currency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
}
$currencyId = (string)$currency->id;
$currencyCode = $currency->code;
diff --git a/app/Transformers/PiggyBankEventTransformer.php b/app/Transformers/PiggyBankEventTransformer.php
index 66e72cb579..3706b1c362 100644
--- a/app/Transformers/PiggyBankEventTransformer.php
+++ b/app/Transformers/PiggyBankEventTransformer.php
@@ -67,7 +67,7 @@ class PiggyBankEventTransformer extends AbstractTransformer
$this->piggyRepos->setUser($account->user);
// get associated currency or fall back to the default:
- $currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
+ $currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
// get associated journal and transaction, if any:
$journalId = $event->transaction_journal_id;
diff --git a/app/Transformers/PiggyBankTransformer.php b/app/Transformers/PiggyBankTransformer.php
index 43770a8924..de320841b2 100644
--- a/app/Transformers/PiggyBankTransformer.php
+++ b/app/Transformers/PiggyBankTransformer.php
@@ -67,7 +67,7 @@ class PiggyBankTransformer extends AbstractTransformer
$this->piggyRepos->setUser($account->user);
// get currency from account, or use default.
- $currency = $this->accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
+ $currency = $this->accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
// note
$notes = $this->piggyRepos->getNoteText($piggyBank);
diff --git a/app/Transformers/V2/PiggyBankTransformer.php b/app/Transformers/V2/PiggyBankTransformer.php
index 82e9b05de6..bb3abdd430 100644
--- a/app/Transformers/V2/PiggyBankTransformer.php
+++ b/app/Transformers/V2/PiggyBankTransformer.php
@@ -131,7 +131,7 @@ class PiggyBankTransformer extends AbstractTransformer
$this->notes[$id] = $note;
}
- $this->default = app('amount')->getDefaultCurrencyByUser(auth()->user());
+ $this->default = app('amount')->getDefaultCurrencyByUserGroup(auth()->user()->userGroup);
$this->converter = new ExchangeRateConverter();
}
diff --git a/app/Transformers/V2/TransactionGroupTransformer.php b/app/Transformers/V2/TransactionGroupTransformer.php
index e67db9c145..ca31cae79f 100644
--- a/app/Transformers/V2/TransactionGroupTransformer.php
+++ b/app/Transformers/V2/TransactionGroupTransformer.php
@@ -43,7 +43,6 @@ use stdClass;
*/
class TransactionGroupTransformer extends AbstractTransformer
{
- use ConvertsExchangeRates;
private ExchangeRateConverter $converter;
private array $currencies = [];
diff --git a/database/migrations/2023_10_21_113213_add_currency_pivot_tables.php b/database/migrations/2023_10_21_113213_add_currency_pivot_tables.php
index 213a714df6..ae1325870a 100644
--- a/database/migrations/2023_10_21_113213_add_currency_pivot_tables.php
+++ b/database/migrations/2023_10_21_113213_add_currency_pivot_tables.php
@@ -24,6 +24,7 @@
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
diff --git a/resources/lang/en_US/validation.php b/resources/lang/en_US/validation.php
index 72fb97c7af..b51a8a1209 100644
--- a/resources/lang/en_US/validation.php
+++ b/resources/lang/en_US/validation.php
@@ -34,6 +34,7 @@ return [
'invalid_query_currency' => 'Your query contains accounts that have different currency settings, which is not allowed.',
'iban' => 'This is not a valid IBAN.',
'zero_or_more' => 'The value cannot be negative.',
+ 'no_asset_account' => 'This is not an asset account.',
'date_or_time' => 'The value must be a valid date or time value (ISO 8601).',
'source_equals_destination' => 'The source account equals the destination account.',
'unique_account_number_for_user' => 'It looks like this account number is already in use.',
diff --git a/tests/unit/Support/Calendar/CalculatorTest.php b/tests/unit/Support/Calendar/CalculatorTest.php
index 3c0e265002..0b96509464 100644
--- a/tests/unit/Support/Calendar/CalculatorTest.php
+++ b/tests/unit/Support/Calendar/CalculatorTest.php
@@ -25,8 +25,8 @@ declare(strict_types=1);
namespace Tests\unit\Support\Calendar;
+use FireflyIII\Exceptions\IntervalException;
use FireflyIII\Support\Calendar\Calculator;
-use FireflyIII\Support\Calendar\Exceptions\IntervalException;
use FireflyIII\Support\Calendar\Periodicity;
use Generator;
use PHPUnit\Framework\TestCase;