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
+49-7Lines changed: 49 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,10 @@ This library provides attributes for extending the PHP language (e.g. adding `pa
4
4
The intention, at least initially, is that these extra language features are enforced by static analysis tools (such as Psalm, PHPStan and, ideally, PhpStorm) and NOT at runtime.
5
5
6
6
**Language feature added:**
7
-
-[package](#package)
8
-
-[friend](#friend)
9
-
-[sealed](#sealed)
7
+
-[Friend](#friend)
8
+
-[Package](#package)
9
+
-[Sealed](#sealed)
10
+
-[TestTag](#testtag)
10
11
11
12
12
13
### Contents
@@ -15,9 +16,10 @@ The intention, at least initially, is that these extra language features are enf
15
16
-[PHPStan](#phpstan)
16
17
-[Psalm](#psalm)
17
18
-[New Language Features](#new-language-features)
18
-
-[package](#package)
19
-
-[friend](#friend)
20
-
-[sealed](#sealed)
19
+
-[Package](#package)
20
+
-[Friend](#friend)
21
+
-[Sealed](#sealed)
22
+
-[TestTag](#testtag)
21
23
-[Further examples](#further-examples)
22
24
-[Contributing](#contributing)
23
25
@@ -148,7 +150,7 @@ namespace Bar {
148
150
**NOTES:**
149
151
150
152
- If adding the `#[Package]` to a method, this method MUST have public visibility.
151
-
- If a class is marked with `#[Package]` then all its public methods are treated as having protected visibility.
153
+
- If a class is marked with `#[Package]` then all its public methods are treated as having package visibility.
152
154
- This is currently limited to method calls (including `__construct`).
153
155
- Namespaces must match exactly. E.g. a package level method in `Foo\Bar` is only accessible from `Foo\Bar`. It is not accessible from `Foo` or `Foo\Bar\Baz`
154
156
@@ -229,6 +231,46 @@ class Failure extends Result {}
229
231
class AnotherClass extends Result {}
230
232
```
231
233
234
+
## TestTag
235
+
236
+
The `#[TestTag]` attribute is an idea borrowed from hardware testing. Methods marked with this attribute are only available to test code.
237
+
238
+
E.g.
239
+
240
+
```php
241
+
class Person {
242
+
243
+
#[TestTag]
244
+
public function setId(int $id)
245
+
{
246
+
$this->id = $id;
247
+
}
248
+
}
249
+
250
+
251
+
function updatePersonId(Person $person): void
252
+
{
253
+
$person->setId(10); // ERROR - not test code.
254
+
}
255
+
256
+
257
+
class PersonTest
258
+
{
259
+
public function setup(): void
260
+
{
261
+
$person = new Person();
262
+
$person->setId(10); // OK - This is test code.
263
+
}
264
+
}
265
+
```
266
+
267
+
NOTES:
268
+
- Methods with the`#[TestTag]` MUST have public visibility.
269
+
- For determining what is "test code" see the relevant plugin. E.g. the [PHPStan extension](https://github.com/DaveLiddament/phpstan-php-language-extensions) can be setup to either:
270
+
- Assume all classes that end `Test` is test code. See [className config option](https://github.com/DaveLiddament/phpstan-php-language-extensions#exclude-checks-on-class-names-ending-with-test).
271
+
- Assume all classes within a namespace is test code. See [namespace config option](https://github.com/DaveLiddament/phpstan-php-language-extensions#exclude-checks-based-on-test-namespace).
272
+
273
+
232
274
## Further examples
233
275
234
276
More detailed examples of how to use attributes is found in [examples](examples/).
0 commit comments