| Conditions | 15 |
| Paths | 368 |
| Total Lines | 74 |
| Code Lines | 30 |
| Lines | 6 |
| Ratio | 8.11 % |
| 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.'); |
||
| 64 | public function __construct($group = null) |
||
| 65 | { |
||
| 66 | // Create a singleton instance once |
||
| 67 | empty(Captcha::$instance) and Captcha::$instance = $this; |
||
| 68 | |||
| 69 | // No config group name given |
||
| 70 | if (! is_string($group)) { |
||
| 71 | $group = 'default'; |
||
| 72 | } |
||
| 73 | |||
| 74 | // Load and validate config group |
||
| 75 | if (! is_array($config = Kohana::config('captcha.'.$group))) { |
||
| 76 | throw new Kohana_Exception('captcha.undefined_group', $group); |
||
| 77 | } |
||
| 78 | |||
| 79 | // All captcha config groups inherit default config group |
||
| 80 | if ($group !== 'default') { |
||
| 81 | // Load and validate default config group |
||
| 82 | if (! is_array($default = Kohana::config('captcha.default'))) { |
||
| 83 | throw new Kohana_Exception('captcha.undefined_group', 'default'); |
||
| 84 | } |
||
| 85 | |||
| 86 | // Merge config group with default config group |
||
| 87 | $config += $default; |
||
| 88 | } |
||
| 89 | |||
| 90 | // Assign config values to the object |
||
| 91 | foreach ($config as $key => $value) { |
||
| 92 | if (array_key_exists($key, Captcha::$config)) { |
||
| 93 | Captcha::$config[$key] = $value; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | // Store the config group name as well, so the drivers can access it |
||
| 98 | Captcha::$config['group'] = $group; |
||
| 99 | |||
| 100 | // If using a background image, check if it exists |
||
| 101 | if (! empty($config['background'])) { |
||
| 102 | Captcha::$config['background'] = str_replace('\\', '/', realpath($config['background'])); |
||
| 103 | |||
| 104 | View Code Duplication | if (! is_file(Captcha::$config['background'])) { |
|
| 105 | throw new Kohana_Exception('captcha.file_not_found', Captcha::$config['background']); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // If using any fonts, check if they exist |
||
| 110 | if (! empty($config['fonts'])) { |
||
| 111 | Captcha::$config['fontpath'] = str_replace('\\', '/', realpath($config['fontpath'])).'/'; |
||
| 112 | |||
| 113 | foreach ($config['fonts'] as $font) { |
||
| 114 | View Code Duplication | if (! is_file(Captcha::$config['fontpath'].$font)) { |
|
| 115 | throw new Kohana_Exception('captcha.file_not_found', Captcha::$config['fontpath'].$font); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // Set driver name |
||
| 121 | $driver = 'Captcha_'.ucfirst($config['style']).'_Driver'; |
||
| 122 | |||
| 123 | // Load the driver |
||
| 124 | if (! Kohana::auto_load($driver)) { |
||
| 125 | throw new Kohana_Exception('core.driver_not_found', $config['style'], get_class($this)); |
||
| 126 | } |
||
| 127 | |||
| 128 | // Initialize the driver |
||
| 129 | $this->driver = new $driver; |
||
| 130 | |||
| 131 | // Validate the driver |
||
| 132 | if (! ($this->driver instanceof Captcha_Driver)) { |
||
| 133 | throw new Kohana_Exception('core.driver_implements', $config['style'], get_class($this), 'Captcha_Driver'); |
||
| 134 | } |
||
| 135 | |||
| 136 | Kohana::log('debug', 'Captcha Library initialized'); |
||
| 137 | } |
||
| 138 | |||
| 273 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.