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 |
||
31 | class Utility |
||
32 | { |
||
33 | /** |
||
34 | * Set a cookie in the response |
||
35 | * |
||
36 | * @param ResponseInterface $response |
||
37 | * @param string $name |
||
38 | * @param string $value |
||
39 | * @param int $ttl |
||
40 | * @param string $path |
||
41 | * @param string $domain |
||
42 | * @param bool $secure |
||
43 | * @param bool $httponly |
||
44 | * @return ResponseInterface |
||
45 | * @access public |
||
46 | */ |
||
47 | public static function setCookie( |
||
71 | |||
72 | /** |
||
73 | * Unset a cookie |
||
74 | * |
||
75 | * @param ResponseInterface $response |
||
76 | * @param string $name |
||
77 | * @param string $path |
||
78 | * @return ResponseInterface |
||
79 | * @access public |
||
80 | */ |
||
81 | public static function unsetCookie( |
||
88 | |||
89 | /** |
||
90 | * Set public cache header |
||
91 | * |
||
92 | * @param ResponseInterface $response |
||
93 | * @param int $cacheTime cache time in minutes |
||
94 | * @return ResponseInterface |
||
95 | * @access public |
||
96 | */ |
||
97 | View Code Duplication | public static function publicCache( |
|
107 | |||
108 | /** |
||
109 | * Set private_no_expire cache header |
||
110 | * |
||
111 | * @param ResponseInterface $response |
||
112 | * @param int $cacheTime cache time in minutes |
||
113 | * @return ResponseInterface |
||
114 | * @access public |
||
115 | */ |
||
116 | View Code Duplication | public static function privateNoExpireCache( |
|
125 | |||
126 | /** |
||
127 | * Set private cache header |
||
128 | * |
||
129 | * @param ResponseInterface $response |
||
130 | * @return ResponseInterface |
||
131 | * @access protected |
||
132 | */ |
||
133 | public static function privateCache( |
||
140 | |||
141 | /** |
||
142 | * Set no cache header |
||
143 | * |
||
144 | * @param ResponseInterface $response |
||
145 | * @return ResponseInterface |
||
146 | * @access public |
||
147 | */ |
||
148 | public static function noCache( |
||
159 | |||
160 | protected static function timeStamp(/*# int */ $ttl= 0) |
||
164 | |||
165 | protected static function addExpire(/*# string */ &$cookie, $ttl) |
||
172 | |||
173 | protected static function addDomain(/*# string */ &$cookie, $domain) |
||
179 | |||
180 | protected static function addPath(/*# string */ &$cookie, $path) |
||
186 | |||
187 | protected static function addSecure(/*# string */ &$cookie, $secure) |
||
193 | |||
194 | protected static function addHttpOnly(/*# string */ &$cookie, $httponly) |
||
200 | } |
||
201 |
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.