Refactor findNull to find

This commit is contained in:
James Cole
2021-06-30 06:17:38 +02:00
parent b7ae5eda35
commit 70da5917c9
68 changed files with 120 additions and 273 deletions

View File

@@ -130,17 +130,7 @@ class AccountRepository implements AccountRepositoryInterface
$query->whereIn('account_types.type', $types);
}
// See reference nr. 9
$accounts = $query->get(['accounts.*']);
/** @var Account $account */
foreach ($accounts as $account) {
if ($account->iban === $iban) {
return $account;
}
}
return null;
return $query->where('iban', $iban)->first(['accounts.*']);
}
/**
@@ -161,7 +151,7 @@ class AccountRepository implements AccountRepositoryInterface
$accounts = $query->get(['accounts.*']);
// See reference nr. 10
// See reference nr. 10
/** @var Account $account */
foreach ($accounts as $account) {
@@ -181,7 +171,7 @@ class AccountRepository implements AccountRepositoryInterface
*
* @return Account|null
*/
public function findNull(int $accountId): ?Account
public function find(int $accountId): ?Account
{
return $this->user->accounts()->find($accountId);
}

View File

@@ -95,7 +95,7 @@ interface AccountRepositoryInterface
*
* @return Account|null
*/
public function findNull(int $accountId): ?Account;
public function find(int $accountId): ?Account;
/**
* @param Account $account

View File

@@ -207,7 +207,7 @@ class AccountTasker implements AccountTaskerInterface
$sourceId = (int)$journal['destination_account_id'];
$currencyId = (int)$journal['currency_id'];
$key = sprintf('%s-%s', $sourceId, $currencyId);
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->findNull($currencyId);
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->find($currencyId);
$report['accounts'][$key] = $report['accounts'][$key] ?? [
'id' => $sourceId,
'name' => $journal['destination_account_name'],
@@ -269,7 +269,7 @@ class AccountTasker implements AccountTaskerInterface
$currencyId = (int)$journal['currency_id'];
$key = sprintf('%s-%s', $sourceId, $currencyId);
if (!array_key_exists($key, $report['accounts'])) {
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->findNull($currencyId);
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->find($currencyId);
$report['accounts'][$key] = [
'id' => $sourceId,
'name' => $journal['source_account_name'],

View File

@@ -146,18 +146,7 @@ class BillRepository implements BillRepositoryInterface
*/
public function findByName(string $name): ?Bill
{
$bills = $this->user->bills()->get(['bills.*']);
// See reference nr. 6
/** @var Bill $bill */
foreach ($bills as $bill) {
if ($bill->name === $name) {
return $bill;
}
}
return null;
return $this->user->bills()->where('name', $name)->first(['bills.*']);
}
/**
@@ -505,6 +494,7 @@ class BillRepository implements BillRepositoryInterface
$currentStart = clone $nextExpectedMatch;
}
return $set;
}
@@ -641,7 +631,7 @@ class BillRepository implements BillRepositoryInterface
$cache->addProperty('nextDateMatch');
$cache->addProperty($date);
if ($cache->has()) {
return $cache->get();
return $cache->get();
}
// find the most recent date for this bill NOT in the future. Cache this date:
$start = clone $bill->date;
@@ -669,7 +659,7 @@ class BillRepository implements BillRepositoryInterface
$cache->addProperty('nextExpectedMatch');
$cache->addProperty($date);
if ($cache->has()) {
return $cache->get();
return $cache->get();
}
// find the most recent date for this bill NOT in the future. Cache this date:
$start = clone $bill->date;

View File

@@ -127,7 +127,7 @@ class BudgetRepository implements BudgetRepositoryInterface
{
Log::debug('Now in findBudget()');
Log::debug(sprintf('Searching for budget with ID #%d...', $budgetId));
$result = $this->findNull((int)$budgetId);
$result = $this->find((int)$budgetId);
if (null === $result && null !== $budgetName && '' !== $budgetName) {
Log::debug(sprintf('Searching for budget with name %s...', $budgetName));
$result = $this->findByName((string)$budgetName);
@@ -164,12 +164,8 @@ class BudgetRepository implements BudgetRepositoryInterface
*
* @return Budget|null
*/
public function findNull(int $budgetId = null): ?Budget
public function find(int $budgetId = null): ?Budget
{
if (null === $budgetId) {
return null;
}
return $this->user->budgets()->find($budgetId);
}
@@ -348,7 +344,7 @@ class BudgetRepository implements BudgetRepositoryInterface
$repos = app(CurrencyRepositoryInterface::class);
$currency = null;
if (array_key_exists('currency_id', $data)) {
$currency = $repos->findNull((int)$data['currency_id']);
$currency = $repos->find((int)$data['currency_id']);
}
if (array_key_exists('currency_code', $data)) {
$currency = $repos->findByCode((string)$data['currency_code']);
@@ -454,7 +450,7 @@ class BudgetRepository implements BudgetRepositoryInterface
$repos = app(CurrencyRepositoryInterface::class);
$currencyId = (int)($data['currency_id'] ?? 0);
$currencyCode = (string)($data['currency_code'] ?? '');
$currency = $repos->findNull($currencyId);
$currency = $repos->find($currencyId);
if (null === $currency) {
$currency = $repos->findByCodeNull($currencyCode);
}

View File

@@ -74,13 +74,12 @@ interface BudgetRepositoryInterface
public function findByName(?string $name): ?Budget;
/**
* See reference nr. 12
*
* @param int|null $budgetId
*
* @return Budget|null
*/
public function findNull(int $budgetId = null): ?Budget;
public function find(int $budgetId = null): ?Budget;
/**
* This method returns the oldest journal or transaction date known to this budget.

View File

@@ -87,17 +87,7 @@ class CategoryRepository implements CategoryRepositoryInterface
*/
public function findByName(string $name): ?Category
{
$categories = $this->user->categories()->get(['categories.*']);
// See reference nr. 7
foreach ($categories as $category) {
if ($category->name === $name) {
return $category;
}
}
return null;
return $this->user->categories()->where('name', $name)->first(['categories.*']);
}
/**
@@ -111,7 +101,7 @@ class CategoryRepository implements CategoryRepositoryInterface
{
Log::debug('Now in findCategory()');
Log::debug(sprintf('Searching for category with ID #%d...', $categoryId));
$result = $this->findNull((int)$categoryId);
$result = $this->find((int)$categoryId);
if (null === $result) {
Log::debug(sprintf('Searching for category with name %s...', $categoryName));
$result = $this->findByName((string)$categoryName);
@@ -135,7 +125,7 @@ class CategoryRepository implements CategoryRepositoryInterface
*
* @return Category|null
*/
public function findNull(int $categoryId): ?Category
public function find(int $categoryId): ?Category
{
return $this->user->categories()->find($categoryId);
}

View File

@@ -70,7 +70,7 @@ interface CategoryRepositoryInterface
*
* @return Category|null
*/
public function findNull(int $categoryId): ?Category;
public function find(int $categoryId): ?Category;
/**
* @param Category $category

View File

@@ -38,7 +38,6 @@ use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\CurrencyDestroyService;
use FireflyIII\Services\Internal\Update\CurrencyUpdateService;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Log;
@@ -346,20 +345,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $result;
}
/**
* Find by ID, return NULL if not found.
* Used in Import Currency!
*
* @param int $currencyId
*
* @return TransactionCurrency|null
* @deprecated
*/
public function findNull(int $currencyId): ?TransactionCurrency
{
return TransactionCurrency::find($currencyId);
}
/**
* @return Collection
*/

View File

@@ -163,15 +163,6 @@ interface CurrencyRepositoryInterface
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency;
/**
* Find by ID, return NULL if not found.
*
* @param int $currencyId
*
* @return TransactionCurrency|null
*/
public function findNull(int $currencyId): ?TransactionCurrency;
/**
* @return Collection
*/

View File

@@ -87,9 +87,9 @@ class JournalRepository implements JournalRepositoryInterface
*
* @return TransactionJournal|null
*/
public function findNull(int $journalId): ?TransactionJournal
public function find(int $journalId): ?TransactionJournal
{
return $this->user->transactionJournals()->where('id', $journalId)->first();
return $this->user->transactionJournals()->find($journalId);
}
/**
@@ -123,62 +123,6 @@ class JournalRepository implements JournalRepositoryInterface
return $transaction->account;
}
/**
* Return a list of all destination accounts related to journal.
*
* @param TransactionJournal $journal
* @param bool $useCache
*
* @return Collection
*/
public function getJournalDestinationAccounts(TransactionJournal $journal, bool $useCache = true): Collection
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('destination-account-list');
if ($useCache && $cache->has()) {
return $cache->get();
}
$transactions = $journal->transactions()->where('amount', '>', 0)->orderBy('transactions.account_id')->with('account')->get();
$list = new Collection;
/** @var Transaction $t */
foreach ($transactions as $t) {
$list->push($t->account);
}
$list = $list->unique('id');
$cache->store($list);
return $list;
}
/**
* Return a list of all source accounts related to journal.
*
* @param TransactionJournal $journal
* @param bool $useCache
*
* @return Collection
*/
public function getJournalSourceAccounts(TransactionJournal $journal, bool $useCache = true): Collection
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('source-account-list');
if ($useCache && $cache->has()) {
return $cache->get();
}
$transactions = $journal->transactions()->where('amount', '<', 0)->orderBy('transactions.account_id')->with('account')->get();
$list = new Collection;
/** @var Transaction $t */
foreach ($transactions as $t) {
$list->push($t->account);
}
$list = $list->unique('id');
$cache->store($list);
return $list;
}
/**
* Return total amount of journal. Is always positive.
*

View File

@@ -58,14 +58,14 @@ interface JournalRepositoryInterface
public function findByType(array $types): Collection;
/**
* See reference nr. 1
* See reference nr. 1
* Find a specific journal.
*
* @param int $journalId
*
* @return TransactionJournal|null
*/
public function findNull(int $journalId): ?TransactionJournal;
public function find(int $journalId): ?TransactionJournal;
/**
* Get users very first transaction journal.
@@ -84,28 +84,6 @@ interface JournalRepositoryInterface
*/
public function getDestinationAccount(TransactionJournal $journal): Account;
/**
* See reference nr. 2
* Return a list of all destination accounts related to journal.
*
* @param TransactionJournal $journal
*
* @return Collection
* @deprecated
*/
public function getJournalDestinationAccounts(TransactionJournal $journal): Collection;
/**
* See reference nr. 3
* Return a list of all source accounts related to journal.
*
* @param TransactionJournal $journal
*
* @return Collection
* @deprecated
*/
public function getJournalSourceAccounts(TransactionJournal $journal): Collection;
/**
* Return total amount of journal. Is always positive.
*
@@ -121,7 +99,7 @@ interface JournalRepositoryInterface
public function getLast(): ?TransactionJournal;
/**
* See reference nr. 4
* See reference nr. 4
*
* @param TransactionJournalLink $link
*
@@ -150,7 +128,7 @@ interface JournalRepositoryInterface
public function getSourceAccount(TransactionJournal $journal): Account;
/**
* See reference nr. 5
* See reference nr. 5
*
* @param int $journalId
*/

View File

@@ -118,7 +118,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
*
* @return LinkType|null
*/
public function findNull(int $linkTypeId): ?LinkType
public function find(int $linkTypeId): ?LinkType
{
return LinkType::find($linkTypeId);
}
@@ -247,7 +247,7 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
*/
public function storeLink(array $information, TransactionJournal $inward, TransactionJournal $outward): ?TransactionJournalLink
{
$linkType = $this->findNull((int)($information['link_type_id'] ?? 0));
$linkType = $this->find((int)($information['link_type_id'] ?? 0));
if (null === $linkType) {
$linkType = $this->findByName($information['link_type_name']);

View File

@@ -79,7 +79,7 @@ interface LinkTypeRepositoryInterface
*
* @return LinkType|null
*/
public function findNull(int $linkTypeId): ?LinkType;
public function find(int $linkTypeId): ?LinkType;
/**
* See if such a link already exists (and get it).

View File

@@ -71,14 +71,9 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
*
* @return PiggyBank|null
*/
public function findNull(int $piggyBankId): ?PiggyBank
public function find(int $piggyBankId): ?PiggyBank
{
$piggyBank = $this->user->piggyBanks()->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']);
if (null !== $piggyBank) {
return $piggyBank;
}
return null;
$piggyBank = $this->user->piggyBanks()->find($piggyBankId);
}
/**
@@ -92,7 +87,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
Log::debug('Searching for piggy information.');
if (null !== $piggyBankId) {
$searchResult = $this->findNull((int)$piggyBankId);
$searchResult = $this->find((int)$piggyBankId);
if (null !== $searchResult) {
Log::debug(sprintf('Found piggy based on #%d, will return it.', $piggyBankId));

View File

@@ -115,7 +115,7 @@ interface PiggyBankRepositoryInterface
*
* @return PiggyBank|null
*/
public function findNull(int $piggyBankId): ?PiggyBank;
public function find(int $piggyBankId): ?PiggyBank;
/**
* @param int|null $piggyBankId

View File

@@ -105,12 +105,7 @@ class RuleRepository implements RuleRepositoryInterface
*/
public function find(int $ruleId): ?Rule
{
$rule = $this->user->rules()->find($ruleId);
if (null === $rule) {
return null;
}
return $rule;
return $this->user->rules()->find($ruleId);
}
/**

View File

@@ -119,12 +119,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
*/
public function find(int $ruleGroupId): ?RuleGroup
{
$group = $this->user->ruleGroups()->find($ruleGroupId);
if (null === $group) {
return null;
}
return $group;
return $this->user->ruleGroups()->find($ruleGroupId);
}
/**

View File

@@ -113,7 +113,7 @@ class TagRepository implements TagRepositoryInterface
*
* @return Tag|null
*/
public function findNull(int $tagId): ?Tag
public function find(int $tagId): ?Tag
{
return $this->user->tags()->find($tagId);
}

View File

@@ -74,7 +74,7 @@ interface TagRepositoryInterface
*
* @return Tag|null
*/
public function findNull(int $tagId): ?Tag;
public function find(int $tagId): ?Tag;
/**
* @param Tag $tag

View File

@@ -87,7 +87,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
*/
public function find(int $groupId): ?TransactionGroup
{
return $this->user->transactionGroups()->where('id', $groupId)->first();
return $this->user->transactionGroups()->find($groupId);
}
/**

View File

@@ -183,7 +183,7 @@ class UserRepository implements UserRepositoryInterface
*
* @return User|null
*/
public function findNull(int $userId): ?User
public function find(int $userId): ?User
{
return User::find($userId);
}
@@ -269,8 +269,6 @@ class UserRepository implements UserRepositoryInterface
*/
public function hasRole(User $user, string $role): bool
{
// See reference nr. 8
/** @var Role $userRole */
foreach ($user->roles as $userRole) {
if ($userRole->name === $role) {

View File

@@ -114,7 +114,7 @@ interface UserRepositoryInterface
*
* @return User|null
*/
public function findNull(int $userId): ?User;
public function find(int $userId): ?User;
/**
* Returns the first user in the DB. Generally only works when there is just one.