Fix various phpstan issues.

This commit is contained in:
James Cole
2025-09-07 10:59:07 +02:00
parent 75dfdcc220
commit 39cf0533d9
15 changed files with 44 additions and 58 deletions

View File

@@ -11,8 +11,6 @@ parameters:
reportUnmatchedIgnoredErrors: true reportUnmatchedIgnoredErrors: true
ignoreErrors: ignoreErrors:
# TODO: slowly remove these exceptions and fix the issues found. # TODO: slowly remove these exceptions and fix the issues found.
- '#Dynamic call to static method#' # all the Laravel ORM things depend on this.
- identifier: varTag.nativeType
- identifier: varTag.type - identifier: varTag.type
- -
identifier: larastan.noEnvCallsOutsideOfConfig identifier: larastan.noEnvCallsOutsideOfConfig
@@ -24,11 +22,11 @@ parameters:
- '#expects view-string\|null, string given#' - '#expects view-string\|null, string given#'
# phpstan can't handle this so we ignore them. # phpstan can't handle this so we ignore them.
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::before#' - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::before#' # is custom scope
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::after#' - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::after#' # is custom scope
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::withTrashed#' - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::withTrashed#' # is to allow soft delete
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::accountTypeIn#' - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::accountTypeIn#' # is a custom scope
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::withTrashed#' - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::withTrashed#' # is to allow soft delete
# The level 8 is the highest level. original was 5 # The level 8 is the highest level. original was 5
# 7 is more than enough, higher just leaves NULL things. # 7 is more than enough, higher just leaves NULL things.

View File

@@ -157,7 +157,6 @@ abstract class Controller extends BaseController
/** @var User $user */ /** @var User $user */
$user = auth()->user(); $user = auth()->user();
/** @var Preference $pageSize */
$pageSize = (int)app('preferences')->getForUser($user, 'listPageSize', 50)->data; $pageSize = (int)app('preferences')->getForUser($user, 'listPageSize', 50)->data;
$bag->set($integer, $pageSize); $bag->set($integer, $pageSize);
} }

View File

@@ -66,7 +66,7 @@ class RemovesDatabaseDecryption extends Command
* @var string $table * @var string $table
* @var array $fields * @var array $fields
*/ */
foreach ($tables as $table => $fields) { foreach ($tables as $table => $fields) { // @phpstan-ignore-line
$this->decryptTable($table, $fields); $this->decryptTable($table, $fields);
} }

View File

