1 | <?php |
||
7 | class RelationshipMethodNameGenerator extends AbstractNameGenerator { |
||
8 | |||
9 | /** |
||
10 | * Returns the method name in studly case to prefix with add/get/set/remove |
||
11 | * and call it on a model class |
||
12 | * |
||
13 | * @param Relationship $relationship |
||
14 | * @return string |
||
15 | */ |
||
16 | public function generateMethodName(Relationship $relationship) { |
||
27 | |||
28 | /** |
||
29 | * Returns the plural method name in studly case to prefix with add/get/set/remove |
||
30 | * and call it on a model class |
||
31 | * |
||
32 | * @param Relationship $relationship |
||
33 | * @return string |
||
34 | */ |
||
35 | public function generatePluralMethodName(Relationship $relationship) { |
||
46 | |||
47 | /** |
||
48 | * Returns the reverse method name in studly case to prefix with add/get/set/remove |
||
49 | * and call it on a model class |
||
50 | * |
||
51 | * @param Relationship $relationship |
||
52 | * @return string |
||
53 | */ |
||
54 | public function generateReverseMethodName(Relationship $relationship) { |
||
65 | |||
66 | /** |
||
67 | * Returns the reverse plural method name in studly case to prefix with add/get/set/remove |
||
68 | * and call it on a model class |
||
69 | * |
||
70 | * @param Relationship $relationship |
||
71 | * @return string |
||
72 | */ |
||
73 | public function generateReversePluralMethodName(Relationship $relationship) { |
||
84 | } |
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: