Skip to content

Firefly III: a personal finances manager

Fix #1452.

James Cole4c04415e80a6
diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php
index 96dbe6d289..8857bd8ea0 100644
--- a/app/Http/Controllers/TagController.php
+++ b/app/Http/Controllers/TagController.php
@@ -145,27 +145,18 @@ class TagController extends Controller
public function index(TagRepositoryInterface $repository)
{
// start with oldest tag
- $oldestTag = $repository->oldestTag();
- /** @var Carbon $start */
- $start = new Carbon;
- if (null !== $oldestTag) {
- /** @var Carbon $start */
- $start = $oldestTag->date; // @codeCoverageIgnore
- }
- if (null === $oldestTag) {
- /** @var Carbon $start */
- $start = clone session('first');
- }
-
- $now = new Carbon;
+ $oldestTagDate = null === $repository->oldestTag() ? clone session('first') : $repository->oldestTag()->date;
+ $newestTagDate = null === $repository->newestTag() ? new Carbon : $repository->newestTag()->date;
+ $oldestTagDate->startOfYear();
+ $newestTagDate->endOfYear();
$clouds = [];
$clouds['no-date'] = $repository->tagCloud(null);
- while ($now > $start) {
- $year = $now->year;
+ while ($newestTagDate > $oldestTagDate) {
+ $year = $newestTagDate->year;
$clouds[$year] = $repository->tagCloud($year);
- $now->subYear();
+ $newestTagDate->subYear();
}
$count = $repository->count();
diff --git a/app/Models/Tag.php b/app/Models/Tag.php
index cb7861d845..9a085b0325 100644
--- a/app/Models/Tag.php
+++ b/app/Models/Tag.php
@@ -32,9 +32,11 @@ use FireflyIII\Models\TransactionJournal;
/**
* Class Tag.
+ *
* @property Collection $transactionJournals
* @property string $tag
* @property int $id
+ * @property \Carbon\Carbon $date
*/
class Tag extends Model
{
diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php
index 0bd3980bf9..90c545cb58 100644
--- a/app/Repositories/Tag/TagRepository.php
+++ b/app/Repositories/Tag/TagRepository.php
@@ -336,7 +336,6 @@ class TagRepository implements TagRepositoryInterface
}
)
->groupBy(['tags.id', 'tags.tag']);
-
// add date range (or not):
if (null === $year) {
Log::debug('Get tags without a date.');
@@ -464,4 +463,13 @@ class TagRepository implements TagRepositoryInterface
{
return $this->user->tags()->find($tagId);
}
-}
+
+ /**
+ * Will return the newest tag (if known) or NULL.
+ *
+ * @return Tag|null
+ */
+ public function newestTag(): ?Tag
+ {
+ return $this->user->tags()->whereNotNull('date')->orderBy('date', 'DESC')->first();
+}}
diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php
index ab5c537575..e4e8b44ba5 100644
--- a/app/Repositories/Tag/TagRepositoryInterface.php
+++ b/app/Repositories/Tag/TagRepositoryInterface.php
@@ -116,10 +116,16 @@ interface TagRepositoryInterface
public function lastUseDate(Tag $tag): Carbon;
/**
+ * Will return the newest tag (if known) or NULL.
* @return Tag|null
*/
public function oldestTag(): ?Tag;
+ /**
+ * Will return the newest tag (if known) or NULL.
+ * @return Tag|null
+ */
+ public function newestTag(): ?Tag;
/**
* @param User $user
*/
diff --git a/tests/Feature/Controllers/TagControllerTest.php b/tests/Feature/Controllers/TagControllerTest.php
index 29fbf5046f..7efd4a623c 100644
--- a/tests/Feature/Controllers/TagControllerTest.php
+++ b/tests/Feature/Controllers/TagControllerTest.php
@@ -130,6 +130,8 @@ class TagControllerTest extends TestCase
$repository->shouldReceive('count')->andReturn(0);
$repository->shouldReceive('tagCloud')->andReturn([]);
$repository->shouldReceive('oldestTag')->andReturn(null)->once();
+ $repository->shouldReceive('newestTag')->andReturn(null)->once();
+
$this->be($this->user());
$response = $this->get(route('tags.index'));