1 | <?php |
||
22 | class FrequentlyAskedQuestion implements FrequentlyAskedQuestionInterface |
||
23 | { |
||
24 | use SectionAssociationTrait; |
||
25 | use ToggleableTrait, |
||
26 | TranslatableTrait { |
||
27 | __construct as private initializeTranslationsCollection; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $id; |
||
34 | |||
35 | /** |
||
36 | * @var null|string |
||
37 | */ |
||
38 | protected $code; |
||
39 | |||
40 | /** |
||
41 | * @var null|int |
||
42 | */ |
||
43 | protected $position; |
||
44 | |||
45 | public function __construct() |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function getId(): int |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function getCode(): ?string |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function setCode(?string $code): void |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function getPosition(): ?int |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function setPosition(?int $position): void |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function getQuestion(): ?string |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function setQuestion(?string $question): void |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function getAnswer(): ?string |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function setAnswer(?string $answer): void |
||
122 | |||
123 | /** |
||
124 | * @return TranslationInterface|FrequentlyAskedQuestionTranslationInterface |
||
125 | */ |
||
126 | protected function getFrequentlyAskedQuestionTranslation(): TranslationInterface |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | protected function createTranslation(): TranslationInterface |
||
138 | } |
||
139 |
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 implementation 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 interface: