Various phpstan fixes [skip ci]

This commit is contained in:
James Cole
2025-01-04 08:42:06 +01:00
parent d4942efd8e
commit c43b37baef
34 changed files with 43 additions and 1229 deletions

View File

@@ -1,168 +0,0 @@
<?php
/*
* ConvertsExchangeRates.php
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Http\Api;
use Carbon\Carbon;
use FireflyIII\Models\TransactionCurrency;
/**
* Trait ConvertsExchangeRates
*/
trait ConvertsExchangeRates
{
private ?bool $enabled = null;
/**
* @deprecated
*/
public function cerChartSet(array $set): array
{
if (null === $this->enabled) {
$this->getPreference();
}
// if not enabled, return the same array but without conversion:
return $set;
$this->enabled = false;
if (false === $this->enabled) {
$set['converted'] = false;
return $set;
}
$set['converted'] = true;
/** @var TransactionCurrency $native */
$native = app('amount')->getDefaultCurrency();
$currency = $this->getCurrency((int) $set['currency_id']);
if ($native->id === $currency->id) {
$set['native_currency_id'] = (string) $currency->id;
$set['native_currency_code'] = $currency->code;
$set['native_currency_symbol'] = $currency->symbol;
$set['native_currency_decimal_places'] = $currency->decimal_places;
return $set;
}
foreach ($set['entries'] as $date => $entry) {
$carbon = Carbon::createFromFormat(\DateTimeInterface::ATOM, $date);
$rate = $this->getRate($currency, $native, $carbon);
$rate = '0' === $rate ? '1' : $rate;
app('log')->debug(sprintf('bcmul("%s", "%s")', (string) $entry, $rate));
$set['entries'][$date] = (float) bcmul((string) $entry, $rate);
}
return $set;
}
/**
* @deprecated
*/
private function getPreference(): void
{
$this->enabled = config('cer.currency_conversion');
}
/**
* @deprecated
*/
private function getCurrency(int $currencyId): TransactionCurrency
{
$result = TransactionCurrency::find($currencyId);
if (null === $result) {
return app('amount')->getDefaultCurrency();
}
return $result;
}
/**
* For a sum of entries, get the exchange rate to the native currency of
* the user.
*
* @deprecated
*/
public function cerSum(array $entries): array
{
if (null === $this->enabled) {
$this->getPreference();
}
// if false, return the same array without conversion info
if (false === $this->enabled) {
$return = [];
/** @var array $entry */
foreach ($entries as $entry) {
$entry['converted'] = false;
$return[] = $entry;
}
return $return;
}
/** @var TransactionCurrency $native */
$native = app('amount')->getDefaultCurrency();
$return = [];
/** @var array $entry */
foreach ($entries as $entry) {
$currency = $this->getCurrency((int) $entry['id']);
if ($currency->id !== $native->id) {
$amount = $this->convertAmount($entry['sum'], $currency, $native);
$entry['converted'] = true;
$entry['native_sum'] = $amount;
$entry['native_currency_id'] = (string) $native->id;
$entry['native_currency_name'] = $native->name;
$entry['native_currency_symbol'] = $native->symbol;
$entry['native_currency_code'] = $native->code;
$entry['native_currency_decimal_places'] = $native->decimal_places;
}
if ($currency->id === $native->id) {
$entry['converted'] = false;
$entry['native_sum'] = $entry['sum'];
$entry['native_currency_id'] = (string) $native->id;
$entry['native_currency_name'] = $native->name;
$entry['native_currency_symbol'] = $native->symbol;
$entry['native_currency_code'] = $native->code;
$entry['native_currency_decimal_places'] = $native->decimal_places;
}
$return[] = $entry;
}
return $return;
}
/**
* @deprecated
*/
private function convertAmount(string $amount, TransactionCurrency $from, TransactionCurrency $to, ?Carbon $date = null): string
{
app('log')->debug(sprintf('Converting %s from %s to %s', $amount, $from->code, $to->code));
$date ??= today(config('app.timezone'));
$rate = $this->getRate($from, $to, $date);
return bcmul($amount, $rate);
}
}

View File

@@ -40,10 +40,7 @@ use Illuminate\Support\Facades\Log;
class ExchangeRateConverter
{
// use ConvertsExchangeRates;
private array $fallback = [];
private bool $ignoreSettings = false;
private bool $isPrepared = false;
private bool $noPreparedRates = false;
private array $prepared = [];
private int $queryCount = 0;
@@ -275,84 +272,6 @@ class ExchangeRateConverter
return $euro->id;
}
/**
* @throws FireflyException
*/
public function prepare(TransactionCurrency $from, TransactionCurrency $to, Carbon $start, Carbon $end): void
{
if (false === $this->enabled()) {
return;
}
Log::debug('prepare()');
$start->startOfDay();
$end->endOfDay();
Log::debug(sprintf('Preparing for %s to %s between %s and %s', $from->code, $to->code, $start->format('Y-m-d'), $end->format('Y-m-d')));
$set = $this->userGroup
->currencyExchangeRates()
->where('from_currency_id', $from->id)
->where('to_currency_id', $to->id)
->where('date', '<=', $end->format('Y-m-d'))
->where('date', '>=', $start->format('Y-m-d'))
->orderBy('date', 'DESC')->get()
;
++$this->queryCount;
if (0 === $set->count()) {
Log::debug('No prepared rates found in this period, use the fallback');
$this->fallback($from, $to, $start);
$this->noPreparedRates = true;
$this->isPrepared = true;
Log::debug('prepare DONE()');
return;
}
$this->isPrepared = true;
// so there is a fallback just in case. Now loop the set of rates we DO have.
$temp = [];
$count = 0;
foreach ($set as $rate) {
$date = $rate->date->format('Y-m-d');
$temp[$date] ??= [
$from->id => [
$to->id => $rate->rate,
],
];
++$count;
}
Log::debug(sprintf('Found %d rates in this period.', $count));
$currentStart = clone $start;
while ($currentStart->lte($end)) {
$currentDate = $currentStart->format('Y-m-d');
$this->prepared[$currentDate] ??= [];
$fallback = $temp[$currentDate][$from->id][$to->id] ?? $this->fallback[$from->id][$to->id] ?? '0';
if (0 === count($this->prepared[$currentDate]) && 0 !== bccomp('0', $fallback)) {
// fill from temp or fallback or from temp (see before)
$this->prepared[$currentDate][$from->id][$to->id] = $fallback;
}
$currentStart->addDay();
}
}
/**
* If there are no exchange rate in the "prepare" array, future searches for any exchange rate
* will result in nothing: otherwise the preparation had been unnecessary. So, to fix this Firefly III
* will set two fallback currency exchange rates, A > B and B > A using the regular getCurrencyRate method.
*
* This method in turn will fall back on the default exchange rate (if present) or on "1" if necessary.
*
* @throws FireflyException
*/
private function fallback(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): void
{
Log::debug('fallback()');
$fallback = $this->getRate($from, $to, $date);
$fallback = 0 === bccomp('0', $fallback) ? '1' : $fallback;
$this->fallback[$from->id][$to->id] = $fallback;
$this->fallback[$to->id][$from->id] = bcdiv('1', $fallback);
Log::debug(sprintf('Fallback rate %s > %s = %s', $from->code, $to->code, $fallback));
Log::debug(sprintf('Fallback rate %s > %s = %s', $to->code, $from->code, bcdiv('1', $fallback)));
}
public function setIgnoreSettings(bool $ignoreSettings): void
{
$this->ignoreSettings = $ignoreSettings;