| Conditions | 16 |
| Paths | 392 |
| Total Lines | 73 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| 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.'); |
||
| 24 | public static function connect($config = null) |
||
| 25 | { |
||
| 26 | if (! class_exists('Swift', false)) { |
||
| 27 | // Load SwiftMailer |
||
| 28 | require Kohana::find_file('vendor', 'swift/Swift'); |
||
| 29 | |||
| 30 | // Register the Swift ClassLoader as an autoload |
||
| 31 | spl_autoload_register(array('Swift_ClassLoader', 'load')); |
||
| 32 | } |
||
| 33 | |||
| 34 | // Load default configuration |
||
| 35 | ($config === null) and $config = Kohana::config('email'); |
||
| 36 | |||
| 37 | switch ($config['driver']) { |
||
| 38 | case 'smtp': |
||
| 39 | // Set port |
||
| 40 | $port = empty($config['options']['port']) ? null : (int) $config['options']['port']; |
||
| 41 | |||
| 42 | if (empty($config['options']['encryption'])) { |
||
| 43 | // No encryption |
||
| 44 | $encryption = Swift_Connection_SMTP::ENC_OFF; |
||
| 45 | } else { |
||
| 46 | // Set encryption |
||
| 47 | switch (strtolower($config['options']['encryption'])) { |
||
| 48 | case 'tls': $encryption = Swift_Connection_SMTP::ENC_TLS; break; |
||
| 49 | case 'ssl': $encryption = Swift_Connection_SMTP::ENC_SSL; break; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | // Create a SMTP connection |
||
| 54 | $connection = new Swift_Connection_SMTP($config['options']['hostname'], $port, $encryption); |
||
|
|
|||
| 55 | |||
| 56 | // Do authentication, if part of the DSN |
||
| 57 | empty($config['options']['username']) or $connection->setUsername($config['options']['username']); |
||
| 58 | empty($config['options']['password']) or $connection->setPassword($config['options']['password']); |
||
| 59 | |||
| 60 | if (! empty($config['options']['auth'])) { |
||
| 61 | // Get the class name and params |
||
| 62 | list($class, $params) = arr::callback_string($config['options']['auth']); |
||
| 63 | |||
| 64 | if ($class === 'PopB4Smtp') { |
||
| 65 | // Load the PopB4Smtp class manually, due to its odd filename |
||
| 66 | require Kohana::find_file('vendor', 'swift/Swift/Authenticator/$PopB4Smtp$'); |
||
| 67 | } |
||
| 68 | |||
| 69 | // Prepare the class name for auto-loading |
||
| 70 | $class = 'Swift_Authenticator_'.$class; |
||
| 71 | |||
| 72 | // Attach the authenticator |
||
| 73 | $connection->attachAuthenticator(($params === null) ? new $class : new $class($params[0])); |
||
| 74 | } |
||
| 75 | |||
| 76 | // Set the timeout to 5 seconds |
||
| 77 | $connection->setTimeout(empty($config['options']['timeout']) ? 5 : (int) $config['options']['timeout']); |
||
| 78 | break; |
||
| 79 | case 'sendmail': |
||
| 80 | // Create a sendmail connection |
||
| 81 | $connection = new Swift_Connection_Sendmail( |
||
| 82 | empty($config['options']) ? Swift_Connection_Sendmail::AUTO_DETECT : $config['options'] |
||
| 83 | ); |
||
| 84 | |||
| 85 | // Set the timeout to 5 seconds |
||
| 86 | $connection->setTimeout(5); |
||
| 87 | break; |
||
| 88 | default: |
||
| 89 | // Use the native connection |
||
| 90 | $connection = new Swift_Connection_NativeMail($config['options']); |
||
| 91 | break; |
||
| 92 | } |
||
| 93 | |||
| 94 | // Create the SwiftMailer instance |
||
| 95 | return email::$mail = new Swift($connection); |
||
| 96 | } |
||
| 97 | |||
| 161 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: