Skip to content

Commit d784ca6

Browse files
authored
Merge pull request #22 from JHWelch/add-some-types
Add `MetricBuilder` Static Types
2 parents 7ab4bff + 3fbeefb commit d784ca6

3 files changed

Lines changed: 68 additions & 20 deletions

File tree

src/Metric.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Metric extends Model
1212
/**
1313
* The attributes that aren't mass assignable.
1414
*
15-
* @var array
15+
* @var array<string>
1616
*/
1717
protected $guarded = [];
1818

@@ -37,6 +37,8 @@ protected function casts(): array
3737

3838
/**
3939
* Create a new Eloquent query builder for the model.
40+
*
41+
* @return MetricBuilder<static>
4042
*/
4143
public function newEloquentBuilder($query): MetricBuilder
4244
{

src/MetricBuilder.php

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@
55
use Carbon\CarbonInterface;
66
use Illuminate\Database\Eloquent\Builder;
77

8+
/**
9+
* @template TMetric of Metric
10+
*
11+
* @extends Builder<TMetric>
12+
*/
813
class MetricBuilder extends Builder
914
{
1015
/**
1116
* Get metrics for today.
17+
*
18+
* @return $this
1219
*/
13-
public function today(): self
20+
public function today(): static
1421
{
1522
return $this->onDate(today());
1623
}
1724

1825
/**
1926
* Get metrics for yesterday.
27+
*
28+
* @return $this
2029
*/
21-
public function yesterday(): self
30+
public function yesterday(): static
2231
{
2332
return $this->onDate(
2433
today()->subDay()
@@ -27,24 +36,30 @@ public function yesterday(): self
2736

2837
/**
2938
* Get metrics for this hour.
39+
*
40+
* @return $this
3041
*/
31-
public function thisHour(): self
42+
public function thisHour(): static
3243
{
3344
return $this->onDateTime(now());
3445
}
3546

3647
/**
3748
* Get metrics for last hour.
49+
*
50+
* @return $this
3851
*/
39-
public function lastHour(): self
52+
public function lastHour(): static
4053
{
4154
return $this->onDateTime(now()->subHour());
4255
}
4356

4457
/**
4558
* Get metrics for this week.
59+
*
60+
* @return $this
4661
*/
47-
public function thisWeek(): self
62+
public function thisWeek(): static
4863
{
4964
return $this->betweenDates(
5065
today()->startOfWeek(),
@@ -54,8 +69,10 @@ public function thisWeek(): self
5469

5570
/**
5671
* Get metrics for last week.
72+
*
73+
* @return $this
5774
*/
58-
public function lastWeek(): self
75+
public function lastWeek(): static
5976
{
6077
return $this->betweenDates(
6178
today()->subWeek()->startOfWeek(),
@@ -65,8 +82,10 @@ public function lastWeek(): self
6582

6683
/**
6784
* Get metrics for this month.
85+
*
86+
* @return $this
6887
*/
69-
public function thisMonth(): self
88+
public function thisMonth(): static
7089
{
7190
return $this->betweenDates(
7291
today()->startOfMonth(),
@@ -76,8 +95,10 @@ public function thisMonth(): self
7695

7796
/**
7897
* Get metrics for last month.
98+
*
99+
* @return $this
79100
*/
80-
public function lastMonth(): self
101+
public function lastMonth(): static
81102
{
82103
return $this->betweenDates(
83104
today()->subMonth()->startOfMonth(),
@@ -87,8 +108,10 @@ public function lastMonth(): self
87108

88109
/**
89110
* Get metrics for last month without overflow.
111+
*
112+
* @return $this
90113
*/
91-
public function lastMonthNoOverflow(): self
114+
public function lastMonthNoOverflow(): static
92115
{
93116
return $this->betweenDates(
94117
today()->subMonthNoOverflow()->startOfMonth(),
@@ -98,8 +121,10 @@ public function lastMonthNoOverflow(): self
98121

99122
/**
100123
* Get metrics for this quarter.
124+
*
125+
* @return $this
101126
*/
102-
public function thisQuarter(): self
127+
public function thisQuarter(): static
103128
{
104129
return $this->betweenDates(
105130
today()->startOfQuarter(),
@@ -109,8 +134,10 @@ public function thisQuarter(): self
109134

110135
/**
111136
* Get metrics for last quarter.
137+
*
138+
* @return $this
112139
*/
113-
public function lastQuarter(): self
140+
public function lastQuarter(): static
114141
{
115142
return $this->betweenDates(
116143
today()->subQuarter()->startOfQuarter(),
@@ -120,8 +147,10 @@ public function lastQuarter(): self
120147

121148
/**
122149
* Get metrics for last quarter without overflow.
150+
*
151+
* @return $this
123152
*/
124-
public function lastQuarterNoOverflow(): self
153+
public function lastQuarterNoOverflow(): static
125154
{
126155
return $this->betweenDates(
127156
today()->subQuarterNoOverflow()->startOfQuarter(),
@@ -131,8 +160,10 @@ public function lastQuarterNoOverflow(): self
131160

132161
/**
133162
* Get metrics for this year.
163+
*
164+
* @return $this
134165
*/
135-
public function thisYear(): self
166+
public function thisYear(): static
136167
{
137168
return $this->betweenDates(
138169
today()->startOfYear(),
@@ -142,8 +173,10 @@ public function thisYear(): self
142173

143174
/**
144175
* Get metrics for last year.
176+
*
177+
* @return $this
145178
*/
146-
public function lastYear(): self
179+
public function lastYear(): static
147180
{
148181
return $this->betweenDates(
149182
today()->subYear()->startOfYear(),
@@ -153,8 +186,10 @@ public function lastYear(): self
153186

154187
/**
155188
* Get metrics for last year without overflow.
189+
*
190+
* @return $this
156191
*/
157-
public function lastYearNoOverflow(): self
192+
public function lastYearNoOverflow(): static
158193
{
159194
return $this->betweenDates(
160195
today()->subYearNoOverflow()->startOfYear(),
@@ -164,8 +199,10 @@ public function lastYearNoOverflow(): self
164199

165200
/**
166201
* Get metrics between two dates.
202+
*
203+
* @return $this
167204
*/
168-
public function betweenDates(CarbonInterface $start, CarbonInterface $end): self
205+
public function betweenDates(CarbonInterface $start, CarbonInterface $end): static
169206
{
170207
return $this->whereRaw(
171208
'(year, month, day) >= (?, ?, ?) AND (year, month, day) <= (?, ?, ?)',
@@ -178,8 +215,10 @@ public function betweenDates(CarbonInterface $start, CarbonInterface $end): self
178215

179216
/**
180217
* Get metrics between two datetimes (including hours).
218+
*
219+
* @return $this
181220
*/
182-
public function betweenDateTimes(CarbonInterface $start, CarbonInterface $end): self
221+
public function betweenDateTimes(CarbonInterface $start, CarbonInterface $end): static
183222
{
184223
return $this->whereRaw(
185224
'(year, month, day, hour) >= (?, ?, ?, ?) AND (year, month, day, hour) <= (?, ?, ?, ?)',
@@ -192,8 +231,10 @@ public function betweenDateTimes(CarbonInterface $start, CarbonInterface $end):
192231

193232
/**
194233
* Get metrics on a specific date.
234+
*
235+
* @return $this
195236
*/
196-
public function onDate(CarbonInterface $date): self
237+
public function onDate(CarbonInterface $date): static
197238
{
198239
return $this->where(function (Builder $query) use ($date) {
199240
$query
@@ -205,8 +246,10 @@ public function onDate(CarbonInterface $date): self
205246

206247
/**
207248
* Get metrics on a specific date and hour.
249+
*
250+
* @return $this
208251
*/
209-
public function onDateTime(CarbonInterface $hour): self
252+
public function onDateTime(CarbonInterface $hour): static
210253
{
211254
return $this->onDate($hour)->where('hour', $hour->hour);
212255
}

src/MetricFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
66

7+
/**
8+
* @extends Factory<Metric>
9+
*/
710
class MetricFactory extends Factory
811
{
912
/**

0 commit comments

Comments
 (0)