Various code cleanup.

This commit is contained in:
James Cole 2021-04-07 05:55:51 +02:00
parent cb746200fa
commit 0846d4898f
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
11 changed files with 56 additions and 162 deletions

View File

@ -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']),

View File

@ -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']);

View File

@ -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);
}
/**

View File

@ -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()

View File

@ -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;

View File

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

View File

@ -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

View File

@ -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

View File

@ -23,7 +23,8 @@
declare(strict_types=1);
namespace FireflyIII\Repositories\Telemetry;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
/**
* Interface TelemetryRepositoryInterface

View File

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

View File

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