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 JwtTokenTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | // @codingStandardsIgnoreStart |
||
19 | const EXAMPLE_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ'; |
||
20 | const KID_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleU9uZSJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.Zhhr_UtsTzjrBZmi8AAgJYqCINiiEc45v94_3nvxW1A'; |
||
21 | // @codingStandardsIgnoreEnd |
||
22 | |||
23 | /** |
||
24 | * @test |
||
25 | */ |
||
26 | public function willDecodeClaimsOnConstruction() |
||
35 | |||
36 | /** |
||
37 | * @test |
||
38 | */ |
||
39 | public function willDecodeHeadersOnConstruction() |
||
47 | |||
48 | /** |
||
49 | * @test |
||
50 | * @return JwtToken |
||
51 | */ |
||
52 | public function willDecodeWithArray() |
||
71 | |||
72 | |||
73 | /** |
||
74 | * @test |
||
75 | */ |
||
76 | public function willResultNullWhenKidOmitted() |
||
81 | |||
82 | /** |
||
83 | * @test |
||
84 | */ |
||
85 | public function canGetKidWhenPresent() |
||
90 | |||
91 | /** |
||
92 | * @test |
||
93 | */ |
||
94 | public function canGetSubject() |
||
98 | |||
99 | |||
100 | /** |
||
101 | * @test |
||
102 | * @expectedException \InvalidArgumentException |
||
103 | */ |
||
104 | View Code Duplication | public function willFailWhenSignatureValidationIsUnsuccessful() |
|
114 | |||
115 | |||
116 | /** |
||
117 | * @test |
||
118 | */ |
||
119 | View Code Duplication | public function willNitFailWhenSignatureValidationIsSuccessful() |
|
130 | |||
131 | /** |
||
132 | * @test |
||
133 | * @expectedException \InvalidArgumentException |
||
134 | */ |
||
135 | public function willFailWhenTokenDoesNotContain3Segments() |
||
141 | } |
||
142 |
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.