Conditions | 3 |
Paths | 3 |
Total Lines | 17 |
Code Lines | 9 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
23 | public function generate(TaxonInterface $taxon, $locale = null) |
||
24 | { |
||
25 | $name = $taxon->getTranslation($locale)->getName(); |
||
|
|||
26 | |||
27 | Assert::notEmpty($name, 'Cannot generate slug without a name.'); |
||
28 | |||
29 | $slug = $this->transliterate($name); |
||
30 | |||
31 | $parentTaxon = $taxon->getParent(); |
||
32 | if (null === $parentTaxon) { |
||
33 | return $slug; |
||
34 | } |
||
35 | |||
36 | $parentSlug = $parentTaxon->getTranslation($locale)->getSlug() ?: $this->generate($parentTaxon, $locale); |
||
37 | |||
38 | return $parentSlug . '/' . $slug; |
||
39 | } |
||
40 | |||
52 |
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: