Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions internal/phpast/docparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ func TestStripRedundantDocParams(t *testing.T) {
added: map[string]string{"d": "\\DateTime"},
want: "<?php\nclass A {\n public function m($d) {}\n}",
},
{
name: "matches a union regardless of member order",
src: "<?php\nclass A {\n /**\n * @param string|array $c\n */\n public function m($c) {}\n}",
added: map[string]string{"c": "array|string"},
want: "<?php\nclass A {\n public function m($c) {}\n}",
},
{
name: "matches an imported short name against a fully qualified union member",
src: "<?php\nclass A {\n /**\n * @param CompositeExpression|string $e\n */\n public function m($e) {}\n}",
added: map[string]string{"e": "\\Doctrine\\DBAL\\Query\\Expression\\CompositeExpression|string"},
want: "<?php\nclass A {\n public function m($e) {}\n}",
},
{
name: "keeps a tag whose type differs from the added type",
src: "<?php\nclass A {\n /**\n * @param mixed $v\n */\n public function m($v) {}\n}",
Expand Down
15 changes: 14 additions & 1 deletion internal/phpast/phpast.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"reflect"
"regexp"
"slices"
"strings"

"github.com/rectorphp/php-parser-in-go/pkg/ast"
Expand Down Expand Up @@ -214,8 +215,20 @@ func docIsEmpty(lines []string) bool {
return true
}

// normalizeDocType reduces a type to a form that compares equal regardless of
// union member order and of whether a class is written as a short name or a
// fully qualified one: each member is cut to its last name part and the members
// are sorted.
func normalizeDocType(name string) string {
return strings.TrimPrefix(name, "\\")
parts := strings.Split(name, "|")
for i, part := range parts {
if index := strings.LastIndex(part, "\\"); index >= 0 {
part = part[index+1:]
}
parts[i] = part
}
slices.Sort(parts)
return strings.Join(parts, "|")
}

// leadingToken returns the first token of a function or method, which carries
Expand Down
Loading