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 |
||
9 | class UserManagerTest extends WebTest |
||
10 | { |
||
11 | |||
12 | /** @var UserManager */ |
||
13 | protected $manager; |
||
14 | |||
15 | public function setUp() |
||
16 | { |
||
17 | parent::setUp(); |
||
18 | $this->manager = $this->getContainer()->get('starkerxp_user.manager.user'); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * @group user |
||
23 | * @group manager |
||
24 | */ |
||
25 | public function testFindAll() |
||
26 | { |
||
27 | $this->loadFixtureFiles(['@StarkerxpUserBundle/Tests/DataFixtures/UserManager/UserManager.yml',]); |
||
28 | $this->assertCount(10, $this->manager->findAll()); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @group user |
||
33 | * @group manager |
||
34 | */ |
||
35 | public function testInsertNewUser() |
||
36 | { |
||
37 | $this->loadFixtureFiles(['@StarkerxpUserBundle/Tests/DataFixtures/UserManager/DefaultUser.yml',]); |
||
38 | $user = new User('[email protected]', ["ROLE_SUPER_ADMIN"]); |
||
39 | $user->setPlainPassword("azerty"); |
||
40 | $this->manager->insert($user); |
||
41 | $this->assertCount(2, $this->manager->findAll()); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @group user |
||
46 | * @group manager |
||
47 | */ |
||
48 | View Code Duplication | public function testUpdateUser() |
|
58 | |||
59 | } |
||
60 |
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.