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 GalleryBundleTest extends WebTestBase { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Modules to enable. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | public static $modules = [ |
||
| 20 | 'media', |
||
| 21 | 'media_entity', |
||
| 22 | 'media_entity_slideshow', |
||
| 23 | 'node', |
||
| 24 | ]; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The test media bundle. |
||
| 28 | * |
||
| 29 | * @var \Drupal\media_entity\MediaBundleInterface |
||
| 30 | */ |
||
| 31 | protected $testBundle; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | View Code Duplication | protected function setUp() { |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Tests gallery media bundle creation from config files. |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function testMediaBundleCreationFromModule() { |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Tests thumbnail of the gallery item. |
||
| 70 | */ |
||
| 71 | public function testGalleryItemThumbnail() { |
||
| 72 | // Let's add one image and one video. |
||
| 73 | $imageItem = $this->addImageItem(); |
||
| 74 | $videoItem = $this->addVideoItem(); |
||
| 75 | |||
| 76 | $this->drupalGet('media/add/gallery'); |
||
| 77 | $edit = [ |
||
| 78 | 'name[0][value]' => 'Gallery item', |
||
| 79 | 'field_slide[0][target_id]' => $imageItem['name[0][value]'] . ' (' . $imageItem['id'] . ')', |
||
| 80 | 'field_slide[0][_weight]' => 0, |
||
| 81 | ]; |
||
| 82 | $this->drupalPostAjaxForm(NULL, $edit, 'field_slide_add_more'); |
||
| 83 | $edit['field_slide[1][target_id]'] = $videoItem['name[0][value]'] . ' (' . $videoItem['id'] . ')'; |
||
| 84 | $edit['field_slide[1][_weight]'] = 1; |
||
| 85 | $this->drupalPostForm(NULL, $edit, t('Save and publish')); |
||
| 86 | |||
| 87 | // Let's load all the media items. |
||
| 88 | $gallery_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute(); |
||
| 89 | $gallery = $this->loadMediaItem(reset($gallery_id)); |
||
| 90 | $image = $this->loadMediaItem($imageItem['id']); |
||
| 91 | $video = $this->loadMediaItem($videoItem['id']); |
||
| 92 | // Let's check thumbnail now. |
||
| 93 | $gallery_thumbnail = $gallery->getType()->thumbnail($gallery); |
||
| 94 | $image_thumbnail = $image->getType()->thumbnail($image); |
||
| 95 | $video_thumbnail = $video->getType()->thumbnail($video); |
||
| 96 | $this->assertEqual($gallery_thumbnail, $image_thumbnail, "Correct thumbnail detected."); |
||
| 97 | |||
| 98 | $this->drupalGet('media/add/gallery'); |
||
| 99 | $edit = [ |
||
| 100 | 'name[0][value]' => 'Gallery item 2', |
||
| 101 | 'field_slide[0][target_id]' => $videoItem['name[0][value]'] . ' (' . $videoItem['id'] . ')', |
||
| 102 | 'field_slide[0][_weight]' => 0, |
||
| 103 | ]; |
||
| 104 | $this->drupalPostAjaxForm(NULL, $edit, 'field_slide_add_more'); |
||
| 105 | $edit['field_slide[1][target_id]'] = $imageItem['name[0][value]'] . ' (' . $imageItem['id'] . ')'; |
||
| 106 | $edit['field_slide[1][_weight]'] = 1; |
||
| 107 | $this->drupalPostForm(NULL, $edit, t('Save and publish')); |
||
| 108 | |||
| 109 | // Let's check the thumbnail again. |
||
| 110 | $gallery_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute(); |
||
| 111 | $gallery = $this->loadMediaItem(reset($gallery_id)); |
||
| 112 | $gallery_thumbnail = $gallery->getType()->thumbnail($gallery); |
||
| 113 | $this->assertEqual($gallery_thumbnail, $video_thumbnail, "Correct thumbnail detected."); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Adds image type item. |
||
| 118 | */ |
||
| 119 | public function addImageItem() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Adds video type item. |
||
| 141 | */ |
||
| 142 | public function addVideoItem() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Loads the media entity item. |
||
| 158 | * |
||
| 159 | * @param int $id |
||
| 160 | * The id of the item. |
||
| 161 | * |
||
| 162 | * @return \Drupal\media_entity\MediaInterface |
||
| 163 | * The media entity item. |
||
| 164 | */ |
||
| 165 | public function loadMediaItem($id) { |
||
| 171 | |||
| 172 | } |
||
| 173 |
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.