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 namespace Comodojo\Cache\Providers; |
||
27 | abstract class AbstractProvider implements CacheItemPoolInterface { |
||
28 | |||
29 | /** |
||
30 | * Current logger |
||
31 | * |
||
32 | * @var LoggerInterface |
||
33 | */ |
||
34 | protected $logger; |
||
35 | |||
36 | /** |
||
37 | * Class constructor |
||
38 | * |
||
39 | * @throws \Comodojo\Exception\CacheException |
||
40 | */ |
||
41 | 203 | public function __construct(LoggerInterface $logger = null) { |
|
42 | |||
43 | 203 | $this->setLogger($logger); |
|
44 | |||
45 | 203 | } |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function getLogger() { |
||
51 | |||
52 | return $this->logger; |
||
53 | |||
54 | } |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 203 | View Code Duplication | public function setLogger(LoggerInterface $logger = null) { |
|
|||
60 | |||
61 | 203 | $this->logger = is_null($logger) ? LogManager::create('cache',false)->getLogger() : $logger; |
|
62 | |||
63 | 203 | return $this; |
|
64 | |||
65 | } |
||
66 | |||
67 | abstract public function getItem($key); |
||
68 | |||
69 | abstract public function getItems(array $keys = array()); |
||
70 | |||
71 | abstract public function hasItem($key); |
||
72 | |||
73 | abstract public function clear(); |
||
74 | |||
75 | abstract public function deleteItem($key); |
||
76 | |||
77 | abstract public function deleteItems(array $keys); |
||
78 | |||
79 | abstract public function save(CacheItemInterface $item); |
||
80 | |||
81 | abstract public function saveDeferred(CacheItemInterface $item); |
||
82 | |||
83 | abstract public function commit(); |
||
84 | |||
85 | } |
||
86 |
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.