Skip to content

Commit 1188f18

Browse files
Add Method attribute
1 parent 58749df commit 1188f18

8 files changed

+94
-4
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
9595
| Attribute | PHPDoc Annotations |
9696
|-------------------------------------------------------|---------------------------|
9797
| [IsReadOnly](doc/IsReadOnly.md) | `@readonly` |
98+
| [Method](doc/Method.md) | `@method` |
9899
| [Param](doc/Param.md) | `@param` |
99100
| [Property](doc/Property.md) | `@property` `@var` |
100101
| [PropertyRead](doc/PropertyRead.md) | `@property-read` |

Diff for: doc/Method.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# `Method` Attribute
2+
3+
This attribute is the equivalent of the `@method` annotation and is used to specify the methods defined through magic `__call` methods, including methods called in a parent class.
4+
5+
## Arguments
6+
7+
The attribute accepts one or more strings which describe the signature of these methods. The attribute itself does not have a knowledge of which signatures are valid and which are not and this will depend on the implementation for each particular tool.
8+
9+
We expect that the attribute will be able to accept all the signatures accepted by static analysis tools for the `@method` annotation.
10+
11+
The arguments need to be unnamed arguments.
12+
13+
If the class has more than one method that we want to specify, the types for the different properties can either be declared as a list of strings for a single `Method` attribute or as a list of `Method` attributes (or even a combination of both, though we don't expect this to be actually used).
14+
15+
## Example usage
16+
17+
```php
18+
<?php
19+
20+
use PhpStaticAnalysis\Attributes\Method;
21+
22+
#[Method('string getString()')]
23+
#[Method(
24+
'void setString(string $text)',
25+
'static string staticGetter()',
26+
)]
27+
class MethodExample
28+
{
29+
...
30+
}
31+
```

Diff for: doc/PropertyRead.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This attribute is the equivalent of the `@property-read` annotation and is used
66

77
The attribute accepts one or more strings which describe the type of the properties. 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.
88

9-
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 `@property` annotation.
9+
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 `@property-read` annotation.
1010

1111
The arguments can be named arguments and the type is applied to the properties with the same name in the class.
1212

Diff for: doc/PropertyWrite.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This attribute is the equivalent of the `@property-write` annotation and is used
66

77
The attribute accepts one or more strings which describe the type of the properties. 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.
88

9-
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 `@property` annotation.
9+
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 `@property-write` annotation.
1010

1111
The arguments can be named arguments and the type is applied to the properties with the same name in the class.
1212

Diff for: src/Method.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStaticAnalysis\Attributes;
6+
7+
use Attribute;
8+
9+
#[Attribute(
10+
Attribute::TARGET_CLASS |
11+
Attribute::IS_REPEATABLE
12+
)]
13+
final class Method
14+
{
15+
public function __construct(
16+
string ...$params
17+
) {
18+
}
19+
}

Diff for: tests/MethodTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpStaticAnalysis\Attributes\Method;
6+
use PHPUnit\Framework\TestCase;
7+
8+
#[Method('string getString()')]
9+
#[Method(
10+
'void setString(string $text)',
11+
'static string staticGetter()',
12+
)]
13+
class MethodTest extends TestCase
14+
{
15+
public function testClassMethods(): void
16+
{
17+
$reflection = new ReflectionClass($this);
18+
$this->assertEquals([
19+
'string getString()',
20+
'void setString(string $text)',
21+
'static string staticGetter()',
22+
], self::getMethodsFromReflection($reflection));
23+
}
24+
25+
public static function getMethodsFromReflection(
26+
ReflectionClass $reflection
27+
): array {
28+
$attributes = $reflection->getAttributes();
29+
$methods = [];
30+
foreach ($attributes as $attribute) {
31+
if ($attribute->getName() === Method::class) {
32+
$attribute->newInstance();
33+
$methods = array_merge($methods, $attribute->getArguments());
34+
}
35+
}
36+
37+
return $methods;
38+
}
39+
}

Diff for: tests/PropertyReadTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testClassProperties(): void
2525
}
2626

2727
public static function getPropertiesFromReflection(
28-
ReflectionProperty | ReflectionClass $reflection
28+
ReflectionClass $reflection
2929
): array {
3030
$attributes = $reflection->getAttributes();
3131
$properties = [];

Diff for: tests/PropertyWriteTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testClassProperties(): void
2525
}
2626

2727
public static function getPropertiesFromReflection(
28-
ReflectionProperty | ReflectionClass $reflection
28+
ReflectionClass $reflection
2929
): array {
3030
$attributes = $reflection->getAttributes();
3131
$properties = [];

0 commit comments

Comments
 (0)