| 1 | <?php |
||
| 8 | class User extends Model |
||
| 9 | { |
||
| 10 | |||
| 11 | /** @var int */ |
||
| 12 | protected $id; |
||
| 13 | |||
| 14 | /** @var string */ |
||
| 15 | protected $username; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Create a new User given a username. |
||
| 19 | * @param string $username |
||
| 20 | */ |
||
| 21 | public function __construct($username) |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Get the username. |
||
| 28 | * @return string |
||
| 29 | */ |
||
| 30 | public function getUsername() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Get the user's ID on the given project. |
||
| 37 | * @param Project $project |
||
| 38 | * @return int |
||
| 39 | */ |
||
| 40 | public function getId(Project $project) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get a list of this user's groups on the given project. |
||
| 47 | */ |
||
| 48 | public function getGroups(Project $project) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the full URL to Special:UserRights for this user on the given project. |
||
| 54 | * @param Project $project |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | public function userRightsUrl(Project $project) |
||
| 62 | } |
||
| 63 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: