Various code cleanup.

This commit is contained in:
James Cole
2017-02-25 05:57:01 +01:00
parent 444439fdab
commit 8c6972d12d
35 changed files with 133 additions and 171 deletions

View File

@@ -14,7 +14,6 @@ declare(strict_types = 1);
namespace FireflyIII\Export\Collector;
use Carbon\Carbon;
use Crypt;
use DB;
use FireflyIII\Models\Transaction;
use Illuminate\Database\Query\JoinClause;

View File

@@ -13,7 +13,6 @@ declare(strict_types = 1);
namespace FireflyIII\Export\Entry;
use Crypt;
use Steam;
/**

View File

@@ -80,6 +80,26 @@ class Support
return $result;
}
/**
* @return Collection
*/
public function getTopExpenses(): Collection
{
$transactions = $this->getExpenses()->sortBy('transaction_amount');
return $transactions;
}
/**
* @return Collection
*/
public function getTopIncome(): Collection
{
$transactions = $this->getIncome()->sortByDesc('transaction_amount');
return $transactions;
}
/**
* @param Collection $collection
* @param int $sortFlag
@@ -162,26 +182,6 @@ class Support
return $return;
}
/**
* @return Collection
*/
public function getTopExpenses(): Collection
{
$transactions = $this->getExpenses()->sortBy('transaction_amount');
return $transactions;
}
/**
* @return Collection
*/
public function getTopIncome(): Collection
{
$transactions = $this->getIncome()->sortByDesc('transaction_amount');
return $transactions;
}
/**
* @param Collection $collection
*

View File

@@ -44,6 +44,8 @@ interface AttachmentHelperInterface
/**
* @param Model $model
*
* @param array $files
*
* @return bool
*/
public function saveAttachmentsForModel(Model $model, array $files = null): bool;

View File

@@ -430,6 +430,20 @@ class JournalCollector implements JournalCollectorInterface
return $this;
}
/**
* @param Collection $tags
*
* @return JournalCollectorInterface
*/
public function setTags(Collection $tags): JournalCollectorInterface
{
$this->joinTagTables();
$tagIds = $tags->pluck('id')->toArray();
$this->query->whereIn('tag_transaction_journal.tag_id', $tagIds);
return $this;
}
/**
* @param array $types
*
@@ -712,18 +726,4 @@ class JournalCollector implements JournalCollectorInterface
$this->query->leftJoin('tag_transaction_journal', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
}
}
/**
* @param Collection $tags
*
* @return JournalCollectorInterface
*/
public function setTags(Collection $tags): JournalCollectorInterface
{
$this->joinTagTables();
$tagIds = $tags->pluck('id')->toArray();
$this->query->whereIn('tag_transaction_journal.tag_id', $tagIds);
return $this;
}
}

View File

@@ -122,12 +122,15 @@ class RegisterController extends Controller
*/
protected function create(array $data)
{
return User::create(
/** @var User $user */
$user = User::create(
[
'email' => $data['email'],
'password' => bcrypt($data['password']),
]
);
return $user;
}
/**

View File

@@ -79,9 +79,11 @@ class TwoFactorController extends Controller
/**
* @param TokenFormRequest $request
* @SuppressWarnings(PHPMD.UnusedFormalParameter) // it's unused but the class does some validation.
* @param CookieJar $cookieJar
*
* @return mixed
* @SuppressWarnings(PHPMD.UnusedFormalParameter) // it's unused but the class does some validation.
*
*/
public function postIndex(TokenFormRequest $request, CookieJar $cookieJar)
{

View File

@@ -105,7 +105,8 @@ class BudgetController extends Controller
}
/**
* @param Budget $budget
* @param Request $request
* @param Budget $budget
*
* @return View
*/
@@ -214,6 +215,8 @@ class BudgetController extends Controller
}
/**
* @param BudgetIncomeRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postUpdateIncome(BudgetIncomeRequest $request)

View File

@@ -203,6 +203,7 @@ class BudgetReportController extends Controller
* Returns the budget limits belonging to the given budget and valid on the given day.
*
* @param Collection $budgetLimits
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
@@ -268,21 +269,4 @@ class BudgetReportController extends Controller
return $grouped;
}
/**
* @param Collection $set
*
* @return array
*/
private function groupByOpposingAccount(Collection $set): array
{
$grouped = [];
/** @var Transaction $transaction */
foreach ($set as $transaction) {
$accountId = $transaction->opposing_account_id;
$grouped[$accountId] = $grouped[$accountId] ?? '0';
$grouped[$accountId] = bcadd($transaction->transaction_amount, $grouped[$accountId]);
}
return $grouped;
}
}

