Code cleanup.

This commit is contained in:
James Cole
2018-04-02 14:42:07 +02:00
parent f96f38b172
commit 7d02d0f762
55 changed files with 200 additions and 281 deletions

View File

@@ -43,7 +43,6 @@ class AccountFactory
* @param array $data
*
* @return Account
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function create(array $data): Account
{
@@ -64,8 +63,8 @@ class AccountFactory
'user_id' => $this->user->id,
'account_type_id' => $type->id,
'name' => $data['name'],
'virtual_balance' => strlen(strval($data['virtualBalance'])) === 0 ? '0' : $data['virtualBalance'],
'active' => true === $data['active'] ? true : false,
'virtual_balance' => $data['virtualBalance'] ?? '0',
'active' => true === $data['active'],
'iban' => $data['iban'],
];
@@ -117,8 +116,6 @@ class AccountFactory
* @param string $accountType
*
* @return Account
* @throws \FireflyIII\Exceptions\FireflyException
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function findOrCreate(string $accountName, string $accountType): Account
{
@@ -161,13 +158,13 @@ class AccountFactory
*/
protected function getAccountType(?int $accountTypeId, ?string $accountType): ?AccountType
{
$accountTypeId = intval($accountTypeId);
$accountTypeId = (int)$accountTypeId;
if ($accountTypeId > 0) {
return AccountType::find($accountTypeId);
}
$type = config('firefly.accountTypeByIdentifier.' . strval($accountType));
$type = config('firefly.accountTypeByIdentifier.' . (string)$accountType);
$result = AccountType::whereType($type)->first();
if (is_null($result) && !is_null($accountType)) {
if (null === $result && null !== $accountType) {
// try as full name:
$result = AccountType::whereType($accountType)->first();
}

View File

@@ -47,7 +47,7 @@ class BillFactory
{
$matchArray = explode(',', $data['match']);
$matchArray = array_unique($matchArray);
$match = join(',', $matchArray);
$match = implode(',', $matchArray);
/** @var Bill $bill */
$bill = Bill::create(
@@ -81,14 +81,14 @@ class BillFactory
*/
public function find(?int $billId, ?string $billName): ?Bill
{
$billId = intval($billId);
$billName = strval($billName);
$billId = (int)$billId;
$billName = (string)$billName;
// first find by ID:
if ($billId > 0) {
/** @var Bill $bill */
$bill = $this->user->bills()->find($billId);
if (!is_null($bill)) {
if (null !== $bill) {
return $bill;
}
}
@@ -96,7 +96,7 @@ class BillFactory
// then find by name:
if (strlen($billName) > 0) {
$bill = $this->findByName($billName);
if (!is_null($bill)) {
if (null !== $bill) {
return $bill;
}
}

View File

@@ -44,8 +44,8 @@ class BudgetFactory
*/
public function find(?int $budgetId, ?string $budgetName): ?Budget
{
$budgetId = intval($budgetId);
$budgetName = strval($budgetName);
$budgetId = (int)$budgetId;
$budgetName = (string)$budgetName;
if (strlen($budgetName) === 0 && $budgetId === 0) {
return null;
@@ -55,14 +55,14 @@ class BudgetFactory
if ($budgetId > 0) {
/** @var Budget $budget */
$budget = $this->user->budgets()->find($budgetId);
if (!is_null($budget)) {
if (null !== $budget) {
return $budget;
}
}
if (strlen($budgetName) > 0) {
$budget = $this->findByName($budgetName);
if (!is_null($budget)) {
if (null !== $budget) {
return $budget;
}
}

View File

@@ -44,16 +44,18 @@ class CategoryFactory
*/
public function findByName(string $name): ?Category
{
$result = null;
/** @var Collection $collection */
$collection = $this->user->categories()->get();
/** @var Category $category */
foreach ($collection as $category) {
if ($category->name === $name) {
return $category;
$result = $category;
break;
}
}
return null;
return $result;
}
/**
@@ -64,8 +66,8 @@ class CategoryFactory
*/
public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category
{
$categoryId = intval($categoryId);
$categoryName = strval($categoryName);
$categoryId = (int)$categoryId;
$categoryName = (string)$categoryName;
Log::debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName));
@@ -76,14 +78,14 @@ class CategoryFactory
if ($categoryId > 0) {
/** @var Category $category */
$category = $this->user->categories()->find($categoryId);
if (!is_null($category)) {
if (null !== $category) {
return $category;
}
}
if (strlen($categoryName) > 0) {
$category = $this->findByName($categoryName);
if (!is_null($category)) {
if (null !== $category) {
return $category;
}

View File

@@ -46,7 +46,7 @@ class PiggyBankEventFactory
public function create(TransactionJournal $journal, ?PiggyBank $piggyBank): ?PiggyBankEvent
{
Log::debug(sprintf('Now in PiggyBankEventCreate for a %s', $journal->transactionType->type));
if (is_null($piggyBank)) {
if (null === $piggyBank) {
return null;
}

View File

@@ -43,8 +43,8 @@ class PiggyBankFactory
*/
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
{
$piggyBankId = intval($piggyBankId);
$piggyBankName = strval($piggyBankName);
$piggyBankId = (int)$piggyBankId;
$piggyBankName = (string)$piggyBankName;
if (strlen($piggyBankName) === 0 && $piggyBankId === 0) {
return null;
}
@@ -52,7 +52,7 @@ class PiggyBankFactory
if ($piggyBankId > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->user->piggyBanks()->find($piggyBankId);
if (!is_null($piggyBank)) {
if (null !== $piggyBank) {
return $piggyBank;
}
}
@@ -61,7 +61,7 @@ class PiggyBankFactory
if (strlen($piggyBankName) > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->findByName($piggyBankName);
if (!is_null($piggyBank)) {
if (null !== $piggyBank) {
return $piggyBank;
}
}

View File

@@ -65,7 +65,7 @@ class TagFactory
*/
public function findOrCreate(string $tag): ?Tag
{
if (is_null($this->tags)) {
if (null === $this->tags) {
$this->tags = $this->user->tags()->get();
}

View File

@@ -65,24 +65,24 @@ class TransactionCurrencyFactory
*/
public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
$currencyCode = strval($currencyCode);
$currencyId = intval($currencyId);
$currencyCode = (string)$currencyCode;
$currencyId = (int)$currencyId;
if (strlen($currencyCode) === 0 && intval($currencyId) === 0) {
if (strlen($currencyCode) === 0 && (int)$currencyId === 0) {
return null;
}
// first by ID:
if ($currencyId > 0) {
$currency = TransactionCurrency::find($currencyId);
if (!is_null($currency)) {
if (null !== $currency) {
return $currency;
}
}
// then by code:
if (strlen($currencyCode) > 0) {
$currency = TransactionCurrency::whereCode($currencyCode)->first();
if (!is_null($currency)) {
if (null !== $currency) {
return $currency;
}
}

View File

@@ -90,7 +90,7 @@ class TransactionFactory
$source = $this->create(
[
'description' => $description,
'amount' => app('steam')->negative(strval($data['amount'])),
'amount' => app('steam')->negative((string)$data['amount']),
'foreign_amount' => null,
'currency' => $currency,
'account' => $sourceAccount,
@@ -103,7 +103,7 @@ class TransactionFactory
$dest = $this->create(
[
'description' => $description,
'amount' => app('steam')->positive(strval($data['amount'])),
'amount' => app('steam')->positive((string)$data['amount']),
'foreign_amount' => null,
'currency' => $currency,
'account' => $destinationAccount,
@@ -119,9 +119,9 @@ class TransactionFactory
$this->setForeignCurrency($dest, $foreign);
// set foreign amount:
if (!is_null($data['foreign_amount'])) {
$this->setForeignAmount($source, app('steam')->negative(strval($data['foreign_amount'])));
$this->setForeignAmount($dest, app('steam')->positive(strval($data['foreign_amount'])));
if (null !== $data['foreign_amount']) {
$this->setForeignAmount($source, app('steam')->negative((string)$data['foreign_amount']));
$this->setForeignAmount($dest, app('steam')->positive((string)$data['foreign_amount']));
}
// set budget:

View File

@@ -40,8 +40,6 @@ class TransactionJournalFactory
private $user;
/**
* Create a new transaction journal and associated transactions.
*
* @param array $data
*
* @return TransactionJournal
@@ -89,11 +87,11 @@ class TransactionJournalFactory
$this->connectTags($journal, $data);
// store note:
$this->storeNote($journal, strval($data['notes']));
$this->storeNote($journal, (string)$data['notes']);
// store date meta fields (if present):
$fields = ['sepa-cc', 'sepa-ct-op', 'sepa-ct-id', 'sepa-db', 'sepa-country', 'sepa-ep', 'sepa-ci', 'interest_date', 'book_date', 'process_date',
'due_date', 'payment_date', 'invoice_date', 'internal_reference','bunq_payment_id'];
'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'bunq_payment_id'];
foreach ($fields as $field) {
$this->storeMeta($journal, $data, $field);
@@ -124,7 +122,7 @@ class TransactionJournalFactory
$factory->setUser($this->user);
$piggyBank = $factory->find($data['piggy_bank_id'], $data['piggy_bank_name']);
if (!is_null($piggyBank)) {
if (null !== $piggyBank) {
/** @var PiggyBankEventFactory $factory */
$factory = app(PiggyBankEventFactory::class);
$factory->create($journal, $piggyBank);
@@ -144,7 +142,8 @@ class TransactionJournalFactory
{
$factory = app(TransactionTypeFactory::class);
$transactionType = $factory->find($type);
if (is_null($transactionType)) {
if (null === $transactionType) {
Log::error(sprintf('Could not find transaction type for "%s"', $type)); // @codeCoverageIgnore
throw new FireflyException(sprintf('Could not find transaction type for "%s"', $type)); // @codeCoverageIgnore
}