From 5be317d73c9613df5b9760cc6eedb77a96e94802 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 2 Nov 2016 07:04:14 +0100 Subject: [PATCH] sprintf ALL THE THINGS Signed-off-by: James Cole --- app/Console/Commands/Import.php | 2 +- app/Console/Commands/VerifyDatabase.php | 7 +++++-- app/Helpers/Help/Help.php | 9 ++++++--- app/Http/Controllers/HelpController.php | 2 +- app/Support/Amount.php | 12 ++++++------ app/Support/Twig/Journal.php | 21 ++++++++++----------- app/Support/Twig/Transaction.php | 12 ++++++------ app/Support/Twig/Translation.php | 2 +- 8 files changed, 36 insertions(+), 31 deletions(-) diff --git a/app/Console/Commands/Import.php b/app/Console/Commands/Import.php index 0459e1c75b..09a1f5bec9 100644 --- a/app/Console/Commands/Import.php +++ b/app/Console/Commands/Import.php @@ -66,7 +66,7 @@ class Import extends Command return; } - $this->line('Going to import job with key "' . $job->key . '" of type ' . $job->file_type); + $this->line(sprintf('Going to import job with key "%s" of type "%s"', $job->key, $job->file_type)); $monolog = Log::getMonolog(); $handler = new CommandHandler($this); diff --git a/app/Console/Commands/VerifyDatabase.php b/app/Console/Commands/VerifyDatabase.php index 8571d599af..6050902ecd 100644 --- a/app/Console/Commands/VerifyDatabase.php +++ b/app/Console/Commands/VerifyDatabase.php @@ -127,8 +127,11 @@ class VerifyDatabase extends Command /** @var stdClass $entry */ foreach ($set as $entry) { - $line = 'Notice: User #' . $entry->user_id . ' (' . $entry->email . ') has budget #' . $entry->id . ' ("' . Crypt::decrypt($entry->name) - . '") which has no budget limits.'; + + $line = sprintf( + 'Notice: User #%d (%s) has budget #%d ("%s") which has no budget limits.', + $entry->user_id, $entry->email, $entry->id, Crypt::decrypt($entry->name) + ); $this->line($line); } } diff --git a/app/Helpers/Help/Help.php b/app/Helpers/Help/Help.php index 4b19963bef..a4546e49af 100644 --- a/app/Helpers/Help/Help.php +++ b/app/Helpers/Help/Help.php @@ -35,7 +35,9 @@ class Help implements HelpInterface */ public function getFromCache(string $route, string $language): string { - return Cache::get('help.' . $route . '.' . $language); + $line = sprintf('help.%s.%s', $route, $language); + + return Cache::get($line); } /** @@ -93,7 +95,8 @@ class Help implements HelpInterface */ public function inCache(string $route, string $language):bool { - $result = Cache::has('help.' . $route . '.' . $language); + $line = sprintf('help.%s.%s', $route, $language); + $result = Cache::has($line); if ($result) { Log::debug(sprintf('Cache has this entry: %s', 'help.' . $route . '.' . $language)); } @@ -115,7 +118,7 @@ class Help implements HelpInterface */ public function putInCache(string $route, string $language, string $content) { - $key = 'help.' . $route . '.' . $language; + $key = sprintf('help.%s.%s', $route, $language); Log::debug(sprintf('Will store entry in cache: %s', $key)); Cache::put($key, $content, 10080); // a week. } diff --git a/app/Http/Controllers/HelpController.php b/app/Http/Controllers/HelpController.php index bd1f34c581..3096ac15eb 100644 --- a/app/Http/Controllers/HelpController.php +++ b/app/Http/Controllers/HelpController.php @@ -75,7 +75,7 @@ class HelpController extends Controller if (strlen($content) === 0) { $content = '

' . strval(trans('firefly.route_has_no_help')) . '

'; } - + $help->putInCache($route, $language, $content); return Response::json($content); diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 7937ae148a..c283289933 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -59,14 +59,14 @@ class Amount if ($coloured === true) { if ($amount > 0) { - return '' . $result . ''; + return sprintf('%s', $result); } else { if ($amount < 0) { - return '' . $result . ''; + return sprintf('%s', $result); } } - return '' . $result . ''; + return sprintf('%s', $result); } @@ -139,14 +139,14 @@ class Amount if ($coloured === true) { if ($amount > 0) { - return '' . $result . ''; + return sprintf('%s', $result); } else { if ($amount < 0) { - return '' . $result . ''; + return sprintf('%s', $result); } } - return '' . $result . ''; + return sprintf('%s', $result); } diff --git a/app/Support/Twig/Journal.php b/app/Support/Twig/Journal.php index 62f99d89a6..e98f0ee070 100644 --- a/app/Support/Twig/Journal.php +++ b/app/Support/Twig/Journal.php @@ -146,7 +146,7 @@ class Journal extends Twig_Extension $array[] = '(cash)'; continue; } - $array[] = '' . e($entry->name) . ''; + $array[] = sprintf('%1$s', e($entry->name), route('accounts.show', $entry->id)); } $array = array_unique($array); $result = join(', ', $array); @@ -221,7 +221,7 @@ class Journal extends Twig_Extension $array[] = '(cash)'; continue; } - $array[] = '' . e($entry->name) . ''; + $array[] = sprintf('%1$s', e($entry->name), route('accounts.show', $entry->id)); } $array = array_unique($array); $result = join(', ', $array); @@ -253,12 +253,12 @@ class Journal extends Twig_Extension $budgets = []; // get all budgets: foreach ($journal->budgets as $budget) { - $budgets[] = '' . e($budget->name) . ''; + $budgets[] = sprintf('%1$s', e($budget->name), route('accounts.show', $budget->id)); } // and more! foreach ($journal->transactions as $transaction) { foreach ($transaction->budgets as $budget) { - $budgets[] = '' . e($budget->name) . ''; + $budgets[] = sprintf('%1$s', e($budget->name), route('accounts.show', $budget->id)); } } $string = join(', ', array_unique($budgets)); @@ -288,7 +288,7 @@ class Journal extends Twig_Extension $categories = []; // get all categories for the journal itself (easy): foreach ($journal->categories as $category) { - $categories[] = '' . e($category->name) . ''; + $categories[] = sprintf('%1$s', e($category->name), route('accounts.show', $category->id)); } if (count($categories) === 0) { $set = Category::distinct()->leftJoin('category_transaction', 'categories.id', '=', 'category_transaction.category_id') @@ -299,8 +299,7 @@ class Journal extends Twig_Extension ->get(['categories.*']); /** @var Category $category */ foreach ($set as $category) { - $categories[] = '' . e($category->name) - . ''; + $categories[] = sprintf('%1$s', e($category->name), route('accounts.show', $category->id)); } } @@ -324,16 +323,16 @@ class Journal extends Twig_Extension switch (true) { case $journal->isWithdrawal(): - $txt = ''; + $txt = sprintf('', trans('firefly.withdrawal')); break; case $journal->isDeposit(): - $txt = ''; + $txt = sprintf('', trans('firefly.deposit')); break; case $journal->isTransfer(): - $txt = ''; + $txt = sprintf('', trans('firefly.transfer')); break; case $journal->isOpeningBalance(): - $txt = ''; + $txt = sprintf('', trans('firefly.openingBalance')); break; default: $txt = ''; diff --git a/app/Support/Twig/Transaction.php b/app/Support/Twig/Transaction.php index ceb5e7695b..ef8c97da73 100644 --- a/app/Support/Twig/Transaction.php +++ b/app/Support/Twig/Transaction.php @@ -210,7 +210,7 @@ class Transaction extends Twig_Extension return '(cash)'; } - return '' . e($name) . ''; + return sprintf('%1$s', e($name), route('accounts.show', [$id])); }, ['is_safe' => ['html']] ); @@ -276,7 +276,7 @@ class Transaction extends Twig_Extension return '(cash)'; } - return '' . e($name) . ''; + return sprintf('%1$s', e($name), route('accounts.show', [$id])); }, ['is_safe' => ['html']] ); @@ -294,16 +294,16 @@ class Transaction extends Twig_Extension switch ($transaction->transaction_type_type) { case TransactionType::WITHDRAWAL: - $txt = ''; + $txt = sprintf('' . trans('firefly.withdrawal')); break; case TransactionType::DEPOSIT: - $txt = ''; + $txt = sprintf('', trans('firefly.deposit')); break; case TransactionType::TRANSFER: - $txt = ''; + $txt = sprintf('', trans('firefly.transfer')); break; case TransactionType::OPENING_BALANCE: - $txt = ''; + $txt = sprintf('', trans('firefly.openingBalance')); break; default: $txt = ''; diff --git a/app/Support/Twig/Translation.php b/app/Support/Twig/Translation.php index f9401eee52..cb84f85c8e 100644 --- a/app/Support/Twig/Translation.php +++ b/app/Support/Twig/Translation.php @@ -35,7 +35,7 @@ class Translation extends Twig_Extension $filters[] = new Twig_SimpleFilter( '_', function ($name) { - return trans('firefly.' . $name); + return strval(trans(sprintf('firefly.%s', $name))); }, ['is_safe' => ['html']] );