Fix various phpstan issues.

This commit is contained in:
James Cole
2023-11-04 07:18:03 +01:00
parent dc45131f73
commit ef428a0226
42 changed files with 104 additions and 174 deletions

View File

@@ -198,7 +198,7 @@ class JournalRepository implements JournalRepositoryInterface
*/
public function getSourceAccount(TransactionJournal $journal): Account
{
/** @var Transaction $transaction */
/** @var Transaction|null $transaction */
$transaction = $journal->transactions()->with('account')->where('amount', '<', 0)->first();
if (null === $transaction) {
throw new FireflyException(sprintf('Your administration is broken. Transaction journal #%d has no source transaction.', $journal->id));
@@ -212,7 +212,7 @@ class JournalRepository implements JournalRepositoryInterface
*/
public function reconcileById(int $journalId): void
{
/** @var TransactionJournal $journal */
/** @var TransactionJournal|null $journal */
$journal = $this->user->transactionJournals()->find($journalId);
$journal?->transactions()->update(['reconciled' => true]);
}
@@ -263,7 +263,7 @@ class JournalRepository implements JournalRepositoryInterface
*/
public function unreconcileById(int $journalId): void
{
/** @var TransactionJournal $journal */
/** @var TransactionJournal|null $journal */
$journal = $this->user->transactionJournals()->find($journalId);
$journal?->transactions()->update(['reconciled' => false]);
}

View File

@@ -300,13 +300,9 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
*/
public function getNoteText(PiggyBank $piggyBank): string
{
/** @var Note $note */
/** @var Note|null $note */
$note = $piggyBank->notes()->first();
if (null === $note) {
return '';
}
return $note->text;
return (string)$note?->text;
}
/**

View File

@@ -266,13 +266,9 @@ class RecurringRepository implements RecurringRepositoryInterface
*/
public function getNoteText(Recurrence $recurrence): string
{
/** @var Note $note */
/** @var Note|null $note */
$note = $recurrence->notes()->first();
if (null !== $note) {
return (string)$note->text;
}
return '';
return (string)$note?->text;
}
/**
@@ -573,12 +569,7 @@ class RecurringRepository implements RecurringRepositoryInterface
/** @var RecurrenceFactory $factory */
$factory = app(RecurrenceFactory::class);
$factory->setUser($this->user);
$result = $factory->create($data);
if (null === $result) {
throw new FireflyException($factory->getErrors()->first());
}
return $result;
return $factory->create($data);
}
/**

View File

@@ -161,7 +161,7 @@ class TagRepository implements TagRepositoryInterface
return $set->each(
static function (Attachment $attachment) use ($disk) {
/** @var Note $note */
/** @var Note|null $note */
$note = $attachment->notes()->first();
// only used in v1 view of tags
$attachment->file_exists = $disk->exists($attachment->fileName());

View File

@@ -226,7 +226,7 @@ class UserRepository implements UserRepositoryInterface
*/
public function getRolesInGroup(User $user, int $groupId): array
{
/** @var UserGroup $group */
/** @var UserGroup|null $group */
$group = UserGroup::find($groupId);
if (null === $group) {
throw new FireflyException(sprintf('Could not find group #%d', $groupId));

View File

@@ -52,7 +52,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface
$memberships = $userGroup->groupMemberships()->get();
/** @var GroupMembership $membership */
foreach ($memberships as $membership) {
/** @var User $user */
/** @var User|null $user */
$user = $membership->user()->first();
if (null === $user) {
continue;
@@ -242,7 +242,11 @@ class UserGroupRepository implements UserGroupRepositoryInterface
->where('user_role_id', $owner->id)
->where('user_id', '!=', $user->id)->count();
// if there are no other owners and the current users does not get or keep the owner role, refuse.
if (0 === $ownerCount && (0 === count($data['roles']) || (count($data['roles']) > 0 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)))) {
if (
0 === $ownerCount &&
(0 === count($data['roles']) ||
(count($data['roles']) > 0 && // @phpstan-ignore-line
!in_array(UserRoleEnum::OWNER->value, $data['roles'], true)))) {
app('log')->debug('User needs to keep owner role in this group, refuse to act');
throw new FireflyException('The last owner in this user group must keep the "owner" role.');
}

View File

@@ -53,7 +53,7 @@ class AccountRepository implements AccountRepositoryInterface
app('log')->debug(sprintf('Searching for account named "%s" (of user #%d) of the following type(s)', $name, $this->user->id), ['types' => $types]);
$query->where('accounts.name', $name);
/** @var Account $account */
/** @var Account|null $account */
$account = $query->first(['accounts.*']);
if (null === $account) {
app('log')->debug(sprintf('There is no account with name "%s" of types', $name), $types);

View File

@@ -262,13 +262,10 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if (null === $result) {
app('log')->debug('Grabbing default currency for this user...');
/** @var TransactionCurrency|null $result */
$result = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
}
if (null === $result) {
app('log')->debug('Grabbing EUR as fallback.');
$result = $this->findByCode('EUR');
}
app('log')->debug(sprintf('Final result: %s', $result->code));
if (false === $result->enabled) {
app('log')->debug(sprintf('Also enabled currency %s', $result->code));