1 | <?php |
||
24 | class Page implements PageInterface |
||
25 | { |
||
26 | use ToggleableTrait; |
||
27 | use ProductsAwareTrait; |
||
28 | use SectionableTrait; |
||
29 | use TimestampableTrait; |
||
30 | use TranslatableTrait { |
||
31 | __construct as protected initializeTranslationsCollection; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @var null|int |
||
36 | */ |
||
37 | protected $id; |
||
38 | |||
39 | /** |
||
40 | * @var null|string |
||
41 | */ |
||
42 | protected $code; |
||
43 | |||
44 | public function __construct() |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function getId(): ?int |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function setId(?int $id): void |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function getCode(): ?string |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function setCode(?string $code): void |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function getSlug(): ?string |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function setSlug(?string $slug): void |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function getMetaKeywords(): ?string |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function setMetaKeywords(?string $metaKeywords): void |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function getMetaDescription(): ?string |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function setMetaDescription(?string $metaDescription): void |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function getContent(): ?string |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function setContent(?string $content): void |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function getName(): ?string |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | public function setName(?string $name): void |
||
164 | |||
165 | /** |
||
166 | * @return PageTranslationInterface|TranslationInterface|null |
||
167 | */ |
||
168 | protected function getPageTranslation(): PageTranslationInterface |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | protected function createTranslation(): ?PageTranslationInterface |
||
180 | } |
||
181 |
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: