Skip to content

Firefly III: a personal finances manager

Fix various phpstan issues.

James Colecbecacd652fc
diff --git a/app/Http/Middleware/Range.php b/app/Http/Middleware/Range.php
index 71578d4114..6206520be5 100644
--- a/app/Http/Middleware/Range.php
+++ b/app/Http/Middleware/Range.php
@@ -74,8 +74,8 @@ class Range
}
$today = today(config('app.timezone'));
- $start = app('navigation')->updateStartDate($viewRange, $today);
- $end = app('navigation')->updateEndDate($viewRange, $start);
+ $start = app('navigation')->updateStartDate((string) $viewRange, $today);
+ $end = app('navigation')->updateEndDate((string) $viewRange, $start);
app('session')->put('start', $start);
app('session')->put('end', $end);
diff --git a/app/Models/RecurrenceRepetition.php b/app/Models/RecurrenceRepetition.php
index 2171ee76d4..a40ae9bc4f 100644
--- a/app/Models/RecurrenceRepetition.php
+++ b/app/Models/RecurrenceRepetition.php
@@ -25,12 +25,13 @@ namespace FireflyIII\Models;
use Carbon\Carbon;
use Eloquent;
+use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;
-use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
+
/**
* FireflyIII\Models\RecurrenceRepetition
*
@@ -38,11 +39,11 @@ use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
- * @property int $recurrence_id
+ * @property int $recurrence_id
* @property string $repetition_type
* @property string $repetition_moment
- * @property int|string $repetition_skip
- * @property int|string $weekend
+ * @property int $repetition_skip
+ * @property int $weekend
* @property-read Recurrence $recurrence
* @method static \Illuminate\Database\Eloquent\Builder|RecurrenceRepetition newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|RecurrenceRepetition newQuery()
@@ -104,4 +105,24 @@ class RecurrenceRepetition extends Model
get: static fn($value) => (int)$value,
);
}
+
+ /**
+ * @return Attribute
+ */
+ protected function repetitionSkip(): Attribute
+ {
+ return Attribute::make(
+ get: static fn($value) => (int)$value,
+ );
+ }
+
+ /**
+ * @return Attribute
+ */
+ protected function weekend(): Attribute
+ {
+ return Attribute::make(
+ get: static fn($value) => (int)$value,
+ );
+ }
}
diff --git a/app/Models/TransactionJournalMeta.php b/app/Models/TransactionJournalMeta.php
index cbb17514d0..8dd99d280c 100644
--- a/app/Models/TransactionJournalMeta.php
+++ b/app/Models/TransactionJournalMeta.php
@@ -95,7 +95,7 @@ class TransactionJournalMeta extends Model
{
$data = json_encode($value);
$this->attributes['data'] = $data;
- $this->attributes['hash'] = (string) hash('sha256', $data);
+ $this->attributes['hash'] = hash('sha256',(string) $data);
}
/**
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index 7425867306..029eb1e1c2 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -653,7 +653,7 @@ class BillRepository implements BillRepositoryInterface
return $this->user->bills()
->where('active', true)
->orderBy('bills.name', 'ASC')
- ->get(['bills.*', DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount'),]);
+ ->get(['bills.*', DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount'),]); // @phpstan-ignore-line
}
/**
diff --git a/app/Repositories/Journal/JournalCLIRepository.php b/app/Repositories/Journal/JournalCLIRepository.php
index 8f37b8f1fd..f724df374e 100644
--- a/app/Repositories/Journal/JournalCLIRepository.php
+++ b/app/Repositories/Journal/JournalCLIRepository.php
@@ -201,7 +201,7 @@ class JournalCLIRepository implements JournalCLIRepositoryInterface
{
$query = TransactionJournal::leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->groupBy('transaction_journals.id');
- $result = $query->get(['transaction_journals.id as id', DB::raw('count(transactions.id) as transaction_count')]);
+ $result = $query->get(['transaction_journals.id as id', DB::raw('count(transactions.id) as transaction_count')]); // @phpstan-ignore-line
$journalIds = [];
/** @var stdClass $row */
foreach ($result as $row) {
diff --git a/app/Repositories/Recurring/RecurringRepository.php b/app/Repositories/Recurring/RecurringRepository.php
index 4ee6747606..052a129688 100644
--- a/app/Repositories/Recurring/RecurringRepository.php
+++ b/app/Repositories/Recurring/RecurringRepository.php
@@ -69,9 +69,9 @@ class RecurringRepository implements RecurringRepositoryInterface
// if not, loop set and try to read the recurrence_date. If it matches start or end, return it as well.
$set
= TransactionJournalMeta::where(static function (Builder $q1) use ($recurrence) {
- $q1->where('name', 'recurrence_id');
- $q1->where('data', json_encode((string)$recurrence->id));
- })->get(['journal_meta.transaction_journal_id']);
+ $q1->where('name', 'recurrence_id');
+ $q1->where('data', json_encode((string)$recurrence->id));
+ })->get(['journal_meta.transaction_journal_id']);
// there are X journals made for this recurrence. Any of them meant for today?
foreach ($set as $journalMeta) {
@@ -495,6 +495,10 @@ class RecurringRepository implements RecurringRepositoryInterface
/** @var Preference $pref */
$pref = app('preferences')->getForUser($this->user, 'language', config('firefly.default_language', 'en_US'));
$language = $pref->data;
+ if (is_array($language)) {
+ $language = 'en_US';
+ }
+ $language = (string)$language;
if ('daily' === $repetition->repetition_type) {
return (string)trans('firefly.recurring_daily', [], $language);
}
@@ -532,6 +536,9 @@ class RecurringRepository implements RecurringRepositoryInterface
//
$today = today(config('app.timezone'))->endOfYear();
$repDate = Carbon::createFromFormat('Y-m-d', $repetition->repetition_moment);
+ if(false === $repDate) {
+ $repDate = clone $today;
+ }
$diffInYears = $today->diffInYears($repDate);
$repDate->addYears($diffInYears); // technically not necessary.
$string = $repDate->isoFormat((string)trans('config.month_and_day_no_year_js'));
diff --git a/app/Repositories/UserGroup/UserGroupRepository.php b/app/Repositories/UserGroup/UserGroupRepository.php
index a5fcc65dcd..a720bf90dc 100644
--- a/app/Repositories/UserGroup/UserGroupRepository.php
+++ b/app/Repositories/UserGroup/UserGroupRepository.php
@@ -206,12 +206,15 @@ class UserGroupRepository implements UserGroupRepositoryInterface
{
$owner = UserRole::whereTitle(UserRoleEnum::OWNER)->first();
app('log')->debug('in update membership');
+ /** @var User|null $user */
$user = null;
if (array_key_exists('id', $data)) {
+ /** @var User|null $user */
$user = User::find($data['id']);
app('log')->debug('Found user by ID');
}
if (array_key_exists('email', $data) && '' !== (string)$data['email']) {
+ /** @var User|null $user */
$user = User::whereEmail($data['email'])->first();
app('log')->debug('Found user by email');
}
diff --git a/app/Services/FireflyIIIOrg/Update/UpdateRequest.php b/app/Services/FireflyIIIOrg/Update/UpdateRequest.php
index 58ca8c8db3..fa683b1e34 100644
--- a/app/Services/FireflyIIIOrg/Update/UpdateRequest.php
+++ b/app/Services/FireflyIIIOrg/Update/UpdateRequest.php
@@ -123,10 +123,15 @@ class UpdateRequest implements UpdateRequestInterface
}
// parse response a bit. No message yet.
- $response = $json['firefly_iii'][$channel];
+ $response = $json['firefly_iii'][$channel];
+ $date = Carbon::createFromFormat('Y-m-d', $response['date']);
+ if (false === $date) {
+ $date = today(config('app.timezone'));
+ }
$return['version'] = $response['version'];
$return['level'] = 'success';
- $return['date'] = Carbon::createFromFormat('Y-m-d', $response['date'])->startOfDay();
+ $return['date'] = $date->startOfDay();
+
app('log')->info('Response from update server', $response);
return $return;
diff --git a/app/Services/Internal/Destroy/AccountDestroyService.php b/app/Services/Internal/Destroy/AccountDestroyService.php
index c736265d01..dc8be9c16e 100644
--- a/app/Services/Internal/Destroy/AccountDestroyService.php
+++ b/app/Services/Internal/Destroy/AccountDestroyService.php
@@ -118,7 +118,7 @@ class AccountDestroyService
$collection = Transaction::groupBy('transaction_journal_id', 'account_id')
->where('account_id', $moveTo->id)
- ->get(['transaction_journal_id', 'account_id', DB::raw('count(*) as the_count')]);
+ ->get(['transaction_journal_id', 'account_id', DB::raw('count(*) as the_count')]); // @phpstan-ignore-line
if (0 === $collection->count()) {
return;
}
diff --git a/app/Services/Internal/Support/AccountServiceTrait.php b/app/Services/Internal/Support/AccountServiceTrait.php
index eeed7d1db6..2ce9a4583b 100644
--- a/app/Services/Internal/Support/AccountServiceTrait.php
+++ b/app/Services/Internal/Support/AccountServiceTrait.php
@@ -224,7 +224,11 @@ trait AccountServiceTrait
protected function createOBGroup(Account $account, array $data): TransactionGroup
{
app('log')->debug('Now going to create an OB group.');
- $language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
+ $language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
+ if (is_array($language)) {
+ $language = 'en_US';
+ }
+ $language = (string)$language;
$sourceId = null;
$sourceName = null;
$destId = null;
@@ -478,6 +482,10 @@ trait AccountServiceTrait
}
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
+ if (is_array($language)) {
+ $language = 'en_US';
+ }
+ $language = (string)$language;
// set source and/or destination based on whether the amount is positive or negative.
// first, assume the amount is positive and go from there:
@@ -686,6 +694,10 @@ trait AccountServiceTrait
{
app('log')->debug('Now going to create an OB group.');
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
+ if (is_array($language)) {
+ $language = 'en_US';
+ }
+ $language= (string)$language;
$sourceId = null;
$sourceName = null;
$destId = null;
diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php
index 7cd393f2d2..3f1be9e18e 100644
--- a/app/Services/Internal/Update/AccountUpdateService.php
+++ b/app/Services/Internal/Update/AccountUpdateService.php
@@ -338,6 +338,9 @@ class AccountUpdateService
return;
}
$array = $preference->data;
+ if(!is_array($array)) {
+ $array = [$array];
+ }
app('log')->debug('Old array is: ', $array);
app('log')->debug(sprintf('Must remove : %d', $account->id));
$removeAccountId = $account->id;
diff --git a/app/Support/Amount.php b/app/Support/Amount.php
index 6d08315871..bce86beb31 100644
--- a/app/Support/Amount.php
+++ b/app/Support/Amount.php
@@ -77,7 +77,7 @@ class Amount
$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, $symbol);
$fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);
$fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);
- $result = $fmt->format((float)$rounded); // intentional float
+ $result = (string) $fmt->format((float)$rounded); // intentional float
if (true === $coloured) {
if (1 === bccomp($rounded, '0')) {
diff --git a/app/Support/Http/Controllers/RequestInformation.php b/app/Support/Http/Controllers/RequestInformation.php
index 0e5941775f..faef448fbc 100644
--- a/app/Support/Http/Controllers/RequestInformation.php
+++ b/app/Support/Http/Controllers/RequestInformation.php
@@ -55,7 +55,7 @@ trait RequestInformation
$url = url()->to('/');
$parts = parse_url($url);
- return $parts['host'];
+ return $parts['host'] ?? '';
}
/**
@@ -129,7 +129,9 @@ trait RequestInformation
*/
final protected function getSpecificPageName(): string // get request info
{
- return null === RouteFacade::current()->parameter('objectType') ? '' : '_' . RouteFacade::current()->parameter('objectType');
+ /** @var string|null $param */
+ $param = RouteFacade::current()->parameter('objectType');
+ return null === $param ? '' : sprintf('_%s', $param);
}
/**
@@ -170,20 +172,25 @@ trait RequestInformation
$attributes['location'] = $attributes['location'] ?? '';
$attributes['accounts'] = AccountList::routeBinder($attributes['accounts'] ?? '', new Route('get', '', []));
try {
- $attributes['startDate'] = Carbon::createFromFormat('Ymd', $attributes['startDate'])->startOfDay();
+ $date = Carbon::createFromFormat('Ymd', $attributes['startDate']);
} catch (InvalidArgumentException $e) {
app('log')->debug(sprintf('Not important error message: %s', $e->getMessage()));
- $date = today(config('app.timezone'))->startOfMonth();
- $attributes['startDate'] = $date;
+ $date = today(config('app.timezone'));
+
}
+ $date->startOfMonth();
+ $attributes['startDate'] = $date;
+ unset($date);
try {
- $attributes['endDate'] = Carbon::createFromFormat('Ymd', $attributes['endDate'])->endOfDay();
+ $date2 = Carbon::createFromFormat('Ymd', $attributes['endDate']);
} catch (InvalidArgumentException $e) {
app('log')->debug(sprintf('Not important error message: %s', $e->getMessage()));
- $date = today(config('app.timezone'))->startOfMonth();
- $attributes['endDate'] = $date;
+ $date2 = today(config('app.timezone'));
}
+ $date2->endOfDay();
+ $attributes['endDate'] = $date2;
+
return $attributes;
}