Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Jan 31, 2025
1 parent 7e24c95 commit 27a3f5e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/dynamic_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ This rule will now don't apply to files with the `common/` folder in the path.

##### `@path-group-name`

The `@path-group-name` allow you to group a lot of paths to the named group and give the name.
The `@path-group-name` allow you to group a lot of paths to the named group and give the name. This tag must be first.

```php
/**
Expand Down
41 changes: 21 additions & 20 deletions src/rules/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,40 +129,41 @@ func (p *parser) tryParseLabeledStmt(stmts []ir.Node, proto *Rule) (bool, error)
}

func (p *parser) parseRuleGroups(st ir.Node) bool {

comment := p.commentText(st)

isGroup := false

parsedPhpDoc := phpdoc.Parse(p.typeParser, comment).Parsed

var groupName string
for _, part := range parsedPhpDoc {
part := part.(*phpdoc.RawCommentPart)

tagName := part.Name()
// a little optimisation: if @path-group-name is not first - this is not grouping
foundGroup := false

if tagName != "path-group-name" {
if !isGroup {
break
}
} else {
isGroup = true
groupName = part.ParamsText
for _, part := range parsedPhpDoc {
rawPart, ok := part.(*phpdoc.RawCommentPart)
if !ok {
continue
}

switch tagName {
switch tagName := rawPart.Name(); tagName {
case "path-group-name":
if pathGroups[tagName] == nil {
groupName = rawPart.ParamsText
if pathGroups == nil {
pathGroups = make(map[string][]string)
}
pathGroups[groupName] = make([]string, 0)
pathGroups[groupName] = []string{}
foundGroup = true

case "path":
pathGroups[groupName] = append(pathGroups[groupName], part.Params...)
if !foundGroup {
return false // if @path-group-name not first - return
}
pathGroups[groupName] = append(pathGroups[groupName], rawPart.Params...)
default:
if !foundGroup {
return false
}
}
}

return pathGroups[groupName] != nil
return foundGroup && pathGroups[groupName] != nil
}

func (p *parser) parseRuleInfo(st ir.Node, labelStmt ir.Node, proto *Rule) (Rule, error) {
Expand Down

0 comments on commit 27a3f5e

Please sign in to comment.