Skip to content

Commit 34fbfd4

Browse files
alexandre-dauboisdunglas
authored andcommitted
chore(extgen): remove useless constructors
1 parent 8df4123 commit 34fbfd4

5 files changed

Lines changed: 20 additions & 46 deletions

File tree

internal/extgen/constparser.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,7 @@ var constRegex = regexp.MustCompile(`//\s*export_php:const$`)
1313
var classConstRegex = regexp.MustCompile(`//\s*export_php:classconst\s+(\w+)$`)
1414
var constDeclRegex = regexp.MustCompile(`const\s+(\w+)\s*=\s*(.+)`)
1515

16-
type ConstantParser struct {
17-
constRegex *regexp.Regexp
18-
classConstRegex *regexp.Regexp
19-
constDeclRegex *regexp.Regexp
20-
}
21-
22-
func NewConstantParserWithDefRegex() *ConstantParser {
23-
return &ConstantParser{
24-
constRegex: constRegex,
25-
classConstRegex: classConstRegex,
26-
constDeclRegex: constDeclRegex,
27-
}
28-
}
16+
type ConstantParser struct{}
2917

3018
func (cp *ConstantParser) parse(filename string) (constants []phpConstant, err error) {
3119
file, err := os.Open(filename)
@@ -51,15 +39,15 @@ func (cp *ConstantParser) parse(filename string) (constants []phpConstant, err e
5139
lineNumber++
5240
line := strings.TrimSpace(scanner.Text())
5341

54-
if cp.constRegex.MatchString(line) {
42+
if constRegex.MatchString(line) {
5543
expectConstDecl = true
5644
expectClassConstDecl = false
5745
currentClassName = ""
5846

5947
continue
6048
}
6149

62-
if matches := cp.classConstRegex.FindStringSubmatch(line); len(matches) == 2 {
50+
if matches := classConstRegex.FindStringSubmatch(line); len(matches) == 2 {
6351
expectClassConstDecl = true
6452
expectConstDecl = false
6553
currentClassName = matches[1]
@@ -68,7 +56,7 @@ func (cp *ConstantParser) parse(filename string) (constants []phpConstant, err e
6856
}
6957

7058
if (expectConstDecl || expectClassConstDecl) && strings.HasPrefix(line, "const ") {
71-
matches := cp.constDeclRegex.FindStringSubmatch(line)
59+
matches := constDeclRegex.FindStringSubmatch(line)
7260
if len(matches) == 3 {
7361
name := matches[1]
7462
value := strings.TrimSpace(matches[2])

internal/extgen/constparser_test.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const FalseConstant = false`,
134134
tmpFile := filepath.Join(tmpDir, tt.name+".go")
135135
require.NoError(t, os.WriteFile(tmpFile, []byte(tt.input), 0644))
136136

137-
parser := NewConstantParserWithDefRegex()
137+
parser := &ConstantParser{}
138138
constants, err := parser.parse(tmpFile)
139139
assert.NoError(t, err, "parse() error")
140140

@@ -200,7 +200,7 @@ const InvalidSyntax`,
200200
tmpFile := filepath.Join(tmpDir, tt.name+".go")
201201
require.NoError(t, os.WriteFile(tmpFile, []byte(tt.input), 0644))
202202

203-
parser := NewConstantParserWithDefRegex()
203+
parser := &ConstantParser{}
204204
_, err := parser.parse(tmpFile)
205205
require.NotNil(t, err)
206206

@@ -231,7 +231,7 @@ const ThirdIota = iota`
231231
fileName := filepath.Join(tmpDir, "test.go")
232232
require.NoError(t, os.WriteFile(fileName, []byte(input), 0644))
233233

234-
parser := NewConstantParserWithDefRegex()
234+
parser := &ConstantParser{}
235235
constants, err := parser.parse(fileName)
236236
assert.NoError(t, err, "parse() error")
237237

@@ -343,7 +343,7 @@ const INVALID = "missing class name"`,
343343
tmpFile := filepath.Join(tmpDir, tt.name+".go")
344344
require.NoError(t, os.WriteFile(tmpFile, []byte(tt.input), 0644))
345345

346-
parser := NewConstantParserWithDefRegex()
346+
parser := &ConstantParser{}
347347
constants, err := parser.parse(tmpFile)
348348
assert.NoError(t, err, "parse() error")
349349

@@ -379,8 +379,6 @@ const INVALID = "missing class name"`,
379379
}
380380

381381
func TestConstantParserRegexMatch(t *testing.T) {
382-
parser := NewConstantParserWithDefRegex()
383-
384382
testCases := []struct {
385383
line string
386384
expected bool
@@ -397,15 +395,13 @@ func TestConstantParserRegexMatch(t *testing.T) {
397395

398396
for _, tc := range testCases {
399397
t.Run(tc.line, func(t *testing.T) {
400-
matches := parser.constRegex.MatchString(tc.line)
398+
matches := constRegex.MatchString(tc.line)
401399
assert.Equal(t, tc.expected, matches, "Expected regex match for line '%s'", tc.line)
402400
})
403401
}
404402
}
405403

406404
func TestConstantParserClassConstRegex(t *testing.T) {
407-
parser := NewConstantParserWithDefRegex()
408-
409405
testCases := []struct {
410406
line string
411407
shouldMatch bool
@@ -425,7 +421,7 @@ func TestConstantParserClassConstRegex(t *testing.T) {
425421

426422
for _, tc := range testCases {
427423
t.Run(tc.line, func(t *testing.T) {
428-
matches := parser.classConstRegex.FindStringSubmatch(tc.line)
424+
matches := classConstRegex.FindStringSubmatch(tc.line)
429425

430426
if tc.shouldMatch {
431427
assert.Len(t, matches, 2, "Expected 2 matches for line '%s'", tc.line)
@@ -441,8 +437,6 @@ func TestConstantParserClassConstRegex(t *testing.T) {
441437
}
442438

443439
func TestConstantParserDeclRegex(t *testing.T) {
444-
parser := NewConstantParserWithDefRegex()
445-
446440
testCases := []struct {
447441
line string
448442
shouldMatch bool
@@ -462,7 +456,7 @@ func TestConstantParserDeclRegex(t *testing.T) {
462456

463457
for _, tc := range testCases {
464458
t.Run(tc.line, func(t *testing.T) {
465-
matches := parser.constDeclRegex.FindStringSubmatch(tc.line)
459+
matches := constDeclRegex.FindStringSubmatch(tc.line)
466460

467461
if tc.shouldMatch {
468462
assert.Len(t, matches, 3, "Expected 3 matches for line '%s'", tc.line)

internal/extgen/funcparser.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,7 @@ var phpFuncRegex = regexp.MustCompile(`//\s*export_php:function\s+([^{}\n]+)(?:\
1212
var signatureRegex = regexp.MustCompile(`(\w+)\s*\(([^)]*)\)\s*:\s*(\??[\w|]+)`)
1313
var typeNameRegex = regexp.MustCompile(`(\??[\w|]+)\s+\$?(\w+)`)
1414

15-
type FuncParser struct {
16-
phpFuncRegex *regexp.Regexp
17-
}
18-
19-
func NewFuncParserDefRegex() *FuncParser {
20-
return &FuncParser{
21-
phpFuncRegex: phpFuncRegex,
22-
}
23-
}
15+
type FuncParser struct{}
2416

2517
func (fp *FuncParser) parse(filename string) (functions []phpFunction, err error) {
2618
file, err := os.Open(filename)
@@ -43,7 +35,7 @@ func (fp *FuncParser) parse(filename string) (functions []phpFunction, err error
4335
lineNumber++
4436
line := strings.TrimSpace(scanner.Text())
4537

46-
if matches := fp.phpFuncRegex.FindStringSubmatch(line); matches != nil {
38+
if matches := phpFuncRegex.FindStringSubmatch(line); matches != nil {
4739
signature := strings.TrimSpace(matches[1])
4840
phpFunc, err := fp.parseSignature(signature)
4941
if err != nil {

internal/extgen/funcparser_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func someOtherGoName(num int64) int64 {
101101
fileName := filepath.Join(tmpDir, tt.name+".go")
102102
require.NoError(t, os.WriteFile(fileName, []byte(tt.input), 0644))
103103

104-
parser := NewFuncParserDefRegex()
104+
parser := &FuncParser{}
105105
functions, err := parser.parse(fileName)
106106
require.NoError(t, err)
107107
assert.Len(t, functions, tt.expected, "parse() got wrong number of functions")
@@ -188,7 +188,7 @@ func TestSignatureParsing(t *testing.T) {
188188
},
189189
}
190190

191-
parser := NewFuncParserDefRegex()
191+
parser := &FuncParser{}
192192
for _, tt := range tests {
193193
t.Run(tt.name, func(t *testing.T) {
194194
fn, err := parser.parseSignature(tt.signature)
@@ -270,7 +270,7 @@ func TestParameterParsing(t *testing.T) {
270270
},
271271
}
272272

273-
parser := NewFuncParserDefRegex()
273+
parser := &FuncParser{}
274274
for _, tt := range tests {
275275
t.Run(tt.name, func(t *testing.T) {
276276
param, err := parser.parseParameter(tt.paramStr)
@@ -386,7 +386,7 @@ func voidFunc(message *C.zend_string) {
386386
tmpFile := filepath.Join(tmpDir, tt.name+".go")
387387
require.NoError(t, os.WriteFile(tmpFile, []byte(tt.input), 0644))
388388

389-
parser := NewFuncParserDefRegex()
389+
parser := &FuncParser{}
390390
functions, err := parser.parse(tmpFile)
391391
require.NoError(t, err)
392392

@@ -476,7 +476,7 @@ func validFloat(value float64) float64 {
476476
fileName := filepath.Join(tmpDir, tt.name+".go")
477477
require.NoError(t, os.WriteFile(fileName, []byte(tt.input), 0644))
478478

479-
parser := NewFuncParserDefRegex()
479+
parser := &FuncParser{}
480480
functions, err := parser.parse(fileName)
481481
require.NoError(t, err)
482482

internal/extgen/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type SourceParser struct{}
44

55
// EXPERIMENTAL
66
func (p *SourceParser) ParseFunctions(filename string) ([]phpFunction, error) {
7-
functionParser := NewFuncParserDefRegex()
7+
functionParser := &FuncParser{}
88
return functionParser.parse(filename)
99
}
1010

@@ -16,7 +16,7 @@ func (p *SourceParser) ParseClasses(filename string) ([]phpClass, error) {
1616

1717
// EXPERIMENTAL
1818
func (p *SourceParser) ParseConstants(filename string) ([]phpConstant, error) {
19-
constantParser := NewConstantParserWithDefRegex()
19+
constantParser := &ConstantParser{}
2020
return constantParser.parse(filename)
2121
}
2222

0 commit comments

Comments
 (0)