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 |
||
13 | class AuthManagerTest extends BaseTestCase |
||
14 | { |
||
15 | |||
16 | /** @var AuthManager $authManager */ |
||
17 | protected $authManager; |
||
18 | |||
19 | /** |
||
20 | * @throws \Potievdev\SlimRbac\Exception\CyclicException |
||
21 | * @throws \Potievdev\SlimRbac\Exception\DatabaseException |
||
22 | * @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
||
23 | * @throws \Doctrine\ORM\Query\QueryException |
||
24 | */ |
||
25 | public function setUp() |
||
52 | |||
53 | public function successCasesProvider() |
||
62 | |||
63 | /** |
||
64 | * Testing has permission cases |
||
65 | * @param integer $userId user id |
||
66 | * @param string $roleOrPermission role or permission name |
||
67 | * @throws \Doctrine\ORM\Query\QueryException |
||
68 | * @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException |
||
69 | * @dataProvider successCasesProvider |
||
70 | */ |
||
71 | public function testCheckAccessSuccessCases($userId, $roleOrPermission) |
||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | public function failCasesProvider() |
||
88 | |||
89 | /** |
||
90 | * Testing not have permission cases |
||
91 | * @param integer $userId user id |
||
92 | * @param string $roleOrPermission role or permission name |
||
93 | * @throws \Doctrine\ORM\Query\QueryException |
||
94 | * @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException |
||
95 | * @dataProvider failCasesProvider |
||
96 | */ |
||
97 | public function testCheckAccessFailureCases($userId, $roleOrPermission) |
||
101 | |||
102 | /** |
||
103 | * Testing adding not unique permission |
||
104 | * @expectedException \Potievdev\SlimRbac\Exception\NotUniqueException |
||
105 | * @throws \Potievdev\SlimRbac\Exception\DatabaseException |
||
106 | * @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
||
107 | */ |
||
108 | public function testCheckAddingNotUniquePermission() |
||
113 | |||
114 | /** |
||
115 | * Testing adding not unique role |
||
116 | * @expectedException \Potievdev\SlimRbac\Exception\NotUniqueException |
||
117 | * @throws \Potievdev\SlimRbac\Exception\DatabaseException |
||
118 | * @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
||
119 | */ |
||
120 | public function testCheckAddingNonUniqueRole() |
||
125 | |||
126 | /** |
||
127 | * @expectedException \Potievdev\SlimRbac\Exception\CyclicException |
||
128 | * @throws \Potievdev\SlimRbac\Exception\CyclicException |
||
129 | * @throws \Potievdev\SlimRbac\Exception\DatabaseException |
||
130 | * @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
||
131 | * @throws \Doctrine\ORM\Query\QueryException |
||
132 | */ |
||
133 | public function testCheckCyclicException() |
||
144 | |||
145 | /** |
||
146 | * Testing creating permission |
||
147 | */ |
||
148 | public function testCheckCreatingPermission() |
||
157 | |||
158 | /** |
||
159 | * Testing creating role |
||
160 | */ |
||
161 | public function testCheckCreatingRole() |
||
171 | |||
172 | /** |
||
173 | * @throws \Potievdev\SlimRbac\Exception\DatabaseException |
||
174 | * @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
||
175 | * @expectedException \Potievdev\SlimRbac\Exception\NotUniqueException |
||
176 | */ |
||
177 | View Code Duplication | public function testCheckDoubleAssigningPermissionToSameRole() |
|
193 | |||
194 | /** |
||
195 | * @throws \Doctrine\ORM\Query\QueryException |
||
196 | * @throws \Potievdev\SlimRbac\Exception\CyclicException |
||
197 | * @throws \Potievdev\SlimRbac\Exception\DatabaseException |
||
198 | * @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
||
199 | * @expectedException \Potievdev\SlimRbac\Exception\NotUniqueException |
||
200 | */ |
||
201 | View Code Duplication | public function testCheckAddingSameChildRoleDoubleTime() |
|
217 | } |
||
218 |
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.