Skip to content

Commit

Permalink
updated tests & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Mar 20, 2024
1 parent 1f750de commit b3e9765
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
27 changes: 2 additions & 25 deletions src/linter/block_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,21 +765,7 @@ func (b *blockLinter) checkIfStmt(s *ir.IfStmt) {
func (b *blockLinter) checkDangerousBoolCond(s *ir.IfStmt) {
cond, ok := s.Cond.(*ir.BooleanOrExpr)
if !ok {
switch c := s.Cond.(type) {
case *ir.ConstFetchExpr:
if c.Constant.Value == "true" || c.Constant.Value == "false" {
b.report(s, LevelWarning, "DangerousCondition", "Potential dangerous bool value: you have constant bool value in condition")
fmt.Println("Bad")
}

case *ir.Lnumber:
if c.Value == "0" || c.Value == "1" {
b.report(s, LevelWarning, "DangerousCondition", "Potential dangerous value: you have constant int value that interpreted as bool")
fmt.Println("Bad")
}
case *ir.BooleanAndExpr:
checkIfStatementConditionBool(c.Left, c.Right, b)
}
checkNode(s.Cond, b)
return
}

Expand All @@ -792,30 +778,21 @@ func checkIfStatementConditionBool(left ir.Node, right ir.Node, b *blockLinter)

func checkNode(node ir.Node, b *blockLinter) {
switch n := node.(type) {
case *ir.SimpleVar:
fmt.Println("SimpleVar:", n)
case *ir.ConstFetchExpr:
if n.Constant.Value == "true" || n.Constant.Value == "false" {
if strings.ToLower(n.Constant.Value) == "true" || strings.ToLower(n.Constant.Value) == "false" {

Check failure on line 782 in src/linter/block_linter.go

View workflow job for this annotation

GitHub Actions / Build

equalFold: consider replacing with strings.EqualFold(n.Constant.Value, "true") (gocritic)
b.report(node, LevelWarning, "DangerousCondition", "Potential dangerous bool value: you have constant bool value in condition")
fmt.Println("Bad")
}

case *ir.Lnumber:
if n.Value == "0" || n.Value == "1" {
b.report(node, LevelWarning, "DangerousCondition", "Potential dangerous value: you have constant int value that interpreted as bool")
fmt.Println("Bad")
}
case *ir.BooleanOrExpr:
checkNode(n.Left, b)
checkNode(n.Right, b)

case *ir.BooleanAndExpr:
checkNode(n.Left, b)
checkNode(n.Right, b)
/*default:
fmt.Println("Unknown type:", reflect.TypeOf(node))*/
}

}

func (b *blockLinter) checkIfStmtDupCond(s *ir.IfStmt) {
Expand Down
2 changes: 2 additions & 0 deletions src/tests/checkers/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,7 @@ function f() {
test.Expect = []string{
`since PHP 7.4, using array_key_exists() with an object has been deprecated, use isset() or property_exists() instead`,
`since PHP 7.4, using array_key_exists() with an object has been deprecated, use isset() or property_exists() instead`,
`Potential dangerous value: you have constant int value that interpreted as bool`,
}
test.RunAndMatch()
}
Expand Down Expand Up @@ -2364,6 +2365,7 @@ function f() {
`)
test.Expect = []string{
`Cannot find referenced variable $e`,
`Potential dangerous value: you have constant int value that interpreted as bool`,
}
test.RunAndMatch()
}
44 changes: 33 additions & 11 deletions src/tests/checkers/dangerouse_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ func TestDangerousCondition1(t *testing.T) {
test.AddFile(`<?php
if(true){
}
if(1){
}
`)
test.Expect = []string{
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:2`,
`Potential dangerous bool value: you have constant bool value in condition`,
`Potential dangerous value: you have constant int value that interpreted as bool`,
}
test.RunAndMatch()
}
Expand All @@ -31,12 +36,12 @@ if(1||$a||1||true||false||0){
}
`)
test.Expect = []string{
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:4`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:8`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:8`,
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:8`,
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:8`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:8`,
`Potential dangerous bool value: you have constant bool value in condition`,
`Potential dangerous value: you have constant int value that interpreted as bool`,
`Potential dangerous value: you have constant int value that interpreted as bool`,
`Potential dangerous bool value: you have constant bool value in condition`,
`Potential dangerous bool value: you have constant bool value in condition`,
`Potential dangerous value: you have constant int value that interpreted`,
}
test.RunAndMatch()
}
Expand All @@ -51,10 +56,27 @@ if($a && false && true && 1 && 0){
`)
test.Expect = []string{
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:4`,
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:4`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:4`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:4`,
`Potential dangerous bool value: you have constant bool value in condition`,
`Potential dangerous bool value: you have constant bool value in condition`,
`Potential dangerous value: you have constant int value that interpreted as bool`,
`Potential dangerous value: you have constant int value that interpreted as bool`,
}
test.RunAndMatch()
}

func TestDangerousCondition4(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
$a = true;
if($a || false && 1 || true){
}
`)
test.Expect = []string{
`Potential dangerous bool value: you have constant bool value in condition`,
`Potential dangerous bool value: you have constant bool value in condition`,
`Potential dangerous value: you have constant int value that interpreted as bool`,
}
test.RunAndMatch()
}
1 change: 1 addition & 0 deletions src/tests/checkers/get_type_miss_use_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function getTypeMisUse(mixed $var) {
`use is_object instead of 'gettype($var) === "object"'`,
`use is_int instead of 'gettype(getTypeMisUse($var)) === "integer"'`,
`use is_resource instead of 'gettype(getTypeMisUse($var)) != "resource"'`,
`Potential dangerous bool value: you have constant bool value in condition`,
}

test.RunAndMatch()
Expand Down

0 comments on commit b3e9765

Please sign in to comment.