Clean up code.

This commit is contained in:
James Cole
2020-10-24 07:40:56 +02:00
parent 048e5eeb31
commit ab4bcd3703
20 changed files with 126 additions and 245 deletions

View File

@@ -284,7 +284,6 @@ class CategoryController extends Controller
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$result = [];
foreach ($spent as $currency) {
$currencyId = $currency['currency_id'];
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$destinationId = $journal['destination_account_id'];
@@ -337,7 +336,6 @@ class CategoryController extends Controller
$spent = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$result = [];
foreach ($spent as $currency) {
$currencyId = $currency['currency_id'];
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$sourceId = $journal['source_account_id'];
@@ -536,7 +534,7 @@ class CategoryController extends Controller
'entries' => [],
];
foreach ($categoryRow['transaction_journals'] as $journalId => $journal) {
foreach ($categoryRow['transaction_journals'] as $journal) {
$date = $journal['date']->format($format);
$data[$key]['entries'][$date] = $data[$key]['entries'][$date] ?? '0';
$data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], $journal['amount']);
@@ -616,7 +614,7 @@ class CategoryController extends Controller
'entries' => [],
];
foreach ($categoryRow['transaction_journals'] as $journalId => $journal) {
foreach ($categoryRow['transaction_journals'] as $journal) {
$date = $journal['date']->format($format);
$data[$key]['entries'][$date] = $data[$key]['entries'][$date] ?? '0';
$data[$key]['entries'][$date] = bcadd($data[$key]['entries'][$date], $journal['amount']);
@@ -662,7 +660,7 @@ class CategoryController extends Controller
$cache->addProperty('category-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
//return $cache->get(); // @codeCoverageIgnore
return $cache->get(); // @codeCoverageIgnore
}
/** @var CategoryReportGenerator $generator */
@@ -700,7 +698,6 @@ class CategoryController extends Controller
$spent = $this->opsRepository->listExpenses($start, $end, $accounts, $categories);
$result = [];
foreach ($spent as $currency) {
$currencyId = $currency['currency_id'];
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$result[] = [
@@ -750,7 +747,6 @@ class CategoryController extends Controller
$spent = $this->opsRepository->listIncome($start, $end, $accounts, $categories);
$result = [];
foreach ($spent as $currency) {
$currencyId = $currency['currency_id'];
foreach ($currency['categories'] as $category) {
foreach ($category['transaction_journals'] as $journal) {
$result[] = [
@@ -787,17 +783,4 @@ class CategoryController extends Controller
return $result;
}
/**
* @param array $array
*
* @return bool
*/
private function noAmountInArray(array $array): bool
{
if (0 === count($array)) {
return true;
}
return false;
}
}

View File

@@ -168,23 +168,6 @@ class CreateRecurringTransactions implements ShouldQueue
return $recurrence->active;
}
/**
* Helper function for debug information.
*
* @param array $occurrences
*
* @return array
*/
private function debugArray(array $occurrences): array
{
$return = [];
foreach ($occurrences as $entry) {
$return[] = $entry->format('Y-m-d');
}
return $return;
}
/**
* @param Collection $recurrences
*
@@ -427,23 +410,6 @@ class CreateRecurringTransactions implements ShouldQueue
return $startDate->gt($this->date);
}
/***
* @param Recurrence $recurrence
* @param TransactionGroup $group
*/
private function linkGroupToPiggies(Recurrence $recurrence, TransactionGroup $group): void
{
/** @var TransactionJournal $journal */
foreach ($group->transactionJournals as $journal) {
// get piggy bank ID from meta data:
$piggyBank = $this->repository->getPiggyBank($recurrence);
if (null !== $piggyBank) {
/** @var PiggyBankEventFactory $factory */
$factory = app(PiggyBankEventFactory::class);
$factory->create($journal, $piggyBank);
}
}
}
/**
* Return true if the $repeat_until date is in the past.

View File

@@ -582,46 +582,4 @@ class TagRepository implements TagRepositoryInterface
}
);
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleActions(string $oldName, string $newName): void
{
$types = ['add_tag', 'remove_tag'];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
}
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleTriggers(string $oldName, string $newName): void
{
$types = ['tag_is',];
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_triggers.trigger_type', $types)
->where('rule_triggers.trigger_value', $oldName)
->get(['rule_triggers.*']);
Log::debug(sprintf('Found %d triggers to update.', $triggers->count()));
/** @var RuleTrigger $trigger */
foreach ($triggers as $trigger) {
$trigger->trigger_value = $newName;
$trigger->save();
Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
}
}
}

View File

@@ -81,6 +81,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* OperatorQuerySearch constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
@@ -194,6 +195,7 @@ class OperatorQuerySearch implements SearchInterface
if (0 === count($this->getWords()) && 0 === count($this->getOperators())) {
return new LengthAwarePaginator;
}
return $this->collector->getPaginatedGroups();
}
@@ -212,12 +214,13 @@ class OperatorQuerySearch implements SearchInterface
$this->collector->setUser($this->user);
$this->collector->withAccountInformation()->withCategoryInformation()->withBudgetInformation();
$this->setLimit((int) app('preferences')->getForUser($user, 'listPageSize', 50)->data);
$this->setLimit((int)app('preferences')->getForUser($user, 'listPageSize', 50)->data);
}
/**
* @param Node $searchNode
*
* @throws FireflyException
*/
private function handleSearchNode(Node $searchNode): void
@@ -237,7 +240,7 @@ class OperatorQuerySearch implements SearchInterface
case Emoji::class:
case Mention::class:
Log::debug(sprintf('Now handle %s', $class));
$this->words[] = (string) $searchNode->getValue();
$this->words[] = (string)$searchNode->getValue();
break;
case Field::class:
Log::debug(sprintf('Now handle %s', $class));
@@ -246,15 +249,13 @@ class OperatorQuerySearch implements SearchInterface
$operator = strtolower($searchNode->getValue());
$value = $searchNode->getNode()->getValue();
// must be valid operator:
if (in_array($operator, $this->validOperators, true)) {
if ($this->updateCollector($operator, (string) $value)) {
$this->operators->push(
[
'type' => self::getRootOperator($operator),
'value' => (string) $value,
]
);
}
if (in_array($operator, $this->validOperators, true) && $this->updateCollector($operator, (string)$value)) {
$this->operators->push(
[
'type' => self::getRootOperator($operator),
'value' => (string)$value,
]
);
}
break;
}
@@ -264,6 +265,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* @param string $operator
* @param string $value
*
* @return bool
* @throws FireflyException
*/
@@ -283,6 +285,7 @@ class OperatorQuerySearch implements SearchInterface
// some search operators are ignored, basically:
case 'user_action':
Log::info(sprintf('Ignore search operator "%s"', $operator));
return false;
//
// all account related searches:
@@ -312,7 +315,7 @@ class OperatorQuerySearch implements SearchInterface
$this->searchAccount($value, 1, 3);
break;
case 'source_account_id':
$account = $this->accountRepository->findNull((int) $value);
$account = $this->accountRepository->findNull((int)$value);
if (null !== $account) {
$this->collector->setSourceAccounts(new Collection([$account]));
}
@@ -346,7 +349,7 @@ class OperatorQuerySearch implements SearchInterface
$this->searchAccount($value, 2, 3);
break;
case 'destination_account_id':
$account = $this->accountRepository->findNull((int) $value);
$account = $this->accountRepository->findNull((int)$value);
if (null !== $account) {
$this->collector->setDestinationAccounts(new Collection([$account]));
}
@@ -355,7 +358,7 @@ class OperatorQuerySearch implements SearchInterface
$parts = explode(',', $value);
$collection = new Collection;
foreach ($parts as $accountId) {
$account = $this->accountRepository->findNull((int) $accountId);
$account = $this->accountRepository->findNull((int)$accountId);
if (null !== $account) {
$collection->push($account);
}
@@ -390,6 +393,7 @@ class OperatorQuerySearch implements SearchInterface
break;
case 'description_contains':
$this->words[] = $value;
return false;
case 'description_is':
$this->collector->descriptionIs($value);
@@ -494,17 +498,17 @@ class OperatorQuerySearch implements SearchInterface
// amount
//
case 'amount_exactly':
$amount = app('steam')->positive((string) $value);
$amount = app('steam')->positive((string)$value);
Log::debug(sprintf('Set "%s" using collector with value "%s"', $operator, $amount));
$this->collector->amountIs($amount);
break;
case 'amount_less':
$amount = app('steam')->positive((string) $value);
$amount = app('steam')->positive((string)$value);
Log::debug(sprintf('Set "%s" using collector with value "%s"', $operator, $amount));
$this->collector->amountLess($amount);
break;
case 'amount_more':
$amount = app('steam')->positive((string) $value);
$amount = app('steam')->positive((string)$value);
Log::debug(sprintf('Set "%s" using collector with value "%s"', $operator, $amount));
$this->collector->amountMore($amount);
break;
@@ -520,7 +524,12 @@ class OperatorQuerySearch implements SearchInterface
//
case 'date_is':
$range = $this->parseDateRange($value);
Log::debug(sprintf('Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'), $range['end']->format('Y-m-d')));
Log::debug(
sprintf(
'Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'),
$range['end']->format('Y-m-d')
)
);
$this->collector->setRange($range['start'], $range['end']);
// add to operators manually:
@@ -531,7 +540,12 @@ class OperatorQuerySearch implements SearchInterface
case 'date_before':
Log::debug(sprintf('Value for date_before is "%s"', $value));
$range = $this->parseDateRange($value);
Log::debug(sprintf('Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'), $range['end']->format('Y-m-d')));
Log::debug(
sprintf(
'Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'),
$range['end']->format('Y-m-d')
)
);
// add to operators manually:
$this->operators->push(['type' => 'date_before', 'value' => $range['start']->format('Y-m-d'),]);
@@ -541,7 +555,12 @@ class OperatorQuerySearch implements SearchInterface
case 'date_after':
Log::debug(sprintf('Value for date_after is "%s"', $value));
$range = $this->parseDateRange($value);
Log::debug(sprintf('Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'), $range['end']->format('Y-m-d')));
Log::debug(
sprintf(
'Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'),
$range['end']->format('Y-m-d')
)
);
// add to operators manually:
$this->operators->push(['type' => 'date_after', 'value' => $range['end']->format('Y-m-d'),]);
@@ -568,12 +587,14 @@ class OperatorQuerySearch implements SearchInterface
$this->collector->setInternalReference($value);
break;
}
return true;
}
/**
* searchDirection: 1 = source (default), 2 = destination
* stringPosition: 1 = start (default), 2 = end, 3 = contains, 4 = is
*
* @param string $value
* @param int $searchDirection
* @param int $stringPosition
@@ -617,15 +638,18 @@ class OperatorQuerySearch implements SearchInterface
return;
}
Log::debug(sprintf('Found %d accounts, will filter.', $accounts->count()));
$filtered = $accounts->filter(function (Account $account) use ($value, $stringMethod) {
return $stringMethod(strtolower($account->name), strtolower($value));
});
$filtered = $accounts->filter(
function (Account $account) use ($value, $stringMethod) {
return $stringMethod(strtolower($account->name), strtolower($value));
}
);
if (0 === $filtered->count()) {
Log::debug('Left with zero accounts, search for invalid account.');
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
return;
}
Log::debug(sprintf('Left with %d, set as %s().', $filtered->count(), $collectorMethod));
@@ -636,6 +660,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* searchDirection: 1 = source (default), 2 = destination
* stringPosition: 1 = start (default), 2 = end, 3 = contains, 4 = is
*
* @param string $value
* @param int $searchDirection
* @param int $stringPosition
@@ -675,29 +700,34 @@ class OperatorQuerySearch implements SearchInterface
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
return;
}
// if found, do filter
Log::debug(sprintf('Found %d accounts, will filter.', $accounts->count()));
$filtered = $accounts->filter(function (Account $account) use ($value, $stringMethod) {
// either IBAN or account number!
$ibanMatch = $stringMethod(strtolower((string) $account->iban), strtolower((string) $value));
$accountNrMatch = false;
/** @var AccountMeta $meta */
foreach ($account->accountMeta as $meta) {
if ('account_number' === $meta->name && $stringMethod(strtolower($meta->data), strtolower($value))) {
$accountNrMatch = true;
$filtered = $accounts->filter(
function (Account $account) use ($value, $stringMethod) {
// either IBAN or account number!
$ibanMatch = $stringMethod(strtolower((string)$account->iban), strtolower((string)$value));
$accountNrMatch = false;
/** @var AccountMeta $meta */
foreach ($account->accountMeta as $meta) {
if ('account_number' === $meta->name && $stringMethod(strtolower($meta->data), strtolower($value))) {
$accountNrMatch = true;
}
}
return $ibanMatch || $accountNrMatch;
}
return $ibanMatch || $accountNrMatch;
});
);
if (0 === $filtered->count()) {
Log::debug('Left with zero, search for invalid account');
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
return;
}
Log::debug(sprintf('Left with %d, set as %s().', $filtered->count(), $collectorMethod));
@@ -706,6 +736,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* @param string $value
*
* @return TransactionCurrency|null
*/
private function findCurrency(string $value): ?TransactionCurrency
@@ -721,11 +752,13 @@ class OperatorQuerySearch implements SearchInterface
if (null === $result) {
$result = $this->currencyRepository->findByNameNull($value);
}
return $result;
}
/**
* @param string $operator
*
* @return string
* @throws FireflyException
*/
@@ -737,6 +770,7 @@ class OperatorQuerySearch implements SearchInterface
}
if (true === $config['alias']) {
Log::debug(sprintf('"%s" is an alias for "%s", so return that instead.', $operator, $config['alias_for']));
return $config['alias_for'];
}
Log::debug(sprintf('"%s" is not an alias.', $operator));
@@ -746,6 +780,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* @param string $value
*
* @return array
* @throws FireflyException
*/
@@ -756,6 +791,7 @@ class OperatorQuerySearch implements SearchInterface
return $parser->parseRange($value, today(config('app.timezone')));
}
$date = $parser->parseDate($value);
return [
'start' => $date,
'end' => $date,

View File

@@ -49,9 +49,6 @@ class Steam
*/
public function balance(Account $account, Carbon $date, ?TransactionCurrency $currency = null): string
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
@@ -105,9 +102,6 @@ class Steam
*/
public function balanceIgnoreVirtual(Account $account, Carbon $date): string
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
@@ -177,9 +171,6 @@ class Steam
*/
public function balanceInRange(Account $account, Carbon $start, Carbon $end, ?TransactionCurrency $currency = null): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
@@ -264,9 +255,6 @@ class Steam
*/
public function balancePerCurrency(Account $account, Carbon $date): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
@@ -300,9 +288,6 @@ class Steam
*/
public function balancesByAccounts(Collection $accounts, Carbon $date): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$ids = $accounts->pluck('id')->toArray();
// cache this property.
$cache = new CacheProperties;
@@ -335,9 +320,6 @@ class Steam
*/
public function balancesPerCurrencyByAccounts(Collection $accounts, Carbon $date): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$ids = $accounts->pluck('id')->toArray();
// cache this property.
$cache = new CacheProperties;
@@ -497,9 +479,6 @@ class Steam
*/
public function getLastActivities(array $accounts): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$list = [];
$set = auth()->user()->transactions()

View File

@@ -77,7 +77,7 @@ class Rule extends AbstractExtension
return new TwigFunction(
'allRuleTriggers',
static function () {
$ruleTriggers = array_keys(config('firefly.search.operators'));;
$ruleTriggers = array_keys(config('firefly.search.operators'));
$possibleTriggers = [];
foreach ($ruleTriggers as $key) {
if ('user_action' !== $key) {

View File

@@ -362,7 +362,7 @@ class FireflyValidator extends Validator
}
// and finally a "will match everything check":
$classes = array_keys(config('firefly.search.operators'));;
$classes = array_keys(config('firefly.search.operators'));
/** @var TriggerInterface $class */
$class = $classes[$triggerType] ?? false;
if (false === $class) {