Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Sep 5, 2024
1 parent 7bb3c80 commit 382f79c
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 16 deletions.
7 changes: 1 addition & 6 deletions src/linter/block_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package linter
import (
"bytes"
"fmt"
"github.com/VKCOM/noverify/src/phpdoc"
"strings"

"github.com/VKCOM/noverify/src/constfold"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/VKCOM/noverify/src/ir/phpcore"
"github.com/VKCOM/noverify/src/linter/autogen"
"github.com/VKCOM/noverify/src/meta"
"github.com/VKCOM/noverify/src/phpdoc"
"github.com/VKCOM/noverify/src/quickfix"
"github.com/VKCOM/noverify/src/solver"
"github.com/VKCOM/noverify/src/types"
Expand Down Expand Up @@ -228,11 +228,6 @@ func (b *blockLinter) checkMethodTypeHint(method *ir.ClassMethodStmt) {
if ok {
var variable = typedParam.Variable

// maybe we don`t need it and order the same as param
if variable.Name != typeContainer.Var[1:] {
continue
}

var paramType, ok = typedParam.VariableType.(*ir.Name)
if paramType != nil && ok {
// TODO: quickFix -> remove @param from typeHint
Expand Down
2 changes: 1 addition & 1 deletion src/linter/quickfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package linter
import (
"bytes"
"fmt"
"github.com/VKCOM/noverify/src/phpdoc"

"github.com/VKCOM/noverify/src/ir"
"github.com/VKCOM/noverify/src/phpdoc"
"github.com/VKCOM/noverify/src/quickfix"
"github.com/VKCOM/noverify/src/workspace"
)
Expand Down
10 changes: 5 additions & 5 deletions src/linter/root_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ func (r *rootChecker) CheckFunctionTypeHint(fun *ir.FunctionStmt) {
if ok {
var variable = typedParam.Variable

// maybe we don`t need it and order the same as param
if variable.Name != typeContainer.Var[1:] {
continue
}

var paramType, ok = typedParam.VariableType.(*ir.Name)
if paramType != nil && ok {
// TODO: quickFix -> remove @param from typeHint
Expand All @@ -89,6 +84,10 @@ func (r *rootChecker) CheckFunctionTypeHint(fun *ir.FunctionStmt) {
continue
}

if converted.Types == nil {
continue
}

if !types.IsTrivial(converted.Types[0].Elem) && !types.IsClass(converted.Types[0].Elem) {
continue
}
Expand All @@ -98,6 +97,7 @@ func (r *rootChecker) CheckFunctionTypeHint(fun *ir.FunctionStmt) {
var variableWithType = typeParam + " " + varDollar
r.walker.Report(variable, LevelWarning, "implicitParamType", "Type for %s can be wrote explicitly from typeHint", varDollar)
r.walker.addQuickFix("implicitParamType", r.quickfix.FunctionParamTypeReplacementFromTypeHint(variable, variableWithType))
break
}
}
}
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 @@ -555,6 +555,8 @@ function f1() {}
`Void type can only be used as a standalone type for the return type`,
`Void type can only be used as a standalone type for the return type`,
`Void type can only be used as a standalone type for the return type`,
`Type for $x can be wrote explicitly from typeHint`,
`Type for $y can be wrote explicitly from typeHint`,
}
test.RunAndMatch()
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/checkers/closure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function f(callable $s, callable $s1, callable $s2) {}
)
test.Expect = []string{
"Lost return type for callable(...), if the function returns nothing, specify void explicitly",
"Type for $s can be wrote explicitly from typeHint",
}
test.RunAndMatch()
}
7 changes: 6 additions & 1 deletion src/tests/checkers/oop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,9 @@ func TestStaticResolutionInsideSameClass(t *testing.T) {
}

func TestStaticResolutionInsideOtherStaticResolution(t *testing.T) {
linttest.SimpleNegativeTest(t, `<?php
test := linttest.NewSuite(t)

test.AddFile(`<?php
class SomeMainClass extends ParentClass {
/**
* @var string
Expand Down Expand Up @@ -807,6 +809,9 @@ func TestStaticResolutionInsideOtherStaticResolution(t *testing.T) {
$result .= $objectB->testProperty;
echo $result;
}`)

test.Expect = []string{"Type for $testProperty can be wrote explicitly from typeHint"}
test.RunAndMatch()
}

func TestInheritanceLoop(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion src/tests/checkers/paramClobber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function f($x, $y) {
}

func TestParamClobberReferenced(t *testing.T) {
linttest.SimpleNegativeTest(t, `<?php
test := linttest.NewSuite(t)
test.AddFile(`<?php
/**
* @param mixed[] $x
*/
Expand All @@ -27,6 +28,8 @@ function f(array $x) {
return $x;
}
`)
test.Expect = []string{"Type for $x can be wrote explicitly from typeHint"}
test.RunAndMatch()
}

func TestParamClobberConditional(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions src/tests/checkers/phpdoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ func TestPHPDocSyntax(t *testing.T) {
`Expected a type, found '-'; if you want to express 'any' type, use 'mixed'`,
`Malformed @param $a tag (maybe type is missing?)`,
`Malformed @param tag (maybe var is missing?)`,
`Type for $x can be wrote explicitly from typeHint`,
`Type for $y can be wrote explicitly from typeHint`,
`Type for $z can be wrote explicitly from typeHint`,
`Type for can be wrote explicitly from typeHint`,
}
test.RunAndMatch()
}
Expand Down Expand Up @@ -445,6 +449,7 @@ func TestPHPDocType(t *testing.T) {
`Use bool type instead of boolean`,
`Nullable syntax is ?T, not T?`,
`Array syntax is T[], not []T`,
`Type for $x1 can be wrote explicitly from typeHint`,
}
test.RunAndMatch()
}
Expand Down Expand Up @@ -522,6 +527,10 @@ function f2($a) {
`Multiline PHPDoc comment should start with /**, not /*`,
`Multiline PHPDoc comment should start with /**, not /*`,
`Multiline PHPDoc comment should start with /**, not /*`,
`Type for $a can be wrote explicitly from typeHint`,
`Type for $a can be wrote explicitly from typeHint`,
`Type for $item can be wrote explicitly from typeHint`,
`Type for $item2 can be wrote explicitly from typeHint`,
}
test.RunAndMatch()
}
Expand Down
2 changes: 2 additions & 0 deletions src/tests/checkers/printf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function f($s, array $a) {
`'-flag expects a char, found end of string`,
`potential array to string conversion`,
`potential array to string conversion`,
`Type for $a can be wrote explicitly from typeHint`,
}
test.RunAndMatch()
}
Expand Down Expand Up @@ -72,6 +73,7 @@ function f($s, array $a) {
`'-flag expects a char, found end of string`,
`potential array to string conversion`,
`potential array to string conversion`,
`Type for $a can be wrote explicitly from typeHint`,
}
test.RunAndMatch()
}
14 changes: 12 additions & 2 deletions src/tests/checkers/shape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ echo $t->good6['y']->value;
}

func TestShapeReturn(t *testing.T) {
linttest.SimpleNegativeTest(t, `<?php
test := linttest.NewSuite(t)
test.AddFile(`<?php
class MyList {
/** @var \MyList */
public $next;
Expand All @@ -146,10 +147,15 @@ function new_shape1() { return []; }
$s1 = new_shape1();
echo $s1['list']->next->next;
`)
test.Expect = []string{
`Type for $next can be wrote explicitly from typeHint`,
}
test.RunAndMatch()
}

func TestTuple(t *testing.T) {
linttest.SimpleNegativeTest(t, `<?php
test := linttest.NewSuite(t)
test.AddFile(`<?php
class Box { public $value; }
class T {
Expand All @@ -167,4 +173,8 @@ $t = new T();
echo $t->good2[0]->value;
echo $t->good3[1][0]->value;
`)
test.Expect = []string{
`Type for $good1 can be wrote explicitly from typeHint`,
}
test.RunAndMatch()
}
6 changes: 6 additions & 0 deletions src/tests/checkers/typehint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ function f4() {
};
}
`)
test.Expect = []string{
`Type for $a can be wrote explicitly from typeHint`,
`Type for $a can be wrote explicitly from typeHint`,
`Type for $a can be wrote explicitly from typeHint`,
`Type for $a can be wrote explicitly from typeHint`,
}
test.RunAndMatch()
}

Expand Down
21 changes: 21 additions & 0 deletions src/tests/golden/testdata/embeddedrules/golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ WARNING bitwiseOps: Used | bitwise operator over bool operands, perhaps || is in
MAYBE callSimplify: Could simplify to array_key_exists('abc', $array) at testdata/embeddedrules/callSimplify.php:7
$_ = in_array('abc', array_keys($array)); // bad
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
WARNING implicitParamType: Type for $array can be wrote explicitly from typeHint at testdata/embeddedrules/callSimplify.php:6
function in_array_over_array_keys(array $array) {
^^^^^^
MAYBE callSimplify: Could simplify to $str[$index] at testdata/embeddedrules/callSimplify.php:14
$_ = substr($str, $index, 1);
^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -82,6 +85,12 @@ MAYBE callSimplify: Could simplify to $array[] = $val at testdata/embeddedrule
MAYBE callSimplify: Could simplify to $array[] = 10 at testdata/embeddedrules/callSimplify.php:28
array_push($array, 10);
^^^^^^^^^^^^^^^^^^^^^^
WARNING implicitParamType: Type for $array can be wrote explicitly from typeHint at testdata/embeddedrules/callSimplify.php:26
function some_array_push(array $array, int $val) {
^^^^^^
WARNING implicitParamType: Type for $val can be wrote explicitly from typeHint at testdata/embeddedrules/callSimplify.php:26
function some_array_push(array $array, int $val) {
^^^^^^
WARNING indexingSyntax: a{i} indexing is deprecated since PHP 7.4, use a[i] instead at testdata/embeddedrules/indexingSyntax.php:14
$_ = $a{0};
^^^^^
Expand Down Expand Up @@ -157,6 +166,15 @@ WARNING offBy1: Probably intended to use sizeof-1 as an index at testdata/embedd
WARNING offBy1: Probably intended to use count-1 as an index at testdata/embeddedrules/offBy1.php:14
if ($tabs[count($tabs)] == "") {
^^^^^^^^^^^^^^^^^^^
WARNING implicitParamType: Type for $xs can be wrote explicitly from typeHint at testdata/embeddedrules/offBy1.php:7
function test_countindex_bad(array $xs, array $tabs) {
^^^
WARNING implicitParamType: Type for $tabs can be wrote explicitly from typeHint at testdata/embeddedrules/offBy1.php:7
function test_countindex_bad(array $xs, array $tabs) {
^^^
WARNING implicitParamType: Type for $xs can be wrote explicitly from typeHint at testdata/embeddedrules/offBy1.php:24
function test_countindex_good(array $xs) {
^^^
WARNING precedence: == has higher precedence than & at testdata/embeddedrules/precedence.php:4
$_ = 0 == $mask & $x;
^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -310,3 +328,6 @@ MAYBE ternarySimplify: Could rewrite as `$x_arr[10] ?? null` at testdata/embed
MAYBE ternarySimplify: Could rewrite as `(bool)($flags & SOME_MASK)` at testdata/embeddedrules/ternarySimplify.php:46
sink(($flags & SOME_MASK) ? true : false);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
WARNING implicitParamType: Type for $flags can be wrote explicitly from typeHint at testdata/embeddedrules/ternarySimplify.php:45
function ternarySimplify_issue540($flags) {
^^^^^^
Loading

0 comments on commit 382f79c

Please sign in to comment.