GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

app/Http/Controllers/AccountController.php (27 issues)

1
<?php
2
/**
3
 * AccountController.php
4
 * Copyright (c) 2017 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace FireflyIII\Http\Controllers;
24
25
use Carbon\Carbon;
26
use ExpandedForm;
27
use FireflyIII\Exceptions\FireflyException;
28
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
29
use FireflyIII\Http\Requests\AccountFormRequest;
30
use FireflyIII\Models\Account;
31
use FireflyIII\Models\AccountType;
32
use FireflyIII\Models\Note;
33
use FireflyIII\Models\Transaction;
34
use FireflyIII\Models\TransactionType;
35
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
36
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
37
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
38
use FireflyIII\Support\CacheProperties;
39
use Illuminate\Http\Request;
40
use Illuminate\Pagination\LengthAwarePaginator;
41
use Illuminate\Support\Collection;
42
use Preferences;
43
use Steam;
44
use View;
45
46
/**
47
 * Class AccountController.
48
 *
49
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
50
 */
51
class AccountController extends Controller
52
{
53
    /** @var CurrencyRepositoryInterface */
54
    private $currencyRepos;
55
    /** @var JournalRepositoryInterface */
56
    private $journalRepos;
57
    /** @var AccountRepositoryInterface */
58
    private $repository;
59
60
    /**
61
     *
62
     */
63
    public function __construct()
64
    {
65
        parent::__construct();
66
67
        // translations:
68
        $this->middleware(
69
            function ($request, $next) {
70
                app('view')->share('mainTitleIcon', 'fa-credit-card');
71
                app('view')->share('title', trans('firefly.accounts'));
72
73
                $this->repository    = app(AccountRepositoryInterface::class);
74
                $this->currencyRepos = app(CurrencyRepositoryInterface::class);
75
                $this->journalRepos  = app(JournalRepositoryInterface::class);
76
77
                return $next($request);
78
            }
79
        );
80
    }
81
82
    /**
83
     * @param Request $request
84
     * @param string  $what
85
     *
86
     * @return View
87
     */
88
    public function create(Request $request, string $what = 'asset')
89
    {
90
        $allCurrencies      = $this->currencyRepos->get();
91
        $currencySelectList = ExpandedForm::makeSelectList($allCurrencies);
0 ignored issues
show
The method makeSelectList() does not exist on FireflyIII\Support\Facades\ExpandedForm. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        /** @scrutinizer ignore-call */ 
92
        $currencySelectList = ExpandedForm::makeSelectList($allCurrencies);
Loading history...
92
        $defaultCurrency    = app('amount')->getDefaultCurrency();
0 ignored issues
show
The method getDefaultCurrency() does not exist on FireflyIII\Support\Facades\Amount. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        $defaultCurrency    = app('amount')->/** @scrutinizer ignore-call */ getDefaultCurrency();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
        $subTitleIcon       = config('firefly.subIconsByIdentifier.' . $what);
94
        $subTitle           = trans('firefly.make_new_' . $what . '_account');
95
        $roles              = [];
96
        foreach (config('firefly.accountRoles') as $role) {
97
            $roles[$role] = (string)trans('firefly.account_role_' . $role);
98
        }
99
100
        // pre fill some data
101
        $request->session()->flash('preFilled', ['currency_id' => $defaultCurrency->id]);
102
103
        // put previous url in session if not redirect from store (not "create another").
104
        if (true !== session('accounts.create.fromStore')) {
105
            $this->rememberPreviousUri('accounts.create.uri');
106
        }
107
        $request->session()->forget('accounts.create.fromStore');
108
109
        return view('accounts.create', compact('subTitleIcon', 'what', 'subTitle', 'currencySelectList', 'allCurrencies', 'roles'));
110
    }
111
112
    /**
113
     * @param Account $account
114
     *
115
     * @return View
116
     */
117
    public function delete(Account $account)
118
    {
119
        $typeName    = config('firefly.shortNamesByFullName.' . $account->accountType->type);
0 ignored issues
show
The property type does not seem to exist on FireflyIII\Models\AccountType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
120
        $subTitle    = trans('firefly.delete_' . $typeName . '_account', ['name' => $account->name]);
121
        $accountList = ExpandedForm::makeSelectListWithEmpty($this->repository->getAccountsByType([$account->accountType->type]));
0 ignored issues
show
The method makeSelectListWithEmpty() does not exist on FireflyIII\Support\Facades\ExpandedForm. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        /** @scrutinizer ignore-call */ 
122
        $accountList = ExpandedForm::makeSelectListWithEmpty($this->repository->getAccountsByType([$account->accountType->type]));
Loading history...
122
        unset($accountList[$account->id]);
123
124
        // put previous url in session
125
        $this->rememberPreviousUri('accounts.delete.uri');
126
127
        return view('accounts.delete', compact('account', 'subTitle', 'accountList'));
128
    }
129
130
    /**
131
     * @param Request $request
132
     * @param Account $account
133
     *
134
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
135
     */
136
    public function destroy(Request $request, Account $account)
137
    {
138
        $type     = $account->accountType->type;
0 ignored issues
show
The property type does not seem to exist on FireflyIII\Models\AccountType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
139
        $typeName = config('firefly.shortNamesByFullName.' . $type);
140
        $name     = $account->name;
141
        $moveTo   = $this->repository->findNull((int)$request->get('move_account_before_delete'));
142
143
        $this->repository->destroy($account, $moveTo);
144
145
        $request->session()->flash('success', (string)trans('firefly.' . $typeName . '_deleted', ['name' => $name]));
146
        Preferences::mark();
0 ignored issues
show
The method mark() does not exist on FireflyIII\Support\Facades\Preferences. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
        Preferences::/** @scrutinizer ignore-call */ 
147
                     mark();
Loading history...
147
148
        return redirect($this->getPreviousUri('accounts.delete.uri'));
149
    }
150
151
    /**
152
     * Edit an account.
153
     *
154
     * @param Request                    $request
155
     * @param Account                    $account
156
     *
157
     * @param AccountRepositoryInterface $repository
158
     *
159
     * @return View
160
     *
161
     * @SuppressWarnings(PHPMD.CyclomaticComplexity) // long and complex but not that excessively so.
162
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
163
     *
164
165
     */
166
    public function edit(Request $request, Account $account, AccountRepositoryInterface $repository)
167
    {
168
        $what               = config('firefly.shortNamesByFullName')[$account->accountType->type];
0 ignored issues
show
The property type does not seem to exist on FireflyIII\Models\AccountType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
169
        $subTitle           = trans('firefly.edit_' . $what . '_account', ['name' => $account->name]);
170
        $subTitleIcon       = config('firefly.subIconsByIdentifier.' . $what);
171
        $allCurrencies      = $this->currencyRepos->get();
172
        $currencySelectList = ExpandedForm::makeSelectList($allCurrencies);
173
        $roles              = [];
174
        foreach (config('firefly.accountRoles') as $role) {
175
            $roles[$role] = (string)trans('firefly.account_role_' . $role);
176
        }
177
178
        // put previous url in session if not redirect from store (not "return_to_edit").
179
        if (true !== session('accounts.edit.fromUpdate')) {
180
            $this->rememberPreviousUri('accounts.edit.uri');
181
        }
182
        $request->session()->forget('accounts.edit.fromUpdate');
183
184
        // pre fill some useful values.
185
186
        // the opening balance is tricky:
187
        $openingBalanceAmount = (string)$repository->getOpeningBalanceAmount($account);
188
        $openingBalanceDate   = $repository->getOpeningBalanceDate($account);
189
        $default              = app('amount')->getDefaultCurrency();
190
        $currency             = $this->currencyRepos->findNull((int)$repository->getMetaValue($account, 'currency_id'));
191
        if (null === $currency) {
192
            $currency = $default;
193
        }
194
195
        $preFilled = [
196
            'accountNumber'        => $repository->getMetaValue($account, 'accountNumber'),
197
            'accountRole'          => $repository->getMetaValue($account, 'accountRole'),
198
            'ccType'               => $repository->getMetaValue($account, 'ccType'),
199
            'ccMonthlyPaymentDate' => $repository->getMetaValue($account, 'ccMonthlyPaymentDate'),
200
            'BIC'                  => $repository->getMetaValue($account, 'BIC'),
201
            'openingBalanceDate'   => $openingBalanceDate,
202
            'openingBalance'       => $openingBalanceAmount,
203
            'virtualBalance'       => $account->virtual_balance,
0 ignored issues
show
The property virtual_balance does not seem to exist on FireflyIII\Models\Account. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
204
            'currency_id'          => $currency->id,
205
            'notes'                => '',
206
            'active'               => $account->active,
0 ignored issues
show
The property active does not seem to exist on FireflyIII\Models\Account. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
207
        ];
208
        /** @var Note $note */
209
        $note = $this->repository->getNote($account);
210
        if (null !== $note) {
211
            $preFilled['notes'] = $note->text;
212
        }
213
214
215
        $request->session()->flash('preFilled', $preFilled);
216
217
        return view(
218
            'accounts.edit',
219
            compact(
220
                'allCurrencies',
221
                'currencySelectList',
222
                'account',
223
                'currency',
224
                'subTitle',
225
                'subTitleIcon',
226
                'what',
227
                'roles',
228
                'preFilled'
229
            )
230
        );
231
    }
232
233
    /**
234
     * @param Request $request
235
     * @param string  $what
236
     *
237
     * @return View
238
     */
239
    public function index(Request $request, string $what)
240
    {
241
        $what         = $what ?? 'asset';
242
        $subTitle     = trans('firefly.' . $what . '_accounts');
243
        $subTitleIcon = config('firefly.subIconsByIdentifier.' . $what);
244
        $types        = config('firefly.accountTypesByIdentifier.' . $what);
245
        $collection   = $this->repository->getAccountsByType($types);
246
        $total        = $collection->count();
247
        $page         = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
248
        $pageSize     = (int)Preferences::get('listPageSize', 50)->data;
0 ignored issues
show
The method get() does not exist on FireflyIII\Support\Facades\Preferences. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

248
        $pageSize     = (int)Preferences::/** @scrutinizer ignore-call */ get('listPageSize', 50)->data;
Loading history...
249
        $accounts     = $collection->slice(($page - 1) * $pageSize, $pageSize);
250
        unset($collection);
251
        /** @var Carbon $start */
252
        $start = clone session('start', Carbon::now()->startOfMonth());
253
        /** @var Carbon $end */
254
        $end = clone session('end', Carbon::now()->endOfMonth());
255
        $start->subDay();
256
257
        $ids           = $accounts->pluck('id')->toArray();
258
        $startBalances = Steam::balancesByAccounts($accounts, $start);
0 ignored issues
show
The method balancesByAccounts() does not exist on FireflyIII\Support\Facades\Steam. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

258
        /** @scrutinizer ignore-call */ 
259
        $startBalances = Steam::balancesByAccounts($accounts, $start);
Loading history...
259
        $endBalances   = Steam::balancesByAccounts($accounts, $end);
260
        $activities    = Steam::getLastActivities($ids);
0 ignored issues
show
The method getLastActivities() does not exist on FireflyIII\Support\Facades\Steam. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

260
        /** @scrutinizer ignore-call */ 
261
        $activities    = Steam::getLastActivities($ids);
Loading history...
261
262
        $accounts->each(
263
            function (Account $account) use ($activities, $startBalances, $endBalances) {
264
                $account->lastActivityDate = $this->isInArray($activities, $account->id);
0 ignored issues
show
The property lastActivityDate does not seem to exist on FireflyIII\Models\Account. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
265
                $account->startBalance     = $this->isInArray($startBalances, $account->id);
0 ignored issues
show
The property startBalance does not seem to exist on FireflyIII\Models\Account. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
266
                $account->endBalance       = $this->isInArray($endBalances, $account->id);
0 ignored issues
show
The property endBalance does not seem to exist on FireflyIII\Models\Account. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
267
                $account->difference       = bcsub($account->endBalance, $account->startBalance);
0 ignored issues
show
The property difference does not seem to exist on FireflyIII\Models\Account. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
268
            }
269
        );
270
271
        // make paginator:
272
        $accounts = new LengthAwarePaginator($accounts, $total, $pageSize, $page);
273
        $accounts->setPath(route('accounts.index', [$what]));
274
275
        return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'page', 'accounts'));
276
    }
277
278
    /**
279
     * Show an account.
280
     *
281
     * @param Request     $request
282
     * @param Account     $account
283
     * @param Carbon|null $start
284
     * @param Carbon|null $end
285
     *
286
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
287
     *
288
     * @throws FireflyException
289
     *
290
     */
291
    public function show(Request $request, Account $account, Carbon $start = null, Carbon $end = null)
292
    {
293
        if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
0 ignored issues
show
The property type does not seem to exist on FireflyIII\Models\AccountType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
294
            return $this->redirectToOriginalAccount($account);
295
        }
296
        if (null === $start) {
297
            $start = session('start');
298
        }
299
        if (null === $end) {
300
            $end = session('end');
301
        }
302
        if ($end < $start) {
303
            throw new FireflyException('End is after start!'); // @codeCoverageIgnore
304
        }
305
306
        $today        = new Carbon;
307
        $subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type);
308
        $page         = (int)$request->get('page');
309
        $pageSize     = (int)Preferences::get('listPageSize', 50)->data;
310
        $currencyId   = (int)$this->repository->getMetaValue($account, 'currency_id');
311
        $currency     = $this->currencyRepos->findNull($currencyId);
312
        if (0 === $currencyId) {
313
            $currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
314
        }
315
        $fStart    = $start->formatLocalized($this->monthAndDayFormat);
0 ignored issues
show
The method formatLocalized() does not exist on Illuminate\Session\Store. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

315
        /** @scrutinizer ignore-call */ 
316
        $fStart    = $start->formatLocalized($this->monthAndDayFormat);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
316
        $fEnd      = $end->formatLocalized($this->monthAndDayFormat);
317
        $subTitle  = trans('firefly.journals_in_period_for_account', ['name' => $account->name, 'start' => $fStart, 'end' => $fEnd]);
318
        $chartUri  = route('chart.account.period', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]);
0 ignored issues
show
The method format() does not exist on Illuminate\Session\Store. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

318
        $chartUri  = route('chart.account.period', [$account->id, $start->/** @scrutinizer ignore-call */ format('Y-m-d'), $end->format('Y-m-d')]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
319
        $periods   = $this->getPeriodOverview($account, $end);
0 ignored issues
show
It seems like $end can also be of type Illuminate\Session\Store and Illuminate\Session\SessionManager; however, parameter $date of FireflyIII\Http\Controll...er::getPeriodOverview() does only seem to accept null|Carbon\Carbon, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

319
        $periods   = $this->getPeriodOverview($account, /** @scrutinizer ignore-type */ $end);
Loading history...
320
        $collector = app(JournalCollectorInterface::class);
321
        $collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page);
322
        $collector->setRange($start, $end);
0 ignored issues
show
It seems like $end can also be of type Illuminate\Session\Store and Illuminate\Session\SessionManager; however, parameter $end of FireflyIII\Helpers\Colle...orInterface::setRange() does only seem to accept Carbon\Carbon, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

322
        $collector->setRange($start, /** @scrutinizer ignore-type */ $end);
Loading history...
It seems like $start can also be of type Illuminate\Session\Store and Illuminate\Session\SessionManager; however, parameter $start of FireflyIII\Helpers\Colle...orInterface::setRange() does only seem to accept Carbon\Carbon, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

322
        $collector->setRange(/** @scrutinizer ignore-type */ $start, $end);
Loading history...
323
        $transactions = $collector->getPaginatedJournals();
324
        $transactions->setPath(route('accounts.show', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]));
325
        $showAll = false;
326
327
        return view(
328
            'accounts.show',
329
            compact('account', 'showAll', 'currency', 'today', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end', 'chartUri')
330
        );
331
    }
332
333
    /**
334
     * Show an account.
335
     *
336
     * @param Request $request
337
     * @param Account $account
338
     *
339
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
340
     *
341
     * @throws FireflyException
342
     *
343
     */
344
    public function showAll(Request $request, Account $account)
345
    {
346
        if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
0 ignored issues
show
The property type does not seem to exist on FireflyIII\Models\AccountType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
347
            return $this->redirectToOriginalAccount($account);
348
        }
349
        $end          = new Carbon;
350
        $today        = new Carbon;
351
        $start        = $this->repository->oldestJournalDate($account);
352
        $subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type);
353
        $page         = (int)$request->get('page');
354
        $pageSize     = (int)Preferences::get('listPageSize', 50)->data;
355
        $currencyId   = (int)$this->repository->getMetaValue($account, 'currency_id');
356
        $currency     = $this->currencyRepos->findNull($currencyId);
357
        if (0 === $currencyId) {
358
            $currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
359
        }
360
        $subTitle  = trans('firefly.all_journals_for_account', ['name' => $account->name]);
361
        $periods   = new Collection;
362
        $collector = app(JournalCollectorInterface::class);
363
        $collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page);
364
        $transactions = $collector->getPaginatedJournals();
365
        $transactions->setPath(route('accounts.show.all', [$account->id]));
366
        $chartUri = route('chart.account.period', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]);
367
        $showAll  = true;
368
369
        return view(
370
            'accounts.show',
371
            compact('account', 'showAll', 'currency', 'today', 'chartUri', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end')
372
        );
373
    }
374
375
    /**
376
     * @param AccountFormRequest $request
377
     *
378
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
379
     */
380
    public function store(AccountFormRequest $request)
381
    {
382
        $data    = $request->getAccountData();
383
        $account = $this->repository->store($data);
384
        $request->session()->flash('success', (string)trans('firefly.stored_new_account', ['name' => $account->name]));
385
        Preferences::mark();
386
387
        // update preferences if necessary:
388
        $frontPage = Preferences::get('frontPageAccounts', [])->data;
389
        if (count($frontPage) > 0 && AccountType::ASSET === $account->accountType->type) {
0 ignored issues
show
The property type does not seem to exist on FireflyIII\Models\AccountType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
390
            // @codeCoverageIgnoreStart
391
            $frontPage[] = $account->id;
392
            Preferences::set('frontPageAccounts', $frontPage);
0 ignored issues
show
The method set() does not exist on FireflyIII\Support\Facades\Preferences. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

392
            Preferences::/** @scrutinizer ignore-call */ 
393
                         set('frontPageAccounts', $frontPage);
Loading history...
393
            // @codeCoverageIgnoreEnd
394
        }
395
396
        if (1 === (int)$request->get('create_another')) {
397
            // set value so create routine will not overwrite URL:
398
            $request->session()->put('accounts.create.fromStore', true);
399
400
            return redirect(route('accounts.create', [$request->input('what')]))->withInput();
401
        }
402
403
        // redirect to previous URL.
404
        return redirect($this->getPreviousUri('accounts.create.uri'));
405
    }
406
407
    /**
408
     * @param AccountFormRequest $request
409
     * @param Account            $account
410
     *
411
     * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
412
     */
413
    public function update(AccountFormRequest $request, Account $account)
414
    {
415
        $data = $request->getAccountData();
416
        $this->repository->update($account, $data);
417
418
        $request->session()->flash('success', (string)trans('firefly.updated_account', ['name' => $account->name]));
419
        Preferences::mark();
420
421
        if (1 === (int)$request->get('return_to_edit')) {
422
            // set value so edit routine will not overwrite URL:
423
            $request->session()->put('accounts.edit.fromUpdate', true);
424
425
            return redirect(route('accounts.edit', [$account->id]))->withInput(['return_to_edit' => 1]);
426
        }
427
428
        // redirect to previous URL.
429
        return redirect($this->getPreviousUri('accounts.edit.uri'));
430
    }
431
432
    /**
433
     * @param array $array
434
     * @param int   $entryId
435
     *
436
     * @return null|mixed
437
     */
438
    protected function isInArray(array $array, int $entryId)
439
    {
440
        if (isset($array[$entryId])) {
441
            return $array[$entryId];
442
        }
443
444
        return '0';
445
    }
446
447
    /**
448
     * This method returns "period entries", so nov-2015, dec-2015, etc etc (this depends on the users session range)
449
     * and for each period, the amount of money spent and earned. This is a complex operation which is cached for
450
     * performance reasons.
451
     *
452
     * @param Account     $account the account involved
453
     *
454
     * @param Carbon|null $date
455
     *
456
     * @return Collection
457
     *
458
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
459
     */
460
    private function getPeriodOverview(Account $account, ?Carbon $date): Collection
461
    {
462
        $range = Preferences::get('viewRange', '1M')->data;
463
        $start = $this->repository->oldestJournalDate($account);
464
        $end   = $date ?? new Carbon;
465
        if ($end < $start) {
466
            [$start, $end] = [$end, $start]; // @codeCoverageIgnore
467
        }
468
469
        // properties for cache
470
        $cache = new CacheProperties;
471
        $cache->addProperty($start);
472
        $cache->addProperty($end);
473
        $cache->addProperty('account-show-period-entries');
474
        $cache->addProperty($account->id);
475
        if ($cache->has()) {
476
            return $cache->get(); // @codeCoverageIgnore
477
        }
478
        /** @var array $dates */
479
        $dates   = app('navigation')->blockPeriods($start, $end, $range);
0 ignored issues
show
The method blockPeriods() does not exist on FireflyIII\Support\Facades\Navigation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

479
        $dates   = app('navigation')->/** @scrutinizer ignore-call */ blockPeriods($start, $end, $range);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
480
        $entries = new Collection;
481
        // loop dates
482
        foreach ($dates as $currentDate) {
483
484
            // try a collector for income:
485
            /** @var JournalCollectorInterface $collector */
486
            $collector = app(JournalCollectorInterface::class);
487
            $collector->setAccounts(new Collection([$account]))->setRange($currentDate['start'], $currentDate['end'])->setTypes([TransactionType::DEPOSIT])
488
                      ->withOpposingAccount();
489
            $earned = (string)$collector->getJournals()->sum('transaction_amount');
490
491
            // try a collector for expenses:
492
            /** @var JournalCollectorInterface $collector */
493
            $collector = app(JournalCollectorInterface::class);
494
            $collector->setAccounts(new Collection([$account]))->setRange($currentDate['start'], $currentDate['end'])->setTypes([TransactionType::WITHDRAWAL])
495
                      ->withOpposingAccount();
496
            $spent = (string)$collector->getJournals()->sum('transaction_amount');
497
498
            $dateName = app('navigation')->periodShow($currentDate['start'], $currentDate['period']);
0 ignored issues
show
The method periodShow() does not exist on FireflyIII\Support\Facades\Navigation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

498
            $dateName = app('navigation')->/** @scrutinizer ignore-call */ periodShow($currentDate['start'], $currentDate['period']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
499
            $entries->push(
500
                [
501
                    'name'   => $dateName,
502
                    'spent'  => $spent,
503
                    'earned' => $earned,
504
                    'start'  => $currentDate['start']->format('Y-m-d'),
505
                    'end'    => $currentDate['end']->format('Y-m-d'),
506
                ]
507
            );
508
        }
509
510
        $cache->store($entries);
511
512
        return $entries;
513
    }
514
515
    /**
516
     * @param Account $account
517
     *
518
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
519
     *
520
     * @throws FireflyException
521
     */
522
    private function redirectToOriginalAccount(Account $account)
523
    {
524
        /** @var Transaction $transaction */
525
        $transaction = $account->transactions()->first();
526
        if (null === $transaction) {
527
            throw new FireflyException('Expected a transaction. This account has none. BEEP, error.');
528
        }
529
530
        $journal = $transaction->transactionJournal;
531
        /** @var Transaction $opposingTransaction */
532
        $opposingTransaction = $journal->transactions()->where('transactions.id', '!=', $transaction->id)->first();
533
534
        if (null === $opposingTransaction) {
535
            throw new FireflyException('Expected an opposing transaction. This account has none. BEEP, error.'); // @codeCoverageIgnore
536
        }
537
538
        return redirect(route('accounts.show', [$opposingTransaction->account_id]));
539
    }
540
}
541