Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -27,7 +27,6 @@ namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use Illuminate\Console\Command;
use PDO;
use PDOException;
/**
* Class CreateDatabase
@@ -36,15 +35,12 @@ class CreateDatabase extends Command
{
use ShowsFriendlyMessages;
protected $description = 'Tries to create the database if it doesn\'t exist yet.';
protected $signature = 'firefly-iii:create-database';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
@@ -63,16 +59,17 @@ class CreateDatabase extends Command
$this->friendlyLine(sprintf('DSN is %s', $dsn));
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
];
// when it fails, display error
try {
$pdo = new PDO($dsn, env('DB_USERNAME'), env('DB_PASSWORD'), $options);
} catch (PDOException $e) {
$pdo = new \PDO($dsn, env('DB_USERNAME'), env('DB_PASSWORD'), $options);
} catch (\PDOException $e) {
$this->friendlyError(sprintf('Error when connecting to DB: %s', $e->getMessage()));
return 1;
}

View File

@@ -28,18 +28,14 @@ use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Str;
/**
* Class CreateFirstUser
*
* @package FireflyIII\Console\Commands
*/
class CreateFirstUser extends Command
{
use ShowsFriendlyMessages;
protected $description = 'Creates a new user and gives admin rights. Outputs the password on the command line. Strictly for testing.';
protected $signature = 'firefly-iii:create-first-user {email}';
@@ -47,8 +43,6 @@ class CreateFirstUser extends Command
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
@@ -70,11 +64,11 @@ class CreateFirstUser extends Command
'email' => $this->argument('email'),
'role' => 'owner',
];
$password = Str::random(24);
$password = \Str::random(24);
$user = $this->repository->store($data);
$user->password = Hash::make($password);
$user->save();
$user->setRememberToken(Str::random(60));
$user->setRememberToken(\Str::random(60));
$this->friendlyInfo(sprintf('Created new admin user (ID #%d) with email address "%s" and password "%s".', $user->id, $user->email, $password));
$this->friendlyWarning('Change this password.');
@@ -86,8 +80,6 @@ class CreateFirstUser extends Command
* Laravel will execute ALL __construct() methods for ALL commands whenever a SINGLE command is
* executed. This leads to noticeable slow-downs and class calls. To prevent this, this method should
* be called from the handle method instead of using the constructor to initialize the command.
*
*/
private function stupidLaravel(): void
{

View File

@@ -58,17 +58,17 @@ class ForceDecimalSize extends Command
private string $cast;
private array $classes
= [
'accounts' => Account::class,
'auto_budgets' => AutoBudget::class,
'available_budgets' => AvailableBudget::class,
'bills' => Bill::class,
'budget_limits' => BudgetLimit::class,
'piggy_bank_events' => PiggyBankEvent::class,
'piggy_bank_repetitions' => PiggyBankRepetition::class,
'piggy_banks' => PiggyBank::class,
'recurrences_transactions' => RecurrenceTransaction::class,
'transactions' => Transaction::class,
];
'accounts' => Account::class,
'auto_budgets' => AutoBudget::class,
'available_budgets' => AvailableBudget::class,
'bills' => Bill::class,
'budget_limits' => BudgetLimit::class,
'piggy_bank_events' => PiggyBankEvent::class,
'piggy_bank_repetitions' => PiggyBankRepetition::class,
'piggy_banks' => PiggyBank::class,
'recurrences_transactions' => RecurrenceTransaction::class,
'transactions' => Transaction::class,
];
private string $operator;
private string $regularExpression;
@@ -90,7 +90,6 @@ class ForceDecimalSize extends Command
/**
* Execute the console command.
*
*/
public function handle(): int
{
@@ -108,9 +107,6 @@ class ForceDecimalSize extends Command
return 0;
}
/**
* @return void
*/
private function determineDatabaseType(): void
{
// switch stuff based on database connection:
@@ -129,8 +125,6 @@ class ForceDecimalSize extends Command
/**
* This method checks if a basic check can be done or if it needs to be complicated.
*
* @return void
*/
private function correctAmounts(): void
{
@@ -155,13 +149,13 @@ class ForceDecimalSize extends Command
/**
* This method loops all enabled currencies and then calls the method that will fix all objects in this currency.
*
* @return void
* @throws FireflyException
*/
private function correctAmountsByCurrency(): void
{
/** @var Collection $enabled */
$enabled = TransactionCurrency::whereEnabled(1)->get();
/** @var TransactionCurrency $currency */
foreach ($enabled as $currency) {
$this->correctByCurrency($currency);
@@ -171,9 +165,6 @@ class ForceDecimalSize extends Command
/**
* This method loops the available tables that may need fixing, and calls for the right method that can fix them.
*
* @param TransactionCurrency $currency
*
* @return void
* @throws FireflyException
*/
private function correctByCurrency(TransactionCurrency $currency): void
@@ -187,32 +178,46 @@ class ForceDecimalSize extends Command
default:
$message = sprintf('Cannot handle table "%s"', $name);
$this->friendlyError($message);
throw new FireflyException($message);
case 'accounts':
$this->correctAccountAmounts($currency, $fields);
break;
case 'auto_budgets':
case 'available_budgets':
case 'bills':
case 'budget_limits':
case 'recurrences_transactions':
$this->correctGeneric($currency, $name);
break;
case 'currency_exchange_rates':
case 'limit_repetitions':
// do nothing
break;
case 'piggy_bank_events':
$this->correctPiggyEventAmounts($currency, $fields);
break;
case 'piggy_bank_repetitions':
$this->correctPiggyRepetitionAmounts($currency, $fields);
break;
case 'piggy_banks':
$this->correctPiggyAmounts($currency, $fields);
break;
case 'transactions':
$this->correctTransactionAmounts($currency);
break;
}
}
@@ -220,11 +225,6 @@ class ForceDecimalSize extends Command
/**
* This method loops over all accounts and validates the amounts.
*
* @param TransactionCurrency $currency
* @param array $fields
*
* @return void
*/
private function correctAccountAmounts(TransactionCurrency $currency, array $fields): void
{
@@ -234,8 +234,9 @@ class ForceDecimalSize extends Command
/** @var Builder $query */
$query = Account::leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
->where('account_meta.data', json_encode((string)$currency->id));
->where('account_meta.name', 'currency_id')
->where('account_meta.data', json_encode((string)$currency->id))
;
$query->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
foreach ($fields as $field) {
$q->orWhere(
@@ -251,11 +252,12 @@ class ForceDecimalSize extends Command
return;
}
/** @var Account $account */
foreach ($result as $account) {
/** @var string $field */
foreach ($fields as $field) {
$value = $account->$field;
$value = $account->{$field};
if (null === $value) {
continue;
}
@@ -270,11 +272,6 @@ class ForceDecimalSize extends Command
/**
* This method fixes all auto budgets in currency $currency.
*
* @param TransactionCurrency $currency
* @param string $table
*
* @return void
*/
private function correctGeneric(TransactionCurrency $currency, string $table): void
{
@@ -304,11 +301,12 @@ class ForceDecimalSize extends Command
return;
}
/** @var Model $item */
foreach ($result as $item) {
/** @var string $field */
foreach ($fields as $field) {
$value = $item->$field;
$value = $item->{$field};
if (null === $value) {
continue;
}
@@ -323,11 +321,6 @@ class ForceDecimalSize extends Command
/**
* This method fixes all piggy bank events in currency $currency.
*
* @param TransactionCurrency $currency
* @param array $fields
*
* @return void
*/
private function correctPiggyEventAmounts(TransactionCurrency $currency, array $fields): void
{
@@ -337,19 +330,20 @@ class ForceDecimalSize extends Command
/** @var Builder $query */
$query = PiggyBankEvent::leftJoin('piggy_banks', 'piggy_bank_events.piggy_bank_id', '=', 'piggy_banks.id')
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
->where('account_meta.data', json_encode((string)$currency->id))
->where(static function (Builder $q) use ($fields, $currency, $cast, $operator, $regularExpression) {
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(piggy_bank_events.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
});
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
->where('account_meta.data', json_encode((string)$currency->id))
->where(static function (Builder $q) use ($fields, $currency, $cast, $operator, $regularExpression) {
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(piggy_bank_events.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
})
;
$result = $query->get(['piggy_bank_events.*']);
if (0 === $result->count()) {
@@ -357,11 +351,12 @@ class ForceDecimalSize extends Command
return;
}
/** @var PiggyBankEvent $item */
foreach ($result as $item) {
/** @var string $field */
foreach ($fields as $field) {
$value = $item->$field;
$value = $item->{$field};
if (null === $value) {
continue;
}
@@ -378,33 +373,30 @@ class ForceDecimalSize extends Command
/**
* This method fixes all piggy bank repetitions in currency $currency.
*
* @param TransactionCurrency $currency
* @param array $fields
*
* @return void
*/
private function correctPiggyRepetitionAmounts(TransactionCurrency $currency, array $fields)
{
$operator = $this->operator;
$cast = $this->cast;
$regularExpression = $this->regularExpression;
// select all piggy bank repetitions with this currency and issue.
/** @var Builder $query */
$query = PiggyBankRepetition::leftJoin('piggy_banks', 'piggy_bank_repetitions.piggy_bank_id', '=', 'piggy_banks.id')
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
->where('account_meta.data', json_encode((string)$currency->id))
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(piggy_bank_repetitions.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
});
->leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
->where('account_meta.data', json_encode((string)$currency->id))
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(piggy_bank_repetitions.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
})
;
$result = $query->get(['piggy_bank_repetitions.*']);
if (0 === $result->count()) {
@@ -412,11 +404,12 @@ class ForceDecimalSize extends Command
return;
}
/** @var PiggyBankRepetition $item */
foreach ($result as $item) {
/** @var string $field */
foreach ($fields as $field) {
$value = $item->$field;
$value = $item->{$field};
if (null === $value) {
continue;
}
@@ -433,11 +426,6 @@ class ForceDecimalSize extends Command
/**
* This method fixes all piggy banks in currency $currency.
*
* @param TransactionCurrency $currency
* @param array $fields
*
* @return void
*/
private function correctPiggyAmounts(TransactionCurrency $currency, array $fields): void
{
@@ -447,18 +435,19 @@ class ForceDecimalSize extends Command
/** @var Builder $query */
$query = PiggyBank::leftJoin('accounts', 'piggy_banks.account_id', '=', 'accounts.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
->where('account_meta.data', json_encode((string)$currency->id))
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(piggy_banks.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
});
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->where('account_meta.name', 'currency_id')
->where('account_meta.data', json_encode((string)$currency->id))
->where(static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(piggy_banks.%s AS %s)', $field, $cast)), // @phpstan-ignore-line
$operator,
DB::raw(sprintf($regularExpression, $currency->decimal_places))
);
}
})
;
$result = $query->get(['piggy_banks.*']);
if (0 === $result->count()) {
@@ -466,11 +455,12 @@ class ForceDecimalSize extends Command
return;
}
/** @var PiggyBank $item */
foreach ($result as $item) {
/** @var string $field */
foreach ($fields as $field) {
$value = $item->$field;
$value = $item->{$field};
if (null === $value) {
continue;
}
@@ -485,10 +475,6 @@ class ForceDecimalSize extends Command
/**
* This method fixes all transactions in currency $currency.
*
* @param TransactionCurrency $currency
*
* @return void
*/
private function correctTransactionAmounts(TransactionCurrency $currency): void
{
@@ -532,6 +518,7 @@ class ForceDecimalSize extends Command
return;
}
/** @var Transaction $item */
foreach ($result as $item) {
$value = $item->foreign_amount;
@@ -548,9 +535,6 @@ class ForceDecimalSize extends Command
}
}
/**
* @return void
*/
private function updateDecimals(): void
{
$this->friendlyInfo('Going to force the size of DECIMAL columns. Please hold.');
@@ -567,15 +551,16 @@ class ForceDecimalSize extends Command
if ('pgsql' === $type) {
DB::select(sprintf('ALTER TABLE %s ALTER COLUMN %s TYPE DECIMAL(32,12);', $name, $field));
sleep(1);
return;
}
if ('mysql' === $type) {
DB::select(sprintf('ALTER TABLE %s CHANGE COLUMN %s %s DECIMAL(32, 12);', $name, $field, $field));
sleep(1);
return;
}
$this->friendlyError(sprintf('Cannot handle database type "%s".', $type));
}
}
}

View File

@@ -40,7 +40,6 @@ class ForceMigration extends Command
use ShowsFriendlyMessages;
use VerifiesAccessToken;
protected $description = 'This command will force-run all database migrations.';
protected $signature = 'firefly-iii:force-migrations
@@ -74,9 +73,6 @@ class ForceMigration extends Command
return 0;
}
/**
* @return void
*/
private function forceMigration(): void
{
DB::commit();

View File

@@ -1,6 +1,5 @@
<?php
/*
* OutputVersion.php
* Copyright (c) 2023 james@firefly-iii.org
@@ -32,7 +31,6 @@ use Illuminate\Console\Command;
*/
class OutputVersion extends Command
{
protected $description = 'Outputs the Firefly III version';
protected $signature = 'firefly-iii:output-version';
@@ -43,6 +41,7 @@ class OutputVersion extends Command
public function handle(): int
{
echo config('firefly.version');
return 0;
}
}

View File

@@ -23,26 +23,20 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\System;
use Crypt;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Models\Attachment;
use Illuminate\Console\Command;
use Illuminate\Contracts\Encryption\DecryptException;
use Storage;
/**
* Class ScanAttachments.
*
*/
class ScanAttachments extends Command
{
use ShowsFriendlyMessages;
protected $description = 'Rescan all attachments and re-set the correct MD5 hash and mime.';
protected $signature = 'firefly-iii:scan-attachments';
/**
@@ -51,17 +45,20 @@ class ScanAttachments extends Command
public function handle(): int
{
$attachments = Attachment::get();
$disk = Storage::disk('upload');
$disk = \Storage::disk('upload');
/** @var Attachment $attachment */
foreach ($attachments as $attachment) {
$fileName = $attachment->fileName();
$encryptedContent = $disk->get($fileName);
if (null === $encryptedContent) {
app('log')->error(sprintf('No content for attachment #%d under filename "%s"', $attachment->id, $fileName));
continue;
}
try {
$decryptedContent = Crypt::decrypt($encryptedContent); // verified
$decryptedContent = \Crypt::decrypt($encryptedContent); // verified
} catch (DecryptException $e) {
app('log')->error(sprintf('Could not decrypt data of attachment #%d: %s', $attachment->id, $e->getMessage()));
$decryptedContent = $encryptedContent;
@@ -69,6 +66,7 @@ class ScanAttachments extends Command
$tempFileName = tempnam(sys_get_temp_dir(), 'FireflyIII');
if (false === $tempFileName) {
app('log')->error(sprintf('Could not create temporary file for attachment #%d', $attachment->id));
exit(1);
}
file_put_contents($tempFileName, $decryptedContent);

View File

@@ -34,15 +34,12 @@ class SetLatestVersion extends Command
{
use ShowsFriendlyMessages;
protected $description = 'Set latest version in DB.';
protected $signature = 'firefly-iii:set-latest-version {--james-is-cool}';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{

View File

@@ -28,14 +28,11 @@ use Illuminate\Console\Command;
/**
* Class UpgradeFireflyInstructions.
*
*/
class UpgradeFireflyInstructions extends Command
{
use GeneratesInstallationId;
protected $description = 'Instructions in case of upgrade trouble.';
protected $signature = 'firefly:instructions {task}';
@@ -62,9 +59,11 @@ class UpgradeFireflyInstructions extends Command
private function updateInstructions(): void
{
$version = (string)config('firefly.version');
/** @var array $config */
$config = config('upgrade.text.upgrade');
$text = '';
/** @var string $compare */
foreach (array_keys($config) as $compare) {
// if string starts with:
@@ -97,8 +96,6 @@ class UpgradeFireflyInstructions extends Command
/**
* The logo takes up 8 lines of code. So 8 colors can be used.
*
* @return void
*/
private function showLogo(): void
{
@@ -146,27 +143,23 @@ class UpgradeFireflyInstructions extends Command
/**
* Show a nice box.
*
* @param string $text
*/
private function boxed(string $text): void
{
$parts = explode("\n", wordwrap($text));
foreach ($parts as $string) {
$this->line('| ' . sprintf('%-77s', $string) . '|');
$this->line('| '.sprintf('%-77s', $string).'|');
}
}
/**
* Show a nice info box.
*
* @param string $text
*/
private function boxedInfo(string $text): void
{
$parts = explode("\n", wordwrap($text));
foreach ($parts as $string) {
$this->info('| ' . sprintf('%-77s', $string) . '|');
$this->info('| '.sprintf('%-77s', $string).'|');
}
}
@@ -176,9 +169,11 @@ class UpgradeFireflyInstructions extends Command
private function installInstructions(): void
{
$version = (string)config('firefly.version');
/** @var array $config */
$config = config('upgrade.text.install');
$text = '';
/** @var string $compare */
foreach (array_keys($config) as $compare) {
// if string starts with:

View File

@@ -28,7 +28,6 @@ use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
use League\Flysystem\FilesystemException;
use Storage;
/**
* Class VerifySecurityAlerts
@@ -37,7 +36,6 @@ class VerifySecurityAlerts extends Command
{
use ShowsFriendlyMessages;
protected $description = 'Verify security alerts';
protected $signature = 'firefly-iii:verify-security-alerts';
@@ -45,7 +43,6 @@ class VerifySecurityAlerts extends Command
/**
* Execute the console command.
*
* @return int
* @throws FilesystemException
*/
public function handle(): int
@@ -54,7 +51,7 @@ class VerifySecurityAlerts extends Command
// check for security advisories.
$version = config('firefly.version');
$disk = Storage::disk('resources');
$disk = \Storage::disk('resources');
// Next line is ignored because it's a Laravel Facade.
if (!$disk->has('alerts.json')) { // @phpstan-ignore-line
app('log')->debug('No alerts.json file present.');
@@ -100,12 +97,10 @@ class VerifySecurityAlerts extends Command
}
app('log')->debug(sprintf('No security alerts for version %s', $version));
$this->friendlyPositive(sprintf('No security alerts for version %s', $version));
return 0;
}
/**
* @return void
*/
private function removeOldAdvisory(): void
{
try {
@@ -116,11 +111,6 @@ class VerifySecurityAlerts extends Command
}
}
/**
* @param array $array
*
* @return void
*/
private function saveSecurityAdvisory(array $array): void
{
try {