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 DocumentBundleTest 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_document', |
||
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 | protected function setUp() { |
||
59 | |||
60 | /** |
||
61 | * Tests document media bundle creation from config files. |
||
62 | */ |
||
63 | View Code Duplication | public function testMediaBundleCreationFromModule() { |
|
79 | |||
80 | /** |
||
81 | * Tests thumbnails of the document items. |
||
82 | */ |
||
83 | public function testDocumentItemThumbnail() { |
||
84 | // Array of test files and corresponding file icons. |
||
85 | $files = [ |
||
86 | 'Test.pdf' => 'public://media-icons/generic/application-pdf.png', |
||
87 | 'Test.doc' => 'public://media-icons/generic/application-msword.png', |
||
88 | 'Test.docx' => 'public://media-icons/generic/application-vnd.openxmlformats-officedocument.wordprocessingml.document.png', |
||
89 | 'Test.ods' => 'public://media-icons/generic/application-vnd.oasis.opendocument.spreadsheet.png', |
||
90 | 'Test.odt' => 'public://media-icons/generic/application-vnd.oasis.opendocument.text.png', |
||
91 | 'Test.ott' => 'public://media-icons/generic/application-vnd.oasis.opendocument.text-template.png', |
||
92 | 'Test.ppt' => 'public://media-icons/generic/application-vnd.ms-powerpoint.png', |
||
93 | 'Test.pptx' => 'public://media-icons/generic/application-vnd.openxmlformats-officedocument.presentationml.presentation.png', |
||
94 | 'Test.rtf' => 'public://media-icons/generic/application-rtf.png', |
||
95 | 'Test.txt' => 'public://media-icons/generic/text-plain.png', |
||
96 | 'Test.xls' => 'public://media-icons/generic/application-vnd.ms-excel.png', |
||
97 | 'Test.xlsx' => 'public://media-icons/generic/application-vnd.openxmlformats-officedocument.spreadsheetml.sheet.png', |
||
98 | ]; |
||
99 | |||
100 | foreach ($files as $fileName => $thumbnail) { |
||
101 | $file = drupal_get_path('module', 'media') . '/files/' . $fileName; |
||
102 | $name = $this->randomMachineName(); |
||
103 | $this->drupalGet('media/add/document'); |
||
104 | $edit = [ |
||
105 | 'files[field_document_0]' => $file, |
||
106 | ]; |
||
107 | $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button"); |
||
108 | $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value')); |
||
109 | $edit = [ |
||
110 | 'name[0][value]' => $name, |
||
111 | 'form_id' => 'media_document_form', |
||
112 | 'field_document[0][fids]' => $fid, |
||
113 | 'field_document[0][display]' => 1, |
||
114 | ]; |
||
115 | $this->drupalPostForm(NULL, $edit, t('Save and publish')); |
||
116 | $recentThumbnail = $this->getMostRecentThumbnail(); |
||
117 | $this->assertEqual($thumbnail, $recentThumbnail, "Correct thumbnail detected for " . $fileName); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Returns the thumbnail of the most recent document. |
||
123 | * |
||
124 | * @return string |
||
125 | * Path of the thumbnail. |
||
126 | */ |
||
127 | public function getMostRecentThumbnail() { |
||
134 | |||
135 | } |
||
136 |
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.