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\Components; |
||
| 30 | class ConfigurationParser { |
||
| 31 | |||
| 32 | const DEFAULT_CACHE_FOLDER = 'cache'; |
||
| 33 | |||
| 34 | protected static $algorithms = array( |
||
| 35 | 'PICK_FIRST' => 1, |
||
| 36 | 'PICK_LAST' => 2, |
||
| 37 | 'PICK_RANDOM' => 3, |
||
| 38 | 'PICK_BYWEIGHT' => 4, |
||
| 39 | 'PICK_ALL' => 4, |
||
| 40 | 'PICK_TRAVERSE' => 6 |
||
| 41 | ); |
||
| 42 | |||
| 43 | 2 | public static function parse(Configuration $configuration, LoggerInterface $logger) { |
|
| 44 | |||
| 45 | 2 | list($enable, $manager) = self::parseManagerConfiguration($configuration, $logger); |
|
| 46 | 2 | $providers = self::buildProviders($configuration, $logger); |
|
| 47 | |||
| 48 | 1 | return [ |
|
| 49 | 2 | $enable, |
|
| 50 | 2 | $manager, |
|
| 51 | $providers |
||
| 52 | 2 | ]; |
|
| 53 | |||
| 54 | } |
||
| 55 | |||
| 56 | 1 | protected static function BuildApcProvider(LoggerInterface $logger) { |
|
| 61 | |||
| 62 | 1 | protected static function BuildApcuProvider(LoggerInterface $logger) { |
|
| 67 | |||
| 68 | 1 | protected static function BuildFilesystemProvider($cache_folder, LoggerInterface $logger) { |
|
| 73 | |||
| 74 | 1 | protected static function BuildMemcachedProvider($server, $port, $weight, $persistentid, LoggerInterface $logger) { |
|
| 79 | |||
| 80 | 1 | protected static function BuildMemoryProvider(LoggerInterface $logger) { |
|
| 85 | |||
| 86 | 1 | protected static function BuildPhpRedisProvider($server, $port, $timeout, LoggerInterface $logger) { |
|
| 91 | |||
| 92 | protected static function BuildVacuumProvider(LoggerInterface $logger) { |
||
| 97 | |||
| 98 | 2 | protected static function parseManagerConfiguration(Configuration $configuration, LoggerInterface $logger) { |
|
| 99 | |||
| 100 | 2 | $cache = $configuration->get('cache'); |
|
| 101 | |||
| 102 | $stdConfig = [ |
||
| 103 | 2 | 'pick_mode' => null, |
|
| 104 | 2 | 'logger' => $logger, |
|
| 105 | 2 | 'align_cache' => true, |
|
| 106 | 'flap_interval' => null |
||
| 107 | 2 | ]; |
|
| 108 | |||
| 109 | 2 | $enable = true; |
|
| 110 | |||
| 111 | 2 | if ( $cache !== null && is_array($cache) ) { |
|
| 112 | 2 | $lower_cache = array_change_key_case($cache, CASE_LOWER); |
|
| 113 | 2 | if ( isset($lower_cache['logger']) ) unset($lower_cache['logger']); |
|
| 114 | 2 | $stdConfig = array_merge($stdConfig, array_intersect_key($lower_cache, $stdConfig)); |
|
| 115 | 2 | if ( isset($lower_cache['enable']) && $lower_cache['enable'] === false ) $enable = false; |
|
| 116 | 2 | } |
|
| 117 | |||
| 118 | 2 | if ( $stdConfig['pick_mode'] !== null ) $stdConfig['pick_mode'] = self::getPickMode($stdConfig['pick_mode']); |
|
| 119 | |||
| 120 | 2 | return [$enable, array_values($stdConfig)]; |
|
| 121 | |||
| 122 | } |
||
| 123 | |||
| 124 | 2 | protected static function buildProviders(Configuration $configuration, LoggerInterface $logger) { |
|
| 125 | |||
| 126 | 2 | $cache = $configuration->get('cache'); |
|
| 127 | 2 | $build = []; |
|
| 128 | |||
| 129 | 2 | if ( $cache === null ) return $build; |
|
| 130 | |||
| 131 | 2 | $lower_cache = array_change_key_case($cache, CASE_LOWER); |
|
| 132 | |||
| 133 | 2 | if ( !isset($lower_cache['providers']) || !is_array($lower_cache['providers']) ) return $build; |
|
| 134 | |||
| 135 | 2 | $providers = $lower_cache['providers']; |
|
| 136 | |||
| 137 | 2 | foreach ( $providers as $name => $specs ) { |
|
| 138 | |||
| 139 | 2 | if ( !is_array($specs) ) { |
|
| 140 | $logger->error("Invalid specs for cache provider: $name"); |
||
| 141 | continue; |
||
| 142 | } |
||
| 143 | |||
| 144 | 2 | $spec = array_change_key_case($specs, CASE_LOWER); |
|
| 145 | |||
| 146 | 2 | if ( empty($spec['type']) ) { |
|
| 147 | $logger->error("Missing type for cache provider: $name"); |
||
| 148 | continue; |
||
| 149 | } |
||
| 150 | |||
| 151 | 2 | $type = strtoupper($spec['type']); |
|
| 152 | |||
| 153 | switch ( $type ) { |
||
| 154 | |||
| 155 | 2 | case 'APC': |
|
| 156 | 2 | $provider = static::BuildApcProvider($logger); |
|
| 157 | 2 | break; |
|
| 158 | |||
| 159 | 2 | case 'APCU': |
|
| 160 | 2 | $provider = static::BuildApcuProvider($logger); |
|
| 161 | 2 | break; |
|
| 162 | |||
| 163 | 2 | case 'FILESYSTEM': |
|
|
|
|||
| 164 | |||
| 165 | $stdConfig = [ |
||
| 166 | 2 | 'cache_folder' => static::DEFAULT_CACHE_FOLDER, |
|
| 167 | 'logger' => $logger |
||
| 168 | 2 | ]; |
|
| 169 | |||
| 170 | 2 | if ( isset($spec['cache_folder']) ) { |
|
| 171 | 2 | if ( $spec['cache_folder'][0] == "/" ) { |
|
| 172 | $stdConfig['cache_folder'] = $spec['cache_folder']; |
||
| 173 | } else { |
||
| 174 | 2 | $stdConfig['cache_folder'] = $configuration->get('base-path')."/".$spec['cache_folder']; |
|
| 175 | } |
||
| 176 | 2 | } |
|
| 177 | |||
| 178 | 2 | $provider = static::BuildFilesystemProvider(...array_values($stdConfig)); |
|
| 179 | |||
| 180 | 2 | break; |
|
| 181 | |||
| 182 | 2 | View Code Duplication | case 'MEMCACHED': |
| 183 | |||
| 184 | $stdConfig = [ |
||
| 185 | 2 | 'server' => '127.0.0.1', |
|
| 186 | 2 | 'port' => 11211, |
|
| 187 | 2 | 'weight' => 0, |
|
| 188 | 2 | 'persistent_id' => null, |
|
| 189 | 'logger' => $logger |
||
| 190 | 2 | ]; |
|
| 191 | |||
| 192 | 2 | if ( isset($spec['logger']) ) unset($spec['logger']); |
|
| 193 | 2 | $stdConfig = array_merge($stdConfig, array_intersect_key($spec, $stdConfig)); |
|
| 194 | |||
| 195 | 2 | $provider = static::BuildMemcachedProvider(...array_values($stdConfig)); |
|
| 196 | 2 | break; |
|
| 197 | |||
| 198 | 2 | case 'MEMORY': |
|
| 199 | 2 | $provider = static::BuildMemoryProvider($logger); |
|
| 200 | 2 | break; |
|
| 201 | |||
| 202 | 2 | View Code Duplication | case 'PHPREDIS': |
| 203 | |||
| 204 | $stdConfig = [ |
||
| 205 | 2 | 'server' => '127.0.0.1', |
|
| 206 | 2 | 'port' => 6379, |
|
| 207 | 2 | 'timeout' => 0, |
|
| 208 | 'logger' => $logger |
||
| 209 | 2 | ]; |
|
| 210 | |||
| 211 | 2 | if ( isset($spec['logger']) ) unset($spec['logger']); |
|
| 212 | 2 | $stdConfig = array_merge($stdConfig, array_intersect_key($spec, $stdConfig)); |
|
| 213 | |||
| 214 | 2 | $provider = static::BuildPhpRedisProvider(...array_values($stdConfig)); |
|
| 215 | 2 | break; |
|
| 216 | |||
| 217 | case 'VACUUM': |
||
| 218 | $provider = static::BuildVacuumProvider($logger); |
||
| 219 | break; |
||
| 220 | |||
| 221 | default: |
||
| 222 | $logger->error("Unknown type $type for cache provider: $name"); |
||
| 223 | continue 2; |
||
| 224 | break; |
||
| 225 | |||
| 226 | } |
||
| 227 | |||
| 228 | 2 | $build[$name] = (object) [ |
|
| 229 | 2 | "instance" => $provider, |
|
| 230 | 2 | "weight" => isset($spec['weight']) ? |
|
| 231 | 2 | DataFilter::filterInteger($spec['weight'], 0, 100, 0) : 0 |
|
| 232 | 2 | ]; |
|
| 233 | |||
| 234 | 2 | } |
|
| 235 | |||
| 236 | 2 | return $build; |
|
| 237 | |||
| 238 | } |
||
| 239 | |||
| 240 | 2 | protected static function getPickMode($algorithm = null) { |
|
| 249 | |||
| 250 | } |
||
| 251 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.