Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linter: declare section absence #1233

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ src/cmd/stubs/phpstorm-stubs/
y.output
.idea
vendor
src/tests/golden/testdata/quickfix/*.fix
26 changes: 22 additions & 4 deletions src/linter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func (b *blockWalker) EnterNode(n ir.Node) (res bool) {
for _, st := range s.Stmts {
b.addStatement(st)
}
b.handleDeclareSection(s)
case *ir.StmtList:
for _, st := range s.Stmts {
b.addStatement(st)
Expand Down Expand Up @@ -551,6 +552,23 @@ func (b *blockWalker) handleAndCheckGlobalStmt(s *ir.GlobalStmt) {
}
}

func (b *blockWalker) handleDeclareSection(root *ir.Root) {
isExist := false

for _, stmt := range root.Stmts {
_, ok := stmt.(*ir.DeclareStmt)
if ok {
isExist = true
break
}
}

if !isExist {
b.report(root, LevelWarning, "noDeclareSection", "Missed declare(strict_types = 1) directive")
b.r.addQuickFix("noDeclareSection", b.linter.quickfix.CreateDeclareStrictTypes(root))
}
}

func (b *blockWalker) handleFunction(fun *ir.FunctionStmt) bool {
if b.ignoreFunctionBodies {
return false
Expand Down Expand Up @@ -823,10 +841,10 @@ func (b *blockWalker) handleTry(s *ir.TryStmt) bool {
})
}

for nm, types := range varTypes {
for nm, typs := range varTypes {
var flags meta.VarFlags
flags.SetAlwaysDefined(defCounts[nm] == linksCount)
b.ctx.sc.AddVarName(nm, types, "all branches try catch", flags)
b.ctx.sc.AddVarName(nm, typs, "all branches try catch", flags)
}

if finallyCtx != nil {
Expand Down Expand Up @@ -1909,10 +1927,10 @@ func (b *blockWalker) handleSwitch(s *ir.SwitchStmt) bool {
})
}

for nm, types := range varTypes {
for nm, typs := range varTypes {
var flags meta.VarFlags
flags.SetAlwaysDefined(defCounts[nm] == linksCount)
b.ctx.sc.AddVarName(nm, types, "all cases", flags)
b.ctx.sc.AddVarName(nm, typs, "all cases", flags)
}

return false
Expand Down
8 changes: 8 additions & 0 deletions src/linter/quickfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func (g *QuickFixGenerator) NullForNotNullableProperty(prop *ir.PropertyStmt) qu
}
}

func (g *QuickFixGenerator) CreateDeclareStrictTypes(root *ir.Root) quickfix.TextEdit {
return quickfix.TextEdit{
StartPos: root.Position.StartPos,
EndPos: root.Position.StartPos,
Replacement: "declare(strict_types = 1);\n",
}
}

func (g *QuickFixGenerator) GetType(node ir.Node, isFunctionName, nodeText string, isNegative bool) quickfix.TextEdit {
pos := ir.GetPosition(node)

Expand Down
27 changes: 18 additions & 9 deletions src/linter/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ func addBuiltinCheckers(reg *CheckersRegistry) {
After: `$s = strip_tags($s, '<br>')`,
},

{
Name: "noDeclareSection",
Default: true,
Quickfix: true,
Comment: "Report declare(strict_types=1) has not been set.",
Before: ` `,
After: `declare(strict_types = 1);`,
},

{
Name: "emptyStmt",
Default: true,
Expand Down Expand Up @@ -1185,8 +1194,8 @@ func DiffReports(gitRepo string, diffArgs []string, changesList []git.Change, ch
}
}

old := reportListToMap(oldList)
new := reportListToMap(newList)
oldReportMap := reportListToMap(oldList)
newReportMap := reportListToMap(newList)
changes := gitChangesToMap(changesList)

var mu sync.Mutex
Expand All @@ -1196,7 +1205,7 @@ func DiffReports(gitRepo string, diffArgs []string, changesList []git.Change, ch

limitCh := make(chan struct{}, maxConcurrency)

for filename, list := range new {
for filename, list := range newReportMap {
wg.Add(1)
go func(filename string, list []*Report) {
limitCh <- struct{}{}
Expand All @@ -1212,7 +1221,7 @@ func DiffReports(gitRepo string, diffArgs []string, changesList []git.Change, ch
oldName = filename // full diff mode
}

reports, err := diffReportsList(gitRepo, ignoreCommits, diffArgs, filename, c, old[oldName], list)
reports, err := diffReportsList(gitRepo, ignoreCommits, diffArgs, filename, c, oldReportMap[oldName], list)
if err != nil {
mu.Lock()
resErr = err
Expand Down Expand Up @@ -1266,8 +1275,8 @@ func diffReportsList(gitRepo string, ignoreCommits map[string]struct{}, diffArgs
}
}

old, oldMaxLine := reportListToPerLineMap(oldList)
new, newMaxLine := reportListToPerLineMap(newList)
oldReportMap, oldMaxLine := reportListToPerLineMap(oldList)
newReportMap, newMaxLine := reportListToPerLineMap(newList)

var maxLine = oldMaxLine
if newMaxLine > maxLine {
Expand All @@ -1284,17 +1293,17 @@ func diffReportsList(gitRepo string, ignoreCommits map[string]struct{}, diffArgs
// just deletion
if ok && ch.new.HaveRange && ch.new.Range == 0 {
oldLine = ch.old.To
newLine-- // cancel the increment of newLine, because code was deleted, no new lines added
newLine-- // cancel the increment of newLine, because code was deleted, no newReportMap lines added
continue
}

res = maybeAppendReports(res, new, old, newLine, oldLine, blame, ignoreCommits)
res = maybeAppendReports(res, newReportMap, oldReportMap, newLine, oldLine, blame, ignoreCommits)

if ok {
oldLine = 0 // all changes and additions must be checked
for j := newLine + 1; j <= ch.new.To; j++ {
newLine = j
res = maybeAppendReports(res, new, old, newLine, oldLine, blame, ignoreCommits)
res = maybeAppendReports(res, newReportMap, oldReportMap, newLine, oldLine, blame, ignoreCommits)
}
oldLine = ch.old.To
}
Expand Down
4 changes: 4 additions & 0 deletions src/tests/golden/testdata/quickfix/noDeclareSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function test(): string {
return "test";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
declare(strict_types = 1);
function test(): string {
return "test";
}
Loading