You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+7-5
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@
8
8
9
9
Since the release of PHP 8.0 more and more libraries, frameworks and tools have been updated to use attributes instead of annotations in PHPDocs.
10
10
11
-
However, static analysis tools like PHPStan or Psalm or IDEs like PHPStorm or VS Code have not made this transition to attributes and they still rely on annotations in PHPDocs for a lot of their functionality.
11
+
However, static analysis tools like PHPStan or Psalm or IDEs like PhpStorm or VS Code have not made this transition to attributes and they still rely on annotations in PHPDocs for a lot of their functionality.
12
12
13
13
This library aims to provide a set of PHP attributes which could replace the most commonly used annotations accepted by these tools and will aim to provide related repositories with the extensions or plugins that would allow these attributes to be used in these tools.
14
14
@@ -84,8 +84,10 @@ These are the available attributes and their corresponding PHPDoc annotations:
A plugin that fully supports these attributes will need to be created. Until this is ready you can get partial support by installing PHPStan, our PHPStan extension and enabling PHPStan support in PHPStorm (as described [here](https://www.jetbrains.com/help/phpstorm/using-phpstan.html)). You will then be able to see errors and messages related to these attributes in your code.
115
+
## PhpStorm Support
116
+
A plugin that fully supports these attributes will need to be created. Until this is ready you can get partial support by installing PHPStan, our PHPStan extension and enabling PHPStan support in PhpStorm (as described [here](https://www.jetbrains.com/help/phpstorm/using-phpstan.html)). You will then be able to see errors and messages related to these attributes in your code.
115
117
116
-
Alternatively install Psalm, our Psalm extension and enable Psalm support in PHPStorm (as described [here](https://www.jetbrains.com/help/phpstorm/using-psalm.html))
118
+
Alternatively install Psalm, our Psalm extension and enable Psalm support in PhpStorm (as described [here](https://www.jetbrains.com/help/phpstorm/using-psalm.html))
117
119
118
120
## VS Code Support
119
121
An extension that fully supports these attributes will need to be created. Until this is ready you can get partial support by installing PHPStan, our PHPStan extension and a VS Code extension that supports PHPStan (for example [this one](https://github.com/SanderRonde/phpstan-vscode)). When you enable the extension you will be able to see errors and messages related to these attributes in your code.
This attribute is the equivalent of the `@type` annotation and is used to define new aliases for types and they are scoped to the class where they are defined.
4
+
5
+
We are not using the `Type` name for this attribute because that name is used for the attribute which is equivalent to the `@var` annotation. But if you prefer, you can use the `Type` attribute instead of this one to define these aliases, but we don't recommend it.
6
+
7
+
## Arguments
8
+
9
+
The attribute accepts one or more strings which describe the aliased type. 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.
10
+
11
+
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 `@type` annotation.
12
+
13
+
The arguments can be named arguments and the type is aliased with the name of the argument.
14
+
15
+
They can also be unnamed arguments with a string that contains both the name of the alias and the aliased type, but we recommend using named arguments.
16
+
17
+
If the class has more than one type alias that we want to specify, the aliases can either be declared as a list of strings for a single `DefineType` attribute or as a list of `DefineType` attributes (or even a combination of both, though we don't expect this to be actually used).
18
+
19
+
## Example usage
20
+
21
+
```php
22
+
<?php
23
+
24
+
use PhpStaticAnalysis\Attributes\DefineType;
25
+
26
+
#[DefineType(UserAddress: 'array{street: string, city: string, zip: string}')] // this is an alias of the listed type
This attribute is the equivalent of the `@import-type` annotation and is used to import aliases for types from another class.
4
+
5
+
## Arguments
6
+
7
+
The attribute accepts one or more strings which list the class from which the aliased type neeeds to be imported. 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.
8
+
9
+
The arguments can be named arguments and the type is aliased with the name of the argument and the value is the name of the class from which it needs to be imported.
10
+
11
+
They can also be unnamed arguments with a string that contains both the name of the alias and the name of the class from which it needs to be imported, but we recommend using named arguments.
12
+
13
+
If the class has more than one type alias that we want to specify, the aliases can either be declared as a list of strings for a single `ImportType` attribute or as a list of `ImportType` 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\ImportType;
21
+
22
+
#[ImportType(UserAddress: User::class)] // this type is imported from the user class
Copy file name to clipboardExpand all lines: doc/Type.md
+5
Original file line number
Diff line number
Diff line change
@@ -6,19 +6,24 @@ We could not use `Var` for the name of this attribute because `var` is a reserve
6
6
7
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
8
9
+
This attribute can also be used instead of using the `DefineType` attribute to specify an alias for a type, replacing the `@type` annotation.
10
+
9
11
## Arguments
10
12
11
13
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.
12
14
13
15
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.
14
16
17
+
If used to replace the `@type` tag, the value should be a string that includes both the name of the alias and the type being aliased.
0 commit comments