-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (121 loc) · 3.4 KB
/
Copy pathmain.go
File metadata and controls
142 lines (121 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Command argtyper fills missing parameter types from the literal values passed
// into local method, constructor and function calls.
package main
import (
"fmt"
"os"
"github.com/rectorphp/argtyper/internal/aggregate"
"github.com/rectorphp/argtyper/internal/apply"
"github.com/rectorphp/argtyper/internal/collect"
"github.com/rectorphp/argtyper/internal/diff"
"github.com/rectorphp/argtyper/internal/finder"
"github.com/rectorphp/argtyper/internal/inherit"
"github.com/rectorphp/argtyper/internal/symbols"
)
const usage = `Usage: argtyper add-types [project-path] [--dry]
Find literal values passed into local method/function calls and add them as
parameter type declarations. Defaults to the current directory.
--dry Print the diff of the types that would be added, without writing.`
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, "Error: "+err.Error())
os.Exit(1)
}
}
func run(args []string) error {
if len(args) == 0 || args[0] != "add-types" {
fmt.Println(usage)
if len(args) == 0 {
return nil
}
return fmt.Errorf("unknown command %q", args[0])
}
dry := false
projectPath := "."
for _, arg := range args[1:] {
if arg == "--dry" {
dry = true
continue
}
projectPath = arg
}
files, err := finder.PHPFiles(projectPath)
if err != nil {
return err
}
fmt.Printf("Code dirs found in %q: %v\n\n", projectPath, finder.CodeDirectories(projectPath))
// gather project symbols (enums, constants) so argument values that
// reference them resolve to a type just like literals do
table := symbols.New()
for _, file := range files {
src, err := os.ReadFile(file)
if err != nil {
return err
}
table.CollectSource(src)
}
// build the inheritance table from the project and its vendor directory, so
// a method that overrides an ancestor (including one in vendor) is left alone
inheritance := inherit.New()
vendorFiles, err := finder.VendorPHPFiles(projectPath)
if err != nil {
return err
}
for _, file := range append(append([]string{}, files...), vendorFiles...) {
src, err := os.ReadFile(file)
if err != nil {
return err
}
inheritance.CollectSource(src)
}
// 1. collect literal argument types across the whole project
fmt.Println("1. Collecting argument types...")
var records []collect.Record
for _, file := range files {
src, err := os.ReadFile(file)
if err != nil {
return err
}
records = append(records, collect.FromSource(src, table)...)
}
fmt.Printf(" Found %d arg types\n\n", len(records))
types := aggregate.Resolve(records)
// 2. add the resolved types to parameter declarations
if dry {
fmt.Println("2. Types that would be added (dry run)...")
} else {
fmt.Println("2. Adding types to parameters...")
}
added := 0
for _, file := range files {
src, err := os.ReadFile(file)
if err != nil {
return err
}
output, count, changed := apply.Source(src, types, table, inheritance)
if !changed {
continue
}
if dry {
if patch, ok := diff.Lines(file, string(src), output); ok {
fmt.Print(patch)
}
added += count
continue
}
if err := os.WriteFile(file, []byte(output), 0o644); err != nil {
return err
}
added += count
}
if added == 0 {
fmt.Println(" No new types added. Is your code that good?")
return nil
}
if dry {
fmt.Printf("\n Dry run: %d types would be added\n", added)
return nil
}
fmt.Printf(" Finished! Added %d new types\n", added)
return nil
}