View File

@@ -20,7 +20,7 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
use Navigation;
@@ -50,13 +50,13 @@ class CategoryController extends Controller
/**
* Show an overview for a category for all time, per month/week/year.
*
* @param CRI $repository
* @param AccountRepositoryInterface $accountRepository
* @param Category $category
* @param CategoryRepositoryInterface $repository
* @param AccountRepositoryInterface $accountRepository
* @param Category $category
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function all(CRI $repository, AccountRepositoryInterface $accountRepository, Category $category)
public function all(CategoryRepositoryInterface $repository, AccountRepositoryInterface $accountRepository, Category $category)
{
$cache = new CacheProperties;
$cache->addProperty('chart.category.all');
@@ -106,12 +106,12 @@ class CategoryController extends Controller
}
/**
* @param CRI $repository
* @param Category $category
* @param CategoryRepositoryInterface $repository
* @param Category $category
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function currentPeriod(CRI $repository, Category $category)
public function currentPeriod(CategoryRepositoryInterface $repository, Category $category)
{
$start = clone session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
@@ -121,12 +121,12 @@ class CategoryController extends Controller
}
/**
* @param CRI $repository
* @param AccountRepositoryInterface $accountRepository
* @param CategoryRepositoryInterface $repository
* @param AccountRepositoryInterface $accountRepository
*
* @return \Illuminate\Http\JsonResponse
*/
public function frontpage(CRI $repository, AccountRepositoryInterface $accountRepository)
public function frontpage(CategoryRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
@@ -161,15 +161,15 @@ class CategoryController extends Controller
}
/**
* @param CRI $repository
* @param Category $category
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param CategoryRepositoryInterface $repository
* @param Category $category
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return \Illuminate\Http\JsonResponse|mixed
*/
public function reportPeriod(CRI $repository, Category $category, Collection $accounts, Carbon $start, Carbon $end)
public function reportPeriod(CategoryRepositoryInterface $repository, Category $category, Collection $accounts, Carbon $start, Carbon $end)
{
$cache = new CacheProperties;
$cache->addProperty($start);
@@ -210,14 +210,14 @@ class CategoryController extends Controller
}
/**
* @param CRI $repository
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param CategoryRepositoryInterface $repository
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return \Illuminate\Http\JsonResponse|mixed
*/
public function reportPeriodNoCategory(CRI $repository, Collection $accounts, Carbon $start, Carbon $end)
public function reportPeriodNoCategory(CategoryRepositoryInterface $repository, Collection $accounts, Carbon $start, Carbon $end)
{
$cache = new CacheProperties;
$cache->addProperty($start);
@@ -257,14 +257,14 @@ class CategoryController extends Controller
}
/**
* @param CRI $repository
* @param CategoryRepositoryInterface $repository
* @param Category $category
*
* @param $date
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function specificPeriod(CRI $repository, Category $category, $date)
public function specificPeriod(CategoryRepositoryInterface $repository, Category $category, $date)
{
$carbon = new Carbon($date);
$range = Preferences::get('viewRange', '1M')->data;
@@ -277,14 +277,14 @@ class CategoryController extends Controller
/**
* @param CRI $repository
* @param Category $category
* @param Carbon $start
* @param Carbon $end
* @param CategoryRepositoryInterface $repository
* @param Category $category
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
private function makePeriodChart(CRI $repository, Category $category, Carbon $start, Carbon $end)
private function makePeriodChart(CategoryRepositoryInterface $repository, Category $category, Carbon $start, Carbon $end)
{
$cache = new CacheProperties;
$cache->addProperty($start);

View File

@@ -50,6 +50,8 @@ class CurrencyController extends Controller
}
/**
* @param Request $request
*
* @return View
*/
public function create(Request $request)
@@ -90,6 +92,7 @@ class CurrencyController extends Controller
/**
* @param Request $request
* @param CurrencyRepositoryInterface $repository
* @param TransactionCurrency $currency
*
@@ -115,6 +118,7 @@ class CurrencyController extends Controller
}
/**
* @param Request $request
* @param CurrencyRepositoryInterface $repository
* @param TransactionCurrency $currency
*

View File

@@ -55,9 +55,10 @@ class ExportController extends Controller
}
/**
* @param EJRI $repository
* @param ExportJob $job
*
* @return \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Routing\ResponseFactory
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
* @throws FireflyException
*/
public function download(ExportJobRepositoryInterface $repository, ExportJob $job)

