Refactor old methods.

This commit is contained in:
James Cole
2021-04-06 13:30:09 +02:00
parent 5ceef2e9c3
commit 69b1769f22
54 changed files with 191 additions and 478 deletions

View File

@@ -23,9 +23,8 @@ declare(strict_types=1);
namespace FireflyIII\Support;
use Cache;
use Carbon\Carbon;
use FireflyIII\Models\Category;
use Illuminate\Support\Collection;
use JsonException;
/**
* Class CacheProperties.
@@ -34,10 +33,8 @@ use Illuminate\Support\Collection;
*/
class CacheProperties
{
/** @var string */
protected $hash = '';
/** @var Collection */
protected $properties;
protected string $hash = '';
protected Collection $properties;
/**
*
@@ -52,8 +49,7 @@ class CacheProperties
}
/**
* @param $property
* @param Collection|Carbon|Category|array|int|string $property
* @param mixed $property
*/
public function addProperty($property): void
{
@@ -90,19 +86,24 @@ class CacheProperties
}
/**
* @throws JsonException
*/
private function hash(): void
{
$content = '';
foreach ($this->properties as $property) {
$content .= json_encode($property, JSON_THROW_ON_ERROR, 512);
try {
$content .= json_encode($property, JSON_THROW_ON_ERROR, 512);
} catch (JsonException $e) {
// @ignoreException
$content .= md5(time());
}
}
$this->hash = substr(hash('sha256', $content), 0, 16);
}
/**
* @param $data
* @param (array|mixed)[]|Collection|\Carbon\Carbon|string $data
* @param mixed $data
*/
public function store($data): void
{

View File

@@ -249,13 +249,4 @@ trait AppendsLocationData
}
/**
* Abstract method to ensure filling later.
*
* @param string $field
*
* @return string|null
*/
abstract protected function nullableString(string $field): ?string;
}

View File

@@ -32,6 +32,70 @@ use Log;
*/
trait ConvertsDataTypes
{
/**
* Remove weird chars from strings.
*
* @param string $string
* @param bool $keepNewlines
*
* @return string
*/
public function clearString(string $string, bool $keepNewlines = true): string
{
$search = [
"\u{0001}", // start of heading
"\u{0002}", // start of text
"\u{0003}", // end of text
"\u{0004}", // end of transmission
"\u{0005}", // enquiry
"\u{0006}", // ACK
"\u{0007}", // BEL
"\u{0008}", // backspace
"\u{000E}", // shift out
"\u{000F}", // shift in
"\u{0010}", // data link escape
"\u{0011}", // DC1
"\u{0012}", // DC2
"\u{0013}", // DC3
"\u{0014}", // DC4
"\u{0015}", // NAK
"\u{0016}", // SYN
"\u{0017}", // ETB
"\u{0018}", // CAN
"\u{0019}", // EM
"\u{001A}", // SUB
"\u{001B}", // escape
"\u{001C}", // file separator
"\u{001D}", // group separator
"\u{001E}", // record separator
"\u{001F}", // unit separator
"\u{007F}", // DEL
"\u{00A0}", // non-breaking space
"\u{1680}", // ogham space mark
"\u{180E}", // mongolian vowel separator
"\u{2000}", // en quad
"\u{2001}", // em quad
"\u{2002}", // en space
"\u{2003}", // em space
"\u{2004}", // three-per-em space
"\u{2005}", // four-per-em space
"\u{2006}", // six-per-em space
"\u{2007}", // figure space
"\u{2008}", // punctuation space
"\u{2009}", // thin space
"\u{200A}", // hair space
"\u{200B}", // zero width space
"\u{202F}", // narrow no-break space
"\u{3000}", // ideographic space
"\u{FEFF}", // zero width no -break space
];
$replace = "\x20"; // plain old normal space
$string = str_replace($search, $replace, $string);
$secondSearch = $keepNewlines ? ["\r"] : ["\r", "\n", "\t", "\036", "\025"];
$string = str_replace($secondSearch, '', $string);
return trim($string);
}
/**
* Return integer value.
@@ -45,18 +109,6 @@ trait ConvertsDataTypes
return (int)$this->get($field);
}
/**
* Return string value, but keep newlines.
*
* @param string $field
*
* @return string
*/
public function nlString(string $field): string
{
return app('steam')->nlCleanString((string)($this->get($field) ?? ''));
}
/**
* Return string value.
*
@@ -66,7 +118,19 @@ trait ConvertsDataTypes
*/
public function string(string $field): string
{
return app('steam')->cleanString((string)($this->get($field) ?? ''));
return $this->clearString((string)($this->get($field) ?? ''), false);
}
/**
* Return string value with newlines.
*
* @param string $field
*
* @return string
*/
public function stringWithNewlines(string $field): string
{
return $this->clearString((string)($this->get($field) ?? ''));
}
/**
@@ -222,24 +286,6 @@ trait ConvertsDataTypes
return (int)$string;
}
/**
* Parse and clean a string, but keep the newlines.
*
* @param string|null $string
*
* @return string|null
*/
protected function nlStringFromValue(?string $string): ?string
{
if (null === $string) {
return null;
}
$result = app('steam')->nlCleanString($string);
return '' === $result ? null : $result;
}
/**
* Return integer value, or NULL when it's not set.
*
@@ -261,56 +307,4 @@ trait ConvertsDataTypes
return (int)$value;
}
/**
* Return string value, but keep newlines, or NULL if empty.
*
* @param string $field
*
* @return string
*/
protected function nullableNlString(string $field): ?string
{
if (!$this->has($field)) {
return null;
}
return app('steam')->nlCleanString((string)($this->get($field) ?? ''));
}
/**
* Return string value, or NULL if empty.
*
* @param string $field
*
* @return string|null
*/
protected function nullableString(string $field): ?string
{
if (!$this->has($field)) {
return null;
}
$res = trim(app('steam')->cleanString((string)($this->get($field) ?? '')));
if ('' === $res) {
return null;
}
return $res;
}
/**
* Parse and clean a string.
*
* @param string|null $string
*
* @return string|null
*/
protected function stringFromValue(?string $string): ?string
{
if (null === $string) {
return null;
}
$result = app('steam')->cleanString($string);
return '' === $result ? null : $result;
}
}

