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 |
||
47 | trait VarnishTest |
||
48 | { |
||
49 | /** |
||
50 | * @var VarnishProxy |
||
51 | */ |
||
52 | protected $proxy; |
||
53 | |||
54 | /** |
||
55 | * @var Varnish |
||
56 | */ |
||
57 | protected $proxyClient; |
||
58 | |||
59 | /** |
||
60 | * Start Varnish and discard any cached content. |
||
61 | */ |
||
62 | 23 | protected function setUp() |
|
66 | |||
67 | /** |
||
68 | * Stop Varnish. |
||
69 | */ |
||
70 | 23 | protected function tearDown() |
|
74 | |||
75 | /** |
||
76 | * The default implementation looks at the constant VARNISH_FILE. |
||
77 | * |
||
78 | * @throws \Exception |
||
79 | * |
||
80 | * @return string the path to the varnish server configuration file to use with this test |
||
81 | */ |
||
82 | 10 | protected function getConfigFile() |
|
92 | |||
93 | /** |
||
94 | * Get Varnish binary. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 23 | protected function getBinary() |
|
102 | |||
103 | /** |
||
104 | * Get Varnish port. |
||
105 | * |
||
106 | * @return int |
||
107 | */ |
||
108 | 23 | protected function getCachingProxyPort() |
|
112 | |||
113 | /** |
||
114 | * Get Varnish management port. |
||
115 | * |
||
116 | * @return int |
||
117 | */ |
||
118 | 23 | protected function getVarnishMgmtPort() |
|
122 | |||
123 | /** |
||
124 | * Get Varnish cache directory. |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 23 | protected function getCacheDir() |
|
132 | |||
133 | /** |
||
134 | * Defaults to 4. |
||
135 | * |
||
136 | * @return int |
||
137 | */ |
||
138 | 13 | protected function getVarnishVersion() |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 23 | protected function getProxy() |
|
169 | |||
170 | /** |
||
171 | * Get Varnish proxy client. |
||
172 | * |
||
173 | * @return Varnish |
||
174 | */ |
||
175 | 10 | View Code Duplication | protected function getProxyClient() |
187 | |||
188 | /** |
||
189 | * Get the hostname where your application can be reached. |
||
190 | * |
||
191 | * @throws \Exception |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | 23 | protected function getHostName() |
|
205 | } |
||
206 |
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.