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
10 changes: 8 additions & 2 deletions internal/phpast/docparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ func TestStripRedundantDocParams(t *testing.T) {
want: "<?php\nclass A {\n public function m($e) {}\n}",
},
{
name: "keeps a tag whose type differs from the added type",
name: "drops a mixed tag once any type is added",
src: "<?php\nclass A {\n /**\n * @param mixed $v\n */\n public function m($v) {}\n}",
added: map[string]string{"v": "\\DateTime"},
want: "<?php\nclass A {\n /**\n * @param mixed $v\n */\n public function m($v) {}\n}",
want: "<?php\nclass A {\n public function m($v) {}\n}",
},
{
name: "keeps a tag whose type differs from the added type",
src: "<?php\nclass A {\n /**\n * @param int $v\n */\n public function m($v) {}\n}",
added: map[string]string{"v": "\\DateTime"},
want: "<?php\nclass A {\n /**\n * @param int $v\n */\n public function m($v) {}\n}",
},
}

Expand Down
12 changes: 11 additions & 1 deletion internal/phpast/phpast.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func stripDocParamLines(doc string, added map[string]string) (result string, cha
for i := 0; i < len(lines); i++ {
line := lines[i]
if match := docParamLine.FindStringSubmatch(line); match != nil {
if native, ok := added[match[2]]; ok && normalizeDocType(match[1]) == normalizeDocType(native) {
if native, ok := added[match[2]]; ok && docTypeRedundant(match[1], native) {
changed = true
// also drop a blank comment line that followed the tag
if i+1 < len(lines) && isBlankCommentLine(lines[i+1]) {
Expand Down Expand Up @@ -219,6 +219,16 @@ func docIsEmpty(lines []string) bool {
// 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.
// docTypeRedundant reports whether a `@param` type adds nothing over the type
// just written on the parameter: either it equals the added type, or it is
// `mixed`, which carries no information once a real type is present.
func docTypeRedundant(docType, native string) bool {
if strings.EqualFold(docType, "mixed") {
return true
}
return normalizeDocType(docType) == normalizeDocType(native)
}

func normalizeDocType(name string) string {
parts := strings.Split(name, "|")
for i, part := range parts {
Expand Down
Loading