Skip to content

Firefly III: a personal finances manager

Update find methods to return null

James Cole33db99ffd339
diff --git a/app/Http/Controllers/Chart/ExpenseReportController.php b/app/Http/Controllers/Chart/ExpenseReportController.php
index ed9e3f98ac..03ca051730 100644
--- a/app/Http/Controllers/Chart/ExpenseReportController.php
+++ b/app/Http/Controllers/Chart/ExpenseReportController.php
@@ -197,7 +197,7 @@ class ExpenseReportController extends Controller
$collection->push($expenseAccount);
$revenue = $this->accountRepository->findByName($expenseAccount->name, [AccountType::REVENUE]);
- if (!is_null($revenue->id)) {
+ if (!is_null($revenue)) {
$collection->push($revenue);
}
$combined[$expenseAccount->name] = $collection;
diff --git a/app/Http/Controllers/Report/ExpenseController.php b/app/Http/Controllers/Report/ExpenseController.php
index ecd36ada6a..3fb3b9b7cd 100644
--- a/app/Http/Controllers/Report/ExpenseController.php
+++ b/app/Http/Controllers/Report/ExpenseController.php
@@ -313,7 +313,7 @@ class ExpenseController extends Controller
$collection->push($expenseAccount);
$revenue = $this->accountRepository->findByName($expenseAccount->name, [AccountType::REVENUE]);
- if (!is_null($revenue->id)) {
+ if (!is_null($revenue)) {
$collection->push($revenue);
}
$combined[$expenseAccount->name] = $collection;
diff --git a/app/Import/Object/ImportBill.php b/app/Import/Object/ImportBill.php
index 988b7b9574..f27eaaf070 100644
--- a/app/Import/Object/ImportBill.php
+++ b/app/Import/Object/ImportBill.php
@@ -114,7 +114,7 @@ class ImportBill
Log::debug(sprintf('Finding bill with ID #%d', $this->id['value']));
/** @var Bill $bill */
$bill = $this->repository->find(intval($this->id['value']));
- if (null !== $bill->id) {
+ if (null !== $bill) {
Log::debug(sprintf('Found unmapped bill by ID (#%d): %s', $bill->id, $bill->name));
return $bill;
@@ -199,7 +199,7 @@ class ImportBill
$search = intval($array['mapped']);
$bill = $this->repository->find($search);
- if (null === $bill->id) {
+ if (null === $bill) {
Log::error(sprintf('There is no bill with id #%d. Invalid mapping will be ignored!', $search));
return new Bill;
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index 8e75a1f62a..9f3dcb20b1 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -44,12 +44,13 @@ use Validator;
*/
class AccountRepository implements AccountRepositoryInterface
{
+
/** @var User */
private $user;
-
- use FindAccountsTrait;
/** @var array */
private $validAssetFields = ['accountRole', 'accountNumber', 'currency_id', 'BIC'];
+
+ use FindAccountsTrait;
/** @var array */
private $validCCFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber', 'currency_id', 'BIC'];
/** @var array */
@@ -91,6 +92,16 @@ class AccountRepository implements AccountRepositoryInterface
return true;
}
+ /**
+ * @param int $accountId
+ *
+ * @return Account|null
+ */
+ public function findNull(int $accountId): ?Account
+ {
+ return $this->user->accounts()->find($accountId);
+ }
+
/**
* Return account type by string.
*
@@ -336,7 +347,7 @@ class AccountRepository implements AccountRepositoryInterface
// account may exist already:
$existingAccount = $this->findByName($data['name'], [$type]);
- if (null !== $existingAccount->id) {
+ if (null !== $existingAccount) {
Log::warning(sprintf('There already is an account named "%s" of type "%s".', $data['name'], $type));
return $existingAccount;
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index eb5e70fe0a..f3b13b6864 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -57,10 +57,18 @@ interface AccountRepositoryInterface
/**
* @param int $accountId
*
+ * @deprecated
* @return Account
*/
public function find(int $accountId): Account;
+ /**
+ * @param int $accountId
+ *
+ * @return Account|null
+ */
+ public function findNull(int $accountId): ?Account;
+
/**
* @param string $number
* @param array $types
@@ -81,9 +89,9 @@ interface AccountRepositoryInterface
* @param string $name
* @param array $types
*
- * @return Account
+ * @return Account|null
*/
- public function findByName(string $name, array $types): Account;
+ public function findByName(string $name, array $types): ?Account;
/**
* Return account type by string.
diff --git a/app/Repositories/Account/FindAccountsTrait.php b/app/Repositories/Account/FindAccountsTrait.php
index 8214dd1157..59a92b4f89 100644
--- a/app/Repositories/Account/FindAccountsTrait.php
+++ b/app/Repositories/Account/FindAccountsTrait.php
@@ -109,9 +109,9 @@ trait FindAccountsTrait
* @param string $name
* @param array $types
*
- * @return Account
+ * @return Account|null
*/
- public function findByName(string $name, array $types): Account
+ public function findByName(string $name, array $types): ?Account
{
$query = $this->user->accounts();
@@ -132,7 +132,7 @@ trait FindAccountsTrait
}
Log::debug(sprintf('There is no account with name "%s" or types', $name), $types);
- return new Account;
+ return null;
}
/**
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index ca93440c2f..f61cc41538 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -66,14 +66,12 @@ class BillRepository implements BillRepositoryInterface
*
* @return Bill
*/
- public function find(int $billId): Bill
+ public function find(int $billId): ?Bill
{
- $bill = $this->user->bills()->find($billId);
- if (null === $bill) {
- $bill = new Bill;
- }
+ /** @var Bill $res */
+ $res = $this->user->bills()->find($billId);
- return $bill;
+ return $res;
}
/**
@@ -83,7 +81,7 @@ class BillRepository implements BillRepositoryInterface
*
* @return Bill
*/
- public function findByName(string $name): Bill
+ public function findByName(string $name): ?Bill
{
$bills = $this->user->bills()->get(['bills.*']);
@@ -94,7 +92,7 @@ class BillRepository implements BillRepositoryInterface
}
}
- return new Bill;
+ return null;
}
/**
diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php
index 6f943c2182..1530dab099 100644
--- a/app/Repositories/Bill/BillRepositoryInterface.php
+++ b/app/Repositories/Bill/BillRepositoryInterface.php
@@ -47,18 +47,18 @@ interface BillRepositoryInterface
*
* @param int $billId
*
- * @return Bill
+ * @return Bill|null
*/
- public function find(int $billId): Bill;
+ public function find(int $billId): ?Bill;
/**
* Find a bill by name.
*
* @param string $name
*
- * @return Bill
+ * @return Bill|null
*/
- public function findByName(string $name): Bill;
+ public function findByName(string $name): ?Bill;
/**
* @return Collection
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index efc063217f..e5c8848596 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -194,9 +194,9 @@ class BudgetRepository implements BudgetRepositoryInterface
*
* @param string $name
*
- * @return Budget
+ * @return Budget|null
*/
- public function findByName(string $name): Budget
+ public function findByName(string $name): ?Budget
{
$budgets = $this->user->budgets()->get(['budgets.*']);
/** @var Budget $budget */
@@ -206,7 +206,19 @@ class BudgetRepository implements BudgetRepositoryInterface
}
}
- return new Budget;
+ return null;
+ }
+
+ /**
+ * Find a budget or return NULL
+ *
+ * @param int $budgetId
+ *
+ * @return Budget|null
+ */
+ public function findNull(int $budgetId): ?Budget
+ {
+ return $this->user->budgets()->find($budgetId);
}
/**
diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php
index 1563d2080d..1a6ca75625 100644
--- a/app/Repositories/Budget/BudgetRepositoryInterface.php
+++ b/app/Repositories/Budget/BudgetRepositoryInterface.php
@@ -73,19 +73,29 @@ interface BudgetRepositoryInterface
* Find a budget.
*
* @param int $budgetId
+ * @deprecated
*
* @return Budget
*/
public function find(int $budgetId): Budget;
+ /**
+ * Find a budget or return NULL
+ *
+ * @param int $budgetId
+ *
+ * @return Budget|null
+ */
+ public function findNull(int $budgetId): ?Budget;
+
/**
* Find a budget.
*
* @param string $name
*
- * @return Budget
+ * @return Budget|null
*/
- public function findByName(string $name): Budget;
+ public function findByName(string $name): ?Budget;
/**
* This method returns the oldest journal or transaction date known to this budget.
diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php
index 03ad9d4308..4bf9a67ca8 100644
--- a/app/Repositories/Category/CategoryRepository.php
+++ b/app/Repositories/Category/CategoryRepository.php
@@ -110,6 +110,18 @@ class CategoryRepository implements CategoryRepositoryInterface
return new Category;
}
+ /**
+ * Find a category or return NULL
+ *
+ * @param int $categoryId
+ *
+ * @return Category|null
+ */
+ public function findNull(int $categoryId): ?Category
+ {
+ return $this->user->categories()->find($categoryId);
+ }
+
/**
* @param Category $category
*
diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php
index 5aa9c0278c..e696962f5b 100644
--- a/app/Repositories/Category/CategoryRepositoryInterface.php
+++ b/app/Repositories/Category/CategoryRepositoryInterface.php
@@ -53,11 +53,20 @@ interface CategoryRepositoryInterface
* Find a category.
*
* @param int $categoryId
- *
+ * @deprecated
* @return Category
*/
public function find(int $categoryId): Category;
+ /**
+ * Find a category or return NULL
+ *
+ * @param int $categoryId
+ *
+ * @return Category|null
+ */
+ public function findNull(int $categoryId): ?Category;
+
/**
* Find a category.
*
diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php
index 2dbff1614e..822f9dee22 100644
--- a/app/Repositories/Currency/CurrencyRepository.php
+++ b/app/Repositories/Currency/CurrencyRepository.php
@@ -129,6 +129,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $currency;
}
+ /**
+ * Find by currency code, return NULL if unfound.
+ *
+ * @param string $currencyCode
+ *
+ * @return TransactionCurrency|null
+ */
+ public function findByCodeNull(string $currencyCode): ?TransactionCurrency
+ {
+ return TransactionCurrency::where('code', $currencyCode)->first();
+ }
+
/**
* Find by currency name.
*
@@ -163,6 +175,21 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $currency;
}
+ /**
+ * Find by ID, return NULL if not found.
+ *
+ * @param int $currencyId
+ *
+ * @return TransactionCurrency|null
+ */
+ public function findNull(int $currencyId): ?TransactionCurrency
+ {
+ /** @var TransactionCurrency $res */
+ $res = TransactionCurrency::find($currencyId);
+
+ return $res;
+ }
+
/**
* @return Collection
*/
diff --git a/app/Repositories/Currency/CurrencyRepositoryInterface.php b/app/Repositories/Currency/CurrencyRepositoryInterface.php
index e7d2d66eb3..60df5cf396 100644
--- a/app/Repositories/Currency/CurrencyRepositoryInterface.php
+++ b/app/Repositories/Currency/CurrencyRepositoryInterface.php
@@ -60,6 +60,7 @@ interface CurrencyRepositoryInterface
*
* @param int $currencyId
*
+ * @deprecated
* @return TransactionCurrency
*/
public function find(int $currencyId): TransactionCurrency;
@@ -67,12 +68,23 @@ interface CurrencyRepositoryInterface
/**
* Find by currency code.
*
+ * @deprecated
+ *
* @param string $currencyCode
*
* @return TransactionCurrency
*/
public function findByCode(string $currencyCode): TransactionCurrency;
+ /**
+ * Find by currency code, return NULL if unfound.
+ *
+ * @param string $currencyCode
+ *
+ * @return TransactionCurrency|null
+ */
+ public function findByCodeNull(string $currencyCode): ?TransactionCurrency;
+
/**
* Find by currency name.
*
@@ -91,6 +103,15 @@ interface CurrencyRepositoryInterface
*/
public function findBySymbol(string $currencySymbol): TransactionCurrency;
+ /**
+ * Find by ID, return NULL if not found.
+ *
+ * @param int $currencyId
+ *
+ * @return TransactionCurrency|null
+ */
+ public function findNull(int $currencyId): ?TransactionCurrency;
+
/**
* @return Collection
*/
diff --git a/app/TransactionRules/Actions/SetDestinationAccount.php b/app/TransactionRules/Actions/SetDestinationAccount.php
index e522903edf..f2869624b5 100644
--- a/app/TransactionRules/Actions/SetDestinationAccount.php
+++ b/app/TransactionRules/Actions/SetDestinationAccount.php
@@ -117,7 +117,7 @@ class SetDestinationAccount implements ActionInterface
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::DEFAULT, AccountType::ASSET]);
- if (null === $account->id) {
+ if (null === $account) {
Log::debug(sprintf('There is NO asset account called "%s".', $this->action->action_value));
return false;
@@ -134,7 +134,7 @@ class SetDestinationAccount implements ActionInterface
private function findExpenseAccount()
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::EXPENSE]);
- if (null === $account->id) {
+ if (null === $account) {
// create new revenue account with this name:
$data = [
'name' => $this->action->action_value,
diff --git a/app/TransactionRules/Actions/SetSourceAccount.php b/app/TransactionRules/Actions/SetSourceAccount.php
index 5244211892..1f90c8040e 100644
--- a/app/TransactionRules/Actions/SetSourceAccount.php
+++ b/app/TransactionRules/Actions/SetSourceAccount.php
@@ -116,7 +116,7 @@ class SetSourceAccount implements ActionInterface
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::DEFAULT, AccountType::ASSET]);
- if (null === $account->id) {
+ if (null === $account) {
Log::debug(sprintf('There is NO asset account called "%s".', $this->action->action_value));
return false;
@@ -133,7 +133,7 @@ class SetSourceAccount implements ActionInterface
private function findRevenueAccount()
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::REVENUE]);
- if (null === $account->id) {
+ if (null === $account) {
// create new revenue account with this name:
$data = [
'name' => $this->action->action_value,