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 |
||
| 16 | class EntityReferenceTest extends DriverFieldKernelTestBase |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * {@inheritdoc} |
||
| 21 | */ |
||
| 22 | public static $modules = ['entity_test', 'field', 'user', 'node']; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Machine name of the field type being tested. |
||
| 26 | * |
||
| 27 | * @string |
||
| 28 | */ |
||
| 29 | protected $fieldType = 'entity_reference'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Entities available to reference. |
||
| 33 | * |
||
| 34 | * @array |
||
| 35 | */ |
||
| 36 | protected $entities = []; |
||
| 37 | |||
| 38 | protected function setUp() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Test referencing a node using its title. |
||
| 64 | */ |
||
| 65 | public function testNodeReferenceSingle() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Test referencing multiple nodes using their title. |
||
| 79 | */ |
||
| 80 | public function testNodeReferenceMultiple() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Test referencing a user by name (they don't have a label key or bundles, |
||
| 102 | * so their driver entity plugin has to say what field to reference by). |
||
| 103 | */ |
||
| 104 | public function testUserReferenceByName() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Test referencing a user by mail (they don't have a label key or bundles, |
||
| 114 | * so their driver entity plugin has to say what field to reference by). |
||
| 115 | */ |
||
| 116 | public function testUserReferenceByMail() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Test referencing a role by label. |
||
| 128 | * Roles have string id's so can be referenced by label or id. |
||
| 129 | */ |
||
| 130 | View Code Duplication | public function testRoleReferenceByLabel() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Test referencing a role by id. |
||
| 142 | * Roles have string id's so can be referenced by label or id. |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function testRoleReferenceById() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Test referencing a role by id without underscores. |
||
| 156 | * Roles have string id's so can be referenced by label or id. |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function testRoleReferenceByIdWithoutUnderscores() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Test referencing a role by id case insensitively. |
||
| 170 | * Roles have string id's so can be referenced by label or id. |
||
| 171 | */ |
||
| 172 | |||
| 173 | /* |
||
| 174 | * It would be good to have case insensitive references, but it seems that |
||
| 175 | config entityqueries are intrinsically case sensitive. Test commented out |
||
| 176 | until issue is resolved. |
||
| 177 | |||
| 178 | public function testRoleReferenceCaseInsensitive() { |
||
| 179 | $this->installEntitySchema('user_role'); |
||
| 180 | $role = Role::create(['id' => 'test_role', 'label' => 'Test role label'])->save(); |
||
| 181 | $this->fieldStorageSettings = ['target_type' => 'user_role']; |
||
| 182 | $field = ['TEST_role']; |
||
| 183 | $fieldExpected = [['target_id' => 'test_role']]; |
||
| 184 | $this->assertCreatedWithField($field, $fieldExpected); |
||
| 185 | } |
||
| 186 | */ |
||
| 187 | } |
||
| 188 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.