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:
Complex classes like RedisDataManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RedisDataManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class RedisDataManager extends AbstractDataManager |
||
| 12 | { |
||
| 13 | private $connection; |
||
| 14 | |||
| 15 | private $databaseAliasStorage; |
||
| 16 | |||
| 17 | private $itemsCountCache = false; |
||
| 18 | |||
| 19 | 2 | public function __construct(RedisProxy $connection, RedisDatabaseAliasStorage $databaseAliasStorage) |
|
| 24 | |||
| 25 | public function databases(array $sorting = []) |
||
| 26 | { |
||
| 27 | $keyspace = $this->connection->info('keyspace'); |
||
| 28 | $aliases = $this->databaseAliasStorage->loadAll(); |
||
| 29 | $databases = []; |
||
| 30 | foreach ($keyspace as $db => $info) { |
||
| 31 | $db = str_replace('db', '', $db); |
||
| 32 | $alias = isset($aliases[$db]) ? ' (' . $aliases[$db] . ')' : ''; |
||
| 33 | $info['database'] = $db . $alias; |
||
| 34 | $databases[$db] = $info; |
||
| 35 | } |
||
| 36 | return Multisort::sort($databases, $sorting); |
||
| 37 | } |
||
| 38 | |||
| 39 | protected function getDatabaseNameColumn() |
||
| 43 | |||
| 44 | public function tables(array $sorting = []) |
||
| 45 | { |
||
| 46 | $tables = [ |
||
| 47 | RedisDriver::TYPE_KEY => [ |
||
| 48 | 'list_of_all_keys' => [ |
||
| 49 | 'key' => 'Show all keys', |
||
| 50 | 'number_of_keys' => 0, |
||
| 51 | ] |
||
| 52 | ], |
||
| 53 | RedisDriver::TYPE_HASH => [], |
||
| 54 | RedisDriver::TYPE_SET => [], |
||
| 55 | ]; |
||
| 56 | foreach ($this->connection->keys('*') as $key) { |
||
| 57 | $type = $this->connection->type($key); |
||
| 58 | if ($type === RedisProxy::TYPE_STRING) { |
||
| 59 | $tables[RedisDriver::TYPE_KEY]['list_of_all_keys']['number_of_keys']++; |
||
| 60 | } elseif ($type === RedisProxy::TYPE_HASH) { |
||
| 61 | $result = $this->connection->hlen($key); |
||
| 62 | $tables[RedisDriver::TYPE_HASH][$key] = [ |
||
| 63 | 'key' => $key, |
||
| 64 | 'number_of_fields' => $result, |
||
| 65 | ]; |
||
| 66 | } elseif ($type === RedisProxy::TYPE_SET) { |
||
| 67 | $result = $this->connection->scard($key); |
||
| 68 | $tables[RedisDriver::TYPE_SET][$key] = [ |
||
| 69 | 'key' => $key, |
||
| 70 | 'number_of_members' => $result, |
||
| 71 | ]; |
||
| 72 | } |
||
| 73 | // TODO list and sorted set |
||
| 74 | } |
||
| 75 | return [ |
||
| 76 | RedisDriver::TYPE_KEY => Multisort::sort($tables[RedisDriver::TYPE_KEY], $sorting), |
||
| 77 | RedisDriver::TYPE_HASH => Multisort::sort($tables[RedisDriver::TYPE_HASH], $sorting), |
||
| 78 | RedisDriver::TYPE_SET => Multisort::sort($tables[RedisDriver::TYPE_SET], $sorting), |
||
| 79 | ]; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function itemsCount($type, $table, array $filter = []) |
||
| 83 | { |
||
| 84 | if ($this->itemsCountCache !== false) { |
||
| 85 | return $this->itemsCountCache; |
||
|
|
|||
| 86 | } |
||
| 87 | if ($type == RedisDriver::TYPE_HASH) { |
||
| 88 | if (!$filter) { |
||
| 89 | $this->itemsCountCache = $this->connection->hlen($table); |
||
| 90 | return $this->itemsCountCache; |
||
| 91 | } else { |
||
| 92 | $totalItems = 0; |
||
| 93 | foreach ($filter as $filterParts) { |
||
| 94 | if (isset($filterParts['key'][Filter::OPERATOR_EQUAL])) { |
||
| 95 | $res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]); |
||
| 96 | if ($res) { |
||
| 97 | $item = [ |
||
| 98 | 'key' => $filterParts['key'][Filter::OPERATOR_EQUAL], |
||
| 99 | 'length' => strlen($res), |
||
| 100 | 'value' => $res, |
||
| 101 | ]; |
||
| 102 | if (Filter::apply($item, $filter)) { |
||
| 103 | $totalItems++; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | $this->itemsCountCache = $totalItems; |
||
| 107 | return $this->itemsCountCache; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | $iterator = ''; |
||
| 111 | do { |
||
| 112 | $pattern = null; |
||
| 113 | $res = $this->connection->hscan($table, $iterator, $pattern, 1000); |
||
| 114 | $res = $res ?: []; |
||
| 115 | View Code Duplication | foreach ($res as $key => $value) { |
|
| 116 | $item = [ |
||
| 117 | 'key' => $key, |
||
| 118 | 'length' => strlen($value), |
||
| 119 | 'value' => $value, |
||
| 120 | ]; |
||
| 121 | if (Filter::apply($item, $filter)) { |
||
| 122 | $totalItems++; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } while ($iterator !== 0); |
||
| 126 | $this->itemsCountCache = $totalItems; |
||
| 127 | return $this->itemsCountCache; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | if ($type == RedisDriver::TYPE_KEY) { |
||
| 131 | $totalItems = 0; |
||
| 132 | foreach ($this->connection->keys('*') as $key) { |
||
| 133 | if ($this->connection->type($key) !== RedisProxy::TYPE_STRING) { |
||
| 134 | continue; |
||
| 135 | } |
||
| 136 | $result = $this->connection->get($key); |
||
| 137 | $item = [ |
||
| 138 | 'key' => $key, |
||
| 139 | 'value' => $result, |
||
| 140 | 'length' => strlen($result), |
||
| 141 | ]; |
||
| 142 | |||
| 143 | if (Filter::apply($item, $filter)) { |
||
| 144 | $totalItems++; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | return $totalItems; |
||
| 148 | } |
||
| 149 | if ($type == RedisDriver::TYPE_SET) { |
||
| 150 | if (!$filter) { |
||
| 151 | return $this->connection->scard($table); |
||
| 152 | } |
||
| 153 | $iterator = ''; |
||
| 154 | $totalItems = 0; |
||
| 155 | do { |
||
| 156 | $res = $this->connection->sscan($table, $iterator, null, 1000); |
||
| 157 | $res = $res ?: []; |
||
| 158 | View Code Duplication | foreach ($res as $member) { |
|
| 159 | $item = [ |
||
| 160 | 'member' => $member, |
||
| 161 | 'length' => strlen($member), |
||
| 162 | ]; |
||
| 163 | if (Filter::apply($item, $filter)) { |
||
| 164 | $totalItems++; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } while ($iterator !== 0); |
||
| 168 | return $totalItems; |
||
| 169 | } |
||
| 170 | return 0; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function items($type, $table, $page, $onPage, array $filter = [], array $sorting = []) |
||
| 174 | { |
||
| 175 | $items = []; |
||
| 176 | $offset = ($page - 1) * $onPage; |
||
| 177 | $skipped = 0; |
||
| 178 | if ($type == RedisDriver::TYPE_HASH) { |
||
| 179 | foreach ($filter as $filterParts) { |
||
| 180 | if (isset($filterParts['key'][Filter::OPERATOR_EQUAL])) { |
||
| 181 | $items = []; |
||
| 182 | $res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]); |
||
| 183 | if ($res) { |
||
| 184 | $item = [ |
||
| 185 | 'key' => $filterParts['key'][Filter::OPERATOR_EQUAL], |
||
| 186 | 'length' => strlen($res), |
||
| 187 | 'value' => $res, |
||
| 188 | ]; |
||
| 189 | if (Filter::apply($item, $filter)) { |
||
| 190 | $items[$item['key']] = $item; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | return $items; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | |||
| 198 | $iterator = ''; |
||
| 199 | do { |
||
| 200 | $pattern = null; |
||
| 201 | $res = $this->connection->hscan($table, $iterator, $pattern, $onPage * 10); |
||
| 202 | $res = $res ?: []; |
||
| 203 | foreach ($res as $key => $value) { |
||
| 204 | $item = [ |
||
| 205 | 'key' => $key, |
||
| 206 | 'length' => strlen($value), |
||
| 207 | 'value' => $value, |
||
| 208 | ]; |
||
| 209 | View Code Duplication | if (Filter::apply($item, $filter)) { |
|
| 210 | if ($skipped < $offset) { |
||
| 211 | $skipped++; |
||
| 212 | } else { |
||
| 213 | $items[$key] = $item; |
||
| 214 | if (count($items) === $onPage) { |
||
| 215 | break; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } while ($iterator !== 0 && count($items) < $onPage); |
||
| 221 | } elseif ($type == RedisDriver::TYPE_KEY) { |
||
| 222 | foreach ($this->connection->keys('*') as $key) { |
||
| 223 | if ($this->connection->type($key) !== RedisProxy::TYPE_STRING) { |
||
| 224 | continue; |
||
| 225 | } |
||
| 226 | $result = $this->connection->get($key); |
||
| 227 | |||
| 228 | $item = [ |
||
| 229 | 'key' => $key, |
||
| 230 | 'value' => $result, |
||
| 231 | 'length' => strlen($result), |
||
| 232 | ]; |
||
| 233 | |||
| 234 | View Code Duplication | if (Filter::apply($item, $filter)) { |
|
| 235 | if ($skipped < $offset) { |
||
| 236 | $skipped++; |
||
| 237 | } else { |
||
| 238 | $items[$key] = $item; |
||
| 239 | if (count($items) === $onPage) { |
||
| 240 | break; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } elseif ($type == RedisDriver::TYPE_SET) { |
||
| 246 | $iterator = ''; |
||
| 247 | do { |
||
| 248 | $pattern = null; |
||
| 249 | $res = $this->connection->sscan($table, $iterator, $pattern, $onPage * 10); |
||
| 250 | $res = $res ?: []; |
||
| 251 | foreach ($res as $member) { |
||
| 252 | $item = [ |
||
| 253 | 'member' => $member, |
||
| 254 | 'length' => strlen($member), |
||
| 255 | ]; |
||
| 256 | if (Filter::apply($item, $filter)) { |
||
| 257 | $items[$member] = $item; |
||
| 258 | if (count($items) === $onPage) { |
||
| 259 | break; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } while ($iterator !== 0 && count($items) < $onPage); |
||
| 264 | } |
||
| 265 | |||
| 266 | if ($this->itemsCount($type, $table, $filter) <= $onPage) { |
||
| 267 | $items = Multisort::sort($items, $sorting); |
||
| 268 | } elseif ($sorting) { |
||
| 269 | $this->addMessage('Sorting has not been applied because the number of items is greater then the limit. Increase the limit or modify the filter.'); |
||
| 270 | } |
||
| 271 | |||
| 272 | return $items; |
||
| 273 | } |
||
| 274 | |||
| 275 | public function deleteItem($type, $table, $item) |
||
| 288 | |||
| 289 | public function deleteTable($type, $table) |
||
| 293 | |||
| 294 | public function selectDatabase($database) |
||
| 298 | |||
| 299 | public function execute($commands) |
||
| 300 | { |
||
| 301 | $listOfCommands = array_filter(array_map('trim', explode("\n", $commands)), function ($command) { |
||
| 302 | return $command; |
||
| 303 | }); |
||
| 304 | |||
| 305 | $results = []; |
||
| 306 | foreach ($listOfCommands as $command) { |
||
| 307 | $commandParts = explode(' ', $command); |
||
| 308 | $function = array_shift($commandParts); |
||
| 309 | $function = strtolower($function); |
||
| 310 | $results[$command]['headers'] = $this->headers($function); |
||
| 311 | $rows = call_user_func_array([$this->connection, $function], $commandParts); |
||
| 312 | $items = $this->getItems($function, $rows); |
||
| 313 | $results[$command]['items'] = $items; |
||
| 314 | $results[$command]['count'] = count($items); |
||
| 315 | } |
||
| 316 | return $results; |
||
| 317 | } |
||
| 318 | |||
| 319 | private function headers($function) |
||
| 335 | |||
| 336 | private function getItems($function, $rows) |
||
| 337 | { |
||
| 338 | $items = []; |
||
| 339 | if ($function === 'keys') { |
||
| 352 | } |
||
| 353 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.