| 1 | <?php |
||
| 10 | trait HasComments |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Return all comments for this model. |
||
| 14 | * |
||
| 15 | * @return MorphMany |
||
| 16 | */ |
||
| 17 | public function comments() |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Attach a comment to this model. |
||
| 24 | * |
||
| 25 | * @param string $comment |
||
| 26 | * @return \Illuminate\Database\Eloquent\Model |
||
| 27 | */ |
||
| 28 | public function comment(string $comment) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Attach a comment to this model as a specific user. |
||
| 35 | * |
||
| 36 | * @param Model|null $user |
||
| 37 | * @param string $comment |
||
| 38 | * @return \Illuminate\Database\Eloquent\Model |
||
| 39 | */ |
||
| 40 | public function commentAsUser(?Model $user, string $comment) |
||
| 54 | |||
| 55 | } |
||
| 56 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.