| Conditions | 9 |
| Paths | 21 |
| Total Lines | 54 |
| Code Lines | 23 |
| Lines | 14 |
| Ratio | 25.93 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
| 49 | public function __construct($config = false) |
||
| 50 | { |
||
| 51 | View Code Duplication | if (is_string($config)) { |
|
| 52 | $name = $config; |
||
| 53 | |||
| 54 | // Test the config group name |
||
| 55 | if (($config = Kohana::config('cache.'.$config)) === null) { |
||
| 56 | throw new Kohana_Exception('cache.undefined_group', $name); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | if (is_array($config)) { |
||
| 61 | // Append the default configuration options |
||
| 62 | $config += Kohana::config('cache.default'); |
||
| 63 | } else { |
||
| 64 | // Load the default group |
||
| 65 | $config = Kohana::config('cache.default'); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Cache the config in the object |
||
| 69 | $this->config = $config; |
||
| 70 | |||
| 71 | // Set driver name |
||
| 72 | $driver = 'Cache_'.ucfirst($this->config['driver']).'_Driver'; |
||
| 73 | |||
| 74 | // Load the driver |
||
| 75 | View Code Duplication | if (! Kohana::auto_load($driver)) { |
|
| 76 | throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this)); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Initialize the driver |
||
| 80 | $this->driver = new $driver($this->config['params']); |
||
| 81 | |||
| 82 | // Validate the driver |
||
| 83 | View Code Duplication | if (! ($this->driver instanceof Cache_Driver)) { |
|
| 84 | throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Cache_Driver'); |
||
| 85 | } |
||
| 86 | |||
| 87 | Kohana::log('debug', 'Cache Library initialized'); |
||
| 88 | |||
| 89 | if (Cache::$loaded !== true) { |
||
| 90 | $this->config['requests'] = (int) $this->config['requests']; |
||
| 91 | |||
| 92 | if ($this->config['requests'] > 0 and mt_rand(1, $this->config['requests']) === 1) { |
||
| 93 | // Do garbage collection |
||
| 94 | $this->driver->delete_expired(); |
||
| 95 | |||
| 96 | Kohana::log('debug', 'Cache: Expired caches deleted.'); |
||
| 97 | } |
||
| 98 | |||
| 99 | // Cache has been loaded once |
||
| 100 | Cache::$loaded = true; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 204 |
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.