| Conditions | 15 |
| Paths | 50 |
| Total Lines | 61 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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 |
||
| 72 | public function log($level, $message, array $context = array()) : void |
||
| 73 | { |
||
| 74 | // check, if requested level should be logged |
||
| 75 | // causes InvalidArgumentException in case of unknown level. |
||
| 76 | if ($this->logLevel($level)) { |
||
| 77 | // First open the logfile. |
||
| 78 | $this->openLogfile(); |
||
| 79 | if (!$this->xmlDoc || !$this->xmlRoot) { |
||
| 80 | return; |
||
| 81 | } |
||
| 82 | |||
| 83 | $xmlItem = $this->addChildToDoc('item', '', $this->xmlRoot); |
||
| 84 | |||
| 85 | $this->addChildToDoc('timestamp', date('Y-m-d H:i:s'), $xmlItem); |
||
| 86 | // IP adress |
||
| 87 | if (($this->iOptions & self::LOG_IP) != 0) { |
||
| 88 | $strIP = $_SERVER['REMOTE_ADDR']; |
||
| 89 | if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
||
| 90 | $strIP = $_SERVER['HTTP_X_FORWARDED_FOR']; |
||
| 91 | } |
||
| 92 | $this->addChildToDoc('IP-adress', $strIP, $xmlItem); |
||
| 93 | } |
||
| 94 | // user |
||
| 95 | if (($this->iOptions & self::LOG_USER) != 0) { |
||
| 96 | $this->addChildToDoc('user', $this->strUser, $xmlItem); |
||
| 97 | } |
||
| 98 | // backtrace - caller |
||
| 99 | if (($this->iOptions & self::LOG_BT) != 0) { |
||
| 100 | $this->addChildToDoc('caller', $this->getCaller(), $xmlItem); |
||
| 101 | } |
||
| 102 | // the message |
||
| 103 | $strMessage = $this->replaceContext($message, $context); |
||
| 104 | $this->addChildToDoc('level', strtoupper($level), $xmlItem); |
||
| 105 | $this->addChildToDoc('message', $strMessage, $xmlItem); |
||
| 106 | // user agent |
||
| 107 | if (($this->iOptions & self::LOG_UA) != 0) { |
||
| 108 | $this->addChildToDoc('useragent', $_SERVER["HTTP_USER_AGENT"], $xmlItem); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (count($context) > 0) { |
||
| 112 | foreach ($context as $key => $value) { |
||
| 113 | if ($key == 'exception') { |
||
| 114 | $xmlEx = $this->addChildToDoc('exception', '', $xmlItem); |
||
| 115 | $this->addChildToDoc('msg', (string)$value, $xmlEx); |
||
| 116 | $this->addChildToDoc('class', get_class($value), $xmlEx); |
||
| 117 | $aTrace = $value->getTrace(); |
||
| 118 | foreach ($aTrace as $aTraceItem) { |
||
| 119 | $xmlTrace = $this->addChildToDoc('trace', '', $xmlEx); |
||
| 120 | foreach ($aTraceItem as $key => $value) { |
||
| 121 | $this->addChildToDoc($key, (string)$value, $xmlTrace); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } else if (strpos($message, '{' . $key . '}') === false) { |
||
| 125 | $xmlContext = $this->addChildToDoc('context', '', $xmlItem); |
||
| 126 | $this->addChildToDoc('key', (string)$key, $xmlContext); |
||
| 127 | $this->addChildToDoc('value', (string)$value, $xmlContext); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | $this->xmlDoc->save($this->getFullpath()); |
||
| 133 | } |
||
| 210 |