Fix code quality with rector [skip ci]

This commit is contained in:
James Cole
2025-11-09 09:08:03 +01:00
parent d2610be790
commit 68183a0a0e
209 changed files with 1021 additions and 1248 deletions

View File

@@ -47,7 +47,6 @@ class IsValidAccountTypeList implements ValidationRule
}
$keys = array_keys($this->types);
foreach ($values as $entry) {
$entry = (string) $entry;
if (!in_array($entry, $keys, true)) {
$fail('validation.invalid_account_list')->translate();
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Closure;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
@@ -47,7 +48,7 @@ class BelongsUser implements ValidationRule
return;
}
app('log')->debug(sprintf('Going to validate %s', $attribute));
Log::debug(sprintf('Going to validate %s', $attribute));
$result = match ($attribute) {
'piggy_bank_id' => $this->validatePiggyBankId((int) $value),
@@ -126,7 +127,7 @@ class BelongsUser implements ValidationRule
private function validateBillName(string $value): bool
{
$count = $this->countField(Bill::class, 'name', $value);
app('log')->debug(sprintf('Result of countField for bill name "%s" is %d', $value, $count));
Log::debug(sprintf('Result of countField for bill name "%s" is %d', $value, $count));
return 1 === $count;
}
@@ -147,10 +148,10 @@ class BelongsUser implements ValidationRule
$count = 0;
foreach ($objects as $object) {
$objectValue = trim((string) $object->{$field}); // @phpstan-ignore-line
app('log')->debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
Log::debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
if ($objectValue === $value) {
++$count;
app('log')->debug(sprintf('Hit! Count is now %d', $count));
Log::debug(sprintf('Hit! Count is now %d', $count));
}
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Closure;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
@@ -56,7 +57,7 @@ class BelongsUserGroup implements ValidationRule
return;
}
app('log')->debug(sprintf('Group: Going to validate "%s"', $attribute));
Log::debug(sprintf('Group: Going to validate "%s"', $attribute));
$result = match ($attribute) {
'piggy_bank_id' => $this->validatePiggyBankId((int) $value),
@@ -121,10 +122,10 @@ class BelongsUserGroup implements ValidationRule
$count = 0;
foreach ($objects as $object) {
$objectValue = trim((string) $object->{$field}); // @phpstan-ignore-line
app('log')->debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
Log::debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
if ($objectValue === $value) {
++$count;
app('log')->debug(sprintf('Hit! Count is now %d', $count));
Log::debug(sprintf('Hit! Count is now %d', $count));
}
}
@@ -154,7 +155,7 @@ class BelongsUserGroup implements ValidationRule
private function validateBillName(string $value): bool
{
$count = $this->countField(Bill::class, 'name', $value);
app('log')->debug(sprintf('Result of countField for bill name "%s" is %d', $value, $count));
Log::debug(sprintf('Result of countField for bill name "%s" is %d', $value, $count));
return 1 === $count;
}

View File

@@ -28,8 +28,6 @@ use Closure;
use FireflyIII\Enums\UserRoleEnum;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\UserGroup\UserGroupRepositoryInterface;
use FireflyIII\User;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Log;
@@ -37,14 +35,11 @@ use Override;
class IsAllowedGroupAction implements ValidationRule
{
private array $acceptedRoles;
private readonly UserGroupRepositoryInterface $repository;
// you need these roles to do anything with any endpoint.
private array $acceptedRoles = [UserRoleEnum::OWNER, UserRoleEnum::FULL];
public function __construct(private readonly string $className, private readonly string $methodName)
{
// you need these roles to do anything with any endpoint.
$this->acceptedRoles = [UserRoleEnum::OWNER, UserRoleEnum::FULL];
$this->repository = app(UserGroupRepositoryInterface::class);
}
/**
@@ -69,68 +64,16 @@ class IsAllowedGroupAction implements ValidationRule
break;
}
}
$this->validateUserGroup((int)$value, $fail);
$this->validateUserGroup();
}
private function validateUserGroup(int $userGroupId, Closure $fail): void
private function validateUserGroup(): void
{
try {
throw new FireflyException('Here we are');
} catch (FireflyException $e) {
Log::error($e->getTraceAsString());
}
exit('here we are');
Log::debug(sprintf('validateUserGroup: %s', static::class));
if (!auth()->check()) {
Log::debug('validateUserGroup: user is not logged in, return NULL.');
$fail('validation.no_auth_user_group')->translate();
return;
}
/** @var User $user */
$user = auth()->user();
if (0 !== $userGroupId) {
Log::debug(sprintf('validateUserGroup: user group submitted, search for memberships in group #%d.', $userGroupId));
}
if (0 === $userGroupId) {
$userGroupId = $user->user_group_id;
Log::debug(sprintf('validateUserGroup: no user group submitted, use default group #%d.', $userGroupId));
}
$this->repository->setUser($user);
$memberships = $this->repository->getMembershipsFromGroupId($userGroupId);
if (0 === $memberships->count()) {
Log::debug(sprintf('validateUserGroup: user has no access to group #%d.', $userGroupId));
$fail('validation.no_access_user_group')->translate();
return;
}
// need to get the group from the membership:
$userGroup = $this->repository->getById($userGroupId);
if (null === $userGroup) {
Log::debug(sprintf('validateUserGroup: group #%d does not exist.', $userGroupId));
$fail('validation.belongs_user_or_user_group')->translate();
return;
}
Log::debug(sprintf('validateUserGroup: validate access of user to group #%d ("%s").', $userGroupId, $userGroup->title));
Log::debug(sprintf('validateUserGroup: have %d roles to check.', count($this->acceptedRoles)), $this->acceptedRoles);
/** @var UserRoleEnum $role */
foreach ($this->acceptedRoles as $role) {
if ($user->hasRoleInGroupOrOwner($userGroup, $role)) {
Log::debug(sprintf('validateUserGroup: User has role "%s" in group #%d, return.', $role->value, $userGroupId));
return;
}
Log::debug(sprintf('validateUserGroup: User does NOT have role "%s" in group #%d, continue searching.', $role->value, $userGroupId));
}
Log::debug('validateUserGroup: User does NOT have enough rights to access endpoint.');
$fail('validation.belongs_user_or_user_group')->translate();
}
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException;
@@ -51,13 +52,13 @@ class IsDateOrTime implements ValidationRule
try {
Carbon::createFromFormat('Y-m-d', $value);
} catch (InvalidDateException $e) { // @phpstan-ignore-line
app('log')->error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
$fail('validation.date_or_time')->translate();
return;
} catch (InvalidFormatException $e) {
app('log')->error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage()));
Log::error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage()));
$fail('validation.date_or_time')->translate();
@@ -71,13 +72,13 @@ class IsDateOrTime implements ValidationRule
try {
Carbon::parse($value);
} catch (InvalidDateException $e) { // @phpstan-ignore-line
app('log')->error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
$fail('validation.date_or_time')->translate();
return;
} catch (InvalidFormatException $e) {
app('log')->error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage()));
Log::error(sprintf('"%s" is of an invalid format: %s', $value, $e->getMessage()));
$fail('validation.date_or_time')->translate();

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Closure;
use FireflyIII\Models\UserGroup;
use FireflyIII\Repositories\User\UserRepositoryInterface;
@@ -42,7 +43,7 @@ class IsDefaultUserGroupName implements ValidationRule
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
app('log')->debug(sprintf('Now in %s(%s)', __METHOD__, $value));
Log::debug(sprintf('Now in %s(%s)', __METHOD__, $value));
// are you owner of this group and the name is the same? fail.
/** @var User $user */

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Closure;
use FireflyIII\Enums\TransactionTypeEnum;
use FireflyIII\Validation\AccountValidator;
@@ -39,7 +40,7 @@ class IsTransferAccount implements ValidationRule
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
app('log')->debug(sprintf('Now in %s(%s)', __METHOD__, $value));
Log::debug(sprintf('Now in %s(%s)', __METHOD__, $value));
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
@@ -48,13 +49,13 @@ class IsTransferAccount implements ValidationRule
$validAccount = $validator->validateSource(['name' => (string) $value]);
if (true === $validAccount) {
app('log')->debug('Found account based on name. Return true.');
Log::debug('Found account based on name. Return true.');
// found by name, use repos to return.
return;
}
$validAccount = $validator->validateSource(['id' => (int) $value]);
app('log')->debug(sprintf('Search by id (%d), result is %s.', (int) $value, var_export($validAccount, true)));
Log::debug(sprintf('Search by id (%d), result is %s.', (int) $value, var_export($validAccount, true)));
if (false === $validAccount) {
$fail('validation.not_transfer_account')->translate();

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException;
@@ -54,13 +55,13 @@ class IsValidDateRange implements ValidationRule
$left = Carbon::parse($value);
$right = Carbon::parse($otherValue);
} catch (InvalidDateException $e) { // @phpstan-ignore-line
app('log')->error(sprintf('"%s" or "%s" is not a valid date or time: %s', $value, $otherValue, $e->getMessage()));
Log::error(sprintf('"%s" or "%s" is not a valid date or time: %s', $value, $otherValue, $e->getMessage()));
$fail('validation.date_or_time')->translate();
return;
} catch (InvalidFormatException $e) {
app('log')->error(sprintf('"%s" or "%s" is of an invalid format: %s', $value, $otherValue, $e->getMessage()));
Log::error(sprintf('"%s" or "%s" is of an invalid format: %s', $value, $otherValue, $e->getMessage()));
$fail('validation.date_or_time')->translate();

View File

@@ -41,7 +41,7 @@ class IsValidZeroOrMoreAmount implements ValidationRule
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (true === $this->nullable && null === $value) {
if ($this->nullable && null === $value) {
return;
}
$value = (string) $value;

View File

@@ -47,7 +47,6 @@ class IsValidTransactionTypeList implements ValidationRule
}
$keys = array_keys($this->transactionTypes);
foreach ($values as $entry) {
$entry = (string)$entry;
if (!in_array($entry, $keys, true)) {
$fail('validation.invalid_transaction_type_list')->translate();
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Closure;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Models\Account;
@@ -55,7 +56,7 @@ class UniqueAccountNumber implements ValidationRule
if ('asset' === $this->expectedType) {
$this->expectedType = AccountTypeEnum::ASSET->value;
}
app('log')->debug(sprintf('Expected type is "%s"', $this->expectedType));
Log::debug(sprintf('Expected type is "%s"', $this->expectedType));
}
/**
@@ -87,9 +88,9 @@ class UniqueAccountNumber implements ValidationRule
foreach ($maxCounts as $type => $max) {
$count = $this->countHits($type, $value);
app('log')->debug(sprintf('Count for "%s" and account number "%s" is %d', $type, $value, $count));
Log::debug(sprintf('Count for "%s" and account number "%s" is %d', $type, $value, $count));
if ($count > $max) {
app('log')->debug(
Log::debug(
sprintf(
'account number "%s" is in use with %d account(s) of type "%s", which is too much for expected type "%s"',
$value,
@@ -104,7 +105,7 @@ class UniqueAccountNumber implements ValidationRule
return;
}
}
app('log')->debug('Account number is valid.');
Log::debug('Account number is valid.');
}
private function getMaxOccurrences(): array

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Closure;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Models\Account;
@@ -35,14 +36,13 @@ use Illuminate\Contracts\Validation\ValidationRule;
*/
class UniqueIban implements ValidationRule
{
private array $expectedTypes;
private array $expectedTypes = [];
/**
* Create a new rule instance.
*/
public function __construct(private readonly ?Account $account, ?string $expectedType)
{
$this->expectedTypes = [];
if (null === $expectedType) {
return;
}
@@ -104,9 +104,9 @@ class UniqueIban implements ValidationRule
$value = Steam::filterSpaces($value);
$count = $this->countHits($type, $value);
app('log')->debug(sprintf('Count for "%s" and IBAN "%s" is %d', $type, $value, $count));
Log::debug(sprintf('Count for "%s" and IBAN "%s" is %d', $type, $value, $count));
if ($count > $max) {
app('log')->debug(
Log::debug(
sprintf(
'IBAN "%s" is in use with %d account(s) of type "%s", which is too much for expected types "%s"',
$value,

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Closure;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Contracts\Validation\ValidationRule;
@@ -38,7 +39,7 @@ class ValidJournals implements ValidationRule
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
app('log')->debug('In ValidJournals::passes');
Log::debug('In ValidJournals::passes');
if (!is_array($value)) {
return;
}
@@ -46,13 +47,13 @@ class ValidJournals implements ValidationRule
foreach ($value as $journalId) {
$count = TransactionJournal::where('id', $journalId)->where('user_id', $userId)->count();
if (0 === $count) {
app('log')->debug(sprintf('Count for transaction #%d and user #%d is zero! Return FALSE', $journalId, $userId));
Log::debug(sprintf('Count for transaction #%d and user #%d is zero! Return FALSE', $journalId, $userId));
$fail('validation.invalid_selection')->translate();
return;
}
}
app('log')->debug('Return true!');
Log::debug('Return true!');
}
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Rules;
use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
@@ -105,7 +106,7 @@ class ValidRecurrenceRepetitionValue implements ValidationRule
try {
Carbon::createFromFormat('Y-m-d', $dateString);
} catch (InvalidArgumentException $e) {
app('log')->debug(sprintf('Could not parse date %s: %s', $dateString, $e->getMessage()));
Log::debug(sprintf('Could not parse date %s: %s', $dateString, $e->getMessage()));
return false;
}