Code cleanup

This commit is contained in:
James Cole
2018-04-02 14:50:17 +02:00
parent 379b104778
commit fa7ab45a40
100 changed files with 440 additions and 517 deletions

View File

@@ -127,11 +127,10 @@ trait AccountServiceTrait
* @param array $data
*
* @return TransactionJournal|null
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function storeIBJournal(Account $account, array $data): ?TransactionJournal
{
$amount = strval($data['openingBalance']);
$amount = (string)$data['openingBalance'];
Log::debug(sprintf('Submitted amount is %s', $amount));
if (0 === bccomp($amount, '0')) {
@@ -145,7 +144,7 @@ trait AccountServiceTrait
'type' => TransactionType::OPENING_BALANCE,
'user' => $account->user->id,
'transaction_currency_id' => $currencyId,
'description' => strval(trans('firefly.initial_balance_description', ['account' => $account->name])),
'description' => (string)trans('firefly.initial_balance_description', ['account' => $account->name]),
'completed' => true,
'date' => $data['openingBalanceDate'],
'bill_id' => null,
@@ -232,7 +231,6 @@ trait AccountServiceTrait
* @param array $data
*
* @return bool
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function updateIB(Account $account, array $data): bool
{
@@ -268,9 +266,9 @@ trait AccountServiceTrait
public function updateIBJournal(Account $account, TransactionJournal $journal, array $data): bool
{
$date = $data['openingBalanceDate'];
$amount = strval($data['openingBalance']);
$amount = (string)$data['openingBalance'];
$negativeAmount = bcmul($amount, '-1');
$currencyId = intval($data['currency_id']);
$currencyId = (int)$data['currency_id'];
Log::debug(sprintf('Submitted amount for opening balance to update is "%s"', $amount));
if (0 === bccomp($amount, '0')) {
@@ -291,13 +289,13 @@ trait AccountServiceTrait
// update transactions:
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
if (intval($account->id) === intval($transaction->account_id)) {
if ((int)$account->id === (int)$transaction->account_id) {
Log::debug(sprintf('Will (eq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $amount));
$transaction->amount = $amount;
$transaction->transaction_currency_id = $currencyId;
$transaction->save();
}
if (!(intval($account->id) === intval($transaction->account_id))) {
if (!((int)$account->id === (int)$transaction->account_id)) {
Log::debug(sprintf('Will (neq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $negativeAmount));
$transaction->amount = $negativeAmount;
$transaction->transaction_currency_id = $currencyId;
@@ -383,9 +381,8 @@ trait AccountServiceTrait
*/
public function validIBData(array $data): bool
{
$data['openingBalance'] = strval($data['openingBalance'] ?? '');
if (isset($data['openingBalance']) && null !== $data['openingBalance'] && strlen($data['openingBalance']) > 0
&& isset($data['openingBalanceDate'])) {
$data['openingBalance'] = (string)($data['openingBalance'] ?? '');
if (isset($data['openingBalance'], $data['openingBalanceDate']) && \strlen($data['openingBalance']) > 0) {
Log::debug('Array has valid opening balance data.');
return true;

View File

@@ -72,7 +72,7 @@ trait JournalServiceTrait
$factory->setUser($journal->user);
$bill = $factory->find($data['bill_id'], $data['bill_name']);
if (!is_null($bill)) {
if (null !== $bill) {
$journal->bill_id = $bill->id;
$journal->save();
@@ -110,10 +110,10 @@ trait JournalServiceTrait
*/
protected function storeNote(TransactionJournal $journal, ?string $notes): void
{
$notes = strval($notes);
$notes = (string)$notes;
if (strlen($notes) > 0) {
$note = $journal->notes()->first();
if (is_null($note)) {
if (null === $note) {
$note = new Note;
$note->noteable()->associate($journal);
}
@@ -123,7 +123,7 @@ trait JournalServiceTrait
return;
}
$note = $journal->notes()->first();
if (!is_null($note)) {
if (null !== $note) {
$note->delete();
}

View File

@@ -104,12 +104,12 @@ trait TransactionServiceTrait
*/
public function findAccount(?string $expectedType, ?int $accountId, ?string $accountName): Account
{
$accountId = intval($accountId);
$accountName = strval($accountName);
$accountId = (int)$accountId;
$accountName = (string)$accountName;
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($this->user);
if (is_null($expectedType)) {
if (null === $expectedType) {
return $repository->findNull($accountId);
}
@@ -215,7 +215,7 @@ trait TransactionServiceTrait
*/
protected function setBudget(Transaction $transaction, ?Budget $budget): void
{
if (is_null($budget)) {
if (null === $budget) {
$transaction->budgets()->sync([]);
return;
@@ -232,7 +232,7 @@ trait TransactionServiceTrait
*/
protected function setCategory(Transaction $transaction, ?Category $category): void
{
if (is_null($category)) {
if (null === $category) {
$transaction->categories()->sync([]);
return;
@@ -259,7 +259,7 @@ trait TransactionServiceTrait
*/
protected function setForeignCurrency(Transaction $transaction, ?TransactionCurrency $currency): void
{
if (is_null($currency)) {
if (null === $currency) {
$transaction->foreign_currency_id = null;
$transaction->save();

View File

@@ -41,7 +41,6 @@ class AccountUpdateService
* @param array $data
*
* @return Account
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function update(Account $account, array $data): Account
{
@@ -68,7 +67,7 @@ class AccountUpdateService
// update note:
if (isset($data['notes']) && null !== $data['notes']) {
$this->updateNote($account, strval($data['notes']));
$this->updateNote($account, (string)$data['notes']);
}
return $account;

View File

@@ -45,7 +45,7 @@ class BillUpdateService
$matchArray = explode(',', $data['match']);
$matchArray = array_unique($matchArray);
$match = join(',', $matchArray);
$match = implode(',', $matchArray);
$bill->name = $data['name'];
$bill->match = $match;
@@ -60,7 +60,7 @@ class BillUpdateService
// update note:
if (isset($data['notes']) && null !== $data['notes']) {
$this->updateNote($bill, strval($data['notes']));
$this->updateNote($bill, (string)$data['notes']);
}
return $bill;

View File

@@ -45,7 +45,7 @@ class TransactionUpdateService
public function reconcile(int $transactionId): ?Transaction
{
$transaction = Transaction::find($transactionId);
if (!is_null($transaction)) {
if (null !== $transaction) {
$transaction->reconciled = true;
$transaction->save();
@@ -79,20 +79,20 @@ class TransactionUpdateService
// update description:
$transaction->description = $description;
$foreignAmount = null;
if (floatval($transaction->amount) < 0) {
if ((float)$transaction->amount < 0) {
// this is the source transaction.
$type = $this->accountType($journal, 'source');
$account = $this->findAccount($type, $data['source_id'], $data['source_name']);
$amount = app('steam')->negative(strval($data['amount']));
$foreignAmount = app('steam')->negative(strval($data['foreign_amount']));
$amount = app('steam')->negative((string)$data['amount']);
$foreignAmount = app('steam')->negative((string)$data['foreign_amount']);
}
if (floatval($transaction->amount) > 0) {
if ((float)$transaction->amount > 0) {
// this is the destination transaction.
$type = $this->accountType($journal, 'destination');
$account = $this->findAccount($type, $data['destination_id'], $data['destination_name']);
$amount = app('steam')->positive(strval($data['amount']));
$foreignAmount = app('steam')->positive(strval($data['foreign_amount']));
$amount = app('steam')->positive((string)$data['amount']);
$foreignAmount = app('steam')->positive((string)$data['foreign_amount']);
}
// update the actual transaction:
@@ -107,11 +107,11 @@ class TransactionUpdateService
// set foreign currency
$foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']);
// set foreign amount:
if (!is_null($data['foreign_amount']) && !is_null($foreign)) {
if (null !== $data['foreign_amount'] && null !== $foreign) {
$this->setForeignCurrency($transaction, $foreign);
$this->setForeignAmount($transaction, $foreignAmount);
}
if (is_null($data['foreign_amount']) || is_null($foreign)) {
if (null === $data['foreign_amount'] || null === $foreign) {
$this->setForeignCurrency($transaction, null);
$this->setForeignAmount($transaction, null);
}