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 |
||
13 | class GetPattern implements GetPatternInterface |
||
14 | { |
||
15 | /** |
||
16 | * The cache instance |
||
17 | * |
||
18 | * @var \BrowscapPHP\Cache\BrowscapCacheInterface |
||
19 | */ |
||
20 | private $cache; |
||
21 | |||
22 | /** |
||
23 | * a logger instance |
||
24 | * |
||
25 | * @var \Psr\Log\LoggerInterface |
||
26 | */ |
||
27 | private $logger; |
||
28 | |||
29 | /** |
||
30 | * class contructor |
||
31 | * |
||
32 | * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache |
||
33 | * @param \Psr\Log\LoggerInterface $logger |
||
34 | */ |
||
35 | 1 | public function __construct(BrowscapCacheInterface $cache, LoggerInterface $logger) |
|
40 | |||
41 | /** |
||
42 | * Gets some possible patterns that have to be matched against the user agent. With the given |
||
43 | * user agent string, we can optimize the search for potential patterns: |
||
44 | * - We check the first characters of the user agent (or better: a hash, generated from it) |
||
45 | * - We compare the length of the pattern with the length of the user agent |
||
46 | * (the pattern cannot be longer than the user agent!) |
||
47 | * |
||
48 | * @param string $userAgent |
||
49 | * |
||
50 | * @return \Generator |
||
51 | */ |
||
52 | public function getPatterns(string $userAgent) : \Generator |
||
118 | } |
||
119 |
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.