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 |
||
| 12 | class ImageBundleTest extends WebTestBase { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Modules to enable. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | public static $modules = ['media', 'media_entity', 'media_entity_image', 'image', 'node']; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The test media bundle. |
||
| 23 | * |
||
| 24 | * @var \Drupal\media_entity\MediaBundleInterface |
||
| 25 | */ |
||
| 26 | protected $testBundle; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | protected function setUp() { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Tests image media bundle creation from config files. |
||
| 49 | */ |
||
| 50 | View Code Duplication | public function testMediaBundleCreationFromModule() { |
|
|
|
|||
| 51 | $type_configuration = [ |
||
| 52 | 'source_field' => 'field_image', |
||
| 53 | 'gather_exif' => false |
||
| 54 | ]; |
||
| 55 | |||
| 56 | $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.'); |
||
| 57 | $this->assertEqual($this->testBundle->get('label'), 'Image', 'Correct label detected.'); |
||
| 58 | $this->assertEqual($this->testBundle->get('description'), 'Use Image for uploading locally hosted images.', 'Correct description detected.'); |
||
| 59 | $this->assertEqual($this->testBundle->get('type'), 'image', 'Correct plugin ID detected.'); |
||
| 60 | $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.'); |
||
| 61 | $this->assertEqual($this->testBundle->get('field_map'), [], 'Correct field map detected.'); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Tests item creation and thumbnail |
||
| 66 | */ |
||
| 67 | public function testMediaBundleItemCreation() { |
||
| 95 | } |
||
| 96 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.