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));
}