| Conditions | 12 |
| Paths | 48 |
| Total Lines | 52 |
| Code Lines | 23 |
| 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.'); |
||
| 108 | public static function send($to, $from, $subject, $message, $html = false) |
||
| 109 | { |
||
| 110 | // Connect to SwiftMailer |
||
| 111 | (email::$mail === null) and email::connect(); |
||
| 112 | |||
| 113 | // Determine the message type |
||
| 114 | $html = ($html === true) ? 'text/html' : 'text/plain'; |
||
| 115 | |||
| 116 | // Create the message |
||
| 117 | $message = new Swift_Message($subject, $message, $html, '8bit', 'utf-8'); |
||
| 118 | |||
| 119 | if (is_string($to)) { |
||
| 120 | // Single recipient |
||
| 121 | $recipients = new Swift_Address($to); |
||
| 122 | } elseif (is_array($to)) { |
||
| 123 | if (isset($to[0]) and isset($to[1])) { |
||
| 124 | // Create To: address set |
||
| 125 | $to = array('to' => $to); |
||
| 126 | } |
||
| 127 | |||
| 128 | // Create a list of recipients |
||
| 129 | $recipients = new Swift_RecipientList; |
||
| 130 | |||
| 131 | foreach ($to as $method => $set) { |
||
| 132 | if (! in_array($method, array('to', 'cc', 'bcc'))) { |
||
| 133 | // Use To: by default |
||
| 134 | $method = 'to'; |
||
| 135 | } |
||
| 136 | |||
| 137 | // Create method name |
||
| 138 | $method = 'add'.ucfirst($method); |
||
| 139 | |||
| 140 | if (is_array($set)) { |
||
| 141 | // Add a recipient with name |
||
| 142 | $recipients->$method($set[0], $set[1]); |
||
| 143 | } else { |
||
| 144 | // Add a recipient without name |
||
| 145 | $recipients->$method($set); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | if (is_string($from)) { |
||
| 151 | // From without a name |
||
| 152 | $from = new Swift_Address($from); |
||
| 153 | } elseif (is_array($from)) { |
||
| 154 | // From with a name |
||
| 155 | $from = new Swift_Address($from[0], $from[1]); |
||
| 156 | } |
||
| 157 | |||
| 158 | return email::$mail->send($message, $recipients, $from); |
||
| 159 | } |
||
| 160 | } // End email |
||
| 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: