diff --git a/phpdotnet/phd/Package/Generic/XHTML.php b/phpdotnet/phd/Package/Generic/XHTML.php index b5ab2b90..1491fb1a 100644 --- a/phpdotnet/phd/Package/Generic/XHTML.php +++ b/phpdotnet/phd/Package/Generic/XHTML.php @@ -1449,12 +1449,7 @@ public function format_fieldsynopsis($open, $name, $attrs) { public function format_fieldsynopsis_modifier_text($value, $tag) { $this->cchunk["fieldsynopsis"]["modifier"] = trim($value); if ($this->getRole() === "attribute") { - $attribute = trim(strtolower($value), "#[]\\"); - $href = Format::getFilename("class.$attribute"); - if ($href) { - return '' .$value. ' '; - } - return false; + return $this->format_attribute_modifier_text($value); } return $this->TEXT($value); } @@ -1492,15 +1487,31 @@ public function format_methodparam_modifier($open, $name, $attrs, $props) { public function format_modifier_text($value, $tag) { if ($this->getRole() === "attribute") { - $attribute = trim(strtolower($value), "#[]\\"); - $href = Format::getFilename("class.$attribute"); - if ($href) { - return '' .$value. ' '; - } + return $this->format_attribute_modifier_text($value); } return false; } + private function format_attribute_modifier_text(string $value): string { + // Anything that is not a leading "#[\Attribute(" / "#[\Attribute]" chunk + // e.g. "|" separator between arguments passes through. + if (!preg_match('/^(#\[)(.+?)([](])$/', $value, $match)) { + if (trim($value) === '|') { + return ' | '; + } + return $value; + } + + [, $prefix, $name, $suffix] = $match; + $attribute = strtolower(ltrim($name, "\\")); + $href = $this->getFilename('class.' . $attribute); + if (!$href) { + return $value; + } + + return $prefix . '' . $name . '' . $suffix; + } + public function format_methodsynopsis($open, $name, $attrs, $props) { if ($open) { @@ -1909,6 +1920,9 @@ public function format_screen($open, $name, $attrs) { } public function format_constant($open, $name, $attrs, $props) { + if ($this->getRole() === "attribute") { + return ""; + } if ($open) { if (str_contains($props["innerXml"], 'pushRole("constant_group"); diff --git a/tests/package/generic/attribute_formatting_001.phpt b/tests/package/generic/attribute_formatting_001.phpt index 76bbf787..5c4c6d2b 100644 --- a/tests/package/generic/attribute_formatting_001.phpt +++ b/tests/package/generic/attribute_formatting_001.phpt @@ -42,7 +42,7 @@ Content:

2. Class methodparameter with known attribute

-
public mysqli::__construct(#[\KnownAttribute] stringnull $password = null)
+
public mysqli::__construct(#[\KnownAttribute]stringnull $password = null)
@@ -54,7 +54,7 @@ Content:

4. Function parameter with known attribute

-
bool password_verify(#[\KnownAttribute] string $password, string $hash)
+
bool password_verify(#[\KnownAttribute]string $password, string $hash)
diff --git a/tests/package/generic/attribute_formatting_002.phpt b/tests/package/generic/attribute_formatting_002.phpt index 6e7aec9d..b5e0d17f 100644 --- a/tests/package/generic/attribute_formatting_002.phpt +++ b/tests/package/generic/attribute_formatting_002.phpt @@ -62,8 +62,8 @@ Content:

2. Class with known attributes

}
@@ -80,8 +80,8 @@ Content:

4. Method with known attributes

- @@ -96,8 +96,8 @@ Content:

6. Constructor with known attributes

- @@ -130,21 +130,21 @@ Content:

8. Class, constructor and methods with known attributes

-
#[\KnownAttribute]
- #[\AnotherKnownAttribute]
+
#[\KnownAttribute]
+ #[\AnotherKnownAttribute]
public ClassName::methodName1()
- @@ -161,8 +161,8 @@ Content:

10. Function with known attributes

- diff --git a/tests/package/generic/attribute_formatting_003.phpt b/tests/package/generic/attribute_formatting_003.phpt index 00ce7e77..0506abaa 100644 --- a/tests/package/generic/attribute_formatting_003.phpt +++ b/tests/package/generic/attribute_formatting_003.phpt @@ -75,8 +75,8 @@ Content: {
/* Properties/Constants */
- #[\KnownAttribute]
- #[\AnotherKnownAttribute]
+ #[\KnownAttribute]
+ #[\AnotherKnownAttribute]
public readonly string diff --git a/tests/package/generic/attribute_formatting_004.phpt b/tests/package/generic/attribute_formatting_004.phpt new file mode 100644 index 00000000..fca533ac --- /dev/null +++ b/tests/package/generic/attribute_formatting_004.phpt @@ -0,0 +1,91 @@ +--TEST-- +Attribute formatting 004 - Attribute with constant arguments +--FILE-- +xmlFile = $xmlFile; + +$format = new TestGenericChunkedXHTML($config, $outputHandler); + +$format->SQLiteIndex( + null, null, + "class.attribute", + "class.attribute", + "", "", "", "", "", "", 0, +); +$format->SQLiteIndex( + null, null, + "attribute.constants.target-class", + "class.attribute", + "", "", "", "", "", "", 0, +); +$format->SQLiteIndex( + null, null, + "attribute.constants.target-class-constant", + "class.attribute", + "", "", "", "", "", "", 0, +); +$format->SQLiteIndex( + null, null, + "attribute.constants.target-property", + "class.attribute", + "", "", "", "", "", "", 0, +); + +$render = new TestRender(new Reader($outputHandler), $config, $format); + +$render->run(); +?> +--EXPECT-- +Filename: attribute-formatting-004.html +Content: +
+
+

1. Attribute with one constant argument

+
+ + #[\Attribute(Attribute::TARGET_CLASS)]
+ final + class Attribute + {
+ }
+
+ +
+

2. Attribute with multiple constant arguments

+
+ }
+
+ +
+

3. Attribute with unknown constant argument

+
+ + #[\Attribute(Attribute::TARGET_UNKNOWN)]
+ final + class Attribute + {
+ }
+
+ +
+

4. Unknown attribute with constant argument

+
+ + #[\UnknownAttribute(Attribute::TARGET_CLASS)]
+ final + class Attribute + {
+ }
+
+
diff --git a/tests/package/generic/data/attribute_formatting_004.xml b/tests/package/generic/data/attribute_formatting_004.xml new file mode 100644 index 00000000..ef67532d --- /dev/null +++ b/tests/package/generic/data/attribute_formatting_004.xml @@ -0,0 +1,45 @@ + +
+ 1. Attribute with one constant argument + + + #[\Attribute(Attribute::TARGET_CLASS)] + final + Attribute + + +
+ +
+ 2. Attribute with multiple constant arguments + + + #[\Attribute(Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_PROPERTY)] + final + Attribute + + +
+ +
+ 3. Attribute with unknown constant argument + + + #[\Attribute(Attribute::TARGET_UNKNOWN)] + final + Attribute + + +
+ +
+ 4. Unknown attribute with constant argument + + + #[\UnknownAttribute(Attribute::TARGET_CLASS)] + final + Attribute + + +
+