-
Notifications
You must be signed in to change notification settings - Fork 58
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: added checks for functions #605
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
72aa8dd
linter: rec
b4de606
linter: added checks for functions
3e80ebc
Merge branch 'master' into StoreFQNinCurrentFunction
i582 15e9c14
linter: refactor
9d51f94
linter: refactor
3a7e339
linter: fixed crash of baseline-test on windows
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,11 +118,22 @@ func NewWalkerForReferencesSearcher(filename string, block BlockCheckerCreateFun | |
// InitFromParser initializes common fields that are needed for RootWalker work | ||
func (d *RootWalker) InitFromParser(contents []byte, parser *php7.Parser) { | ||
lines := bytes.Split(contents, []byte("\n")) | ||
|
||
trimedLines := make([]bool, len(lines)) | ||
for i := range lines { | ||
trimedLines[i] = bytes.HasSuffix(lines[i], []byte("\r")) | ||
lines[i] = bytes.TrimSuffix(lines[i], []byte("\r")) | ||
} | ||
|
||
linesPositions := make([]int, len(lines)) | ||
pos := 0 | ||
for idx, ln := range lines { | ||
linesPositions[idx] = pos | ||
pos += len(ln) + 1 | ||
|
||
if trimedLines[idx] { | ||
pos += 1 | ||
} | ||
} | ||
|
||
d.fileContents = contents | ||
|
@@ -446,10 +457,6 @@ func (d *RootWalker) report(n node.Node, lineNumber int, level int, checkName, m | |
startChar = 0 | ||
endChar = len(startLn) | ||
|
||
if strings.HasSuffix(string(startLn), "\r") { | ||
endChar = endChar - 1 | ||
} | ||
|
||
pos = position.Position{ | ||
StartLine: lineNumber, | ||
EndLine: lineNumber, | ||
|
@@ -529,8 +536,8 @@ func (d *RootWalker) reportHash(pos *position.Position, startLine []byte, checkN | |
// want such methods to be considered as a single "scope". | ||
scope := "file" | ||
switch { | ||
case d.ctx.st.CurrentClass != "" && d.ctx.st.CurrentFunction != "": | ||
scope = d.ctx.st.CurrentClass + "::" + d.ctx.st.CurrentFunction | ||
case d.ctx.st.CurrentClass != "" && d.ctx.st.CurrentMethod != "": | ||
scope = d.ctx.st.CurrentClass + "::" + d.ctx.st.CurrentMethod | ||
case d.ctx.st.CurrentFunction != "": | ||
scope = d.ctx.st.CurrentFunction | ||
} | ||
|
@@ -641,7 +648,7 @@ type handleFuncResult struct { | |
callsParentConstructor bool | ||
} | ||
|
||
func (d *RootWalker) handleFuncStmts(params []meta.FuncParam, uses, stmts []node.Node, sc *meta.Scope) handleFuncResult { | ||
func (d *RootWalker) handleFuncStmts(fun node.Node, params []meta.FuncParam, uses, stmts []node.Node, sc *meta.Scope, phpDoc *phpDocParseResult) handleFuncResult { | ||
b := &BlockWalker{ | ||
ctx: &blockContext{sc: sc}, | ||
r: d, | ||
|
@@ -698,7 +705,12 @@ func (d *RootWalker) handleFuncStmts(params []meta.FuncParam, uses, stmts []node | |
prematureExitFlags = cleanFlags | ||
} | ||
|
||
// if the function contains only one statement and this statement is throw. | ||
oneThrowStatementFunction := b.containsThrow && len(stmts) == 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not two? Ten? Why this special case only? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in
|
||
|
||
switch { | ||
case !b.containsReturnOrYield && !phpDoc.returnType.IsEmpty() && !phpDoc.returnType.Is("void") && len(stmts) != 0 && !oneThrowStatementFunction: | ||
d.Report(fun, LevelWarning, "mismatchingReturn", "Mismatching @return annotations") | ||
case b.bareReturn && b.returnsValue: | ||
b.returnTypes = b.returnTypes.AppendString("null") | ||
case b.returnTypes.IsEmpty() && b.returnsValue: | ||
|
@@ -1000,7 +1012,7 @@ func (d *RootWalker) enterClassMethod(meth *stmt.ClassMethod) bool { | |
if stmtList, ok := meth.Stmt.(*stmt.StmtList); ok { | ||
stmts = stmtList.Stmts | ||
} | ||
funcInfo := d.handleFuncStmts(params, nil, stmts, sc) | ||
funcInfo := d.handleFuncStmts(meth, params, nil, stmts, sc, &doc) | ||
actualReturnTypes := funcInfo.returnTypes | ||
exitFlags := funcInfo.prematureExitFlags | ||
if nm == `__construct` { | ||
|
@@ -1454,7 +1466,7 @@ func (d *RootWalker) enterFunction(fun *stmt.Function) bool { | |
|
||
params, minParamsCnt := d.parseFuncArgs(fun.Params, phpDocParamTypes, sc) | ||
|
||
funcInfo := d.handleFuncStmts(params, nil, fun.Stmts, sc) | ||
funcInfo := d.handleFuncStmts(fun, params, nil, fun.Stmts, sc, &doc) | ||
actualReturnTypes := funcInfo.returnTypes | ||
exitFlags := funcInfo.prematureExitFlags | ||
d.addScope(fun, sc) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exitFlags should already contain this information in the way you want?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to know if there is a
return
oryield
in the body of the function in order to accurately determine"mismatchingReturn"
. I'm not sure that exitFlags holds the information I need. Comment mentions other things// if function has exit / die / throw, then ExitFlags will be <> 0
.