@@ -15,9 +15,11 @@ The intention, at least initially, is that these extra language features are enf
1515
1616** Language feature added:**
1717- [ Friend] ( #friend )
18+ - [ MustUseResult] ( #mustuseresult )
1819- [ NamespaceVisibility] ( #namespaceVisibility )
1920- [ InjectableVersion] ( #injectableVersion )
2021- [ Override] ( #override )
22+ - [ RestrictTraitTo] ( #restricttraitto )
2123- [ Sealed] ( #sealed )
2224- [ TestTag] ( #testtag )
2325
@@ -29,9 +31,11 @@ The intention, at least initially, is that these extra language features are enf
2931 - [ Psalm] ( #psalm )
3032- [ New Language Features] ( #new-language-features )
3133 - [ Friend] ( #friend )
34+ - [ MustUseResult] ( #mustuseresult )
3235 - [ NamespaceVisibility] ( #namespaceVisibility )
3336 - [ InjectableVersion] ( #injectableVersion )
3437 - [ Override] ( #override )
38+ - [ RestrictTraitTo] ( #restricttraitto )
3539 - [ Sealed] ( #sealed )
3640 - [ TestTag] ( #testtag )
3741 - Deprecated
@@ -129,7 +133,40 @@ $person = new Person();
129133 ```
130134- This is currently limited to method calls (including ` __construct ` ).
131135
136+ ## MustUseResult
132137
138+ Add #[ MustUseResult] attribute that can be used on methods. This enforces the result from the method call must be used.
139+
140+ E.g. if you have a class like this:
141+
142+ ``` php
143+
144+ class Money {
145+
146+ public function __construct(public readonly int $pence)
147+ {}
148+
149+ #[MustUseResult]
150+ public function add(int $pence): self
151+ {
152+ return new self($pence + $this->pence);
153+ }
154+ }
155+ ```
156+
157+ You might misuse the ` add ` method in this way:
158+
159+ ``` php
160+ $cost = new Money(5);
161+ $cost->add(6); // ERROR - This statement has no effect.
162+ ```
163+
164+ But this would be OK:
165+
166+ ``` php
167+ $cost = new Money(5);
168+ $updatedCost = $cost->add(6); // OK - The return from add method is being used.
169+ ```
133170
134171## NamespaceVisibility
135172
@@ -382,6 +419,32 @@ NOTE:
382419- If you are using PHP 8.3 then use the real ` #[Override] ` attribute.
383420- This implementation doesn't consider traits.
384421
422+ ## RestrictTraitTo
423+
424+ This limits the use of a Trait to only be used by a specified class of a child of that class.
425+
426+ E.g. this trait is limited to classes that are or extend ` Controller `
427+
428+ ``` php
429+ #[RestrictTraitTo(Controller::class)]
430+ trait ControllerHelpers {}
431+ ```
432+
433+ This would be allowed:
434+ ``` php
435+ class LoginController extends Controller {
436+ use ControllerHelpers;
437+ }
438+ ```
439+
440+ But this would NOT be allowed:
441+ ``` php
442+ class Repository {
443+ use ControllerHelpers;
444+ }
445+ ```
446+
447+
385448## Sealed
386449
387450This is inspired by the rejected [ sealed classes RFC] ( https://wiki.php.net/rfc/sealed_classes )
0 commit comments