| Conditions | 11 |
| Paths | 36 |
| Total Lines | 49 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 133 | public function log($type, $message, $format = "\[Y-m-d H:i:s\]", $time = null) { |
||
| 134 | $msgType = null; |
||
| 135 | switch ($type) { |
||
| 136 | case (self::LOG_INFO): |
||
| 137 | $msgType = OutputStream::MSG_INFO; |
||
| 138 | break; |
||
| 139 | case (self::LOG_DEBUG): |
||
| 140 | $msgType = OutputStream::MSG_DEBUG; |
||
| 141 | break; |
||
| 142 | case (self::LOG_SUCCESS): |
||
| 143 | $msgType = OutputStream::MSG_SUCCESS; |
||
| 144 | break; |
||
| 145 | case (self::LOG_WARNING): |
||
| 146 | $msgType = OutputStream::MSG_WARNING; |
||
| 147 | break; |
||
| 148 | case (self::LOG_ERROR): |
||
| 149 | $msgType = OutputStream::MSG_ERROR; |
||
| 150 | break; |
||
| 151 | default: |
||
| 152 | throw new LoggerException("Invalid message type"); |
||
| 153 | } |
||
| 154 | |||
| 155 | switch ($this->direction) { |
||
| 156 | case (self::TO_OUTPUT_STREAM): |
||
| 157 | OutputStream::msg($msgType, $message, $format, $time); |
||
| 158 | |||
| 159 | return; |
||
| 160 | case (self::TO_FILE): |
||
| 161 | $message = "({$type}) " . $message; |
||
| 162 | |||
| 163 | if (strpos($message, "{{time}}") === false) { |
||
| 164 | $message = "{{time}} " . $message; |
||
| 165 | } |
||
| 166 | if (is_null($time)) { |
||
| 167 | $time = time(); |
||
| 168 | } |
||
| 169 | $message = str_replace("{{time}}", date($format, $time), $message); |
||
| 170 | |||
| 171 | file_put_contents($this->fileName, "{$message}\n", FILE_APPEND); |
||
| 172 | |||
| 173 | return; |
||
| 174 | case (self::TO_DB): |
||
| 175 | $this->dbObject->log($type, $message, $time = null); |
||
| 176 | |||
| 177 | return; |
||
| 178 | default: |
||
| 179 | throw new LoggerException("Invalid logging output direction type"); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 208 |