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 | * Exempt from strict schema checking. |
||
15 | * |
||
16 | * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker |
||
17 | * |
||
18 | * @var bool |
||
19 | */ |
||
20 | protected $strictConfigSchema = FALSE; |
||
21 | |||
22 | /** |
||
23 | * Modules to enable. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | public static $modules = [ |
||
28 | 'media', |
||
29 | 'media_entity', |
||
30 | 'media_entity_slideshow', |
||
31 | 'node', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * The test media bundle. |
||
36 | * |
||
37 | * @var \Drupal\media_entity\MediaBundleInterface |
||
38 | */ |
||
39 | protected $testBundle; |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | View Code Duplication | protected function setUp() { |
|
|
|||
45 | parent::setUp(); |
||
46 | $this->testBundle = $this->container->get('entity_type.manager')->getStorage('media_bundle')->load('gallery'); |
||
47 | |||
48 | $adminUser = $this->drupalCreateUser([ |
||
49 | 'view media', |
||
50 | 'create media', |
||
51 | 'update media', |
||
52 | 'update any media', |
||
53 | 'delete media', |
||
54 | 'delete any media', |
||
55 | 'access media overview', |
||
56 | 'access gallery_media_library entity browser pages', |
||
57 | ]); |
||
58 | $this->drupalLogin($adminUser); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Tests gallery media bundle creation from config files. |
||
63 | */ |
||
64 | View Code Duplication | public function testMediaBundleCreationFromModule() { |
|
76 | |||
77 | /** |
||
78 | * Tests thumbnail of the gallery item. |
||
79 | */ |
||
80 | public function testGalleryItemThumbnail() { |
||
118 | |||
119 | /** |
||
120 | * Tests that gallery option isn't available in gallery create bundle filters. |
||
121 | */ |
||
122 | public function testGalleryOption() { |
||
123 | // Open the media library iframe used on add gallery page. |
||
124 | $this->drupalGet('entity-browser/modal/gallery_media_library'); |
||
125 | $this->assertNoOption('edit-bundle-1', 'gallery'); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Adds image type item. |
||
130 | */ |
||
131 | public function addImageItem() { |
||
132 | // Let's add image first. |
||
133 | $name = $this->randomMachineName(); |
||
134 | $testImage = current($this->drupalGetTestFiles('image')); |
||
135 | $file_path = $this->container->get('file_system')->realpath($testImage->uri); |
||
136 | $edit = [ |
||
137 | 'name[0][value]' => $name, |
||
138 | 'files[field_image_0]' => $file_path, |
||
139 | ]; |
||
140 | // Save the image. |
||
141 | $this->drupalPostForm('media/add/image', $edit, t('Save and publish')); |
||
142 | $this->drupalPostForm(NULL, ['field_image[0][alt]' => $name], t('Save and publish')); |
||
143 | // Obtain the image id. |
||
144 | $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'image')->sort('created', 'DESC')->execute(); |
||
145 | $media_id = reset($media_id); |
||
146 | $edit['id'] = $media_id; |
||
147 | |||
148 | return $edit; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Adds video type item. |
||
153 | */ |
||
154 | public function addVideoItem() { |
||
167 | |||
168 | /** |
||
169 | * Loads the media entity item. |
||
170 | * |
||
171 | * @param int $id |
||
172 | * The id of the item. |
||
173 | * |
||
174 | * @return \Drupal\media_entity\MediaInterface |
||
175 | * The media entity item. |
||
176 | */ |
||
177 | public function loadMediaItem($id) { |
||
183 | |||
184 | } |
||
185 |
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.