Skip to content

Commit a1ae269

Browse files
committed
chore: modernize Go code
1 parent 6ad34b1 commit a1ae269

17 files changed

+47
-67
lines changed

caddy/admin_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ func TestAutoScaleWorkerThreads(t *testing.T) {
112112
amountOfThreads := len(getDebugState(t, tester).ThreadDebugStates)
113113

114114
// try to spawn the additional threads by spamming the server
115-
for tries := 0; tries < maxTries; tries++ {
115+
for range maxTries {
116116
wg.Add(requestsPerTry)
117-
for i := 0; i < requestsPerTry; i++ {
117+
for range requestsPerTry {
118118
go func() {
119119
tester.AssertGetResponse(endpoint, http.StatusOK, "slept for 2 ms and worked for 1000 iterations")
120120
wg.Done()
@@ -164,9 +164,9 @@ func TestAutoScaleRegularThreadsOnAutomaticThreadLimit(t *testing.T) {
164164
amountOfThreads := len(getDebugState(t, tester).ThreadDebugStates)
165165

166166
// try to spawn the additional threads by spamming the server
167-
for tries := 0; tries < maxTries; tries++ {
167+
for range maxTries {
168168
wg.Add(requestsPerTry)
169-
for i := 0; i < requestsPerTry; i++ {
169+
for range requestsPerTry {
170170
go func() {
171171
tester.AssertGetResponse(endpoint, http.StatusOK, "slept for 2 ms and worked for 1000 iterations")
172172
wg.Done()

caddy/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
267267
return nil
268268
}
269269

270-
func parseGlobalOption(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
270+
func parseGlobalOption(d *caddyfile.Dispenser, _ any) (any, error) {
271271
app := &FrankenPHPApp{}
272272
if err := app.UnmarshalCaddyfile(d); err != nil {
273273
return nil, err

caddy/caddy_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestPHP(t *testing.T) {
4242
}
4343
`, "caddyfile")
4444

45-
for i := 0; i < 100; i++ {
45+
for i := range 100 {
4646
wg.Add(1)
4747

4848
go func(i int) {
@@ -105,7 +105,7 @@ func TestWorker(t *testing.T) {
105105
}
106106
`, "caddyfile")
107107

108-
for i := 0; i < 100; i++ {
108+
for i := range 100 {
109109
wg.Add(1)
110110

111111
go func(i int) {
@@ -157,7 +157,7 @@ func TestGlobalAndModuleWorker(t *testing.T) {
157157
}
158158
`, "caddyfile")
159159

160-
for i := 0; i < 10; i++ {
160+
for i := range 10 {
161161
wg.Add(1)
162162

163163
go func(i int) {
@@ -209,7 +209,7 @@ func TestNamedModuleWorkers(t *testing.T) {
209209
}
210210
`, "caddyfile")
211211

212-
for i := 0; i < 10; i++ {
212+
for i := range 10 {
213213
wg.Add(1)
214214

215215
go func(i int) {
@@ -456,7 +456,7 @@ func TestMetrics(t *testing.T) {
456456
`, "caddyfile")
457457

458458
// Make some requests
459-
for i := 0; i < 10; i++ {
459+
for i := range 10 {
460460
wg.Add(1)
461461
go func(i int) {
462462
tester.AssertGetResponse(fmt.Sprintf("http://localhost:"+testPort+"/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
@@ -529,7 +529,7 @@ func TestWorkerMetrics(t *testing.T) {
529529
workerName, _ := fastabs.FastAbs("../testdata/index.php")
530530

531531
// Make some requests
532-
for i := 0; i < 10; i++ {
532+
for i := range 10 {
533533
wg.Add(1)
534534
go func(i int) {
535535
tester.AssertGetResponse(fmt.Sprintf("http://localhost:"+testPort+"/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
@@ -621,7 +621,7 @@ func TestNamedWorkerMetrics(t *testing.T) {
621621
`, "caddyfile")
622622

623623
// Make some requests
624-
for i := 0; i < 10; i++ {
624+
for i := range 10 {
625625
wg.Add(1)
626626
go func(i int) {
627627
tester.AssertGetResponse(fmt.Sprintf("http://localhost:"+testPort+"/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
@@ -712,7 +712,7 @@ func TestAutoWorkerConfig(t *testing.T) {
712712
workerName, _ := fastabs.FastAbs("../testdata/index.php")
713713

714714
// Make some requests
715-
for i := 0; i < 10; i++ {
715+
for i := range 10 {
716716
wg.Add(1)
717717
go func(i int) {
718718
tester.AssertGetResponse(fmt.Sprintf("http://localhost:"+testPort+"/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
@@ -872,7 +872,7 @@ func TestPHPIniBlockConfiguration(t *testing.T) {
872872

873873
func testSingleIniConfiguration(tester *caddytest.Tester, key string, value string) {
874874
// test twice to ensure the ini setting is not lost
875-
for i := 0; i < 2; i++ {
875+
for range 2 {
876876
tester.AssertGetResponse(
877877
"http://localhost:"+testPort+"/ini.php?key="+key,
878878
http.StatusOK,
@@ -940,7 +940,7 @@ func TestMaxWaitTime(t *testing.T) {
940940
wg := sync.WaitGroup{}
941941
success := atomic.Bool{}
942942
wg.Add(10)
943-
for i := 0; i < 10; i++ {
943+
for range 10 {
944944
go func() {
945945
statusCode := getStatusCode("http://localhost:"+testPort+"/sleep.php?sleep=10", t)
946946
if statusCode == http.StatusGatewayTimeout {
@@ -987,7 +987,7 @@ func TestMaxWaitTimeWorker(t *testing.T) {
987987
wg := sync.WaitGroup{}
988988
success := atomic.Bool{}
989989
wg.Add(10)
990-
for i := 0; i < 10; i++ {
990+
for range 10 {
991991
go func() {
992992
statusCode := getStatusCode("http://localhost:"+testPort+"/sleep.php?sleep=10000&iteration=1", t)
993993
if statusCode == http.StatusGatewayTimeout {
@@ -1074,7 +1074,7 @@ func TestMultiWorkersMetrics(t *testing.T) {
10741074
`, "caddyfile")
10751075

10761076
// Make some requests
1077-
for i := 0; i < 10; i++ {
1077+
for i := range 10 {
10781078
wg.Add(1)
10791079
go func(i int) {
10801080
tester.AssertGetResponse(fmt.Sprintf("http://localhost:"+testPort+"/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
@@ -1180,7 +1180,7 @@ func TestDisabledMetrics(t *testing.T) {
11801180
`, "caddyfile")
11811181

11821182
// Make some requests
1183-
for i := 0; i < 10; i++ {
1183+
for i := range 10 {
11841184
wg.Add(1)
11851185
go func(i int) {
11861186
tester.AssertGetResponse(fmt.Sprintf("http://localhost:"+testPort+"/index.php?i=%d", i), http.StatusOK, fmt.Sprintf("I am by birth a Genevese (%d)", i))
@@ -1285,7 +1285,7 @@ func TestWorkerRestart(t *testing.T) {
12851285
))
12861286

12871287
// Make some requests
1288-
for i := 0; i < 10; i++ {
1288+
for i := range 10 {
12891289
wg.Add(1)
12901290
go func(i int) {
12911291
tester.AssertGetResponse(fmt.Sprintf("http://localhost:"+testPort+"/worker-restart.php?i=%d", i), http.StatusOK, fmt.Sprintf("Counter (%d)", i))

caddy/module.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log/slog"
77
"net/http"
88
"path/filepath"
9+
"slices"
910
"strconv"
1011
"strings"
1112

@@ -450,12 +451,8 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
450451
tryPolicy = ""
451452
}
452453

453-
for _, tf := range tryFiles {
454-
if tf == dirIndex {
455-
dirRedir = true
456-
457-
break
458-
}
454+
if slices.Contains(tryFiles, dirIndex) {
455+
dirRedir = true
459456
}
460457
}
461458

context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type frankenPHPContext struct {
2929

3030
responseWriter http.ResponseWriter
3131

32-
done chan interface{}
32+
done chan any
3333
startedAt time.Time
3434
}
3535

@@ -42,7 +42,7 @@ func fromContext(ctx context.Context) (fctx *frankenPHPContext, ok bool) {
4242
// NewRequestWithContext creates a new FrankenPHP request context.
4343
func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Request, error) {
4444
fc := &frankenPHPContext{
45-
done: make(chan interface{}),
45+
done: make(chan any),
4646
startedAt: time.Now(),
4747
request: r,
4848
}

frankenphp_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ func testFlush(t *testing.T, opts *testOptions) {
522522
if j == 0 {
523523
assert.Equal(t, []byte("He"), buf)
524524
} else {
525-
assert.Equal(t, []byte(fmt.Sprintf("llo %d", i)), buf)
525+
assert.Equal(t, fmt.Appendf(nil, "llo %d", i), buf)
526526
}
527527

528528
j++
@@ -663,7 +663,7 @@ func TestEnvIsNotResetInWorkerMode(t *testing.T) {
663663
// reproduction of https://github.com/php/frankenphp/issues/1061
664664
func TestModificationsToEnvPersistAcrossRequests(t *testing.T) {
665665
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
666-
for j := 0; j < 3; j++ {
666+
for range 3 {
667667
result, _ := testGet("http://example.com/env/overwrite-env.php", handler, t)
668668
assert.Equal(t, "custom_value", result, "a var directly added to $_ENV should persist")
669669
}
@@ -780,8 +780,7 @@ func BenchmarkHelloWorld(b *testing.B) {
780780
req := httptest.NewRequest("GET", "http://example.com/index.php", nil)
781781
w := httptest.NewRecorder()
782782

783-
b.ResetTimer()
784-
for i := 0; i < b.N; i++ {
783+
for b.Loop() {
785784
handler(w, req)
786785
}
787786
}
@@ -846,8 +845,7 @@ func BenchmarkEcho(b *testing.B) {
846845
req := httptest.NewRequest("POST", "http://example.com/echo.php", r)
847846
w := httptest.NewRecorder()
848847

849-
b.ResetTimer()
850-
for i := 0; i < b.N; i++ {
848+
for b.Loop() {
851849
r.Reset(body)
852850
handler(w, req)
853851
}
@@ -917,8 +915,7 @@ func BenchmarkServerSuperGlobal(b *testing.B) {
917915
req := httptest.NewRequest("GET", "http://example.com/server-variable.php", nil)
918916
w := httptest.NewRecorder()
919917

920-
b.ResetTimer()
921-
for i := 0; i < b.N; i++ {
918+
for b.Loop() {
922919
handler(w, req)
923920
}
924921
}

internal/extgen/classparser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ func (cp *classParser) parseMethodSignature(className, signature string) (*phpCl
306306

307307
var params []phpParameter
308308
if paramsStr != "" {
309-
paramParts := strings.Split(paramsStr, ",")
310-
for _, part := range paramParts {
309+
paramParts := strings.SplitSeq(paramsStr, ",")
310+
for part := range paramParts {
311311
param, err := cp.parseMethodParameter(strings.TrimSpace(part))
312312
if err != nil {
313313
return nil, fmt.Errorf("parsing parameter '%s': %w", part, err)

internal/extgen/docs_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ func BenchmarkDocumentationGenerator_GenerateMarkdown(b *testing.B) {
378378
generator: generator,
379379
}
380380

381-
b.ResetTimer()
382-
for i := 0; i < b.N; i++ {
381+
for b.Loop() {
383382
_, err := docGen.generateMarkdown()
384383
assert.NoError(b, err)
385384
}

internal/extgen/funcparser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ func (fp *FuncParser) parseSignature(signature string) (*phpFunction, error) {
128128

129129
var params []phpParameter
130130
if paramsStr != "" {
131-
paramParts := strings.Split(paramsStr, ",")
132-
for _, part := range paramParts {
131+
paramParts := strings.SplitSeq(paramsStr, ",")
132+
for part := range paramParts {
133133
param, err := fp.parseParameter(strings.TrimSpace(part))
134134
if err != nil {
135135
return nil, fmt.Errorf("parsing parameter '%s': %w", part, err)

internal/extgen/srcanalyzer_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,7 @@ func internalTwo() {
371371

372372
analyzer := &SourceAnalyzer{}
373373

374-
b.ResetTimer()
375-
for i := 0; i < b.N; i++ {
374+
for b.Loop() {
376375
_, _, err := analyzer.analyze(filename)
377376
require.NoError(b, err)
378377
}
@@ -391,8 +390,7 @@ func test3() {
391390

392391
analyzer := &SourceAnalyzer{}
393392

394-
b.ResetTimer()
395-
for i := 0; i < b.N; i++ {
393+
for b.Loop() {
396394
analyzer.extractInternalFunctions(content)
397395
}
398396
}

0 commit comments

Comments
 (0)