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 |
||
56 | class Browscap |
||
57 | { |
||
58 | /** |
||
59 | * Parser to use |
||
60 | * |
||
61 | * @var \BrowscapPHP\Parser\ParserInterface |
||
62 | */ |
||
63 | private $parser = null; |
||
64 | |||
65 | /** |
||
66 | * Formatter to use |
||
67 | * |
||
68 | * @var \BrowscapPHP\Formatter\FormatterInterface |
||
69 | */ |
||
70 | private $formatter = null; |
||
71 | |||
72 | /** |
||
73 | * The cache instance |
||
74 | * |
||
75 | * @var \BrowscapPHP\Cache\BrowscapCacheInterface |
||
76 | */ |
||
77 | private $cache = null; |
||
78 | |||
79 | /** |
||
80 | * @var @var \Psr\Log\LoggerInterface |
||
81 | */ |
||
82 | private $logger = null; |
||
83 | |||
84 | /** |
||
85 | * Set theformatter instance to use for the getBrowser() result |
||
86 | * |
||
87 | * @param \BrowscapPHP\Formatter\FormatterInterface $formatter |
||
88 | * |
||
89 | * @return \BrowscapPHP\Browscap |
||
90 | */ |
||
91 | 6 | public function setFormatter(Formatter\FormatterInterface $formatter) |
|
97 | |||
98 | /** |
||
99 | * @return \BrowscapPHP\Formatter\FormatterInterface |
||
100 | */ |
||
101 | 4 | public function getFormatter() |
|
109 | |||
110 | /** |
||
111 | * Gets a cache instance |
||
112 | * |
||
113 | * @return \BrowscapPHP\Cache\BrowscapCacheInterface |
||
114 | */ |
||
115 | 9 | View Code Duplication | public function getCache() |
129 | |||
130 | /** |
||
131 | * Sets a cache instance |
||
132 | * |
||
133 | * @param \BrowscapPHP\Cache\BrowscapCacheInterface|\WurflCache\Adapter\AdapterInterface $cache |
||
134 | * |
||
135 | * @throws \BrowscapPHP\Exception |
||
136 | * @return \BrowscapPHP\Browscap |
||
137 | */ |
||
138 | 7 | View Code Duplication | public function setCache($cache) |
154 | |||
155 | /** |
||
156 | * Sets the parser instance to use |
||
157 | * |
||
158 | * @param \BrowscapPHP\Parser\ParserInterface $parser |
||
159 | * |
||
160 | * @return \BrowscapPHP\Browscap |
||
161 | */ |
||
162 | 4 | public function setParser(ParserInterface $parser) |
|
168 | |||
169 | /** |
||
170 | * returns an instance of the used parser class |
||
171 | * |
||
172 | * @return \BrowscapPHP\Parser\ParserInterface |
||
173 | */ |
||
174 | 6 | public function getParser() |
|
189 | |||
190 | /** |
||
191 | * Sets a logger instance |
||
192 | * |
||
193 | * @param \Psr\Log\LoggerInterface $logger |
||
194 | * |
||
195 | * @return \BrowscapPHP\Browscap |
||
196 | */ |
||
197 | 2 | public function setLogger(LoggerInterface $logger) |
|
203 | |||
204 | /** |
||
205 | * returns a logger instance |
||
206 | * |
||
207 | * @return \Psr\Log\LoggerInterface |
||
208 | */ |
||
209 | 4 | public function getLogger() |
|
217 | |||
218 | /** |
||
219 | * parses the given user agent to get the information about the browser |
||
220 | * |
||
221 | * if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it |
||
222 | * |
||
223 | * @param string $userAgent the user agent string |
||
224 | * |
||
225 | * @throws \BrowscapPHP\Exception |
||
226 | * @return \stdClass the object containing the browsers details. Array if |
||
227 | * $return_array is set to true. |
||
228 | */ |
||
229 | 5 | public function getBrowser($userAgent = null) |
|
253 | } |
||
254 |
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.