Skip to content

Commit

Permalink
rules: fixed checking @filter in dynamic rules (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil42Russia authored Apr 1, 2024
1 parent 3ef4699 commit ccaafc7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/linter/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -1826,8 +1826,8 @@ func (d *rootWalker) checkFilterSet(m *phpgrep.MatchData, sc *meta.Scope, filter
}
if filter.Regexp != nil {
if vr, ok := nn.(*ir.SimpleVar); ok {
if filter.Regexp.MatchString(vr.Name) {
return true
if !filter.Regexp.MatchString(vr.Name) {
return false
}
}
}
Expand Down
66 changes: 56 additions & 10 deletions src/tests/rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,26 +419,72 @@ function bad(string $x) {

func TestRulesFilter(t *testing.T) {
rfile := `<?php
function basic_rules() {
/**
* @warning Don't use $var variable
* @filter $var ^book_id$
*/
$var;
}
function id_check() {
/**
* @warning Don't use $id variable
* @filter $id ^id$
* @warning Don't use the name $id for the variable
* @filter $id ^(user|owner)_id$
*/
any: {
$id == 0;
$id === 0;
}
}
function type_type_rules() {
/**
* @warning Don't use $animal variable
* @type int $animal
* @filter $animal ^animal_(name|id)$
*/
$id;
$animal == 0;
}
`
test := linttest.NewSuite(t)
test.RuleFile = rfile
test.AddFile(`<?php
$id = 100;
echo $id;
echo $id == 0;
if ($id == 0) {}
function basic_test() {
$book_id = 100;
echo $book_id;
echo $book_id == 0;
if ($book_id == 0) {}
}
function test(int $user_id, int $id, int $owner_id, int $chat_id, int $id_owner) {
$_ = $user_id === 0;
$_ = $user_id == 0;
$_ = $id === 0;
$_ = $id == 0;
$_ = $owner_id === 0;
$_ = $owner_id == 0;
$_ = $chat_id === 0;
$_ = $chat_id == 0;
$_ = $id_owner === 0;
$_ = $id_owner == 0;
}
function type_type_check(string $animal_name, int $animal_id) {
$_ = $animal_name == 0;
$_ = $animal_id == 0;
}
`)
test.Expect = []string{
`Don't use $id variable`,
`Don't use $id variable`,
`Don't use $id variable`,
`Don't use $book_id variable`,
`Don't use $book_id variable`,
`Don't use $book_id variable`,
`Don't use the name $user_id for the variable`,
`Don't use the name $user_id for the variable`,
`Don't use the name $owner_id for the variable`,
`Don't use the name $owner_id for the variable`,
`Don't use $animal_id variable`,
}
test.RunRulesTest()
}

0 comments on commit ccaafc7

Please sign in to comment.