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

dinamic rules: multiple path #1224

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 27 additions & 9 deletions src/linter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ func mergeTypeMaps(left types.Map, right types.Map) types.Map {
// The types are inferred as follows:
// 1. If there is a @return annotation, then its value becomes the return type;
//
// 2. If there is a type hint, then it is added to the types from the @return.
// If the @return is empty, then the type matches the type hint itself;
// 2. If there is a type hint, then it is added to the types from the @return.
// If the @return is empty, then the type matches the type hint itself;
//
// 3. If the resulting type is mixed[], then if the actual type is a specific
// array type, then we use it, otherwise we combine this type with the
// resulting mixed[] type.
// 3. If the resulting type is mixed[], then if the actual type is a specific
// array type, then we use it, otherwise we combine this type with the
// resulting mixed[] type.
//
// 4. If there is no @return annotation and type hint, then the return type is equal to
// the union of the types that are returned from the function by return.
// 4. If there is no @return annotation and type hint, then the return type is equal to
// the union of the types that are returned from the function by return.
func functionReturnType(phpdocReturnType types.Map, hintReturnType types.Map, actualReturnTypes types.Map) types.Map {
var returnTypes types.Map
if !phpdocReturnType.Empty() || !hintReturnType.Empty() {
Expand Down Expand Up @@ -494,10 +494,28 @@ func cloneRulesForFile(filename string, ruleSet *rules.ScopedSet) *rules.ScopedS
for kind, ruleByKind := range &ruleSet.RulesByKind {
res := make([]rules.Rule, 0, len(ruleByKind))
for _, rule := range ruleByKind {
if !strings.Contains(filename, rule.Path) || isFilePathExcluded(filename, rule) {
if isFilePathExcluded(filename, rule) {
continue
}
res = append(res, rule)

match := false

if rule.Paths == nil {
if strings.Contains(filename, "") {
match = true
}
} else {
for _, path := range rule.Paths {
if strings.Contains(filename, path) {
match = true
break
}
}
}

if match {
res = append(res, rule)
}
}
clone.Set(ir.NodeKind(kind), res)
}
Expand Down
10 changes: 6 additions & 4 deletions src/rules/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (p *parser) parseRuleInfo(st ir.Node, labelStmt ir.Node, proto *Rule) (Rule
rule.Level = proto.Level
rule.Message = proto.Message
rule.Location = proto.Location
rule.Path = proto.Path
rule.Paths = proto.Paths

rule.Filters = make([]map[string]Filter, len(proto.Filters))
for i, filterSet := range proto.Filters {
Expand Down Expand Up @@ -231,10 +231,12 @@ func (p *parser) parseRuleInfo(st ir.Node, labelStmt ir.Node, proto *Rule) (Rule
if len(part.Params) != 1 {
return rule, p.errorf(st, "@path expects exactly 1 param, got %d", len(part.Params))
}
if rule.Path != "" {
return rule, p.errorf(st, "duplicate @path constraint")

if rule.Paths == nil {
rule.Paths = make([]string, 0)
}
rule.Path = part.Params[0]

rule.Paths = append(rule.Paths, part.Params...)
case "path-exclude":
if len(part.Params) != 1 {
return rule, p.errorf(st, "@exclude expects exactly 1 param, got %d", len(part.Params))
Expand Down
2 changes: 1 addition & 1 deletion src/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Rule struct {

// Path is a filter-like rule switcher.
// A rule is only applied to a file that contains a Path as a substring in its name.
Path string
Paths []string

// PathExcludes is a filter-like rule switcher.
// A rule is not applied to a file that contains a PathExcludes as a substring in its name.
Expand Down
4 changes: 2 additions & 2 deletions src/rules/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func formatRule(r *Rule) string {
buf.WriteString(" * @maybe " + r.Message + "\n")
}

if r.Path != "" {
buf.WriteString(" * @path " + r.Path + "\n")
for _, path := range r.Paths {
buf.WriteString(" * @path " + path + "\n")
}

if r.PathExcludes != nil {
Expand Down
13 changes: 0 additions & 13 deletions src/tests/rules/rules_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,6 @@ $_ = foo();
`,
expect: "<test>:7: @path expects exactly 1 param, got 2",
},
{
name: `DuplicatedPath`,
rule: `<?php
/**
* @name Some
* @maybe Some
* @path hell;
* @path earth;
*/
$_ = foo();
`,
expect: "<test>:8: duplicate @path constraint",
},
{
name: `PathExpectsExactlyTwoParam`,
rule: `<?php
Expand Down
30 changes: 30 additions & 0 deletions src/tests/rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,36 @@ eval(${"var"});
test.RunRulesTest()
}

func TestRuleMultiPathFilter(t *testing.T) {
rfile := `<?php
/**
* @name varEval
* @warning don't eval from variable
* @path my/site/ads_
* @path my/site/admin_
*/
eval(${"var"});
`
test := linttest.NewSuite(t)
test.RuleFile = rfile
code := `<?php
$hello = 'echo 123;';
eval($hello);
eval('echo 456;');
`
test.AddNamedFile("/home/john/my/site/foo.php", code)
test.AddNamedFile("/home/john/my/site/ads_foo.php", code)
test.AddNamedFile("/home/john/my/site/ads_bar.php", code)
test.AddNamedFile("/home/john/my/site/admin_table.php", code)

test.Expect = []string{
`don't eval from variable`,
`don't eval from variable`,
`don't eval from variable`,
}
test.RunRulesTest()
}

func TestAnyRules(t *testing.T) {
rfile := `<?php
/**
Expand Down
Loading