Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 13 | class ConfigurableLanguageTest extends DriverEntityKernelTestBase |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * {@inheritdoc} |
||
| 18 | */ |
||
| 19 | public static $modules = ['language',]; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Machine name of the entity type being tested. |
||
| 23 | * |
||
| 24 | * @string |
||
| 25 | */ |
||
| 26 | protected $entityType = 'configurable_language'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Our entity is a config entity. |
||
| 30 | * |
||
| 31 | * @boolean |
||
| 32 | */ |
||
| 33 | protected $config = true; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Test that a language can be created and deleted. |
||
| 37 | */ |
||
| 38 | public function testLanguageCreateDelete() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Test that a language can be created and deleted. |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function testLanguageCreateDeleteByWrapper() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Test that a default label is provided based on langcode. |
||
| 89 | */ |
||
| 90 | public function testLanguageDefaultLabel() |
||
| 105 | } |
||
| 106 |
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: