Skip to content

Commit

Permalink
Fix a lot of phpstan things
Browse files Browse the repository at this point in the history
  • Loading branch information
JC5 committed Nov 26, 2023
1 parent a6c355c commit 68f01d9
Show file tree
Hide file tree
Showing 53 changed files with 214 additions and 120 deletions.
2 changes: 1 addition & 1 deletion app/Api/V2/Controllers/Chart/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function dashboard(DateRequest $request): JsonResponse
'native_id' => (string)$default->id,
'native_code' => $default->code,
'native_symbol' => $default->symbol,
'native_decimal_places' => (int)$default->decimal_places,
'native_decimal_places' => $default->decimal_places,
'start' => $start->toAtomString(),
'end' => $end->toAtomString(),
'period' => '1D',
Expand Down
6 changes: 3 additions & 3 deletions app/Api/V2/Controllers/Chart/BalanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public function balance(BalanceChartRequest $request): JsonResponse
'currency_symbol' => $default->symbol,
'currency_code' => $default->code,
'currency_name' => $default->name,
'currency_decimal_places' => (int)$default->decimal_places,
'currency_decimal_places' => $default->decimal_places,
'native_id' => (string)$defaultCurrencyId,
'native_symbol' => $default->symbol,
'native_code' => $default->code,
'native_name' => $default->name,
'native_decimal_places' => (int)$default->decimal_places,
'native_decimal_places' => $default->decimal_places,
];


Expand All @@ -131,7 +131,7 @@ public function balance(BalanceChartRequest $request): JsonResponse
'native_id' => (string)$default->id,
'native_code' => $default->code,
'native_symbol' => $default->symbol,
'native_decimal_places' => (int)$default->decimal_places,
'native_decimal_places' => $default->decimal_places,
];

// set the array (in monetary info) with spent/earned in this $period, if it does not exist.
Expand Down
2 changes: 1 addition & 1 deletion app/Api/V2/Controllers/Chart/BudgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private function processExpenses(int $budgetId, array $array, Carbon $start, Car
'native_code' => $this->currency->code,
'native_name' => $this->currency->name,
'native_symbol' => $this->currency->symbol,
'native_decimal_places' => (int)$this->currency->decimal_places,
'native_decimal_places' => $this->currency->decimal_places,
'start' => $start->toAtomString(),
'end' => $end->toAtomString(),
'spent' => '0',
Expand Down
4 changes: 2 additions & 2 deletions app/Api/V2/Controllers/Chart/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ public function dashboard(DateRequest $request): JsonResponse
'currency_code' => $currency->code,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => (int)$currency->decimal_places,
'currency_decimal_places' => $currency->decimal_places,
'native_id' => (string)$default->id,
'native_code' => $default->code,
'native_name' => $default->name,
'native_symbol' => $default->symbol,
'native_decimal_places' => (int)$default->decimal_places,
'native_decimal_places' => $default->decimal_places,
'period' => null,
'start' => $start->toAtomString(),
'end' => $end->toAtomString(),
Expand Down
4 changes: 2 additions & 2 deletions app/Api/V2/Controllers/Summary/BasicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,15 @@ private function getLeftToSpendInfo(Carbon $start, Carbon $end): array
'currency_id' => (string)$default->id,
'currency_code' => $default->code,
'currency_symbol' => $default->symbol,
'currency_decimal_places' => (int)$default->decimal_places,
'currency_decimal_places' => $default->decimal_places,
];
$nativePerDay = [
'key' => 'left-per-day-to-spend-in-native',
'value' => '0',
'currency_id' => (string)$default->id,
'currency_code' => $default->code,
'currency_symbol' => $default->symbol,
'currency_decimal_places' => (int)$default->decimal_places,
'currency_decimal_places' => $default->decimal_places,
];

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/Correction/DeleteEmptyJournals.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function deleteUnevenJournals(): void
{
$set = Transaction::whereNull('deleted_at')
->groupBy('transactions.transaction_journal_id')
->get([DB::raw('COUNT(transactions.transaction_journal_id) as the_count'), 'transaction_journal_id']);
->get([DB::raw('COUNT(transactions.transaction_journal_id) as the_count'), 'transaction_journal_id']); // @phpstan-ignore-line
$total = 0;
/** @var Transaction $row */
foreach ($set as $row) {
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/Correction/FixGroupAccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function handle(): int
{
$groups = [];
$res = TransactionJournal::groupBy('transaction_group_id')
->get(['transaction_group_id', DB::raw('COUNT(transaction_group_id) as the_count')]);
->get(['transaction_group_id', DB::raw('COUNT(transaction_group_id) as the_count')]);// @phpstan-ignore-line
/** @var TransactionJournal $journal */
foreach ($res as $journal) {
if ((int)$journal->the_count > 1) {
Expand Down
20 changes: 16 additions & 4 deletions app/Console/Commands/Export/ExportData.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ExportData extends Command

protected $description = 'Command to export data from Firefly III.';

protected $signature = 'firefly-iii:export-data
protected $signature = 'firefly-iii:export-data
{--user=1 : The user ID that the export should run for.}
{--token= : The user\'s access token.}
{--start= : First transaction to export. Defaults to your very first transaction. Only applies to transaction export.}
Expand Down Expand Up @@ -190,14 +190,23 @@ private function getDateParameter(string $field): Carbon
{
$date = today(config('app.timezone'))->subYear();
$error = false;
if (null !== $this->option($field)) {

if (!in_array($field, ['start', 'end'], true)) {
throw new FireflyException(sprintf('Invalid field "%s" given, can only be "start" or "end".', $field));
}

if (is_string($this->option($field))) {
try {
$date = Carbon::createFromFormat('!Y-m-d', $this->option($field));
} catch (InvalidArgumentException $e) {
app('log')->error($e->getMessage());
$this->friendlyError(sprintf('%s date "%s" must be formatted YYYY-MM-DD. Field will be ignored.', $field, $this->option('start')));
$error = true;
}
if (false === $date) {
$this->friendlyError(sprintf('%s date "%s" must be formatted YYYY-MM-DD.', $field, $this->option('start')));
throw new FireflyException(sprintf('%s date "%s" must be formatted YYYY-MM-DD.', $field, $this->option('start')));
}
}
if (null === $this->option($field)) {
app('log')->info(sprintf('No date given in field "%s"', $field));
Expand All @@ -208,12 +217,15 @@ private function getDateParameter(string $field): Carbon
$journal = $this->journalRepository->firstNull();
$date = null === $journal ? today(config('app.timezone'))->subYear() : $journal->date;
$date->startOfDay();
return $date;
}

if (true === $error && 'end' === $field) {
// field can only be 'end' at this point, so no need to include it in the check.
if (true === $error) {
$date = today(config('app.timezone'));
$date->endOfDay();
return $date;
}

if ('end' === $field) {
$date->endOfDay();
}
Expand Down
1 change: 1 addition & 0 deletions app/Console/Commands/System/CreateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function handle(): int

// only continue when no error.
// with PDO, try to list DB's (
/** @var array $stmt */
$stmt = $pdo->query('SHOW DATABASES;');
// slightly more complex but less error-prone.
foreach ($stmt as $row) {
Expand Down
20 changes: 13 additions & 7 deletions app/Console/Commands/System/ForceDecimalSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,14 @@ private function correctAccountAmounts(TransactionCurrency $currency, array $fie
}
/** @var Account $account */
foreach ($result as $account) {
/** @var string $field */
foreach ($fields as $field) {
$value = $account->$field;
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
$pow = 10** (int)$currency->decimal_places;
$pow = 10** $currency->decimal_places;
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->friendlyInfo(sprintf('Account #%d has %s with value "%s", this has been corrected to "%s".', $account->id, $field, $value, $correct));
Account::find($account->id)->update([$field => $correct]);
Expand All @@ -286,6 +287,7 @@ private function correctGeneric(TransactionCurrency $currency, string $table): v
/** @var Builder $query */
$query = $class::where('transaction_currency_id', $currency->id)->where(
static function (Builder $q) use ($fields, $currency, $operator, $cast, $regularExpression) {
/** @var string $field */
foreach ($fields as $field) {
$q->orWhere(
DB::raw(sprintf('CAST(%s AS %s)', $field, $cast)), // @phpstan-ignore-line
Expand All @@ -304,13 +306,14 @@ static function (Builder $q) use ($fields, $currency, $operator, $cast, $regular
}
/** @var Model $item */
foreach ($result as $item) {
/** @var string $field */
foreach ($fields as $field) {
$value = $item->$field;
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
$pow = 10** (int)$currency->decimal_places;
$pow = 10** $currency->decimal_places;
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->friendlyWarning(sprintf('%s #%d has %s with value "%s", this has been corrected to "%s".', $table, $item->id, $field, $value, $correct));
$class::find($item->id)->update([$field => $correct]);
Expand Down Expand Up @@ -356,13 +359,14 @@ private function correctPiggyEventAmounts(TransactionCurrency $currency, array $
}
/** @var PiggyBankEvent $item */
foreach ($result as $item) {
/** @var string $field */
foreach ($fields as $field) {
$value = $item->$field;
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
$pow = 10** (int)$currency->decimal_places;
$pow = 10** $currency->decimal_places;
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->friendlyWarning(
sprintf('Piggy bank event #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct)
Expand Down Expand Up @@ -410,13 +414,14 @@ private function correctPiggyRepetitionAmounts(TransactionCurrency $currency, ar
}
/** @var PiggyBankRepetition $item */
foreach ($result as $item) {
/** @var string $field */
foreach ($fields as $field) {
$value = $item->$field;
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
$pow = 10** (int)$currency->decimal_places;
$pow = 10** $currency->decimal_places;
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->friendlyWarning(
sprintf('Piggy bank repetition #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct)
Expand Down Expand Up @@ -463,13 +468,14 @@ private function correctPiggyAmounts(TransactionCurrency $currency, array $field
}
/** @var PiggyBank $item */
foreach ($result as $item) {
/** @var string $field */
foreach ($fields as $field) {
$value = $item->$field;
if (null === $value) {
continue;
}
// fix $field by rounding it down correctly.
$pow = 10** (int)$currency->decimal_places;
$pow = 10** $currency->decimal_places;
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->friendlyWarning(sprintf('Piggy bank #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
PiggyBank::find($item->id)->update([$field => $correct]);
Expand Down Expand Up @@ -506,7 +512,7 @@ private function correctTransactionAmounts(TransactionCurrency $currency): void
continue;
}
// fix $field by rounding it down correctly.
$pow = (float)10** (int)$currency->decimal_places;
$pow = (float)10** $currency->decimal_places;
$correct = bcdiv((string)round((float)$value * $pow), (string)$pow, 12);
$this->friendlyWarning(sprintf('Transaction #%d has amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct));
Transaction::find($item->id)->update(['amount' => $correct]);
Expand All @@ -533,7 +539,7 @@ private function correctTransactionAmounts(TransactionCurrency $currency): void
continue;
}
// fix $field by rounding it down correctly.
$pow = (float)10** (int)$currency->decimal_places;
$pow = (float)10** $currency->decimal_places;
$correct = bcdiv((string)round((float)$value * $pow), (string)$pow, 12);
$this->friendlyWarning(
sprintf('Transaction #%d has foreign amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct)
Expand Down
10 changes: 6 additions & 4 deletions app/Console/Commands/System/ScanAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ public function handle(): int
$decryptedContent = $encryptedContent;
}
$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);
$md5 = md5_file($tempFileName);
$mime = mime_content_type($tempFileName);
$attachment->md5 = $md5;
$attachment->mime = $mime;
$attachment->md5 = (string) md5_file($tempFileName);
$attachment->mime = (string) mime_content_type($tempFileName);
$attachment->save();
$this->friendlyInfo(sprintf('Fixed attachment #%d', $attachment->id));
}
Expand Down
18 changes: 10 additions & 8 deletions app/Console/Commands/System/UpgradeFireflyInstructions.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ public function handle(): int
*/
private function updateInstructions(): void
{
/** @var string $version */
$version = config('firefly.version');
$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:
if (str_starts_with($version, $compare)) {
$text = $config[$compare];
$text = (string) $config[$compare];
}
}

Expand All @@ -78,7 +79,7 @@ private function updateInstructions(): void
$this->showLine();

$this->boxed('');
if (null === $text || '' === $text) {
if ('' === $text) {
$this->boxed(sprintf('Thank you for updating to Firefly III, v%s', $version));
$this->boxedInfo('There are no extra upgrade instructions.');
$this->boxed('Firefly III should be ready for use.');
Expand Down Expand Up @@ -174,22 +175,23 @@ private function boxedInfo(string $text): void
*/
private function installInstructions(): void
{
/** @var string $version */
$version = config('firefly.version');
$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:
if (str_starts_with($version, $compare)) {
$text = $config[$compare];
$text = (string) $config[$compare];
}
}
$this->newLine();
$this->showLogo();
$this->newLine();
$this->showLine();
$this->boxed('');
if (null === $text || '' === $text) {
if ('' === $text) {
$this->boxed(sprintf('Thank you for installing Firefly III, v%s!', $version));
$this->boxedInfo('There are no extra installation instructions.');
$this->boxed('Firefly III should be ready for use.');
Expand Down
5 changes: 5 additions & 0 deletions app/Console/Commands/Tools/ApplyRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

/**
* Class ApplyRules
Expand Down Expand Up @@ -296,6 +297,10 @@ private function verifyInputDates(): void
if (null !== $endString && '' !== $endString) {
$inputEnd = Carbon::createFromFormat('Y-m-d', $endString);
}
if(false === $inputEnd || false === $inputStart) {
Log::error('Could not parse start or end date in verifyInputDate().');
return;
}

if ($inputStart > $inputEnd) {
[$inputEnd, $inputStart] = [$inputStart, $inputEnd];
Expand Down
4 changes: 2 additions & 2 deletions app/Console/Commands/Upgrade/FixPostgresSequences.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ public function handle(): int
continue;
}

if ($nextId->nextval < $highestId->max) {
if ($nextId->nextval < $highestId->max) { // @phpstan-ignore-line
DB::select(sprintf('SELECT setval(\'%s_id_seq\', %d)', $tableToCheck, $highestId->max));
$highestId = DB::table($tableToCheck)->select(DB::raw('MAX(id)'))->first();
$nextId = DB::table($tableToCheck)->select(DB::raw(sprintf('nextval(\'%s_id_seq\')', $tableToCheck)))->first();
if ($nextId->nextval > $highestId->max) {
if ($nextId->nextval > $highestId->max) { // @phpstan-ignore-line
$this->friendlyInfo(sprintf('Table "%s" autoincrement corrected.', $tableToCheck));
}
if ($nextId->nextval <= $highestId->max) {
Expand Down
Loading

0 comments on commit 68f01d9

Please sign in to comment.