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 | * 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_image', |
||
31 | 'image', |
||
32 | 'node', |
||
33 | 'editor', |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * The test media bundle. |
||
38 | * |
||
39 | * @var \Drupal\media_entity\MediaBundleInterface |
||
40 | */ |
||
41 | protected $testBundle; |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | View Code Duplication | protected function setUp() { |
|
|
|||
47 | parent::setUp(); |
||
48 | $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('image'); |
||
49 | |||
50 | $adminUser = $this->drupalCreateUser([ |
||
51 | 'view media', |
||
52 | 'create media', |
||
53 | 'update media', |
||
54 | 'update any media', |
||
55 | 'delete media', |
||
56 | 'delete any media', |
||
57 | 'access media overview', |
||
58 | ]); |
||
59 | $this->drupalLogin($adminUser); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Tests image media bundle creation from config files. |
||
64 | */ |
||
65 | View Code Duplication | public function testMediaBundleCreationFromModule() { |
|
78 | |||
79 | /** |
||
80 | * Tests item creation and thumbnail. |
||
81 | */ |
||
82 | public function testMediaBundleItemCreation() { |
||
110 | |||
111 | } |
||
112 |
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.