View File

@@ -70,14 +70,14 @@ class OperatorQuerySearch implements SearchInterface
private CurrencyRepositoryInterface $currencyRepository;
private Carbon $date;
private int $limit;
private Collection $modifiers;
private Collection $modifiers;
private Collection $operators;
private string $originalQuery;
private int $page;
private ParsedQuery $query;
private float $startTime;
private TagRepositoryInterface $tagRepository;
private TransactionTypeRepositoryInterface $typeRepository; // obsolete
private TransactionTypeRepositoryInterface $typeRepository; // obsolete
private User $user;
private array $validOperators;
private array $words;
@@ -179,7 +179,7 @@ private Collection $modifiers;
public function searchTransactions(): LengthAwarePaginator
{
if (0 === count($this->getWords()) && 0 === count($this->getOperators())) {
return new LengthAwarePaginator;
return new LengthAwarePaginator([],0,5,1);
}
return $this->collector->getPaginatedGroups();
@@ -245,7 +245,9 @@ private Collection $modifiers;
Log::error(sprintf('Cannot handle node %s', $class));
throw new FireflyException(sprintf('Firefly III search cant handle "%s"-nodes', $class));
case Subquery::class:
/** @var Subquery $searchNode */
// loop all notes in subquery:
/** @var Node $subNode */
foreach ($searchNode->getNodes() as $subNode) {
$this->handleSearchNode($subNode); // lets hope its not too recursive!
}

View File

@@ -179,8 +179,6 @@ class Steam
$formatted = $start->format('Y-m-d');
$startBalance = $this->balance($account, $start, $currency);
/** @var AccountRepositoryInterface $repository */
$balances[$formatted] = $startBalance;
if (null === $currency) {
$repository = app(AccountRepositoryInterface::class);
@@ -333,136 +331,6 @@ class Steam
return $result;
}
/**
* Remove weird chars from strings.
*
* @param string $string
* TODO migrate to trait.
*
* @return string
* @deprecated
*/
public function cleanString(string $string): string
{
$search = [
"\u{0001}", // start of heading
"\u{0002}", // start of text
"\u{0003}", // end of text
"\u{0004}", // end of transmission
"\u{0005}", // enquiry
"\u{0006}", // ACK
"\u{0007}", // BEL
"\u{0008}", // backspace
"\u{000E}", // shift out
"\u{000F}", // shift in
"\u{0010}", // data link escape
"\u{0011}", // DC1
"\u{0012}", // DC2
"\u{0013}", // DC3
"\u{0014}", // DC4
"\u{0015}", // NAK
"\u{0016}", // SYN
"\u{0017}", // ETB
"\u{0018}", // CAN
"\u{0019}", // EM
"\u{001A}", // SUB
"\u{001B}", // escape
"\u{001C}", // file separator
"\u{001D}", // group separator
"\u{001E}", // record separator
"\u{001F}", // unit separator
"\u{007F}", // DEL
"\u{00A0}", // non-breaking space
"\u{1680}", // ogham space mark
"\u{180E}", // mongolian vowel separator
"\u{2000}", // en quad
"\u{2001}", // em quad
"\u{2002}", // en space
"\u{2003}", // em space
"\u{2004}", // three-per-em space
"\u{2005}", // four-per-em space
"\u{2006}", // six-per-em space
"\u{2007}", // figure space
"\u{2008}", // punctuation space
"\u{2009}", // thin space
"\u{200A}", // hair space
"\u{200B}", // zero width space
"\u{202F}", // narrow no-break space
"\u{3000}", // ideographic space
"\u{FEFF}", // zero width no -break space
];
$replace = "\x20"; // plain old normal space
$string = str_replace($search, $replace, $string);
$string = str_replace(["\n", "\t", "\r"], "\x20", $string);
return trim($string);
}
/**
* Remove weird chars from strings, but keep newlines and tabs.
*
* @param string $string
* TODO migrate to trait.
*
* @return string
* @deprecated
*/
public function nlCleanString(string $string): string
{
$search = [
"\u{0001}", // start of heading
"\u{0002}", // start of text
"\u{0003}", // end of text
"\u{0004}", // end of transmission
"\u{0005}", // enquiry
"\u{0006}", // ACK
"\u{0007}", // BEL
"\u{0008}", // backspace
"\u{000E}", // shift out
"\u{000F}", // shift in
"\u{0010}", // data link escape
"\u{0011}", // DC1
"\u{0012}", // DC2
"\u{0013}", // DC3
"\u{0014}", // DC4
"\u{0015}", // NAK
"\u{0016}", // SYN
"\u{0017}", // ETB
"\u{0018}", // CAN
"\u{0019}", // EM
"\u{001A}", // SUB
"\u{001B}", // escape
"\u{001C}", // file separator
"\u{001D}", // group separator
"\u{001E}", // record separator
"\u{001F}", // unit separator
"\u{007F}", // DEL
"\u{00A0}", // non-breaking space
"\u{1680}", // ogham space mark
"\u{180E}", // mongolian vowel separator
"\u{2000}", // en quad
"\u{2001}", // em quad
"\u{2002}", // en space
"\u{2003}", // em space
"\u{2004}", // three-per-em space
"\u{2005}", // four-per-em space
"\u{2006}", // six-per-em space
"\u{2007}", // figure space
"\u{2008}", // punctuation space
"\u{2009}", // thin space
"\u{200A}", // hair space
"\u{200B}", // zero width space
"\u{202F}", // narrow no-break space
"\u{3000}", // ideographic space
"\u{FEFF}", // zero width no -break space
];
$replace = "\x20"; // plain old normal space
$string = str_replace($search, $replace, $string);
$string = str_replace("\r", '', $string);
return trim($string);
}
/**
* @param array $accounts
*
@@ -584,7 +452,6 @@ class Steam
*/
public function getLocale(): string // get preference
{
/** @var string $language */
$locale = app('preferences')->get('locale', config('firefly.default_locale', 'equal'))->data;
if ('equal' === $locale) {
$locale = $this->getLanguage();

View File

@@ -93,8 +93,7 @@ class AmountFormat extends AbstractExtension
* Will format the amount by the currency related to the given account.
*
* @return TwigFunction
* @deprecated
* TODO remove me because it executes a query in a view.
* TODO remove me when layout v1 is deprecated.
*/
protected function formatAmountByAccount(): TwigFunction
{

View File

@@ -355,8 +355,7 @@ class General extends AbstractExtension
/**
* @return TwigFunction
* @deprecated because it uses a query in a view
* TODO remove me.
* TODO remove me once layout v1 is deprecated.
*/
protected function getMetaField(): TwigFunction
{