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 |
||
22 | class ContentJwt implements JwtInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $teamId; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $key; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $content; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $tmpDir; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $certificateFilePath; |
||
48 | |||
49 | /** |
||
50 | * Constructor. |
||
51 | * |
||
52 | * @param string $teamId |
||
53 | * @param string $key |
||
54 | * @param string $content |
||
55 | * @param string $tmpDir |
||
56 | */ |
||
57 | public function __construct(string $teamId, string $key, string $content, string $tmpDir) |
||
64 | |||
65 | /** |
||
66 | * Implement __destruct |
||
67 | * Remove temporary file |
||
68 | */ |
||
69 | public function __destruct() |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function getTeamId(): string |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function getKey(): string |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | View Code Duplication | public function getPath(): string |
|
106 | |||
107 | /** |
||
108 | * Create a temporary file |
||
109 | * |
||
110 | * @return string Path to temporary file |
||
111 | * |
||
112 | * @throws \RuntimeException |
||
113 | */ |
||
114 | View Code Duplication | private function createTemporaryFile(): string |
|
163 | |||
164 | /** |
||
165 | * Remove temporary file |
||
166 | * |
||
167 | * @param string $filePath |
||
168 | */ |
||
169 | private function removeTemporaryFile($filePath): void |
||
179 | } |
||
180 |
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.