Conditions | 3 |
Paths | 3 |
Total Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 12 |
Changes | 0 |
1 | <?php |
||
35 | public function translate(string $string) : string |
||
36 | { |
||
37 | $translation = new TranslationConfig( |
||
38 | $string, |
||
39 | $this->target, |
||
40 | $this->source |
||
41 | ); |
||
42 | |||
43 | try { |
||
44 | return $this->translator->getTranslation($translation)->getText(); |
||
|
|||
45 | } catch (RequestException $th) { |
||
46 | if ($th->getMessage() === '400 {"message":"Value for \'target_lang\' is not supported."}') { |
||
47 | throw LanguageCodeNotExist::throw($this->source, $this->target); |
||
48 | } |
||
49 | |||
50 | throw $th; |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 |
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: