Optimize some code.

This commit is contained in:
James Cole
2018-07-17 22:21:03 +02:00
parent 9299efd086
commit b886cc1333
19 changed files with 194 additions and 127 deletions

View File

@@ -103,6 +103,8 @@ class ShowController extends Controller
* @param JournalRepositoryInterface $repository
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function noBudgetAll(Request $request, JournalRepositoryInterface $repository)
{

View File

@@ -131,6 +131,9 @@ class BudgetReportController extends Controller
* @param Carbon $end
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function mainChart(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end): JsonResponse
{

View File

@@ -60,6 +60,8 @@ class CategoryController extends Controller
* @param Category $category
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function all(CategoryRepositoryInterface $repository, AccountRepositoryInterface $accountRepository, Category $category): JsonResponse
{
@@ -69,13 +71,8 @@ class CategoryController extends Controller
if ($cache->has()) {
return response()->json($cache->get()); // @codeCoverageIgnore
}
$start = $repository->firstUseDate($category);
if (null === $start) {
$start = new Carbon; // @codeCoverageIgnore
}
$start = $start ?? new Carbon;
$range = app('preferences')->get('viewRange', '1M')->data;
$start = app('navigation')->startOfPeriod($start, $range);
$end = new Carbon;
@@ -83,19 +80,15 @@ class CategoryController extends Controller
$chartData = [
[
'label' => (string)trans('firefly.spent'),
'entries' => [],
'type' => 'bar',
'entries' => [], 'type' => 'bar',
],
[
'label' => (string)trans('firefly.earned'),
'entries' => [],
'type' => 'bar',
'entries' => [], 'type' => 'bar',
],
[
'label' => (string)trans('firefly.sum'),
'entries' => [],
'type' => 'line',
'fill' => false,
'entries' => [], 'type' => 'line', 'fill' => false,
],
];
@@ -167,6 +160,8 @@ class CategoryController extends Controller
* @param Carbon $end
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function reportPeriod(Category $category, Collection $accounts, Carbon $start, Carbon $end): JsonResponse
{
@@ -225,6 +220,8 @@ class CategoryController extends Controller
* @param Carbon $end
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function reportPeriodNoCategory(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
{
@@ -297,6 +294,8 @@ class CategoryController extends Controller
* @param Carbon $end
*
* @return array
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function makePeriodChart(Category $category, Carbon $start, Carbon $end): array
{

View File

@@ -72,6 +72,8 @@ class CategoryReportController extends Controller
* @param string $others
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function accountExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
{
@@ -94,6 +96,8 @@ class CategoryReportController extends Controller
* @param string $others
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function accountIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
{
@@ -119,6 +123,8 @@ class CategoryReportController extends Controller
* @param string $others
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function categoryExpense(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
{
@@ -144,6 +150,7 @@ class CategoryReportController extends Controller
* @param string $others
*
* @return JsonResponse
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function categoryIncome(Collection $accounts, Collection $categories, Carbon $start, Carbon $end, string $others): JsonResponse
{
@@ -169,6 +176,10 @@ class CategoryReportController extends Controller
* @param Carbon $end
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function mainChart(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): JsonResponse
{
@@ -276,6 +287,8 @@ class CategoryReportController extends Controller
* @param Carbon $end
*
* @return Collection
*
*
*/
private function getExpenses(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): Collection
{

View File

@@ -64,6 +64,7 @@ class ExpenseReportController extends Controller
);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $expense
@@ -71,6 +72,10 @@ class ExpenseReportController extends Controller
* @param Carbon $end
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function mainChart(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): JsonResponse
{

View File

@@ -59,6 +59,9 @@ class PiggyBankController extends Controller
* @param PiggyBank $piggyBank
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function history(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank): JsonResponse
{

View File

@@ -94,6 +94,8 @@ class ReportController extends Controller
* @param Carbon $end
*
* @return \Illuminate\Http\JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function operations(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
{
@@ -123,7 +125,6 @@ class ReportController extends Controller
'entries' => [],
],
];
foreach ($source['earned'] as $date => $amount) {
$carbon = new Carbon($date);
$label = $carbon->formatLocalized($format);
@@ -151,6 +152,9 @@ class ReportController extends Controller
* @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function sum(Collection $accounts, Carbon $start, Carbon $end): JsonResponse
{
@@ -238,6 +242,8 @@ class ReportController extends Controller
* @param Carbon $end
*
* @return array
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function getChartData(Collection $accounts, Carbon $start, Carbon $end): array
{

View File

@@ -57,6 +57,7 @@ class TagReportController extends Controller
$this->generator = app(GeneratorInterface::class);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $tags
@@ -65,6 +66,8 @@ class TagReportController extends Controller
* @param string $others
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function accountExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others): JsonResponse
{
@@ -81,6 +84,7 @@ class TagReportController extends Controller
return response()->json($data);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $tags
@@ -89,6 +93,8 @@ class TagReportController extends Controller
* @param string $others
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function accountIncome(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others): JsonResponse
{
@@ -105,6 +111,7 @@ class TagReportController extends Controller
return response()->json($data);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $tags
@@ -112,6 +119,8 @@ class TagReportController extends Controller
* @param Carbon $end
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function budgetExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): JsonResponse
{
@@ -128,6 +137,7 @@ class TagReportController extends Controller
return response()->json($data);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $tags
@@ -135,6 +145,8 @@ class TagReportController extends Controller
* @param Carbon $end
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function categoryExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): JsonResponse
{
@@ -151,6 +163,7 @@ class TagReportController extends Controller
return response()->json($data);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $tags
@@ -158,6 +171,10 @@ class TagReportController extends Controller
* @param Carbon $end
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function mainChart(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): JsonResponse
{
@@ -257,6 +274,7 @@ class TagReportController extends Controller
return response()->json($data);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $tags
@@ -265,6 +283,8 @@ class TagReportController extends Controller
* @param string $others
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function tagExpense(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others): JsonResponse
{
@@ -281,6 +301,7 @@ class TagReportController extends Controller
return response()->json($data);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $tags
@@ -289,6 +310,8 @@ class TagReportController extends Controller
* @param string $others
*
* @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function tagIncome(Collection $accounts, Collection $tags, Carbon $start, Carbon $end, string $others): JsonResponse
{
@@ -305,6 +328,7 @@ class TagReportController extends Controller
return response()->json($data);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $tags
@@ -312,6 +336,8 @@ class TagReportController extends Controller
* @param Carbon $end
*
* @return Collection
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
private function getExpenses(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): Collection
{
@@ -327,6 +353,7 @@ class TagReportController extends Controller
return $collector->getJournals();
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
* @param Collection $tags
@@ -334,6 +361,8 @@ class TagReportController extends Controller
* @param Carbon $end
*
* @return Collection
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
private function getIncome(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): Collection
{

View File

@@ -37,6 +37,8 @@ use URL;
/**
* Class Controller.
*
* @SuppressWarnings(PHPMD.NumberOfChildren)
*/
class Controller extends BaseController
{
@@ -78,26 +80,9 @@ class Controller extends BaseController
// get shown-intro-preference:
if (auth()->check()) {
// some routes have a "what" parameter, which indicates a special page:
$specificPage = null === Route::current()->parameter('what') ? '' : '_' . Route::current()->parameter('what');
$page = str_replace('.', '_', Route::currentRouteName());
// indicator if user has seen the help for this page ( + special page):
$key = 'shown_demo_' . $page . $specificPage;
// is there an intro for this route?
$intro = config('intro.' . $page);
$specialIntro = config('intro.' . $page . $specificPage);
$shownDemo = true;
// either must be array and either must be > 0
if ((\is_array($intro) || \is_array($specialIntro)) && (\count($intro) > 0 || \count($specialIntro) > 0)) {
$shownDemo = app('preferences')->get($key, false)->data;
Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %d', $key, $shownDemo));
}
// share language
$language = app('preferences')->get('language', config('firefly.default_language', 'en_US'))->data;
$language = $this->getLanguage();
$page = $this->getPageName();
$shownDemo = $this->hasSeenDemo();
app('view')->share('language', $language);
app('view')->share('shownDemo', $shownDemo);
app('view')->share('current_route_name', $page);
@@ -174,4 +159,56 @@ class Controller extends BaseController
{
session()->put($identifier, URL::previous());
}
/**
* @return string
*/
private function getLanguage(): string
{
/** @var string $language */
$language = app('preferences')->get('language', config('firefly.default_language', 'en_US'))->data;
return $language;
}
/**
* @return string
*/
private function getPageName(): string
{
return str_replace('.', '_', Route::currentRouteName());
}
/**
* @return string
*/
private function getSpecificPageName(): string
{
return null === Route::current()->parameter('what') ? '' : '_' . Route::current()->parameter('what');
}
/**
* @return bool
*/
private function hasSeenDemo(): bool
{
$page = $this->getPageName();
$specificPage = $this->getSpecificPageName();
// indicator if user has seen the help for this page ( + special page):
$key = 'shown_demo_' . $page . $specificPage;
// is there an intro for this route?
$intro = config('intro.' . $page) ?? [];
$specialIntro = config('intro.' . $page . $specificPage) ?? [];
// some routes have a "what" parameter, which indicates a special page:
$shownDemo = true;
// both must be array and either must be > 0
if (\count($intro) > 0 || \count($specialIntro) > 0) {
$shownDemo = app('preferences')->get($key, false)->data;
Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %d', $key, $shownDemo));
}
return $shownDemo;
}
}

View File

@@ -241,6 +241,8 @@ class CurrencyController extends Controller
* @param CurrencyFormRequest $request
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function store(CurrencyFormRequest $request)
{
@@ -270,7 +272,6 @@ class CurrencyController extends Controller
}
if (null === $currency) {
$request->session()->flash('error', (string)trans('firefly.could_not_store_currency'));
}
return $redirect;

View File

@@ -37,6 +37,8 @@ use Route as RouteFacade;
/**
* Class DebugController
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DebugController extends Controller
{
@@ -100,6 +102,9 @@ class DebugController extends Controller
* @param Request $request
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function index(Request $request)
{
@@ -191,9 +196,8 @@ class DebugController extends Controller
$return = ' ';
/** @var Route $route */
foreach ($set as $route) {
$name = $route->getName();
if (null !== $name && \strlen($name) > 0 && \in_array('GET', $route->methods(), true)) {
$name = (string)$route->getName();
if (\in_array('GET', $route->methods(), true)) {
$found = false;
foreach ($ignore as $string) {
if (!(false === stripos($name, $string))) {

View File

@@ -82,6 +82,9 @@ class Authenticate
* @return mixed
* @throws \Illuminate\Auth\AuthenticationException
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function authenticate(array $guards)
{

View File

@@ -52,17 +52,17 @@ class AuthenticateTwoFactor
}
/** @noinspection PhpUnusedParameterInspection */
/**
* @param $request
* @param Closure $next
* @param array ...$guards
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|mixed
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\Container\ContainerExceptionInterface
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function handle($request, Closure $next, ...$guards)
public function handle($request, Closure $next)
{
/** @noinspection PhpUndefinedMethodInspection */
if ($this->auth->guest()) {

View File

@@ -60,12 +60,11 @@ class Binder
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string[] ...$guards
*
* @return mixed
*
*/
public function handle($request, Closure $next, ...$guards)
public function handle($request, Closure $next)
{
foreach ($request->route()->parameters() as $key => $value) {
if (isset($this->binders[$key])) {

View File

@@ -45,6 +45,9 @@ class Installer
* @param \Closure $next
*
* @return mixed
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function handle($request, Closure $next)
{

View File

@@ -49,7 +49,7 @@ class IsSandStormUser
}
if (1 === (int)getenv('SANDSTORM')) {
Session::flash('warning', (string)trans('firefly.sandstorm_not_available'));
app('session')->flash('warning', (string)trans('firefly.sandstorm_not_available'));
return response()->redirectTo(route('index'));
}

View File

@@ -27,7 +27,6 @@ use Carbon\Carbon;
use Closure;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Http\Request;
use Session;
/**
* Class SessionFilter.
@@ -114,25 +113,21 @@ class Range
}
/**
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function setRange(): void
{
// ignore preference. set the range to be the current month:
if (!Session::has('start') && !Session::has('end')) {
if (!app('session')->has('start') && !app('session')->has('end')) {
$viewRange = app('preferences')->get('viewRange', '1M')->data;
if (null === $viewRange) {
$viewRange = '1M';
app('preferences')->set('viewRange', '1M');
}
$start = new Carbon;
$start = app('navigation')->updateStartDate($viewRange, $start);
$end = app('navigation')->updateEndDate($viewRange, $start);
Session::put('start', $start);
Session::put('end', $end);
app('session')->put('start', $start);
app('session')->put('end', $end);
}
if (!Session::has('first')) {
if (!app('session')->has('first')) {
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$journal = $repository->firstNull();
@@ -141,7 +136,7 @@ class Range
if (null !== $journal) {
$first = $journal->date ?? $first;
}
Session::put('first', $first);
app('session')->put('first', $first);
}
}
}

View File

@@ -38,15 +38,14 @@ class RedirectIfTwoFactorAuthenticated
* @param string|null $guard
*
* @return mixed
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
$is2faEnabled = app('preferences')->get('twoFactorAuthEnabled', false)->data;
$has2faSecret = null !== app('preferences')->get('twoFactorAuthSecret');
// grab 2auth information from cookie.
$is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated');
if ($is2faEnabled && $has2faSecret && $is2faAuthed) {

View File

@@ -62,73 +62,39 @@ class Sandstorm
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$userId = (string)$request->header('X-Sandstorm-User-Id');
Log::debug(sprintf('Sandstorm user ID is "%s"', $userId));
$count = $repository->count();
// if there already is one user in this instance, we assume this is
// the "main" user. Firefly's nature does not allow other users to
// access the same data so we have no choice but to simply login
// the new user to the same account and just forget about Bob and Alice
// and any other differences there may be between these users.
if (1 === $count && \strlen($userId) > 0) {
// login as first user user.
$user = $repository->first();
/** @noinspection NullPointerExceptionInspection */
Auth::guard($guard)->login($user);
app('view')->share('SANDSTORM_ANON', false);
return $next($request);
}
if (1 === $count && '' === $userId) {
// login but indicate anonymous
$user = User::first();
/** @noinspection NullPointerExceptionInspection */
Auth::guard($guard)->login($user);
app('view')->share('SANDSTORM_ANON', true);
return $next($request);
}
if (0 === $count && \strlen($userId) > 0) {
// create new user.
// catch anonymous:
$userId = $userId === '' ? 'anonymous' : $userId;
$email = $userId . '@firefly';
/** @var User $user */
$user = $repository->store(
$user = $repository->findByEmail($email) ?? $this->createUser($email);
Log::debug(sprintf('Sandstorm user email is "%s"', $email));
Auth::guard($guard)->login($user);
$repository->attachRole($user, 'owner');
app('view')->share('SANDSTORM_ANON', false);
}
return $next($request);
}
/**
* @param string $email
*
* @return User
*/
private function createUser(string $email): User
{
$repository = app(UserRepositoryInterface::class);
return $repository->store(
[
'blocked' => false,
'blocked_code' => null,
'email' => $email,
]
);
Auth::guard($guard)->login($user);
// also make the user an admin
$repository->attachRole($user, 'owner');
// share value.
app('view')->share('SANDSTORM_ANON', false);
return $next($request);
}
if (0 === $count && '' === $userId) {
throw new FireflyException('The first visit to a new Firefly III administration cannot be by a guest user.');
}
if ($count > 1) {
throw new FireflyException('Your Firefly III installation has more than one user, which is weird.');
}
}
// if in Sandstorm, user logged in, still must check if user is anon.
$userId = (string)$request->header('X-Sandstorm-User-Id');
if ('' === $userId) {
app('view')->share('SANDSTORM_ANON', true);
return $next($request);
}
app('view')->share('SANDSTORM_ANON', false);
return $next($request);
}
}