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