Skip to content

Commit

Permalink
fixed bug with array and callable types
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Aug 14, 2024
1 parent 5f7baa0 commit 7dab6f0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
30 changes: 25 additions & 5 deletions src/linter/quickfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package linter
import (
"bytes"
"fmt"

"github.com/VKCOM/noverify/src/ir"
"github.com/VKCOM/noverify/src/quickfix"
"github.com/VKCOM/noverify/src/workspace"
"github.com/VKCOM/php-parser/pkg/position"
)

type QuickFixGenerator struct {
Expand Down Expand Up @@ -47,11 +47,31 @@ func (g *QuickFixGenerator) NullForNotNullableProperty(prop *ir.PropertyStmt) qu
}
}

func (g *QuickFixGenerator) NullableType(param *ir.Name) quickfix.TextEdit {
func (g *QuickFixGenerator) NullableType(param ir.Node) quickfix.TextEdit {
var pos *position.Position
var value string

switch v := param.(type) {
case *ir.Name:
pos = &position.Position{
StartPos: v.Position.StartPos,
EndPos: v.Position.EndPos,
}
value = v.Value
case *ir.Identifier:
pos = &position.Position{
StartPos: v.Position.StartPos,
EndPos: v.Position.EndPos,
}
value = v.Value
default:
panic("unexpected type")
}

return quickfix.TextEdit{
StartPos: param.Position.StartPos,
EndPos: param.Position.EndPos,
Replacement: "?" + param.Value,
StartPos: pos.StartPos,
EndPos: pos.EndPos,
Replacement: "?" + value,
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/linter/root_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,14 @@ func (r *rootChecker) checkFuncParam(p *ir.Parameter) {
}

func (r *rootChecker) CheckParamNullability(p *ir.Parameter) {
param, paramOk := p.VariableType.(*ir.Name)
var paramType ir.Node
paramType, paramOk := p.VariableType.(*ir.Name)
if !paramOk {
return
paramIdentifier, paramIdentifierOk := p.VariableType.(*ir.Identifier)
if !paramIdentifierOk {
return
}
paramType = paramIdentifier
}

defValue, defValueOk := p.DefaultValue.(*ir.ConstFetchExpr)
Expand All @@ -623,8 +628,8 @@ func (r *rootChecker) CheckParamNullability(p *ir.Parameter) {
return
}

r.walker.Report(param, LevelWarning, "nullableType", "parameter with null default value should be explicitly nullable")
r.walker.addQuickFix("nullableType", r.quickfix.NullableType(param))
r.walker.Report(paramType, LevelWarning, "nullableType", "parameter with null default value should be explicitly nullable")
r.walker.addQuickFix("nullableType", r.quickfix.NullableType(paramType))
}

func (r *rootChecker) CheckTypeHintFunctionParam(p *ir.Parameter) {
Expand Down

0 comments on commit 7dab6f0

Please sign in to comment.