View File

@@ -27,7 +27,9 @@ class JavascriptController extends Controller
{
/**
* @param Request $request
*
* @return $this
*/
public function variables(Request $request)
{

View File

@@ -329,7 +329,9 @@ class JsonController extends Controller
}
/**
* @param JournalRepositoryInterface $repository
*
* @return \Illuminate\Http\JsonResponse
*/
public function transactionTypes(JournalRepositoryInterface $repository)
{

View File

@@ -56,7 +56,7 @@ class SearchController extends Controller
$limit = 20;
// ui stuff:
$subTitle = '';
$subTitle = '';
// query stuff
$query = null;

View File

@@ -219,7 +219,7 @@ class TagController extends Controller
}
}
return view('tags.index', compact('title', 'mainTitleIcon', 'types', 'collection','count'));
return view('tags.index', compact('title', 'mainTitleIcon', 'types', 'collection', 'count'));
}
/**

View File

@@ -14,11 +14,11 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Middleware;
use Closure;
use Cookie;
use Illuminate\Support\Facades\Auth;
use Preferences;
use Session;
use Log;
use Cookie;
/**
* Class RedirectIfTwoFactorAuthenticated
*

View File

@@ -35,6 +35,7 @@ class Sandstorm
* @param string|null $guard
*
* @return mixed
* @throws FireflyException
*/
public function handle(Request $request, Closure $next, $guard = null)
{
@@ -59,30 +60,34 @@ class Sandstorm
// and any other differences there may be between these users.
if ($count === 1 && strlen($userId) > 0) {
// login as first user user.
$user = User::first();
$user = User::first();
Auth::guard($guard)->login($user);
View::share('SANDSTORM_ANON', false);
return $next($request);
}
if ($count === 1 && strlen($userId) === 0) {
// login but indicate anonymous
$user = User::first();
$user = User::first();
Auth::guard($guard)->login($user);
View::share('SANDSTORM_ANON', true);
return $next($request);
}
if ($count === 0 && strlen($userId) > 0) {
// create new user.
$email = $userId . '@firefly';
$user = User::create(
/** @var User $user */
$user = User::create(
[
'email' => $email,
'password' => str_random(16),
]
);
Auth::guard($guard)->login($user);
return $next($request);
}
@@ -91,30 +96,7 @@ class Sandstorm
}
if ($count > 1) {
die('Cannot happen.');
}
exit;
if (strlen($userId) > 0) {
// find user?
$email = $userId . '@firefly';
$user = User::whereEmail($email)->first();
if (is_null($user)) {
$user = User::create(
[
'email' => $email,
'password' => str_random(16),
]
);
}
// login user:
Auth::guard($guard)->login($user);
} else {
echo 'user id no length, guest?';
exit;
throw new FireflyException('Your Firefly III installation has more than one user, which is weird.');
}
}

View File

@@ -13,7 +13,7 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Mapper;
use FireflyIII\Models\TransactionCurrency as TC;
use FireflyIII\Models\TransactionCurrency;
/**
* Class TransactionCurrencies
@@ -28,7 +28,7 @@ class TransactionCurrencies implements MapperInterface
*/
public function getMap(): array
{
$currencies = TC::get();
$currencies = TransactionCurrency::get();
$list = [];
foreach ($currencies as $currency) {
$list[$currency->id] = $currency->name . ' (' . $currency->code . ')';

View File

@@ -16,7 +16,6 @@ namespace FireflyIII\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class LimitRepetition
@@ -42,26 +41,6 @@ class LimitRepetition extends Model
protected $dates = ['created_at', 'updated_at', 'startdate', 'enddate'];
protected $hidden = ['amount_encrypted'];
/**
* @param $value
*
* @return mixed
*/
public static function routeBinder($value)
{
if (auth()->check()) {
$object = self::where('limit_repetitions.id', $value)
->leftJoin('budget_limits', 'budget_limits.id', '=', 'limit_repetitions.budget_limit_id')
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
->where('budgets.user_id', auth()->user()->id)
->first(['limit_repetitions.*']);
if ($object) {
return $object;
}
}
throw new NotFoundHttpException;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/

View File

@@ -108,6 +108,19 @@ class PiggyBank extends Model
return $value;
}
public function getSuggestedMonthlyAmount()
{
if ($this->targetdate && $this->currentRelevantRep()->currentamount < $this->targetamount) {
$thisMonth = Carbon::now()->month;
$targetMonth = $this->targetdate->month;
$remainingAmount = $this->targetamount - $this->currentRelevantRep()->currentamount;
return $thisMonth < $targetMonth ? $remainingAmount / ($targetMonth - $thisMonth) : $remainingAmount;
}
return 0;
}
/**
*
* @param Carbon $date
@@ -129,17 +142,6 @@ class PiggyBank extends Model
}
public function getSuggestedMonthlyAmount()
{
if ($this->targetdate && $this->currentRelevantRep()->currentamount < $this->targetamount) {
$thisMonth = Carbon::now()->month;
$targetMonth = $this->targetdate->month;
$remainingAmount = $this->targetamount - $this->currentRelevantRep()->currentamount;
return $thisMonth < $targetMonth ? $remainingAmount / ($targetMonth - $thisMonth) : $remainingAmount ;
}
return 0;
}
/**
* Get all of the piggy bank's notes.
*/

View File

@@ -457,7 +457,8 @@ class AccountRepository implements AccountRepositoryInterface
$name = $data['name'];
$opposing = $this->storeOpposingAccount($name);
$transactionType = TransactionType::whereType(TransactionType::OPENING_BALANCE)->first();
$journal = TransactionJournal::create(
/** @var TransactionJournal $journal */
$journal = TransactionJournal::create(
[
'user_id' => $this->user->id,
'transaction_type_id' => $transactionType->id,

View File

@@ -525,8 +525,7 @@ class BillRepository implements BillRepositoryInterface
*/
public function store(array $data): Bill
{
/** @var Bill $bill */
$bill = Bill::create(
[
'name' => $data['name'],

View File

@@ -193,6 +193,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
*/
public function store(array $data): TransactionCurrency
{
/** @var TransactionCurrency $currency */
$currency = TransactionCurrency::create(
[
'name' => $data['name'],

View File

@@ -40,6 +40,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
*/
public function createEvent(PiggyBank $piggyBank, string $amount): PiggyBankEvent
{
/** @var PiggyBankEvent $event */
$event = PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]);
return $event;
@@ -173,7 +174,8 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
public function store(array $data): PiggyBank
{
$data['order'] = $this->getMaxOrder() + 1;
$piggyBank = PiggyBank::create($data);
/** @var PiggyBank $piggyBank */
$piggyBank = PiggyBank::create($data);
$this->updateNote($piggyBank, $data['note']);

View File

@@ -41,7 +41,7 @@ class PiggyBank extends Twig_Extension
$functions[] = new Twig_SimpleFunction(
'suggestedMonthlyAmount', function (PB $piggyBank) {
return $piggyBank->getSuggestedMonthlyAmount();
return $piggyBank->getSuggestedMonthlyAmount();
}
);

View File

@@ -14,7 +14,6 @@ declare(strict_types = 1);
namespace FireflyIII\Support\Twig;
use Amount;
use Crypt;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction as TransactionModel;
use FireflyIII\Models\TransactionCurrency;

View File

@@ -79,7 +79,7 @@ function showDownload() {
function showError(text) {
"use strict";
$('#export-error').show();
$('#export-error>p').text(text);
$('#export-error').find('p').text(text);
}
function callExport() {

View File

@@ -24,7 +24,7 @@ $(function () {
$('.addMoney').on('click', addMoney);
$('.removeMoney').on('click', removeMoney);
$('#sortable-piggy tbody').sortable(
$('#sortable-piggy').find('tbody').sortable(
{
helper: fixHelper,
stop: stopSorting,

View File

@@ -29,10 +29,10 @@ $(function () {
{
locale: {
format: 'YYYY-MM-DD',
firstDay: 1,
firstDay: 1
},
minDate: minDate,
drops: 'up',
drops: 'up'
}
);

View File

@@ -300,7 +300,7 @@ function testRuleTriggers() {
}
// Show the modal dialog
$("#testTriggerModal").modal();
modal.modal();
}).fail(function () {
alert('Cannot get transactions for given triggers.');
});

View File

@@ -25,7 +25,7 @@ $(function () {
{
helper: fixHelper,
stop: sortStop,
cursor: "move",
cursor: "move"
}
);

View File

@@ -35,8 +35,7 @@ $(document).ready(function () {
function updateDescription() {
$.getJSON('json/transaction-journals/' + what).done(function (data) {
$('input[name="description"]').typeahead('destroy');
$('input[name="description"]').typeahead({source: data});
$('input[name="description"]').typeahead('destroy').typeahead({source: data});
});
}

View File

@@ -10,9 +10,7 @@
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{

View File

@@ -3,8 +3,7 @@
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{