|
4 | 4 |
|
5 | 5 | namespace Qameta\Allure\Attribute;
|
6 | 6 |
|
| 7 | +use Closure; |
| 8 | +use Doctrine\Common\Annotations\AnnotationReader; |
| 9 | +use Qameta\Allure\Exception\InvalidMethodNameException; |
7 | 10 | use Qameta\Allure\Model;
|
| 11 | +use Qameta\Allure\Model\ModelProviderInterface; |
| 12 | +use ReflectionClass; |
| 13 | +use ReflectionException; |
| 14 | +use ReflectionFunction; |
| 15 | +use ReflectionMethod; |
| 16 | +use ReflectionProperty; |
8 | 17 |
|
9 |
| -class AttributeParser |
| 18 | +use function array_merge; |
| 19 | +use function is_string; |
| 20 | + |
| 21 | +class AttributeParser implements ModelProviderInterface |
10 | 22 | {
|
11 | 23 |
|
12 |
| - private ?string $title = null; |
| 24 | + private ?string $displayName = null; |
13 | 25 |
|
14 | 26 | private ?string $description = null;
|
15 | 27 |
|
@@ -41,11 +53,59 @@ public function __construct(
|
41 | 53 | $this->processAnnotations(...$attributes);
|
42 | 54 | }
|
43 | 55 |
|
| 56 | + /** |
| 57 | + * @param class-string|object|null $classOrObject |
| 58 | + * @param callable-string|Closure|null $methodOrFunction |
| 59 | + * @param string|null $property |
| 60 | + * @param array<string, LinkTemplateInterface> $linkTemplates |
| 61 | + * @return list<ModelProviderInterface> |
| 62 | + * @throws ReflectionException |
| 63 | + */ |
| 64 | + public static function createForChain( |
| 65 | + string|object|null $classOrObject, |
| 66 | + string|Closure|null $methodOrFunction = null, |
| 67 | + ?string $property = null, |
| 68 | + array $linkTemplates = [], |
| 69 | + ): array { |
| 70 | + $reader = new LegacyAttributeReader( |
| 71 | + new AnnotationReader(), |
| 72 | + new AttributeReader(), |
| 73 | + ); |
| 74 | + $annotations = []; |
| 75 | + |
| 76 | + if (isset($classOrObject)) { |
| 77 | + $annotations[] = $reader->getClassAnnotations(new ReflectionClass($classOrObject)); |
| 78 | + if (isset($property)) { |
| 79 | + $annotations[] = $reader->getPropertyAnnotations(new ReflectionProperty($classOrObject, $property)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (isset($methodOrFunction)) { |
| 84 | + $annotations[] = isset($classOrObject) |
| 85 | + ? $reader->getMethodAnnotations( |
| 86 | + new ReflectionMethod( |
| 87 | + $classOrObject, |
| 88 | + is_string($methodOrFunction) |
| 89 | + ? $methodOrFunction |
| 90 | + : throw new InvalidMethodNameException($methodOrFunction), |
| 91 | + ), |
| 92 | + ) |
| 93 | + : $reader->getFunctionAnnotations(new ReflectionFunction($methodOrFunction)); |
| 94 | + } |
| 95 | + |
| 96 | + return [ |
| 97 | + new self( |
| 98 | + array_merge(...$annotations), |
| 99 | + $linkTemplates, |
| 100 | + ) |
| 101 | + ]; |
| 102 | + } |
| 103 | + |
44 | 104 | private function processAnnotations(AttributeInterface ...$attributes): void
|
45 | 105 | {
|
46 | 106 | foreach ($attributes as $attribute) {
|
47 |
| - if ($attribute instanceof TitleInterface) { |
48 |
| - $this->title = $attribute->getValue(); |
| 107 | + if ($attribute instanceof DisplayNameInterface) { |
| 108 | + $this->displayName = $attribute->getValue(); |
49 | 109 | }
|
50 | 110 | if ($attribute instanceof DescriptionInterface) {
|
51 | 111 | if ($attribute->isHtml()) {
|
@@ -126,9 +186,17 @@ public function getParameters(): array
|
126 | 186 | return $this->parameters;
|
127 | 187 | }
|
128 | 188 |
|
| 189 | + /** |
| 190 | + * @deprecated Please use {@see getDisplayName()} method. |
| 191 | + */ |
129 | 192 | public function getTitle(): ?string
|
130 | 193 | {
|
131 |
| - return $this->title; |
| 194 | + return $this->getDisplayName(); |
| 195 | + } |
| 196 | + |
| 197 | + public function getDisplayName(): ?string |
| 198 | + { |
| 199 | + return $this->displayName; |
132 | 200 | }
|
133 | 201 |
|
134 | 202 | public function getDescription(): ?string
|
|
0 commit comments