Skip to content

Commit c972a47

Browse files
Allow using the Type attribute to specify the return type of a function or method
1 parent 3da360a commit c972a47

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ And then install any needed extensions/plugins for the tools that you use.
9090

9191
## List of implemented attributes
9292

93-
These are the available attributes and their corresponding PHPDoc annotation:
94-
95-
| Attribute | PHPDoc Annotation |
96-
|---|-------------------|
97-
| [IsReadOnly](doc/IsReadOnly.md) | `@readonly` |
98-
| [Param](doc/Param.md) | `@param` |
99-
| [Returns](doc/Returns.md) | `@return` |
100-
| [Template](doc/Template.md) | `@template` |
101-
| [Type](doc/Type.md) | `@var` |
93+
These are the available attributes and their corresponding PHPDoc annotations:
94+
95+
| Attribute | PHPDoc Annotations |
96+
|---|--------------------|
97+
| [IsReadOnly](doc/IsReadOnly.md) | `@readonly` |
98+
| [Param](doc/Param.md) | `@param` |
99+
| [Returns](doc/Returns.md) | `@return` |
100+
| [Template](doc/Template.md) | `@template` |
101+
| [Type](doc/Type.md) | `@var` `@return` |
102102

doc/Returns.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This attribute is the equivalent of the `@return` annotation. It can be used on
44

55
We could not use `Return` for the name of this attribute because `return` is a reserved word in PHP.
66

7+
Instead of using this attribute, you can also use the `Type` aatribute which provides equivalent functionality.
8+
79
## Arguments
810

911
The attribute accepts a string which describes the type of the value returned by the function or method. The attribute itself does not have a knowledge of which types are valid and which are not and this will depend on the implementation for each particular tool.
@@ -22,7 +24,7 @@ class ReturnsExample
2224
#[Returns('Array<string>')]
2325
public function getNames(): array
2426
{
25-
...
27+
return ['Fred', 'John'];
2628
}
2729
}
2830
```

doc/Type.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# `Type` Attribute
22

3-
This attribute is the equivalent of the `@var` annotation *when used for class properties or class constants*.
3+
This attribute is the equivalent of the `@var` annotation *when used for class properties or class constants* and is used to specify the type of this property or constant.
44

55
We could not use `Var` for the name of this attribute because `var` is a reserved word in PHP. By using `Type` we emphasize that this attribute is used to declare the type of a class property or constant.
66

7+
This attribute can also be used instead of using the `Returns` attribute to specify the type of the return value of a function or method, replacing the `@return` annotation.
8+
79
## Arguments
810

9-
The attribute accepts a string which describes the type of the class property or constant. The attribute itself does not have a knowledge of which types are valid and which are not and this will depend on the implementation for each particular tool.
11+
The attribute accepts a string which describes the type of the class property, constant or return value. The attribute itself does not have a knowledge of which types are valid and which are not and this will depend on the implementation for each particular tool.
1012

1113
We expect that the attribute will be able to accept both basic types like `string` or `array` and more advanced types like `array<string>` or `Collection<int>`. We aim to accept all the types accepted by static analysis tools for the `@var` annotation.
1214

@@ -24,7 +26,12 @@ class TypeExample
2426

2527
#[Type('Array<int>')]
2628
private array $nums;
27-
29+
30+
#[Type('Array<int>')]
31+
private function returnsArray()
32+
{
33+
return [1];
34+
}
2835
...
2936
}
3037
```

src/Type.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
#[Attribute(
1010
Attribute::TARGET_CLASS_CONSTANT |
11-
Attribute::TARGET_PROPERTY
11+
Attribute::TARGET_PROPERTY |
12+
Attribute::TARGET_METHOD |
13+
Attribute::TARGET_FUNCTION
1214
)]
1315
final class Type
1416
{

tests/TypeTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public function testMultiplePropertyTypes(): void
6060
$this->assertTrue($errorThrown);
6161
}
6262

63+
public function testMethodType(): void
64+
{
65+
$this->assertEquals('string', $this->methodType());
66+
}
67+
68+
public function testFunctionType(): void
69+
{
70+
$this->assertEquals('string', functionType());
71+
}
72+
6373
private function propertyType(): string
6474
{
6575
return $this->getType('property');
@@ -85,9 +95,27 @@ private function multiplePropertyTypes(): string
8595
return $this->getType('propertyWithMultipleTypes');
8696
}
8797

98+
#[Type('string')]
99+
private function methodType(): string
100+
{
101+
return $this->getMethodType(__FUNCTION__);
102+
}
103+
88104
private function getType(string $propertyName): string
89105
{
90106
$reflection = new ReflectionProperty($this, $propertyName);
107+
return self::getTypeFromReflection($reflection);
108+
}
109+
110+
private function getMethodType(string $methodName): string
111+
{
112+
$reflection = new ReflectionMethod($this, $methodName);
113+
return self::getTypeFromReflection($reflection);
114+
}
115+
116+
public static function getTypeFromReflection(
117+
ReflectionProperty | ReflectionMethod | ReflectionFunction $reflection
118+
): string {
91119
$attributes = $reflection->getAttributes();
92120
$type = '';
93121
foreach ($attributes as $attribute) {
@@ -100,3 +128,10 @@ private function getType(string $propertyName): string
100128
return $type;
101129
}
102130
}
131+
132+
#[Type('string')]
133+
function functionType(): string
134+
{
135+
$reflection = new ReflectionFunction(__FUNCTION__);
136+
return TypeTest::getTypeFromReflection($reflection);
137+
}

0 commit comments

Comments
 (0)