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

@@ -24,17 +24,13 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\Upgrade;
use Crypt;
use DB;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Preference;
use Illuminate\Console\Command;
use Illuminate\Contracts\Encryption\DecryptException;
use JsonException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use stdClass;
/**
* Class DecryptDatabase
@@ -49,7 +45,6 @@ class DecryptDatabase extends Command
/**
* Execute the console command.
*
* @return int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
@@ -68,6 +63,7 @@ class DecryptDatabase extends Command
'transactions' => ['description'],
'journal_links' => ['comment'],
];
/**
* @var string $table
* @var array $fields
@@ -75,13 +71,11 @@ class DecryptDatabase extends Command
foreach ($tables as $table => $fields) {
$this->decryptTable($table, $fields);
}
return 0;
}
/**
* @param string $table
* @param array $fields
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
@@ -102,9 +96,6 @@ class DecryptDatabase extends Command
}
/**
* @param string $table
*
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
@@ -112,6 +103,7 @@ class DecryptDatabase extends Command
{
$configName = sprintf('is_decrypted_%s', $table);
$configVar = null;
try {
$configVar = app('fireflyconfig')->get($configName, false);
} catch (FireflyException $e) {
@@ -124,27 +116,19 @@ class DecryptDatabase extends Command
return false;
}
/**
* @param string $table
* @param string $field
*/
private function decryptField(string $table, string $field): void
{
$rows = DB::table($table)->get(['id', $field]);
/** @var stdClass $row */
$rows = \DB::table($table)->get(['id', $field]);
/** @var \stdClass $row */
foreach ($rows as $row) {
$this->decryptRow($table, $field, $row);
}
}
/**
* @param string $table
* @param string $field
* @param stdClass $row
*/
private function decryptRow(string $table, string $field, stdClass $row): void
private function decryptRow(string $table, string $field, \stdClass $row): void
{
$original = $row->$field;
$original = $row->{$field};
if (null === $original) {
return;
}
@@ -168,7 +152,7 @@ class DecryptDatabase extends Command
}
if ($value !== $original) {
DB::table($table)->where('id', $id)->update([$field => $value]);
\DB::table($table)->where('id', $id)->update([$field => $value]);
}
}
@@ -178,12 +162,13 @@ class DecryptDatabase extends Command
* @param mixed $value
*
* @return string
*
* @throws FireflyException
*/
private function tryDecrypt($value)
{
try {
$value = Crypt::decrypt($value);
$value = \Crypt::decrypt($value);
} catch (DecryptException $e) {
if ('The MAC is invalid.' === $e->getMessage()) {
throw new FireflyException($e->getMessage(), 0, $e);
@@ -193,16 +178,12 @@ class DecryptDatabase extends Command
return $value;
}
/**
* @param int $id
* @param string $value
*/
private function decryptPreferencesRow(int $id, string $value): void
{
// try to json_decrypt the value.
try {
$newValue = json_decode($value, true, 512, JSON_THROW_ON_ERROR) ?? $value;
} catch (JsonException $e) {
} catch (\JsonException $e) {
$message = sprintf('Could not JSON decode preference row #%d: %s. This does not have to be a problem.', $id, $e->getMessage());
$this->friendlyError($message);
app('log')->warning($message);
@@ -212,7 +193,7 @@ class DecryptDatabase extends Command
return;
}
/** @var Preference|null $object */
/** @var null|Preference $object */
$object = Preference::find($id);
if (null !== $object) {
$object->data = $newValue;