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 |
||
| 12 | class MemcacheDataManager extends AbstractDataManager |
||
| 13 | { |
||
| 14 | private $translator; |
||
| 15 | |||
| 16 | private $connection; |
||
| 17 | |||
| 18 | private $database; |
||
| 19 | |||
| 20 | public function __construct(Memcache $connection, ITranslator $translator) |
||
| 25 | |||
| 26 | public function databases(array $sorting = []) |
||
| 27 | { |
||
| 28 | $stats = $this->connection->getExtendedStats(); |
||
| 29 | $allSlabs = $this->connection->getExtendedStats('slabs'); |
||
| 30 | $databases = []; |
||
| 31 | foreach ($stats as $server => $serverInfo) { |
||
| 32 | $databases[$server] = [ |
||
| 33 | 'server' => $server, |
||
| 34 | 'process_id' => $serverInfo['pid'], |
||
| 35 | 'uptime' => $serverInfo['uptime'], |
||
| 36 | 'current_items' => $serverInfo['curr_items'], |
||
| 37 | 'total_items' => $serverInfo['total_items'], |
||
| 38 | 'size' => $serverInfo['bytes'], |
||
| 39 | 'active_slabs' => $allSlabs[$server]['active_slabs'], |
||
| 40 | 'total_malloced' => $allSlabs[$server]['total_malloced'], |
||
| 41 | ]; |
||
| 42 | } |
||
| 43 | return Multisort::sort($databases, $sorting); |
||
| 44 | } |
||
| 45 | |||
| 46 | protected function getDatabaseNameColumn() |
||
| 50 | |||
| 51 | public function tables(array $sorting = []) |
||
| 55 | |||
| 56 | public function itemsCount($type, $table, array $filter = []) |
||
| 57 | { |
||
| 58 | if ($table == 'all') { |
||
| 59 | $slabs = $this->connection->getExtendedStats('slabs'); |
||
| 60 | $databaseSlabs = isset($slabs[$this->database]) ? $slabs[$this->database] : []; |
||
| 61 | $count = 0; |
||
| 62 | View Code Duplication | foreach (array_keys($databaseSlabs) as $slabId) { |
|
|
|
|||
| 63 | if (!is_int($slabId)) { |
||
| 64 | continue; |
||
| 65 | } |
||
| 66 | $slabKeys = $this->getSlabKeys($this->database, $slabId); |
||
| 67 | $count += count($slabKeys); |
||
| 68 | } |
||
| 69 | return $count; |
||
| 70 | } |
||
| 71 | |||
| 72 | $stats = $this->connection->getExtendedStats('cachedump', (int)$table); |
||
| 73 | $keys = isset($stats[$this->database]) ? $stats[$this->database] : []; |
||
| 74 | return count($keys); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function items($type, $table, $page, $onPage, array $filter = [], array $sorting = []) |
||
| 78 | { |
||
| 79 | if ($table == 'all') { |
||
| 80 | $slabs = $this->connection->getExtendedStats('slabs'); |
||
| 81 | $databaseSlabs = isset($slabs[$this->database]) ? $slabs[$this->database] : []; |
||
| 82 | $keys = []; |
||
| 83 | View Code Duplication | foreach (array_keys($databaseSlabs) as $slabId) { |
|
| 84 | if (!is_int($slabId)) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | $keys = array_merge($keys, $this->getSlabKeys($this->database, $slabId)); |
||
| 88 | } |
||
| 89 | } else { |
||
| 90 | $keys = $this->getSlabKeys($this->database, $table); |
||
| 91 | } |
||
| 92 | |||
| 93 | $items = []; |
||
| 94 | foreach ($keys as $key => $info) { |
||
| 95 | $flags = false; |
||
| 96 | $item = [ |
||
| 97 | 'key' => $key, |
||
| 98 | 'value' => $this->connection->get($key, $flags), |
||
| 99 | 'length' => $info[0], |
||
| 100 | 'expiration' => ($info[1] - time()) > 0 ? $info[1] - time() : null, |
||
| 101 | 'compressed' => $flags == MEMCACHE_COMPRESSED ? $this->translator->translate('core.yes') : $this->translator->translate('core.no'), |
||
| 102 | ]; |
||
| 103 | if (Filter::apply($item, $filter)) { |
||
| 104 | $items[$key] = $item; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | $items = Multisort::sort($items, $sorting); |
||
| 109 | return array_slice($items, ($page - 1) * $onPage, $onPage, true); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function deleteItem($type, $table, $item) |
||
| 116 | |||
| 117 | public function selectDatabase($database) |
||
| 121 | |||
| 122 | private function getSlabKeys($database, $slabId) |
||
| 131 | } |
||
| 132 |
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.