From 27a3f5e7a71168349293e6c19f79d7b80f338598 Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Sat, 1 Feb 2025 01:27:48 +0300 Subject: [PATCH] refactoring --- docs/dynamic_rules.md | 2 +- src/rules/parser.go | 41 +++++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/docs/dynamic_rules.md b/docs/dynamic_rules.md index d3079336..20e41c45 100644 --- a/docs/dynamic_rules.md +++ b/docs/dynamic_rules.md @@ -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 /** diff --git a/src/rules/parser.go b/src/rules/parser.go index f77e7a20..a713cffe 100644 --- a/src/rules/parser.go +++ b/src/rules/parser.go @@ -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) {