diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 08c263fb3a..4a9d49b8e1 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -44,6 +44,7 @@ class Handler extends ExceptionHandler * Report or log an exception. * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. + * @SuppressWarnings(PHPMD.ShortVariable) * * @param \Exception $e * diff --git a/app/Handlers/Events/ConnectJournalToPiggyBank.php b/app/Handlers/Events/ConnectJournalToPiggyBank.php index d9a46886db..d520d7c177 100644 --- a/app/Handlers/Events/ConnectJournalToPiggyBank.php +++ b/app/Handlers/Events/ConnectJournalToPiggyBank.php @@ -6,7 +6,6 @@ use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; -use Log; /** * Class ConnectJournalToPiggyBank @@ -28,6 +27,8 @@ class ConnectJournalToPiggyBank /** * Handle the event when journal is saved. * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * * @param JournalCreated $event * * @return boolean @@ -37,45 +38,25 @@ class ConnectJournalToPiggyBank /** @var TransactionJournal $journal */ $journal = $event->journal; $piggyBankId = $event->piggyBankId; - if (intval($piggyBankId) < 1) { - return false; - } - - Log::debug('JournalCreated event: ' . $journal->id . ', ' . $piggyBankId); /** @var PiggyBank $piggyBank */ $piggyBank = Auth::user()->piggybanks()->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']); - if (is_null($piggyBank) || $journal->transactionType->type != 'Transfer') { - return false; - } - Log::debug('Found a piggy bank'); - $amount = $journal->amount; - Log::debug('Amount: ' . $amount); - if ($amount == 0) { + if (is_null($piggyBank)) { return false; } // update piggy bank rep for date of transaction journal. $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first(); if (is_null($repetition)) { - Log::debug('Found no repetition for piggy bank for date ' . $journal->date->format('Y M d')); - return false; } - Log::debug('Found rep! ' . $repetition->id); - - /* - * Add amount when - */ + $amount = $journal->amount; /** @var Transaction $transaction */ foreach ($journal->transactions()->get() as $transaction) { if ($transaction->account_id == $piggyBank->account_id) { if ($transaction->amount < 0) { - $amount = $amount * -1; - Log::debug('Transaction is away from piggy, so amount becomes ' . $amount); - } else { - Log::debug('Transaction is to from piggy, so amount stays ' . $amount); + $amount = $transaction->amount * -1; } } } @@ -83,14 +64,7 @@ class ConnectJournalToPiggyBank $repetition->currentamount += $amount; $repetition->save(); - PiggyBankEvent::create( - [ - 'piggy_bank_id' => $piggyBank->id, - 'transaction_journal_id' => $journal->id, - 'date' => $journal->date, - 'amount' => $amount - ] - ); + PiggyBankEvent::create(['piggy_bank_id' => $piggyBank->id, 'transaction_journal_id' => $journal->id, 'date' => $journal->date, 'amount' => $amount]); return true; diff --git a/app/Helpers/Collection/Expense.php b/app/Helpers/Collection/Expense.php index 022c754bf3..3c0d908ab6 100644 --- a/app/Helpers/Collection/Expense.php +++ b/app/Helpers/Collection/Expense.php @@ -34,19 +34,19 @@ class Expense public function addOrCreateExpense(TransactionJournal $entry) { - $id = $entry->account_id; - if (!$this->expenses->has($id)) { + $accountId = $entry->account_id; + if (!$this->expenses->has($accountId)) { $newObject = new stdClass; $newObject->amount = floatval($entry->queryAmount); $newObject->name = $entry->name; $newObject->count = 1; - $newObject->id = $id; - $this->expenses->put($id, $newObject); + $newObject->id = $accountId; + $this->expenses->put($accountId, $newObject); } else { - $existing = $this->expenses->get($id); + $existing = $this->expenses->get($accountId); $existing->amount += floatval($entry->queryAmount); $existing->count++; - $this->expenses->put($id, $existing); + $this->expenses->put($accountId, $existing); } } diff --git a/app/Helpers/Collection/Income.php b/app/Helpers/Collection/Income.php index ad8586846e..c3e60e0cf7 100644 --- a/app/Helpers/Collection/Income.php +++ b/app/Helpers/Collection/Income.php @@ -35,19 +35,19 @@ class Income public function addOrCreateIncome(TransactionJournal $entry) { - $id = $entry->account_id; - if (!$this->incomes->has($id)) { + $accountId = $entry->account_id; + if (!$this->incomes->has($accountId)) { $newObject = new stdClass; $newObject->amount = floatval($entry->queryAmount); $newObject->name = $entry->name; $newObject->count = 1; - $newObject->id = $id; - $this->incomes->put($id, $newObject); + $newObject->id = $accountId; + $this->incomes->put($accountId, $newObject); } else { - $existing = $this->incomes->get($id); + $existing = $this->incomes->get($accountId); $existing->amount += floatval($entry->queryAmount); $existing->count++; - $this->incomes->put($id, $existing); + $this->incomes->put($accountId, $existing); } } diff --git a/app/Helpers/Report/ReportQuery.php b/app/Helpers/Report/ReportQuery.php index 5a31f9f502..ab5696e562 100644 --- a/app/Helpers/Report/ReportQuery.php +++ b/app/Helpers/Report/ReportQuery.php @@ -40,18 +40,16 @@ class ReportQuery implements ReportQueryInterface { $query = $this->queryJournalsWithTransactions($start, $end); if ($includeShared === false) { - // only get withdrawals not from a shared account - // and transfers from a shared account. $query->where( function (Builder $query) { $query->where( - function (Builder $q) { + function (Builder $q) { // only get withdrawals not from a shared account $q->where('transaction_types.type', 'Withdrawal'); $q->where('acm_from.data', '!=', '"sharedAsset"'); } ); $query->orWhere( - function (Builder $q) { + function (Builder $q) { // and transfers from a shared account. $q->where('transaction_types.type', 'Transfer'); $q->where('acm_to.data', '=', '"sharedAsset"'); } @@ -59,24 +57,14 @@ class ReportQuery implements ReportQueryInterface } ); } else { - // any withdrawal is fine. - $query->where('transaction_types.type', 'Withdrawal'); + $query->where('transaction_types.type', 'Withdrawal'); // any withdrawal is fine. } $query->groupBy('transaction_journals.id')->orderBy('transaction_journals.date'); // get everything, decrypt and return - $data = $query->get( - ['transaction_journals.id', - 'transaction_journals.description', - 'transaction_journals.encrypted', - 'transaction_types.type', + $data = $query->get(['transaction_journals.id', 'transaction_journals.description', 'transaction_journals.encrypted', 'transaction_types.type', DB::Raw('SUM(`t_from`.`amount`) as `queryAmount`'), - 'transaction_journals.date', - 't_to.account_id as account_id', - 'ac_to.name as name', - 'ac_to.encrypted as account_encrypted' - ] - ); + 'transaction_journals.date', 't_to.account_id as account_id', 'ac_to.name as name', 'ac_to.encrypted as account_encrypted']); $data->each( function (Model $object) { diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 4073013202..d7c78c63d3 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -52,9 +52,7 @@ class TransactionController extends Controller $subTitle = trans('form.add_new_' . $what); foreach ($respondTo as $r) { - if (!is_null(Input::get($r))) { - $preFilled[$r] = Input::get($r); - } + $preFilled[$r] = Input::get($r); } Session::put('preFilled', $preFilled); @@ -269,7 +267,9 @@ class TransactionController extends Controller // rescan journal, UpdateJournalConnection event(new JournalSaved($journal)); // ConnectJournalToPiggyBank - event(new JournalCreated($journal, intval($request->get('piggy_bank_id')))); + if ($journal->transactionType->type == 'Transfer' && intval($request->get('piggy_bank_id')) > 0) { + event(new JournalCreated($journal, intval($request->get('piggy_bank_id')))); + } $repository->deactivateReminder($request->get('reminder_id')); diff --git a/app/Http/Middleware/Range.php b/app/Http/Middleware/Range.php index aa7fd7a98b..9faf2b4e00 100644 --- a/app/Http/Middleware/Range.php +++ b/app/Http/Middleware/Range.php @@ -43,6 +43,7 @@ class Range * * @param \Illuminate\Http\Request $request * @param \Closure $theNext + * @SuppressWarnings(PHPMD.CyclomaticComplexity) * * @return mixed */ @@ -63,9 +64,6 @@ class Range Session::put('end', $end); } if (!Session::has('first')) { - /** - * Get helper thing. - */ /** @var \FireflyIII\Repositories\Journal\JournalRepositoryInterface $repository */ $repository = App::make('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); $journal = $repository->first(); @@ -75,16 +73,12 @@ class Range Session::put('first', Carbon::now()->startOfYear()); } } - - // set current / next / prev month. $current = Carbon::now()->format('F Y'); $next = Carbon::now()->endOfMonth()->addDay()->format('F Y'); $prev = Carbon::now()->startOfMonth()->subDay()->format('F Y'); View::share('currentMonthName', $current); View::share('previousMonthName', $prev); View::share('nextMonthName', $next); - - } return $theNext($request); diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index 2ae445e1e9..1e3d4e0d53 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -50,12 +50,11 @@ class JournalFormRequest extends Request /** * @return array * @throws Exception + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function rules() { - // can we switch on the "what"? - $what = Input::get('what'); - + $what = Input::get('what'); $rules = [ 'description' => 'required|min:1,max:255', 'what' => 'required|in:withdrawal,deposit,transfer', @@ -74,8 +73,6 @@ class JournalFormRequest extends Request if (intval(Input::get('budget_id')) != 0) { $rules['budget_id'] = 'exists:budgets,id|belongsToUser:budgets'; } - - break; case 'deposit': $rules['category'] = 'between:1,255'; @@ -93,7 +90,5 @@ class JournalFormRequest extends Request } return $rules; - - } } diff --git a/app/Models/Account.php b/app/Models/Account.php index d3d7b4545a..d9733d77fd 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -28,7 +28,7 @@ class Account extends Model /** * @param array $fields - * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) * * @return Account|null */ diff --git a/app/Models/Category.php b/app/Models/Category.php index 3f1e029535..a37dae2fff 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -37,6 +37,7 @@ class Category extends Model /** * @param array $fields + * @SuppressWarnings(PHPMD.CyclomaticComplexity) * * @return Account|null */ diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 6312b37df9..68955aa201 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -29,6 +29,8 @@ class Tag extends Model /** * @param array $fields + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) * * @return Tag|null */ diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index fd52a224f7..9299f12fcb 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -49,7 +49,47 @@ class EventServiceProvider extends ServiceProvider public function boot(DispatcherContract $events) { parent::boot($events); + $this->registerDeleteEvents(); + $this->registerCreateEvents(); + BudgetLimit::saved( + function (BudgetLimit $budgetLimit) { + $end = Navigation::addPeriod(clone $budgetLimit->startdate, $budgetLimit->repeat_freq, 0); + $end->subDay(); + $set = $budgetLimit->limitrepetitions()->where('startdate', $budgetLimit->startdate->format('Y-m-d'))->where('enddate', $end->format('Y-m-d')) + ->get(); + if ($set->count() == 0) { + $repetition = new LimitRepetition; + $repetition->startdate = $budgetLimit->startdate; + $repetition->enddate = $end; + $repetition->amount = $budgetLimit->amount; + $repetition->budgetLimit()->associate($budgetLimit); + + try { + $repetition->save(); + } catch (QueryException $e) { + Log::error('Trying to save new LimitRepetition failed!'); + Log::error($e->getMessage()); + } + } else { + if ($set->count() == 1) { + $repetition = $set->first(); + $repetition->amount = $budgetLimit->amount; + $repetition->save(); + + } + } + } + ); + + + } + + /** + * + */ + protected function registerDeleteEvents() + { TransactionJournal::deleted( function (TransactionJournal $journal) { @@ -59,8 +99,6 @@ class EventServiceProvider extends ServiceProvider } } ); - - PiggyBank::deleting( function (PiggyBank $piggyBank) { $reminders = $piggyBank->reminders()->get(); @@ -82,6 +120,14 @@ class EventServiceProvider extends ServiceProvider } ); + } + + /** + * + */ + protected function registerCreateEvents() + { + // move this routine to a filter // in case of repeated piggy banks and/or other problems. PiggyBank::created( @@ -94,47 +140,6 @@ class EventServiceProvider extends ServiceProvider $repetition->save(); } ); - - BudgetLimit::saved( - function (BudgetLimit $budgetLimit) { - - $end = Navigation::addPeriod(clone $budgetLimit->startdate, $budgetLimit->repeat_freq, 0); - $end->subDay(); - - $set = $budgetLimit->limitrepetitions()->where('startdate', $budgetLimit->startdate->format('Y-m-d'))->where('enddate', $end->format('Y-m-d')) - ->get(); - /* - * Create new LimitRepetition: - */ - if ($set->count() == 0) { - - $repetition = new LimitRepetition; - $repetition->startdate = $budgetLimit->startdate; - $repetition->enddate = $end; - $repetition->amount = $budgetLimit->amount; - $repetition->budgetLimit()->associate($budgetLimit); - - try { - $repetition->save(); - } catch (QueryException $e) { - Log::error('Trying to save new LimitRepetition failed!'); - Log::error($e->getMessage()); - } - } else { - if ($set->count() == 1) { - /* - * Update existing one. - */ - $repetition = $set->first(); - $repetition->amount = $budgetLimit->amount; - $repetition->save(); - - } - } - } - ); - - } } diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 71d2a5ba1d..e3e1a90fb9 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -46,6 +46,9 @@ class FireflyServiceProvider extends ServiceProvider Twig::addExtension(new Translation); } + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ public function register() { diff --git a/app/Support/Navigation.php b/app/Support/Navigation.php index a885931c92..b9dd7a652a 100644 --- a/app/Support/Navigation.php +++ b/app/Support/Navigation.php @@ -65,21 +65,11 @@ class Navigation $currentEnd = clone $theCurrentEnd; $functionMap = [ - '1D' => 'addDay', - 'daily' => 'addDay', - '1W' => 'addWeek', - 'week' => 'addWeek', - 'weekly' => 'addWeek', - '1M' => 'addMonth', - 'month' => 'addMonth', - 'monthly' => 'addMonth', - '3M' => 'addMonths', - 'quarter' => 'addMonths', - 'quarterly' => 'addMonths', - '6M' => 'addMonths', - 'half-year' => 'addMonths', - 'year' => 'addYear', - 'yearly' => 'addYear', + '1D' => 'addDay', 'daily' => 'addDay', + '1W' => 'addWeek', 'week' => 'addWeek', 'weekly' => 'addWeek', + '1M' => 'addMonth', 'month' => 'addMonth', 'monthly' => 'addMonth', + '3M' => 'addMonths', 'quarter' => 'addMonths', 'quarterly' => 'addMonths', '6M' => 'addMonths', 'half-year' => 'addMonths', + 'year' => 'addYear', 'yearly' => 'addYear', ]; $modifierMap = [ 'quarter' => 3,