@@ -170,7 +170,7 @@ class GracefulNotFoundHandler extends ExceptionHandler
} }
/** @var null|Account $account */ /** @var null|Account $account */
$account = $user->accounts()->with(['accountType'])->withTrashed()->find($accountId); $account = $user->accounts()->withTrashed()->with(['accountType'])->find($accountId);
if (null === $account) { if (null === $account) {
app('log')->error(sprintf('Could not find account %d, so give big fat error.', $accountId)); app('log')->error(sprintf('Could not find account %d, so give big fat error.', $accountId));

View File

@@ -129,7 +129,7 @@ class UserEventHandler
$groupTitle = $user->email; $groupTitle = $user->email;
$index = 1; $index = 1;
/** @var UserGroup $group */ /** @var UserGroup|null $group */
$group = null; $group = null;
// create a new group. // create a new group.

View File

@@ -412,7 +412,7 @@ class ProfileController extends Controller
// found user.which email address to return to? // found user.which email address to return to?
$set = app('preferences')->beginsWith($user, 'previous_email_'); $set = app('preferences')->beginsWith($user, 'previous_email_');
/** @var string $match */ /** @var string|null $match */
$match = null; $match = null;
foreach ($set as $entry) { foreach ($set as $entry) {
$hashed = hash('sha256', sprintf('%s%s', (string) config('app.key'), $entry->data)); $hashed = hash('sha256', sprintf('%s%s', (string) config('app.key'), $entry->data));

View File

@@ -117,7 +117,7 @@ class BulkController extends Controller
// run rules on changed journals: // run rules on changed journals:
/** @var TransactionJournal $journal */ /** @var TransactionJournal $journal */
foreach ($collection as $journal) { foreach ($collection as $journal) { // @phpstan-ignore-line
event(new UpdatedTransactionGroup($journal->transactionGroup, true, true, false)); event(new UpdatedTransactionGroup($journal->transactionGroup, true, true, false));
} }

View File

@@ -138,7 +138,7 @@ class RecurrenceFormRequest extends FormRequest
* @var int $index * @var int $index
* @var array $transaction * @var array $transaction
*/ */
foreach ($return['transactions'] as $index => $transaction) { foreach ($return['transactions'] as $index => $transaction) { // @phpstan-ignore-line
$categoryName = $transaction['category_name'] ?? null; $categoryName = $transaction['category_name'] ?? null;
if (null !== $categoryName) { if (null !== $categoryName) {
$category = $factory->findOrCreate(null, $categoryName); $category = $factory->findOrCreate(null, $categoryName);

View File

@@ -137,13 +137,10 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
if (!$existingGroup instanceof UserGroup) { if (!$existingGroup instanceof UserGroup) {
$exists = false; $exists = false;
/** @var null|UserGroup $existingGroup */ /** @var UserGroup $existingGroup */
$existingGroup = $this->store(['user' => $user, 'title' => $groupName]); $existingGroup = $this->store(['user' => $user, 'title' => $groupName]);
} }
if (null !== $existingGroup) { $groupName = sprintf('%s-%s', $user->email, substr(sha1(random_int(1000, 9999) . microtime()), 0, 4));
// group already exists
$groupName = sprintf('%s-%s', $user->email, substr(sha1(random_int(1000, 9999).microtime()), 0, 4));
}
++$loop; ++$loop;
} }
@@ -206,7 +203,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
if (array_key_exists('primary_currency_id', $data) && null === $currency) { if (array_key_exists('primary_currency_id', $data) && null === $currency) {
$repository->setUser($this->user); $repository->setUser($this->user);
$currency = $repository->find((int) $data['primary_currency_id']); $currency = $repository->find((int)$data['primary_currency_id']);
} }
if (null !== $currency) { if (null !== $currency) {
$repository->makePrimary($currency); $repository->makePrimary($currency);
@@ -233,7 +230,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
$user = User::find($data['id']); $user = User::find($data['id']);
app('log')->debug('Found user by ID'); app('log')->debug('Found user by ID');
} }
if (array_key_exists('email', $data) && '' !== (string) $data['email']) { if (array_key_exists('email', $data) && '' !== (string)$data['email']) {
/** @var null|User $user */ /** @var null|User $user */
$user = User::whereEmail($data['email'])->first(); $user = User::whereEmail($data['email'])->first();
app('log')->debug('Found user by email'); app('log')->debug('Found user by email');
@@ -251,13 +248,13 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
if (1 === $membershipCount) { if (1 === $membershipCount) {
$lastUserId = $userGroup->groupMemberships()->distinct()->first(['group_memberships.user_id'])->user_id; $lastUserId = $userGroup->groupMemberships()->distinct()->first(['group_memberships.user_id'])->user_id;
// if this is also the user we're editing right now, and we remove all of their roles: // if this is also the user we're editing right now, and we remove all of their roles:
if ($lastUserId === (int) $user->id && 0 === count($data['roles'])) { if ($lastUserId === (int)$user->id && 0 === count($data['roles'])) {
app('log')->debug('User is last in this group, refuse to act'); app('log')->debug('User is last in this group, refuse to act');
throw new FireflyException('You cannot remove the last member from this user group. Delete the user group instead.'); throw new FireflyException('You cannot remove the last member from this user group. Delete the user group instead.');
} }
// if this is also the user we're editing right now, and do not grant them the owner role: // if this is also the user we're editing right now, and do not grant them the owner role:
if ($lastUserId === (int) $user->id && count($data['roles']) > 0 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)) { if ($lastUserId === (int)$user->id && count($data['roles']) > 0 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)) {
app('log')->debug('User needs to have owner role in this group, refuse to act'); app('log')->debug('User needs to have owner role in this group, refuse to act');
throw new FireflyException('The last member in this user group must get or keep the "owner" role.'); throw new FireflyException('The last member in this user group must get or keep the "owner" role.');
@@ -267,8 +264,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface, UserGroupInte
// group has multiple members. How many are owner, except the user we're editing now? // group has multiple members. How many are owner, except the user we're editing now?
$ownerCount = $userGroup->groupMemberships() $ownerCount = $userGroup->groupMemberships()
->where('user_role_id', $owner->id) ->where('user_role_id', $owner->id)
->where('user_id', '!=', $user->id)->count() ->where('user_id', '!=', $user->id)->count();
;
// if there are no other owners and the current users does not get or keep the owner role, refuse. // if there are no other owners and the current users does not get or keep the owner role, refuse.
if ( if (
0 === $ownerCount 0 === $ownerCount

View File

@@ -25,7 +25,6 @@ declare(strict_types=1);
namespace FireflyIII\Support\Form; namespace FireflyIII\Support\Form;
use Carbon\Carbon; use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\MessageBag; use Illuminate\Support\MessageBag;
use Throwable; use Throwable;
@@ -63,7 +62,7 @@ trait FormSupport
} }
$name = str_replace('[]', '', $name); $name = str_replace('[]', '', $name);
return (string) trans('form.'.$name); return (string)trans('form.' . $name);
} }
/** /**
@@ -74,9 +73,9 @@ trait FormSupport
$options ??= []; $options ??= [];
$name = str_replace('[]', '', $name); $name = str_replace('[]', '', $name);
$options['class'] = 'form-control'; $options['class'] = 'form-control';
$options['id'] = 'ffInput_'.$name; $options['id'] = 'ffInput_' . $name;
$options['autocomplete'] = 'off'; $options['autocomplete'] = 'off';
$options['placeholder'] = ucfirst((string) $label); $options['placeholder'] = ucfirst((string)$label);
return $options; return $options;
} }
@@ -146,15 +145,6 @@ trait FormSupport
protected function getDate(): Carbon protected function getDate(): Carbon
{ {
/** @var Carbon $date */ return today(config('app.timezone'));
$date = null;
try {
$date = today(config('app.timezone'));
} catch (InvalidDateException $e) { // @phpstan-ignore-line
app('log')->error($e->getMessage());
}
return $date;
} }
} }

View File

@@ -43,7 +43,7 @@ trait CleansChartData
$return = []; $return = [];
/** /**
* @var mixed $index * @var int $index
* @var array $array * @var array $array
*/ */
foreach ($data as $index => $array) { foreach ($data as $index => $array) {

View File

@@ -102,6 +102,9 @@ trait ConvertsDataTypes
{ {
// assume this all works, because the validator would have caught any errors. // assume this all works, because the validator would have caught any errors.
$parameter = (string)request()->query->get($field); $parameter = (string)request()->query->get($field);
if('' === $parameter) {
return [];
}
$parts = explode(',', $parameter); $parts = explode(',', $parameter);
$sortParameters = []; $sortParameters = [];
foreach ($parts as $part) { foreach ($parts as $part) {

View File

@@ -317,7 +317,7 @@ class SearchRuleEngine implements RuleEngineInterface
Log::debug(sprintf('SearchRuleEngine:: found %d rule(s) to fire.', $this->rules->count())); Log::debug(sprintf('SearchRuleEngine:: found %d rule(s) to fire.', $this->rules->count()));
/** @var Rule $rule */ /** @var Rule $rule */
foreach ($this->rules as $rule) { foreach ($this->rules as $rule) { // @phpstan-ignore-line
$result = $this->fireRule($rule); $result = $this->fireRule($rule);
if (true === $result && true === $rule->stop_processing) { if (true === $result && true === $rule->stop_processing) {
Log::debug(sprintf('Rule #%d has triggered and executed, but calls to stop processing. Since not in the context of a group, do not stop.', $rule->id)); Log::debug(sprintf('Rule #%d has triggered and executed, but calls to stop processing. Since not in the context of a group, do not stop.', $rule->id));
@@ -335,7 +335,7 @@ class SearchRuleEngine implements RuleEngineInterface
// fire each group: // fire each group:
/** @var RuleGroup $group */ /** @var RuleGroup $group */
foreach ($this->groups as $group) { foreach ($this->groups as $group) { // @phpstan-ignore-line
$this->fireGroup($group); $this->fireGroup($group);
} }
} }

View File

@@ -36,7 +36,7 @@ class TransactionTypeSeeder extends Seeder
public function run(): void public function run(): void
{ {
/** @var TransactionTypeEnum $type */ /** @var TransactionTypeEnum $type */
foreach (TransactionTypeEnum::cases() as $type) { foreach (TransactionTypeEnum::cases() as $type) { // @phpstan-ignore-line
if (null === TransactionType::where('type', $type->value)->first()) { if (null === TransactionType::where('type', $type->value)->first()) {
try { try {
TransactionType::create(['type' => $type->value]); TransactionType::create(['type' => $type->value]);

View File

@@ -40,7 +40,7 @@ class UserRoleSeeder extends Seeder
public function run(): void public function run(): void
{ {
/** @var UserRoleEnum $role */ /** @var UserRoleEnum $role */
foreach (UserRoleEnum::cases() as $role) { foreach (UserRoleEnum::cases() as $role) { // @phpstan-ignore-line
if (null === UserRole::where('title', $role->value)->first()) { if (null === UserRole::where('title', $role->value)->first()) {
try { try {
UserRole::create(['title' => $role->value]); UserRole::create(['title' => $role->value]);