Various code cleanup.

This commit is contained in:
James Cole
2021-04-06 17:00:16 +02:00
parent 8572280b7b
commit 38d0f0427f
61 changed files with 419 additions and 369 deletions

View File

@@ -123,7 +123,7 @@ trait AugumentData
foreach ($accountIds as $combinedId) {
$parts = explode('-', $combinedId);
$accountId = (int)$parts[0];
if (isset($grouped[$accountId])) {
if (array_key_exists($accountId, $grouped)) {
$return[$accountId] = $grouped[$accountId][0]['name'];
}
}
@@ -147,7 +147,7 @@ trait AugumentData
$grouped = $budgets->groupBy('id')->toArray();
$return = [];
foreach ($budgetIds as $budgetId) {
if (isset($grouped[$budgetId])) {
if (array_key_exists($budgetId, $grouped)) {
$return[$budgetId] = $grouped[$budgetId][0]['name'];
}
}
@@ -173,7 +173,7 @@ trait AugumentData
foreach ($categoryIds as $combinedId) {
$parts = explode('-', $combinedId);
$categoryId = (int)$parts[0];
if (isset($grouped[$categoryId])) {
if (array_key_exists($categoryId, $grouped)) {
$return[$categoryId] = $grouped[$categoryId][0]['name'];
}
}
@@ -286,7 +286,7 @@ trait AugumentData
$currencyId = (int)$journal['currency_id'];
// if not set, set to zero:
if (!isset($sum['per_currency'][$currencyId])) {
if (!array_key_exists($currencyId, $sum['per_currency'])) {
$sum['per_currency'][$currencyId] = [
'sum' => '0',
'currency' => [

View File

@@ -115,7 +115,7 @@ trait CreateStuff
}
// switch on class existence.
$keys= [];
Log::info(sprintf('PHP version is %s', phpversion()));
if (class_exists(LegacyRSA::class)) {
// PHP 7
@@ -132,8 +132,8 @@ trait CreateStuff
// @codeCoverageIgnoreStart
Log::alert('NO OAuth keys were found. They have been created.');
file_put_contents($publicKey, array_get($keys, 'publickey'));
file_put_contents($privateKey, array_get($keys, 'privatekey'));
file_put_contents($publicKey, $keys['publickey']);
file_put_contents($privateKey, $keys['privatekey']);
}
/**

View File

@@ -57,15 +57,12 @@ trait ModelInformation
'count' => 1,
]
)->render();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Throwable was thrown in getActionsForBill(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
$result = 'Could not render view. See log files.';
}
// @codeCoverageIgnoreEnd
return [$result];
}
@@ -149,13 +146,11 @@ trait ModelInformation
'triggers' => $triggers,
]
)->render();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::debug(sprintf('Throwable was thrown in getTriggersForBill(): %s', $e->getMessage()));
Log::debug($e->getTraceAsString());
$string = '';
// @codeCoverageIgnoreEnd
}
if ('' !== $string) {
$result[] = $string;
@@ -189,9 +184,9 @@ trait ModelInformation
$index = 0;
// amount, description, category, budget, tags, source, destination, notes, currency type
//,type
/** @var Transaction $source */
/** @var Transaction|null $source */
$source = $journal->transactions()->where('amount', '<', 0)->first();
/** @var Transaction $destination */
/** @var Transaction|null $destination */
$destination = $journal->transactions()->where('amount', '>', 0)->first();
if (null === $destination || null === $source) {
return $result;
@@ -267,13 +262,11 @@ trait ModelInformation
'triggers' => $triggers,
]
)->render();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::debug(sprintf('Throwable was thrown in getTriggersForJournal(): %s', $e->getMessage()));
Log::debug($e->getTraceAsString());
$string = '';
// @codeCoverageIgnoreEnd
}
if ('' !== $string) {
$result[] = $string;

View File

@@ -29,6 +29,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
use Log;
@@ -63,15 +64,16 @@ use Log;
*/
trait PeriodOverview
{
protected JournalRepositoryInterface $journalRepos;
/**
* This method returns "period entries", so nov-2015, dec-2015, etc etc (this depends on the users session range)
* and for each period, the amount of money spent and earned. This is a complex operation which is cached for
* performance reasons.
*
* @param Account $account The account involved
* @param Carbon $date The start date.
* @param Carbon $end The end date.
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
@@ -220,7 +222,7 @@ trait PeriodOverview
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
$foreignCurrencyId = $journal['foreign_currency_id'];
if (!isset($return[$currencyId])) {
if (!array_key_exists($currencyId, $return)) {
$return[$currencyId] = [
'amount' => '0',
'count' => 0,
@@ -235,7 +237,7 @@ trait PeriodOverview
$return[$currencyId]['count']++;
if (null !== $foreignCurrencyId && null !== $journal['foreign_amount']) {
if (!isset($return[$foreignCurrencyId])) {
if (!array_key_exists($foreignCurrencyId, $return)) {
$return[$foreignCurrencyId] = [
'amount' => '0',
'count' => 0,
@@ -338,7 +340,7 @@ trait PeriodOverview
* This method has been refactored recently.
*
* @param Carbon $start
* @param Carbon $date
* @param Carbon $end
*
* @return array
*/
@@ -469,10 +471,11 @@ trait PeriodOverview
* This shows a period overview for a tag. It goes back in time and lists all relevant transactions and sums.
*
* @param Tag $tag
*
* @param Carbon $date
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @throws \FireflyIII\Exceptions\FireflyException
*/
protected function getTagPeriodOverview(Tag $tag, Carbon $start, Carbon $end): array // period overview for tags.
{
@@ -542,9 +545,11 @@ trait PeriodOverview
/**
* @param string $transactionType
* @param Carbon $endDate
* @param Carbon $start
* @param Carbon $end
*
* @return array
* @throws \FireflyIII\Exceptions\FireflyException
*/
protected function getTransactionPeriodOverview(string $transactionType, Carbon $start, Carbon $end): array
{

View File

@@ -68,7 +68,7 @@ trait RenderPartialViews
// @codeCoverageIgnoreStart
try {
$view = prefixView('popup.report.balance-amount', compact('journals', 'budget', 'account'))->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
}
@@ -91,7 +91,7 @@ trait RenderPartialViews
// @codeCoverageIgnoreStart
try {
$result = prefixView('reports.options.budget', compact('budgets'))->render();
} catch (Throwable $e) {
} catch (Throwable $e) {// @phpstan-ignore-line
Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
$result = 'Could not render view.';
}
@@ -124,7 +124,7 @@ trait RenderPartialViews
// @codeCoverageIgnoreStart
try {
$view = prefixView('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
}
@@ -153,7 +153,7 @@ trait RenderPartialViews
try {
$view = prefixView('popup.report.category-entry', compact('journals', 'category'))->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
}
@@ -174,7 +174,7 @@ trait RenderPartialViews
// @codeCoverageIgnoreStart
try {
$result = prefixView('reports.options.category', compact('categories'))->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Cannot render reports.options.category: %s', $e->getMessage()));
$result = 'Could not render view.';
}
@@ -217,7 +217,7 @@ trait RenderPartialViews
// @codeCoverageIgnoreStart
try {
$result = prefixView('reports.options.double', compact('set'))->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
$result = 'Could not render view.';
}
@@ -252,7 +252,7 @@ trait RenderPartialViews
// @codeCoverageIgnoreStart
try {
$view = prefixView('popup.report.expense-entry', compact('journals', 'account'))->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
}
@@ -289,7 +289,7 @@ trait RenderPartialViews
]
)->render();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::debug(sprintf('Throwable was thrown in getCurrentActions(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
}
@@ -340,7 +340,7 @@ trait RenderPartialViews
]
)->render();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::debug(sprintf('Throwable was thrown in getCurrentTriggers(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
}
@@ -376,7 +376,7 @@ trait RenderPartialViews
// @codeCoverageIgnoreStart
try {
$view = prefixView('popup.report.income-entry', compact('journals', 'account'))->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not render: %s', $e->getMessage()));
$view = 'Firefly III could not render the view. Please see the log files.';
}
@@ -396,7 +396,7 @@ trait RenderPartialViews
// @codeCoverageIgnoreStart
try {
$result = prefixView('reports.options.no-options')->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Cannot render reports.options.no-options: %s', $e->getMessage()));
$result = 'Could not render view.';
}
@@ -420,7 +420,7 @@ trait RenderPartialViews
// @codeCoverageIgnoreStart
try {
$result = prefixView('reports.options.tag', compact('tags'))->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::error(sprintf('Cannot render reports.options.tag: %s', $e->getMessage()));
$result = 'Could not render view.';
}

View File

@@ -48,7 +48,7 @@ trait RequestInformation
*
* @return string
*/
protected function getDomain(): string // get request info
final protected function getDomain(): string // get request info
{
$url = url()->to('/');
$parts = parse_url($url);
@@ -65,7 +65,7 @@ trait RequestInformation
* @return string
*
*/
protected function getHelpText(string $route, string $language): string // get from internet.
final protected function getHelpText(string $route, string $language): string // get from internet.
{
$help = app(HelpInterface::class);
// get language and default variables.
@@ -119,7 +119,7 @@ trait RequestInformation
/**
* @return string
*/
protected function getPageName(): string // get request info
final protected function getPageName(): string // get request info
{
return str_replace('.', '_', RouteFacade::currentRouteName());
}
@@ -129,7 +129,7 @@ trait RequestInformation
*
* @return string
*/
protected function getSpecificPageName(): string // get request info
final protected function getSpecificPageName(): string // get request info
{
return null === RouteFacade::current()->parameter('objectType') ? '' : '_' . RouteFacade::current()->parameter('objectType');
}
@@ -141,7 +141,7 @@ trait RequestInformation
*
* @return array
*/
protected function getValidTriggerList(TestRuleFormRequest $request): array // process input
final protected function getValidTriggerList(TestRuleFormRequest $request): array // process input
{
$triggers = [];
$data = $request->get('triggers');
@@ -163,7 +163,7 @@ trait RequestInformation
*
* @return bool
*/
protected function hasSeenDemo(): bool // get request info + get preference
final protected function hasSeenDemo(): bool // get request info + get preference
{
$page = $this->getPageName();
$specificPage = $this->getSpecificPageName();
@@ -194,7 +194,7 @@ trait RequestInformation
* @return bool
*
*/
protected function notInSessionRange(Carbon $date): bool // Validate a preference
final protected function notInSessionRange(Carbon $date): bool // Validate a preference
{
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
@@ -219,7 +219,7 @@ trait RequestInformation
*
* @return array
*/
protected function parseAttributes(array $attributes): array // parse input + return result
final protected function parseAttributes(array $attributes): array // parse input + return result
{
$attributes['location'] = $attributes['location'] ?? '';
$attributes['accounts'] = AccountList::routeBinder($attributes['accounts'] ?? '', new Route('get', '', []));
@@ -253,7 +253,7 @@ trait RequestInformation
*
* @throws ValidationException
*/
protected function validatePassword(User $user, string $current, string $new): bool //get request info
final protected function validatePassword(User $user, string $current, string $new): bool //get request info
{
if (!Hash::check($current, $user->password)) {
throw new ValidationException((string)trans('firefly.invalid_current_password'));
@@ -274,7 +274,7 @@ trait RequestInformation
* @return ValidatorContract
* @codeCoverageIgnore
*/
protected function validator(array $data): ValidatorContract
final protected function validator(array $data): ValidatorContract
{
return Validator::make(
$data,

View File

@@ -108,7 +108,7 @@ trait RuleManagement
'count' => $index + 1,
]
)->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::debug(sprintf('Throwable was thrown in getPreviousActions(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
}
@@ -154,7 +154,7 @@ trait RuleManagement
'triggers' => $triggers,
]
)->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
}
@@ -197,7 +197,7 @@ trait RuleManagement
'triggers' => $triggers,
]
)->render();
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line
Log::debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
}

View File

@@ -53,7 +53,7 @@ trait UserNavigation
*
* @return string
*/
protected function getPreviousUri(string $identifier): string
final protected function getPreviousUri(string $identifier): string
{
Log::debug(sprintf('Trying to retrieve URL stored under "%s"', $identifier));
$uri = (string)session($identifier);
@@ -76,7 +76,7 @@ trait UserNavigation
*
* @return bool
*/
protected function isEditableAccount(Account $account): bool
final protected function isEditableAccount(Account $account): bool
{
$editable = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
$type = $account->accountType->type;
@@ -89,9 +89,9 @@ trait UserNavigation
*
* @return bool
*/
protected function isEditableGroup(TransactionGroup $group): bool
final protected function isEditableGroup(TransactionGroup $group): bool
{
/** @var TransactionJournal $journal */
/** @var TransactionJournal|null $journal */
$journal = $group->transactionJournals()->first();
if (null === $journal) {
return false;
@@ -107,13 +107,13 @@ trait UserNavigation
*
* @return RedirectResponse|Redirector
*/
protected function redirectAccountToAccount(Account $account)
final protected function redirectAccountToAccount(Account $account)
{
$type = $account->accountType->type;
if (AccountType::RECONCILIATION === $type || AccountType::INITIAL_BALANCE === $type) {
// reconciliation must be stored somewhere in this account's transactions.
/** @var Transaction $transaction */
/** @var Transaction|null $transaction */
$transaction = $account->transactions()->first();
if (null === $transaction) {
Log::error(sprintf('Account #%d has no transactions. Dont know where it belongs.', $account->id));
@@ -122,7 +122,7 @@ trait UserNavigation
return redirect(route('index'));
}
$journal = $transaction->transactionJournal;
/** @var Transaction $other */
/** @var Transaction|null $other */
$other = $journal->transactions()->where('id', '!=', $transaction->id)->first();
if (null === $other) {
Log::error(sprintf('Account #%d has no valid journals. Dont know where it belongs.', $account->id));
@@ -142,9 +142,9 @@ trait UserNavigation
*
* @return RedirectResponse|Redirector
*/
protected function redirectGroupToAccount(TransactionGroup $group)
final protected function redirectGroupToAccount(TransactionGroup $group)
{
/** @var TransactionJournal $journal */
/** @var TransactionJournal|null $journal */
$journal = $group->transactionJournals()->first();
if (null === $journal) {
Log::error(sprintf('No journals in group #%d', $group->id));
@@ -170,13 +170,13 @@ trait UserNavigation
*
* @return string|null
*/
protected function rememberPreviousUri(string $identifier): ?string
final protected function rememberPreviousUri(string $identifier): ?string
{
$return = app('url')->previous();
/** @var ViewErrorBag $errors */
/** @var ViewErrorBag|null $errors */
$errors = session()->get('errors');
$forbidden = ['json', 'debug'];
if ((null === $errors || (null !== $errors && 0 === $errors->count())) && !Str::contains($return, $forbidden)) {
if ((null === $errors || (0 === $errors->count())) && !Str::contains($return, $forbidden)) {
Log::debug(sprintf('Saving URL %s under key %s', $return, $identifier));
session()->put($identifier, $return);
}