From 0846d4898f11709de52900a7ddd7e6f42da9ee23 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 7 Apr 2021 05:55:51 +0200 Subject: [PATCH] Various code cleanup. --- .../Models/Transaction/StoreRequest.php | 22 ++-- .../Controllers/Chart/BudgetController.php | 1 - app/Models/Attachment.php | 43 +++---- app/Models/TransactionJournalLink.php | 2 + app/Repositories/Rule/RuleRepository.php | 2 +- app/Repositories/Tag/TagRepository.php | 111 ++---------------- .../Tag/TagRepositoryInterface.php | 11 -- .../Telemetry/TelemetryRepository.php | 2 +- .../TelemetryRepositoryInterface.php | 3 +- .../TransactionGroupRepository.php | 9 +- app/Repositories/User/UserRepository.php | 12 +- 11 files changed, 56 insertions(+), 162 deletions(-) diff --git a/app/Api/V1/Requests/Models/Transaction/StoreRequest.php b/app/Api/V1/Requests/Models/Transaction/StoreRequest.php index 74cb4d6c87..8ae39522d5 100644 --- a/app/Api/V1/Requests/Models/Transaction/StoreRequest.php +++ b/app/Api/V1/Requests/Models/Transaction/StoreRequest.php @@ -83,7 +83,7 @@ class StoreRequest extends FormRequest 'order' => $this->integerFromValue((string)$object['order']), 'currency_id' => $this->integerFromValue((string)$object['currency_id']), - 'currency_code' => $this->clearString($object['currency_code'], false), + 'currency_code' => $this->clearString((string)$object['currency_code'], false), // foreign currency info: 'foreign_currency_id' => $this->integerFromValue((string)$object['foreign_currency_id']), @@ -132,21 +132,21 @@ class StoreRequest extends FormRequest 'tags' => $this->arrayFromValue($object['tags']), // all custom fields: - 'internal_reference' => $this->clearString((string)$object['internal_reference'],false), + 'internal_reference' => $this->clearString((string)$object['internal_reference'], false), 'external_id' => $this->clearString((string)$object['external_id'], false), 'original_source' => sprintf('ff3-v%s|api-v%s', config('firefly.version'), config('firefly.api_version')), 'recurrence_id' => $this->integerFromValue($object['recurrence_id']), - 'bunq_payment_id' => $this->clearString((string)$object['bunq_payment_id'],false), + 'bunq_payment_id' => $this->clearString((string)$object['bunq_payment_id'], false), 'external_uri' => $this->clearString((string)$object['external_uri'], false), - 'sepa_cc' => $this->clearString($object['sepa_cc'],false), - 'sepa_ct_op' => $this->clearString($object['sepa_ct_op'],false), - 'sepa_ct_id' => $this->clearString($object['sepa_ct_id'],false), - 'sepa_db' => $this->clearString($object['sepa_db'],false), - 'sepa_country' => $this->clearString($object['sepa_country'],false), - 'sepa_ep' => $this->clearString($object['sepa_ep'],false), - 'sepa_ci' => $this->clearString($object['sepa_ci'],false), - 'sepa_batch_id' => $this->clearString($object['sepa_batch_id'],false), + 'sepa_cc' => $this->clearString($object['sepa_cc'], false), + 'sepa_ct_op' => $this->clearString($object['sepa_ct_op'], false), + 'sepa_ct_id' => $this->clearString($object['sepa_ct_id'], false), + 'sepa_db' => $this->clearString($object['sepa_db'], false), + 'sepa_country' => $this->clearString($object['sepa_country'], false), + 'sepa_ep' => $this->clearString($object['sepa_ep'], false), + 'sepa_ci' => $this->clearString($object['sepa_ci'], false), + 'sepa_batch_id' => $this->clearString($object['sepa_batch_id'], false), // custom date fields. Must be Carbon objects. Presence is optional. 'interest_date' => $this->dateFromValue($object['interest_date']), 'book_date' => $this->dateFromValue($object['book_date']), diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 1cca259abf..6ba5cff53f 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -304,7 +304,6 @@ class BudgetController extends Controller 'amount' => '0', 'currency_symbol' => $journal['currency_symbol'], 'currency_code' => $journal['currency_code'], - 'currency_symbol' => $journal['currency_symbol'], 'currency_name' => $journal['currency_name'], ]; $result[$key]['amount'] = bcadd($journal['amount'], $result[$key]['amount']); diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php index 263f050b5b..ba68970d71 100644 --- a/app/Models/Attachment.php +++ b/app/Models/Attachment.php @@ -22,7 +22,6 @@ declare(strict_types=1); namespace FireflyIII\Models; -use Carbon\Carbon; use Eloquent; use FireflyIII\User; use Illuminate\Database\Eloquent\Collection; @@ -37,24 +36,26 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * FireflyIII\Models\Attachment * - * @property int $id - * @property \Illuminate\Support\Carbon|null $created_at - * @property \Illuminate\Support\Carbon|null $updated_at - * @property \Illuminate\Support\Carbon|null $deleted_at - * @property int $user_id - * @property int $attachable_id - * @property string $attachable_type - * @property string $md5 - * @property string $filename - * @property string|null $title - * @property string|null $description - * @property string $mime - * @property int $size - * @property bool $uploaded - * @property-read Model|\Eloquent $attachable + * @property int $id + * @property \Illuminate\Support\Carbon|null $created_at + * @property \Illuminate\Support\Carbon|null $updated_at + * @property \Illuminate\Support\Carbon|null $deleted_at + * @property int $user_id + * @property int $attachable_id + * @property string $attachable_type + * @property bool $file_exists + * @property string $md5 + * @property string $filename + * @property string|null $title + * @property string|null $description + * @property string $mime + * @property int $size + * @property bool $uploaded + * @property string $notes_text + * @property-read Model|\Eloquent $attachable * @property Collection|\FireflyIII\Models\Note[] $notes - * @property-read int|null $notes_count - * @property-read User $user + * @property-read int|null $notes_count + * @property-read User $user * @method static \Illuminate\Database\Eloquent\Builder|Attachment newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Attachment newQuery() * @method static Builder|Attachment onlyTrashed() @@ -101,13 +102,13 @@ class Attachment extends Model * * @param string $value * - * @throws NotFoundHttpException * @return Attachment + * @throws NotFoundHttpException */ public static function routeBinder(string $value): Attachment { if (auth()->check()) { - $attachmentId = (int) $value; + $attachmentId = (int)$value; /** @var User $user */ $user = auth()->user(); /** @var Attachment $attachment */ @@ -139,7 +140,7 @@ class Attachment extends Model */ public function fileName(): string { - return sprintf('at-%s.data', (string) $this->id); + return sprintf('at-%s.data', (string)$this->id); } /** diff --git a/app/Models/TransactionJournalLink.php b/app/Models/TransactionJournalLink.php index e50bd2e2c8..c8662191fa 100644 --- a/app/Models/TransactionJournalLink.php +++ b/app/Models/TransactionJournalLink.php @@ -46,6 +46,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @property-read Collection|\FireflyIII\Models\Note[] $notes * @property-read int|null $notes_count * @property-read \FireflyIII\Models\TransactionJournal $source + * @property-read string $inward + * @property-read string $outward * @method static Builder|TransactionJournalLink newModelQuery() * @method static Builder|TransactionJournalLink newQuery() * @method static Builder|TransactionJournalLink query() diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.php index 9cb50e7af9..68130393cd 100644 --- a/app/Repositories/Rule/RuleRepository.php +++ b/app/Repositories/Rule/RuleRepository.php @@ -502,7 +502,7 @@ class RuleRepository implements RuleRepositoryInterface */ private function setRuleTrigger(string $moment, Rule $rule): void { - /** @var RuleTrigger $trigger */ + /** @var RuleTrigger|null $trigger */ $trigger = $rule->ruleTriggers()->where('trigger_type', 'user_action')->first(); if (null !== $trigger) { $trigger->trigger_value = $moment; diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 0acb5964b5..9f6ce7bc85 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -29,6 +29,7 @@ use FireflyIII\Factory\TagFactory; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\Attachment; use FireflyIII\Models\Location; +use FireflyIII\Models\Note; use FireflyIII\Models\Tag; use FireflyIII\Models\TransactionType; use FireflyIII\User; @@ -150,12 +151,12 @@ class TagRepository implements TagRepositoryInterface $disk = Storage::disk('upload'); return $set->each( - static function (Attachment $attachment) use ($disk) { - $notes = $attachment->notes()->first(); + static function (Attachment $attachment, int $index) use ($disk) { + /** @var Note $note */ + $note = $attachment->notes()->first(); + // only used in v1 view of tags $attachment->file_exists = $disk->exists($attachment->fileName()); - $attachment->notes = $notes ? $notes->text : ''; - - return $attachment; + $attachment->notes_text = null === $note ? '' : $note->text; } ); } @@ -171,7 +172,7 @@ class TagRepository implements TagRepositoryInterface /** * @param int|null $year * - * @return Collection + * @return array */ public function getTagsInYear(?int $year): array { @@ -384,57 +385,6 @@ class TagRepository implements TagRepositoryInterface return $sums; } - /** - * Generates a tag cloud. - * - * @param int|null $year - * - * @return array - * @deprecated - */ - public function tagCloud(?int $year): array - { - // Some vars - $tags = $this->getTagsInYear($year); - - $max = $this->getMaxAmount($tags); - $min = $this->getMinAmount($tags); - $diff = bcsub($max, $min); - $return = []; - $minimumFont = '12'; // default scale is from 12 to 24, so 12 points. - $maxPoints = '12'; - $pointsPerCoin = '0'; - - Log::debug(sprintf('Minimum is %s, maximum is %s, difference is %s', $min, $max, $diff)); - - if (0 !== bccomp($diff, '0')) { // for each full coin in tag, add so many points - // minus the smallest tag. - $pointsPerCoin = bcdiv($maxPoints, $diff); - } - - Log::debug(sprintf('Each coin in a tag earns it %s points', $pointsPerCoin)); - /** @var Tag $tag */ - foreach ($tags as $tag) { - $amount = (string)$tag->amount_sum; - $amount = '' === $amount ? '0' : $amount; - $amountMin = bcsub($amount, $min); - $pointsForTag = bcmul($amountMin, $pointsPerCoin); - $fontSize = bcadd($minimumFont, $pointsForTag); - Log::debug(sprintf('Tag "%s": Amount is %s, so points is %s', $tag->tag, $amount, $fontSize)); - - // return value for tag cloud: - $return[$tag->id] = [ - 'size' => $fontSize, - 'tag' => $tag->tag, - 'id' => $tag->id, - 'created_at' => $tag->created_at, - 'location' => $this->getLocation($tag), - ]; - } - - return $return; - } - /** * @param Tag $tag * @param Carbon $start @@ -507,51 +457,4 @@ class TagRepository implements TagRepositoryInterface return $tag; } - /** - * @param Collection $tags - * - * @return string - */ - private function getMaxAmount(Collection $tags): string - { - $max = '0'; - /** @var Tag $tag */ - foreach ($tags as $tag) { - $amount = (string)$tag->amount_sum; - $amount = '' === $amount ? '0' : $amount; - $max = 1 === bccomp($amount, $max) ? $amount : $max; - - } - Log::debug(sprintf('Maximum is %s.', $max)); - - return $max; - } - - /** - * @param Collection $tags - * - * @return string - * - */ - private function getMinAmount(Collection $tags): string - { - $min = null; - - /** @var Tag $tag */ - foreach ($tags as $tag) { - $amount = (string)$tag->amount_sum; - $amount = '' === $amount ? '0' : $amount; - - if (null === $min) { - $min = $amount; - } - $min = -1 === bccomp($amount, $min) ? $amount : $min; - } - if (null === $min) { - $min = '0'; - } - Log::debug(sprintf('Minimum is %s.', $min)); - - return $min; - } } diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php index c5a019bd0b..bf231e8fb8 100644 --- a/app/Repositories/Tag/TagRepositoryInterface.php +++ b/app/Repositories/Tag/TagRepositoryInterface.php @@ -187,17 +187,6 @@ interface TagRepositoryInterface */ public function sumsOfTag(Tag $tag, ?Carbon $start, ?Carbon $end): array; - /** - * Generates a tag cloud. - * - * @param int|null $year - * - * @return array - * @deprecated - * - */ - public function tagCloud(?int $year): array; - /** * @param Tag $tag * @param Carbon $start diff --git a/app/Repositories/Telemetry/TelemetryRepository.php b/app/Repositories/Telemetry/TelemetryRepository.php index ad6026a167..403ad37c69 100644 --- a/app/Repositories/Telemetry/TelemetryRepository.php +++ b/app/Repositories/Telemetry/TelemetryRepository.php @@ -25,7 +25,7 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Telemetry; use FireflyIII\Models\Telemetry; -use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Contracts\Pagination\LengthAwarePaginator; /** * Class TelemetryRepository diff --git a/app/Repositories/Telemetry/TelemetryRepositoryInterface.php b/app/Repositories/Telemetry/TelemetryRepositoryInterface.php index 898251fc53..fef7f8ddfc 100644 --- a/app/Repositories/Telemetry/TelemetryRepositoryInterface.php +++ b/app/Repositories/Telemetry/TelemetryRepositoryInterface.php @@ -23,7 +23,8 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Telemetry; -use Illuminate\Pagination\LengthAwarePaginator; + +use Illuminate\Contracts\Pagination\LengthAwarePaginator; /** * Interface TelemetryRepositoryInterface diff --git a/app/Repositories/TransactionGroup/TransactionGroupRepository.php b/app/Repositories/TransactionGroup/TransactionGroupRepository.php index 99575cdade..a0581d398e 100644 --- a/app/Repositories/TransactionGroup/TransactionGroupRepository.php +++ b/app/Repositories/TransactionGroup/TransactionGroupRepository.php @@ -112,7 +112,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface $result[$journalId] = $result[$journalId] ?? []; $current = $attachment->toArray(); $current['file_exists'] = true; - $current['journal_title'] = $attachment->attachable->description; + $current['journal_title'] = $attachment->attachable->description; // @phpstan-ignore-line $result[$journalId][] = $current; } @@ -246,7 +246,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface */ public function getNoteText(int $journalId): ?string { - /** @var Note $note */ + /** @var Note|null $note */ $note = Note ::where('noteable_id', $journalId) ->where('noteable_type', TransactionJournal::class) @@ -269,6 +269,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface { $return = []; $journals = $group->transactionJournals->pluck('id')->toArray(); + $currency = app('amount')->getDefaultCurrencyByUser($this->user); $data = PiggyBankEvent ::whereIn('transaction_journal_id', $journals) ->with('piggyBank', 'piggyBank.account') @@ -357,12 +358,12 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface return $factory->create($data); } catch (DuplicateTransactionException $e) { Log::warning('Group repository caught group factory with a duplicate exception!'); - throw new DuplicateTransactionException($e->getMessage()); + throw new DuplicateTransactionException($e->getMessage(),0, $e); } catch (FireflyException $e) { Log::warning('Group repository caught group factory with an exception!'); Log::error($e->getMessage()); Log::error($e->getTraceAsString()); - throw new FireflyException($e->getMessage()); + throw new FireflyException($e->getMessage(),0, $e); } } diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php index a55703cba1..780d6c6376 100644 --- a/app/Repositories/User/UserRepository.php +++ b/app/Repositories/User/UserRepository.php @@ -253,12 +253,10 @@ class UserRepository implements UserRepositoryInterface ->where('amount', '>', 0) ->whereNull('budgets.deleted_at') ->where('budgets.user_id', $user->id) - ->get(['budget_limits.budget_id']) - ->count(); + ->count('budget_limits.budget_id'); $return['rule_groups'] = $user->ruleGroups()->count(); $return['rules'] = $user->rules()->count(); $return['tags'] = $user->tags()->count(); - var_dump($return);exit; return $return; } @@ -338,7 +336,7 @@ class UserRepository implements UserRepositoryInterface */ public function unblockUser(User $user): void { - $user->blocked = 0; + $user->blocked = false; $user->blocked_code = ''; $user->save(); @@ -355,13 +353,13 @@ class UserRepository implements UserRepositoryInterface public function update(User $user, array $data): User { $this->updateEmail($user, $data['email'] ?? ''); - if (isset($data['blocked']) && is_bool($data['blocked'])) { + if (array_key_exists('blocked', $data) && is_bool($data['blocked'])) { $user->blocked = $data['blocked']; } - if (isset($data['blocked_code']) && '' !== $data['blocked_code'] && is_string($data['blocked_code'])) { + if (array_key_exists('blocked_code', $data) && '' !== $data['blocked_code'] && is_string($data['blocked_code'])) { $user->blocked_code = $data['blocked_code']; } - if (isset($data['role']) && '' === $data['role']) { + if (array_key_exists('role', $data) && '' === $data['role']) { $this->removeRole($user, 'owner'); $this->removeRole($user, 'demo'); }