Various code cleanup.

This commit is contained in:
James Cole
2021-04-06 18:48:02 +02:00
parent fe06a1f7a0
commit cb746200fa
14 changed files with 38 additions and 33 deletions

View File

@@ -96,7 +96,7 @@ class UserRepository implements UserRepositoryInterface
// update user // update user
$user->email = $newEmail; $user->email = $newEmail;
$user->blocked = 1; $user->blocked = true;
$user->blocked_code = 'email_changed'; $user->blocked_code = 'email_changed';
$user->save(); $user->save();
@@ -215,7 +215,7 @@ class UserRepository implements UserRepositoryInterface
*/ */
public function getRoleByUser(User $user): ?string public function getRoleByUser(User $user): ?string
{ {
/** @var Role $role */ /** @var Role|null $role */
$role = $user->roles()->first(); $role = $user->roles()->first();
if (null !== $role) { if (null !== $role) {
return $role->name; return $role->name;
@@ -252,10 +252,13 @@ class UserRepository implements UserRepositoryInterface
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') ->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
->where('amount', '>', 0) ->where('amount', '>', 0)
->whereNull('budgets.deleted_at') ->whereNull('budgets.deleted_at')
->where('budgets.user_id', $user->id)->get(['budget_limits.budget_id'])->count(); ->where('budgets.user_id', $user->id)
->get(['budget_limits.budget_id'])
->count();
$return['rule_groups'] = $user->ruleGroups()->count(); $return['rule_groups'] = $user->ruleGroups()->count();
$return['rules'] = $user->rules()->count(); $return['rules'] = $user->rules()->count();
$return['tags'] = $user->tags()->count(); $return['tags'] = $user->tags()->count();
var_dump($return);exit;
return $return; return $return;
} }

View File

@@ -26,7 +26,6 @@ namespace FireflyIII\Rules;
use Carbon\Carbon; use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException; use Carbon\Exceptions\InvalidDateException;
use Exception;
use Illuminate\Contracts\Validation\Rule; use Illuminate\Contracts\Validation\Rule;
use Log; use Log;
@@ -65,7 +64,7 @@ class IsDateOrTime implements Rule
// probably a date format. // probably a date format.
try { try {
Carbon::createFromFormat('Y-m-d', $value); Carbon::createFromFormat('Y-m-d', $value);
} catch (InvalidDateException | Exception $e) { } catch (InvalidDateException $e) {
Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage())); Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
return false; return false;
@@ -76,7 +75,7 @@ class IsDateOrTime implements Rule
// is an atom string, I hope? // is an atom string, I hope?
try { try {
Carbon::parse($value); Carbon::parse($value);
} catch (InvalidDateException | Exception $e) { } catch (InvalidDateException $e) {
Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage())); Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
return false; return false;

View File

@@ -110,7 +110,7 @@ class IsValidAttachmentModel implements Rule
Transaction::class => 'validateTransaction', Transaction::class => 'validateTransaction',
TransactionJournal::class => 'validateJournal', TransactionJournal::class => 'validateJournal',
]; ];
if (!isset($methods[$this->model])) { if (!array_key_exists($this->model, $methods)) {
Log::error(sprintf('Cannot validate model "%s" in %s.', $this->model, __METHOD__)); Log::error(sprintf('Cannot validate model "%s" in %s.', $this->model, __METHOD__));
return false; return false;

View File

@@ -27,7 +27,8 @@ namespace FireflyIII\Scopes;
use Adldap\Laravel\Scopes\ScopeInterface; use Adldap\Laravel\Scopes\ScopeInterface;
use Adldap\Query\Builder; use Adldap\Query\Builder;
class LdapFilterScope implements ScopeInterface // @phpstan-ignore-next-line
class LdapFilterScope implements ScopeInterface // @phpstan-ignore-line
{ {
/** /**
* If the ADLDAP_AUTH_FILTER is provided, apply the filter to the LDAP query. * If the ADLDAP_AUTH_FILTER is provided, apply the filter to the LDAP query.

View File

@@ -91,7 +91,7 @@ class UpdateRequest implements UpdateRequestInterface
'timeout' => 3.1415, 'timeout' => 3.1415,
]; ];
$res = $client->request('GET', $uri, $options); $res = $client->request('GET', $uri, $options);
} catch (GuzzleException | Exception $e) { } catch (GuzzleException $e) {
Log::error('Ran into Guzzle error.'); Log::error('Ran into Guzzle error.');
Log::error($e->getMessage()); Log::error($e->getMessage());
Log::error($e->getTraceAsString()); Log::error($e->getTraceAsString());
@@ -111,7 +111,7 @@ class UpdateRequest implements UpdateRequestInterface
try { try {
$json = json_decode($body, true, 512, JSON_THROW_ON_ERROR); $json = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException | Exception $e) { } catch (JsonException $e) {
Log::error('Body is not valid JSON'); Log::error('Body is not valid JSON');
Log::error($body); Log::error($body);
$return['message'] = 'Invalid JSON :('; $return['message'] = 'Invalid JSON :(';
@@ -119,7 +119,7 @@ class UpdateRequest implements UpdateRequestInterface
return $return; return $return;
} }
if (!isset($json['firefly_iii'][$channel])) { if (!array_key_exists($channel, $json['firefly_iii'])) {
Log::error(sprintf('No valid update channel "%s"', $channel)); Log::error(sprintf('No valid update channel "%s"', $channel));
Log::error($body); Log::error($body);
$return['message'] = sprintf('Unknown update channel "%s" :(', $channel); $return['message'] = sprintf('Unknown update channel "%s" :(', $channel);

View File

@@ -69,8 +69,8 @@ class AccountDestroyService
// delete account. // delete account.
try { try {
$account->delete(); $account->delete();
} catch (Exception $e) { // @codeCoverageIgnore } catch (Exception $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not delete account: %s', $e->getMessage())); // @codeCoverageIgnore // @ignoreException
} }
} }
@@ -122,7 +122,7 @@ class AccountDestroyService
$collection = Transaction::groupBy('transaction_journal_id', 'account_id') $collection = Transaction::groupBy('transaction_journal_id', 'account_id')
->where('account_id', $moveTo->id) ->where('account_id', $moveTo->id)
->get(['transaction_journal_id', 'account_id', DB::raw('count(*) as the_count')]); ->get(['transaction_journal_id', 'account_id', DB::raw('count(*) as the_count')]); // @phpstan-ignore-line
if (0 === $collection->count()) { if (0 === $collection->count()) {
return; return;
} }

View File

@@ -40,8 +40,8 @@ class BillDestroyService
{ {
try { try {
$bill->delete(); $bill->delete();
} catch (Exception $e) { // @codeCoverageIgnore } catch (Exception $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not delete bill: %s', $e->getMessage())); // @codeCoverageIgnore // @ignoreException
} }
} }

View File

@@ -43,8 +43,8 @@ class BudgetDestroyService
try { try {
$budget->delete(); $budget->delete();
} catch (Exception $e) { // @codeCoverageIgnore } catch (Exception $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not delete budget: %s', $e->getMessage())); // @codeCoverageIgnore // @ignoreException
} }
// also delete auto budget: // also delete auto budget:

View File

@@ -42,8 +42,8 @@ class CategoryDestroyService
{ {
try { try {
$category->delete(); $category->delete();
} catch (Exception $e) { // @codeCoverageIgnore } catch (Exception $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not delete category: %s', $e->getMessage())); // @codeCoverageIgnore // @ignoreException
} }
// also delete all relations between categories and transaction journals: // also delete all relations between categories and transaction journals:

View File

@@ -42,8 +42,8 @@ class CurrencyDestroyService
try { try {
$currency->delete(); $currency->delete();
} catch (Exception $e) { // @codeCoverageIgnore } catch (Exception $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not delete transaction currency: %s', $e->getMessage())); // @codeCoverageIgnore // @ignoreException
} }
} }

View File

@@ -96,8 +96,8 @@ class JournalDestroyService
} }
} }
} catch (Exception $e) { } catch (Exception $e) { // @phpstan-ignore-line
Log::error(sprintf('Could not delete bill: %s', $e->getMessage())); // @codeCoverageIgnore // @ignoreException
} }
} }

View File

@@ -60,8 +60,8 @@ class RecurrenceDestroyService
try { try {
// delete all meta data // delete all meta data
$recurrence->recurrenceMeta()->delete(); $recurrence->recurrenceMeta()->delete();
} catch (Exception $e) { // @codeCoverageIgnore } catch (Exception $e) { // @phpstan-ignore-line
Log::info(sprintf('Could not delete recurrence meta: %s', $e->getMessage())); // @codeCoverageIgnore // @ignoreException
} }
// delete all transactions. // delete all transactions.
/** @var RecurrenceTransaction $transaction */ /** @var RecurrenceTransaction $transaction */
@@ -69,8 +69,8 @@ class RecurrenceDestroyService
$transaction->recurrenceTransactionMeta()->delete(); $transaction->recurrenceTransactionMeta()->delete();
try { try {
$transaction->delete(); $transaction->delete();
} catch (Exception $e) { // @codeCoverageIgnore } catch (Exception $e) { // @phpstan-ignore-line
Log::info(sprintf('Could not delete recurrence transaction: %s', $e->getMessage())); // @codeCoverageIgnore // @ignoreException
} }
} }
// delete all repetitions // delete all repetitions
@@ -79,8 +79,8 @@ class RecurrenceDestroyService
// delete recurrence // delete recurrence
try { try {
$recurrence->delete(); $recurrence->delete();
} catch (Exception $e) { // @codeCoverageIgnore } catch (Exception $e) { // @phpstan-ignore-line
Log::info(sprintf('Could not delete recurrence: %s', $e->getMessage())); // @codeCoverageIgnore // @ignoreException
} }
} }

View File

@@ -162,8 +162,8 @@ trait AccountServiceTrait
if (null !== $dbNote) { if (null !== $dbNote) {
try { try {
$dbNote->delete(); $dbNote->delete();
} catch (Exception $e) { } catch (Exception $e) { // @phpstan-ignore-line
Log::debug($e->getMessage()); // @ignoreException
} }
} }

View File

@@ -260,7 +260,7 @@ class Preferences
{ {
$fullName = sprintf('preference%s%s', $user->id, $name); $fullName = sprintf('preference%s%s', $user->id, $name);
Cache::forget($fullName); Cache::forget($fullName);
/** @var Preference $pref */ /** @var Preference|null $pref */
$pref = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']); $pref = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
if (null !== $pref && null === $value) { if (null !== $pref && null === $value) {
@@ -277,6 +277,8 @@ class Preferences
} }
if(null === $pref) { if(null === $pref) {
$pref = new Preference; $pref = new Preference;
$pref->user_id = $user->id;
$pref->name = $name;
} }
$pref->data = $value; $pref->data = $value;
$pref->save(); $pref->save();