Fix phpstan issues.

This commit is contained in:
James Cole
2025-09-07 07:51:01 +02:00
parent 602df95f3c
commit c3a3bdf525
21 changed files with 52 additions and 219 deletions

View File

@@ -78,7 +78,7 @@ class ParseDateString
*/
public function parseDate(string $date): Carbon
{
app('log')->debug(sprintf('parseDate("%s")', $date));
Log::debug(sprintf('parseDate("%s")', $date));
$date = strtolower($date);
// parse keywords:
if (in_array($date, $this->keywords, true)) {
@@ -107,7 +107,7 @@ class ParseDateString
// maybe a date range
if (10 === strlen($date) && (str_contains($date, 'xx') || str_contains($date, 'xxxx'))) {
app('log')->debug(sprintf('[c] Detected a date range ("%s"), return a fake date.', $date));
Log::debug(sprintf('[c] Detected a date range ("%s"), return a fake date.', $date));
// very lazy way to parse the date without parsing it, because this specific function
// cant handle date ranges.
@@ -158,7 +158,7 @@ class ParseDateString
protected function parseRelativeDate(string $date): Carbon
{
app('log')->debug(sprintf('Now in parseRelativeDate("%s")', $date));
Log::debug(sprintf('Now in parseRelativeDate("%s")', $date));
$parts = explode(' ', $date);
$today = today(config('app.timezone'))->startOfDay();
$functions = [
@@ -179,14 +179,14 @@ class ParseDateString
];
foreach ($parts as $part) {
app('log')->debug(sprintf('Now parsing part "%s"', $part));
Log::debug(sprintf('Now parsing part "%s"', $part));
$part = trim($part);
// verify if correct
$pattern = '/[+-]\d+[wqmdy]/';
$result = preg_match($pattern, $part);
if (0 === $result || false === $result) {
app('log')->error(sprintf('Part "%s" does not match regular expression. Will be skipped.', $part));
if (0 === $result) {
Log::error(sprintf('Part "%s" does not match regular expression. Will be skipped.', $part));
continue;
}
@@ -194,14 +194,14 @@ class ParseDateString
$period = $part[strlen($part) - 1];
$number = (int) substr($part, 1, -1);
if (!array_key_exists($period, $functions[$direction])) {
app('log')->error(sprintf('No method for direction %d and period "%s".', $direction, $period));
Log::error(sprintf('No method for direction %d and period "%s".', $direction, $period));
continue;
}
$func = $functions[$direction][$period];
app('log')->debug(sprintf('Will now do %s(%d) on %s', $func, $number, $today->format('Y-m-d')));
Log::debug(sprintf('Will now do %s(%d) on %s', $func, $number, $today->format('Y-m-d')));
$today->{$func}($number); // @phpstan-ignore-line
app('log')->debug(sprintf('Resulting date is %s', $today->format('Y-m-d')));
Log::debug(sprintf('Resulting date is %s', $today->format('Y-m-d')));
}
return $today;
@@ -260,11 +260,11 @@ class ParseDateString
$pattern = '/^xxxx-xx-(0[1-9]|[12]\d|3[01])$/';
$result = preg_match($pattern, $date);
if (0 !== $result) {
app('log')->debug(sprintf('"%s" is a day range.', $date));
Log::debug(sprintf('"%s" is a day range.', $date));
return true;
}
app('log')->debug(sprintf('"%s" is not a day range.', $date));
Log::debug(sprintf('"%s" is not a day range.', $date));
return false;
}
@@ -287,11 +287,11 @@ class ParseDateString
$pattern = '/^xxxx-(0[1-9]|1[012])-xx$/';
$result = preg_match($pattern, $date);
if (0 !== $result) {
app('log')->debug(sprintf('"%s" is a month range.', $date));
Log::debug(sprintf('"%s" is a month range.', $date));
return true;
}
app('log')->debug(sprintf('"%s" is not a month range.', $date));
Log::debug(sprintf('"%s" is not a month range.', $date));
return false;
}
@@ -301,7 +301,7 @@ class ParseDateString
*/
protected function parseMonthRange(string $date): array
{
app('log')->debug(sprintf('parseMonthRange: Parsed "%s".', $date));
Log::debug(sprintf('parseMonthRange: Parsed "%s".', $date));
$parts = explode('-', $date);
return [
@@ -315,11 +315,11 @@ class ParseDateString
$pattern = '/^(19|20)\d\d-xx-xx$/';
$result = preg_match($pattern, $date);
if (0 !== $result) {
app('log')->debug(sprintf('"%s" is a year range.', $date));
Log::debug(sprintf('"%s" is a year range.', $date));
return true;
}
app('log')->debug(sprintf('"%s" is not a year range.', $date));
Log::debug(sprintf('"%s" is not a year range.', $date));
return false;
}
@@ -329,7 +329,7 @@ class ParseDateString
*/
protected function parseYearRange(string $date): array
{
app('log')->debug(sprintf('parseYearRange: Parsed "%s"', $date));
Log::debug(sprintf('parseYearRange: Parsed "%s"', $date));
$parts = explode('-', $date);
return [
@@ -343,11 +343,11 @@ class ParseDateString
$pattern = '/^xxxx-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$/';
$result = preg_match($pattern, $date);
if (0 !== $result) {
app('log')->debug(sprintf('"%s" is a month/day range.', $date));
Log::debug(sprintf('"%s" is a month/day range.', $date));
return true;
}
app('log')->debug(sprintf('"%s" is not a month/day range.', $date));
Log::debug(sprintf('"%s" is not a month/day range.', $date));
return false;
}
@@ -357,7 +357,7 @@ class ParseDateString
*/
private function parseMonthDayRange(string $date): array
{
app('log')->debug(sprintf('parseMonthDayRange: Parsed "%s".', $date));
Log::debug(sprintf('parseMonthDayRange: Parsed "%s".', $date));
$parts = explode('-', $date);
return [
@@ -372,11 +372,11 @@ class ParseDateString
$pattern = '/^(19|20)\d\d-xx-(0[1-9]|[12]\d|3[01])$/';
$result = preg_match($pattern, $date);
if (0 !== $result) {
app('log')->debug(sprintf('"%s" is a day/year range.', $date));
Log::debug(sprintf('"%s" is a day/year range.', $date));
return true;
}
app('log')->debug(sprintf('"%s" is not a day/year range.', $date));
Log::debug(sprintf('"%s" is not a day/year range.', $date));
return false;
}
@@ -386,7 +386,7 @@ class ParseDateString
*/
private function parseDayYearRange(string $date): array
{
app('log')->debug(sprintf('parseDayYearRange: Parsed "%s".', $date));
Log::debug(sprintf('parseDayYearRange: Parsed "%s".', $date));
$parts = explode('-', $date);
return [
@@ -401,11 +401,11 @@ class ParseDateString
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-xx$/';
$result = preg_match($pattern, $date);
if (0 !== $result) {
app('log')->debug(sprintf('"%s" is a month/year range.', $date));
Log::debug(sprintf('"%s" is a month/year range.', $date));
return true;
}
app('log')->debug(sprintf('"%s" is not a month/year range.', $date));
Log::debug(sprintf('"%s" is not a month/year range.', $date));
return false;
}
@@ -415,7 +415,7 @@ class ParseDateString
*/
protected function parseMonthYearRange(string $date): array
{
app('log')->debug(sprintf('parseMonthYearRange: Parsed "%s".', $date));
Log::debug(sprintf('parseMonthYearRange: Parsed "%s".', $date));
$parts = explode('-', $date);
return [

View File

@@ -1,83 +0,0 @@
<?php
/*
* GetFilterInstructions.php
* Copyright (c) 2024 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\Request;
use FireflyIII\Exceptions\FireflyException;
trait GetFilterInstructions
{
private const string INVALID_FILTER = '%INVALID_JAMES_%';
/**
* @throws FireflyException
*/
final public function getFilterInstructions(string $key): array
{
$config = config(sprintf('firefly.filters.allowed.%s', $key));
$allowed = array_keys($config);
$set = $this->get('filters', []);
$result = [];
if (0 === count($set)) {
return [];
}
foreach ($set as $info) {
$column = $info['column'] ?? 'NOPE';
$filterValue = (string) ($info['filter'] ?? self::INVALID_FILTER);
if (false === in_array($column, $allowed, true)) {
// skip invalid column
continue;
}
$filterType = $config[$column] ?? false;
switch ($filterType) {
default:
throw new FireflyException(sprintf('Do not support filter type "%s"', $filterType));
case 'boolean':
$filterValue = $this->booleanInstruction($filterValue);
break;
case 'string':
break;
}
$result[$column] = $filterValue;
}
return $result;
}
public function booleanInstruction(string $filterValue): ?bool
{
if ('true' === $filterValue) {
return true;
}
if ('false' === $filterValue) {
return false;
}
return null;
}
}

View File

@@ -1,53 +0,0 @@
<?php
/*
* GetSortInstructions.php
* Copyright (c) 2024 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\Request;
trait GetSortInstructions
{
final public function getSortInstructions(string $key): array
{
$allowed = config(sprintf('firefly.sorting.allowed.%s', $key));
$set = $this->get('sorting', []);
$result = [];
if (0 === count($set)) {
return [];
}
foreach ($set as $info) {
$column = $info['column'] ?? 'NOPE';
$direction = $info['direction'] ?? 'NOPE';
if ('asc' !== $direction && 'desc' !== $direction) {
// skip invalid direction
continue;
}
if (false === in_array($column, $allowed, true)) {
// skip invalid column
continue;
}
$result[$column] = $direction;
}
return $result;
}
}

View File

@@ -27,8 +27,8 @@ namespace FireflyIII\Support\Request;
use FireflyIII\Enums\WebhookTrigger;
use FireflyIII\Models\Webhook;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Validator;
trait ValidatesWebhooks
{
@@ -37,7 +37,7 @@ trait ValidatesWebhooks
$validator->after(
function (Validator $validator): void {
Log::debug('Validating webhook');
if ($validator->failed()) {
if (count($validator->failed()) > 0) {
return;
}
$data = $validator->getData();

View File

@@ -181,8 +181,14 @@ class General extends AbstractExtension
return new TwigFilter(
'phphost',
static function (string $string): string {
$proto = (string)parse_url($string, PHP_URL_SCHEME);
$host = (string)parse_url($string, PHP_URL_HOST);
$proto = parse_url($string, PHP_URL_SCHEME);
$host = parse_url($string, PHP_URL_HOST);
if(is_array($host)) {
$host = join(' ', $host);
}
if(is_array($proto)) {
$proto = join(' ', $proto);
}
return e(sprintf('%s://%s', $proto, $host));
}

View File

@@ -176,18 +176,6 @@ class AccountTransformer extends AbstractTransformer
return $accountRole;
}
/**
* TODO duplicated in the V2 transformer.
*/
private function getDate(): Carbon
{
if (null !== $this->parameters->get('date')) {
return $this->parameters->get('date');
}
return today(config('app.timezone'));
}
private function getCCInfo(Account $account, ?string $accountRole, string $accountType): array
{
$monthlyPaymentDate = null;

View File

@@ -108,19 +108,5 @@ class PiggyBankTransformer extends AbstractTransformer
];
}
private function renderAccounts(PiggyBank $piggyBank): array
{
$return = [];
foreach ($piggyBank->accounts()->get() as $account) {
$return[] = [
'id' => (string)$account->id,
'name' => $account->name,
'current_amount' => (string)$account->pivot->current_amount,
'pc_current_amount' => (string)$account->pivot->native_current_amount,
// TODO add balance, add left to save.
];
}
return $return;
}
}

View File

@@ -54,9 +54,6 @@ class WebhookTransformer extends AbstractTransformer
'triggers' => $webhook->meta['triggers'],
'deliveries' => $webhook->meta['deliveries'],
'responses' => $webhook->meta['responses'],
// 'trigger' => $this->getEnum('trigger', $webhook->trigger),
// 'response' => $this->getEnum('response', $webhook->response),
// 'delivery' => $this->getEnum('delivery', $webhook->delivery),
'url' => $webhook->url,
'links' => [
[
@@ -66,16 +63,4 @@ class WebhookTransformer extends AbstractTransformer
],
];
}
private function getEnum(string $type, int $value): string
{
if ('trigger' === $type) {
return WebhookTrigger::from($value)->name;
}
if ('response' === $type) {
return WebhookResponse::from($value)->name;
}
return WebhookDelivery::from($value)->name;
}
}

View File

@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation\Api\Data\Bulk;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Validator;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use function Safe\json_decode;

View File

@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation\AutoBudget;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Validator;
/**
* Trait ValidatesAutoBudgetRequest

View File

@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Validator;
use Illuminate\Support\Facades\Log;
/**

View File

@@ -118,7 +118,7 @@ class FireflyValidator extends Validator
}
$regex = '/^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z/i';
$result = preg_match($regex, $value);
if (false === $result || 0 === $result) {
if (0 === $result) {
return false;
}
@@ -207,7 +207,7 @@ class FireflyValidator extends Validator
$value = strtoupper($value);
// replace characters outside of ASCI range.
$value = (string) iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
$value = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
$search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
$replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'];

View File

@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Validator;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;

View File

@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Validator;
use Carbon\Carbon;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceTransaction;

View File

@@ -24,7 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Validation;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\Validator;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Enums\TransactionTypeEnum;
use FireflyIII\Exceptions\FireflyException;
@@ -296,7 +296,7 @@ trait TransactionValidation
return true;
}
return (bool) $this->isAsset($account);
return $this->isAsset($account);
}
private function isLiability(Account $account): bool

View File

@@ -25,6 +25,7 @@ use Illuminate\Foundation\Application;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Contracts\Debug\ExceptionHandler;
use FireflyIII\Exceptions\Handler;
use function Safe\realpath;
/*
|--------------------------------------------------------------------------

View File

@@ -22,6 +22,8 @@
declare(strict_types=1);
use function Safe\parse_url;
$databaseUrl = getenv('DATABASE_URL');
$host = '';
$username = '';

View File

@@ -21,7 +21,7 @@
*/
declare(strict_types=1);
use function Safe\parse_url;
return [
/*
|--------------------------------------------------------------------------

View File

@@ -21,7 +21,7 @@
*/
declare(strict_types=1);
use function Safe\realpath;
$paths = [realpath(base_path('resources/views'))];
if ('v2' === env('FIREFLY_III_LAYOUT')) {
$paths = [

View File

@@ -21,7 +21,7 @@
*/
declare(strict_types=1);
use function Safe\define;
use Illuminate\Support\Facades\Route;
/*

View File

@@ -22,6 +22,7 @@
declare(strict_types=1);
use Illuminate\Support\Facades\Route;
use function Safe\define;
if (!defined('DATEFORMAT')) {
define('DATEFORMAT', '(19|20)[0-9]{2}-?[0-9]{2}-?[0-9]{2}');