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 |
||
42 | trait NginxTest |
||
43 | { |
||
44 | /** |
||
45 | * @var Nginx |
||
46 | */ |
||
47 | protected $proxyClient; |
||
48 | |||
49 | /** |
||
50 | * @var NginxProxy |
||
51 | */ |
||
52 | protected $proxy; |
||
53 | |||
54 | 6 | protected function setUp() |
|
58 | |||
59 | 6 | protected function tearDown() |
|
63 | |||
64 | /** |
||
65 | * The default implementation looks at the constant NGINX_FILE. |
||
66 | * |
||
67 | * @throws \Exception |
||
68 | * |
||
69 | * @return string the path to the NGINX server configuration file to use with this test |
||
70 | */ |
||
71 | 6 | protected function getConfigFile() |
|
83 | |||
84 | /** |
||
85 | * Defaults to "nginx". |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 6 | protected function getBinary() |
|
93 | |||
94 | /** |
||
95 | * Defaults to 8088. |
||
96 | * |
||
97 | * @return int |
||
98 | */ |
||
99 | 6 | protected function getCachingProxyPort() |
|
103 | |||
104 | /** |
||
105 | * Get NGINX cache directory. |
||
106 | */ |
||
107 | 6 | protected function getCacheDir() |
|
111 | |||
112 | /** |
||
113 | * Get the hostname where your application can be reached. |
||
114 | * |
||
115 | * @throws \Exception |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | 6 | protected function getHostName() |
|
129 | |||
130 | /** |
||
131 | * @return NginxProxy |
||
132 | */ |
||
133 | 6 | protected function getProxy() |
|
150 | |||
151 | /** |
||
152 | * Get proxy client. |
||
153 | * |
||
154 | * @param string $purgeLocation Optional purgeLocation |
||
155 | * |
||
156 | * @return Nginx |
||
157 | */ |
||
158 | 6 | View Code Duplication | protected function getProxyClient($purgeLocation = '') |
173 | } |
||
174 |
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.