From c3a3bdf5257b1ca56b8a074066a053d4de105848 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 7 Sep 2025 07:51:01 +0200 Subject: [PATCH] Fix phpstan issues. --- app/Support/ParseDateString.php | 52 ++++++------ app/Support/Request/GetFilterInstructions.php | 83 ------------------- app/Support/Request/GetSortInstructions.php | 53 ------------ app/Support/Request/ValidatesWebhooks.php | 4 +- app/Support/Twig/General.php | 10 ++- app/Transformers/AccountTransformer.php | 12 --- app/Transformers/PiggyBankTransformer.php | 14 ---- app/Transformers/WebhookTransformer.php | 15 ---- .../Bulk/ValidatesBulkTransactionQuery.php | 2 +- .../AutoBudget/ValidatesAutoBudgetRequest.php | 2 +- app/Validation/CurrencyValidation.php | 2 +- app/Validation/FireflyValidator.php | 4 +- app/Validation/GroupValidation.php | 2 +- app/Validation/RecurrenceValidation.php | 2 +- app/Validation/TransactionValidation.php | 4 +- bootstrap/app.php | 1 + config/database.php | 2 + config/mail.php | 2 +- config/view.php | 2 +- routes/api.php | 2 +- routes/web.php | 1 + 21 files changed, 52 insertions(+), 219 deletions(-) delete mode 100644 app/Support/Request/GetFilterInstructions.php delete mode 100644 app/Support/Request/GetSortInstructions.php diff --git a/app/Support/ParseDateString.php b/app/Support/ParseDateString.php index fd625ebc9c..f2ab22a672 100644 --- a/app/Support/ParseDateString.php +++ b/app/Support/ParseDateString.php @@ -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 [ diff --git a/app/Support/Request/GetFilterInstructions.php b/app/Support/Request/GetFilterInstructions.php deleted file mode 100644 index cd8c68b568..0000000000 --- a/app/Support/Request/GetFilterInstructions.php +++ /dev/null @@ -1,83 +0,0 @@ -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; - } -} diff --git a/app/Support/Request/GetSortInstructions.php b/app/Support/Request/GetSortInstructions.php deleted file mode 100644 index 38343b5a15..0000000000 --- a/app/Support/Request/GetSortInstructions.php +++ /dev/null @@ -1,53 +0,0 @@ -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; - } -} diff --git a/app/Support/Request/ValidatesWebhooks.php b/app/Support/Request/ValidatesWebhooks.php index 564af6510d..15d41f41ef 100644 --- a/app/Support/Request/ValidatesWebhooks.php +++ b/app/Support/Request/ValidatesWebhooks.php @@ -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(); diff --git a/app/Support/Twig/General.php b/app/Support/Twig/General.php index 1ebcd42dc3..0d25908c61 100644 --- a/app/Support/Twig/General.php +++ b/app/Support/Twig/General.php @@ -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)); } diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index 2bd930f6e0..c9e5d3aeee 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -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; diff --git a/app/Transformers/PiggyBankTransformer.php b/app/Transformers/PiggyBankTransformer.php index 0b3b9205ff..f7c45920b9 100644 --- a/app/Transformers/PiggyBankTransformer.php +++ b/app/Transformers/PiggyBankTransformer.php @@ -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; - } } diff --git a/app/Transformers/WebhookTransformer.php b/app/Transformers/WebhookTransformer.php index d3213613fa..89ad612160 100644 --- a/app/Transformers/WebhookTransformer.php +++ b/app/Transformers/WebhookTransformer.php @@ -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; - } } diff --git a/app/Validation/Api/Data/Bulk/ValidatesBulkTransactionQuery.php b/app/Validation/Api/Data/Bulk/ValidatesBulkTransactionQuery.php index 89ea275b39..a218f323ee 100644 --- a/app/Validation/Api/Data/Bulk/ValidatesBulkTransactionQuery.php +++ b/app/Validation/Api/Data/Bulk/ValidatesBulkTransactionQuery.php @@ -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; diff --git a/app/Validation/AutoBudget/ValidatesAutoBudgetRequest.php b/app/Validation/AutoBudget/ValidatesAutoBudgetRequest.php index 1967857616..581e0da61c 100644 --- a/app/Validation/AutoBudget/ValidatesAutoBudgetRequest.php +++ b/app/Validation/AutoBudget/ValidatesAutoBudgetRequest.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Validation\AutoBudget; -use Illuminate\Contracts\Validation\Validator; +use Illuminate\Validation\Validator; /** * Trait ValidatesAutoBudgetRequest diff --git a/app/Validation/CurrencyValidation.php b/app/Validation/CurrencyValidation.php index bc4711ba9e..2bdc1357d1 100644 --- a/app/Validation/CurrencyValidation.php +++ b/app/Validation/CurrencyValidation.php @@ -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; /** diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index c24fcd54ea..485a0cef73 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -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']; diff --git a/app/Validation/GroupValidation.php b/app/Validation/GroupValidation.php index 7a8176f7ec..bf4801856f 100644 --- a/app/Validation/GroupValidation.php +++ b/app/Validation/GroupValidation.php @@ -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; diff --git a/app/Validation/RecurrenceValidation.php b/app/Validation/RecurrenceValidation.php index 9ec2ccd3ab..2ba6e58a95 100644 --- a/app/Validation/RecurrenceValidation.php +++ b/app/Validation/RecurrenceValidation.php @@ -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; diff --git a/app/Validation/TransactionValidation.php b/app/Validation/TransactionValidation.php index 82f9cbdb0d..44e2f857d7 100644 --- a/app/Validation/TransactionValidation.php +++ b/app/Validation/TransactionValidation.php @@ -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 diff --git a/bootstrap/app.php b/bootstrap/app.php index 9c9c5c85b1..2766bcdb56 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -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; /* |-------------------------------------------------------------------------- diff --git a/config/database.php b/config/database.php index 431f405c1e..77fe7499e7 100644 --- a/config/database.php +++ b/config/database.php @@ -22,6 +22,8 @@ declare(strict_types=1); +use function Safe\parse_url; + $databaseUrl = getenv('DATABASE_URL'); $host = ''; $username = ''; diff --git a/config/mail.php b/config/mail.php index 7558f4f936..c0bc8c5adb 100644 --- a/config/mail.php +++ b/config/mail.php @@ -21,7 +21,7 @@ */ declare(strict_types=1); - +use function Safe\parse_url; return [ /* |-------------------------------------------------------------------------- diff --git a/config/view.php b/config/view.php index 9c089067a6..fdf9b39a56 100644 --- a/config/view.php +++ b/config/view.php @@ -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 = [ diff --git a/routes/api.php b/routes/api.php index 569f36ac85..79f5802b74 100644 --- a/routes/api.php +++ b/routes/api.php @@ -21,7 +21,7 @@ */ declare(strict_types=1); - +use function Safe\define; use Illuminate\Support\Facades\Route; /* diff --git a/routes/web.php b/routes/web.php index cc59c95ca8..a5efe1d500 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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}');