1 | <?php |
||
23 | class Page implements PageInterface |
||
24 | { |
||
25 | use SectionAssociationTrait; |
||
26 | use ToggleableTrait; |
||
27 | use ProductAssociationTrait; |
||
28 | use TranslatableTrait { |
||
29 | __construct as protected initializeTranslationsCollection; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @var null|int |
||
34 | */ |
||
35 | protected $id; |
||
36 | |||
37 | /** |
||
38 | * @var null|string |
||
39 | */ |
||
40 | protected $code; |
||
41 | |||
42 | public function __construct() |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | public function getId(): ?int |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function setId(?int $id): void |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function getCode(): ?string |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function setCode(?string $code): void |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function getSlug(): ?string |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function setSlug(?string $slug): void |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function getMetaKeywords(): ?string |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function setMetaKeywords(?string $metaKeywords): void |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function getMetaDescription(): ?string |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function setMetaDescription(?string $metaDescription): void |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function getContent(): ?string |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function setContent(?string $content): void |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function getName(): ?string |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function setName(?string $name): void |
||
160 | |||
161 | /** |
||
162 | * @return PageTranslationInterface|TranslationInterface|null |
||
163 | */ |
||
164 | protected function getPageTranslation(): PageTranslationInterface |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | protected function createTranslation(): ?PageTranslationInterface |
||
176 | } |
||
177 |
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: