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: added smartcasts for simple is_t functions #1100

Closed
wants to merge 3 commits into from
Closed
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
66 changes: 60 additions & 6 deletions src/linter/and_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,28 @@ func (a *andWalker) EnterNode(w ir.Node) (res bool) {
return true

case *ir.FunctionCallExpr:
// If the absence of a function or method is being
// checked, then nothing needs to be done.
if a.inNot {
return res
}

nm, ok := n.Function.(*ir.Name)
if !ok {
break
}

if len(n.Args) == 1 {
switch {
case nm.Value == "is_bool":
a.handleIsTypeSmartcast(n.Arg(0).Expr, "bool")
case nm.Value == "is_double" || nm.Value == "is_float" || nm.Value == "is_real":
a.handleIsTypeSmartcast(n.Arg(0).Expr, "float")
case nm.Value == "is_int" || nm.Value == "is_integer" || nm.Value == "is_long":
a.handleIsTypeSmartcast(n.Arg(0).Expr, "int")
case nm.Value == "is_object":
a.handleIsTypeSmartcast(n.Arg(0).Expr, "object")
case nm.Value == "is_string":
a.handleIsTypeSmartcast(n.Arg(0).Expr, "string")
case nm.Value == "is_null":
a.handleIsTypeSmartcast(n.Arg(0).Expr, "null")
}
}

switch {
case len(n.Args) == 2 && nm.Value == `method_exists`:
obj := n.Arg(0).Expr
Expand Down Expand Up @@ -218,6 +230,48 @@ func (a *andWalker) EnterNode(w ir.Node) (res bool) {
return res
}

func (a *andWalker) handleIsTypeSmartcast(n ir.Node, typ string) {
varNode, ok := n.(*ir.SimpleVar)
if !ok {
return
}

currentType := a.exprType(varNode)

trueType := types.NewMap(typ)
falseType := currentType.Clone().Erase(typ)

if a.inNot {
trueType, falseType = falseType, trueType
}

// If the variable has already been created, then we analyze the next smartcast.
if (irutil.IsBoolAnd(a.path.Current()) || irutil.IsBoolOr(a.path.Current())) &&
a.trueContext.sc.HaveImplicitVar(varNode) {

if a.inNot {
flags := meta.VarAlwaysDefined | meta.VarImplicit
a.trueContext.sc.ReplaceVar(varNode, trueType, "smartcast true", flags)

varInFalse, _ := a.falseContext.sc.GetVar(varNode)
varInFalse.Type = varInFalse.Type.Append(falseType)
} else {
// The types in trueContext must be concatenated.
varInTrue, _ := a.trueContext.sc.GetVar(varNode)
varInTrue.Type = varInTrue.Type.Append(trueType)

// And in falseContext, on the contrary, they are replaced,
// since there are only types that are not in trueContext.
flags := meta.VarAlwaysDefined | meta.VarImplicit
a.falseContext.sc.ReplaceVar(varNode, falseType, "smartcast false", flags)
}
} else {
flags := meta.VarAlwaysDefined | meta.VarImplicit
a.trueContext.sc.ReplaceVar(varNode, trueType, "smartcast true", flags)
a.falseContext.sc.ReplaceVar(varNode, falseType, "smartcast false", flags)
}
}

func (a *andWalker) runRules(w ir.Node) {
kind := ir.GetNodeKind(w)
if a.b.r.anyRset != nil {
Expand Down
13 changes: 10 additions & 3 deletions src/linter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/VKCOM/noverify/src/phpdoctypes"
"github.com/VKCOM/noverify/src/utils"
"github.com/VKCOM/php-parser/pkg/position"
"github.com/VKCOM/php-parser/pkg/token"

Expand Down Expand Up @@ -1521,7 +1522,8 @@ func (b *blockWalker) handleIf(s *ir.IfStmt) bool {
var linksCount int
var contexts []*blockContext

onlyInstanceof := true
applyReturnTypeConstriction := true

// Add all new variables from the condition to the current scope.
irutil.Inspect(s.Cond, func(n ir.Node) bool {
switch n := n.(type) {
Expand All @@ -1531,12 +1533,17 @@ func (b *blockWalker) handleIf(s *ir.IfStmt) bool {
return true
case *ir.InstanceOfExpr:
return false
case *ir.FunctionCallExpr:
if !utils.IsTypeCheckFunctions(n) {
applyReturnTypeConstriction = false
}
return false

case *ir.Assign:
b.handleAssign(n)
return false
default:
onlyInstanceof = false
applyReturnTypeConstriction = false
}

return true
Expand Down Expand Up @@ -1582,7 +1589,7 @@ func (b *blockWalker) handleIf(s *ir.IfStmt) bool {
// }
//
// $a has Boo type
if trueContext.exitFlags != 0 && onlyInstanceof && len(s.ElseIf) == 0 && s.Else == nil {
if trueContext.exitFlags != 0 && applyReturnTypeConstriction && len(s.ElseIf) == 0 && s.Else == nil {
b.ctx = falseContext
}

Expand Down
177 changes: 177 additions & 0 deletions src/tests/exprtype/smartcasts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package exprtype_test

import "testing"

func TestIsString(t *testing.T) {
code := `<?php
/**
* @param mixed $a
* @param mixed $b
*/
function f($a, $b) {
if (is_string($a)) {
exprtype($a, "string");
}

if (is_string($a)) {
exprtype($a, "string");
} else {
exprtype($a, "mixed");
}

if (!is_string($a)) {
exprtype($a, "mixed");
} else {
exprtype($a, "string");
}

if (is_string($a) && is_string($b)) {
exprtype($a, "string");
exprtype($b, "string");
}

if (is_string($a) || is_string($b)) {
exprtype($a, "string");
exprtype($b, "string");
}

if (is_string($a) && !is_string($b)) {
exprtype($a, "string");
exprtype($b, "mixed");
}

if (is_string($a) && !is_string($b)) {
exprtype($a, "string");
exprtype($b, "mixed");
} else {
exprtype($a, "mixed");
exprtype($b, "string");
}
}

/**
* @param string $a
*/
function f1($a) {
if (is_string($a)) {
exprtype($a, "string");
} else {
exprtype($a, "mixed");
}
}
`
runExprTypeTest(t, &exprTypeTestParams{code: code})
}

func TestIsStringWithIsInt(t *testing.T) {
code := `<?php
class Foo {}
/**
* @param mixed $a
* @param Foo $b
*/
function f($a, $b) {
if (is_string($a)) {
exprtype($a, "string");
} else if (is_int($a)) {
exprtype($a, "int");
} else {
exprtype($a, "mixed");
}

if (is_string($b)) {
exprtype($b, "string");
} else if (is_int($b)) {
exprtype($b, "int");
} else {
exprtype($b, "\Foo");
}
}
`
runExprTypeTest(t, &exprTypeTestParams{code: code})
}

func TestIsStringWithReturn(t *testing.T) {
code := `<?php
class Foo {}
/**
* @param mixed $a
* @param ?Foo $b
*/
function f($a, $b) {
if (!is_string($a)) {
return;
}

exprtype($a, "string");

if (is_null($b)) {
return;
}

exprtype($b, "\Foo");
}
`
runExprTypeTest(t, &exprTypeTestParams{code: code})
}

func TestIsInt(t *testing.T) {
code := `<?php
/**
* @param mixed $a
* @param mixed $b
*/
function f($a, $b) {
if (is_int($a)) {
exprtype($a, "int");
}

if (is_int($a)) {
exprtype($a, "int");
} else {
exprtype($a, "mixed");
}

if (!is_int($a)) {
exprtype($a, "mixed");
} else {
exprtype($a, "int");
}

if (is_int($a) && is_int($b)) {
exprtype($a, "int");
exprtype($b, "int");
}

if (is_int($a) || is_int($b)) {
exprtype($a, "int");
exprtype($b, "int");
}

if (is_int($a) && !is_int($b)) {
exprtype($a, "int");
exprtype($b, "mixed");
}

if (is_int($a) && !is_int($b)) {
exprtype($a, "int");
exprtype($b, "mixed");
} else {
exprtype($a, "mixed");
exprtype($b, "int");
}
}

/**
* @param int $a
*/
function f1($a) {
if (is_int($a)) {
exprtype($a, "int");
} else {
exprtype($a, "mixed");
}
}
`
runExprTypeTest(t, &exprTypeTestParams{code: code})
}
22 changes: 22 additions & 0 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,25 @@ func IsSpecialClassName(n ir.Node) bool {
func InVendor(path string) bool {
return strings.Contains(filepath.ToSlash(path), "/vendor/")
}

var typeCheckFunctions = map[string]bool{
"is_bool": true,
"is_double": true,
"is_float": true,
"is_real": true,
"is_int": true,
"is_integer": true,
"is_long": true,
"is_object": true,
"is_string": true,
"is_null": true,
}

func IsTypeCheckFunctions(n *ir.FunctionCallExpr) bool {
name, ok := n.Function.(*ir.Name)
if !ok {
return false
}

return typeCheckFunctions[name.Value]
}