| Conditions | 15 |
| Paths | 3456 |
| Total Lines | 55 |
| Code Lines | 30 |
| 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 |
||
| 33 | public function write($message, $data = null) |
||
| 34 | { |
||
| 35 | //# process message(s) |
||
| 36 | $msgs = [$message]; |
||
| 37 | if (isset($data['msgs'])) { |
||
| 38 | $msgs = array_merge($msgs, $data['msgs']); |
||
| 39 | } |
||
| 40 | // prepend main message in front of metadata added by subclasses |
||
| 41 | foreach (array_reverse($msgs) as $key => $msg) { |
||
| 42 | $this->_add($msg, $key, true); |
||
| 43 | } |
||
| 44 | |||
| 45 | //# add exception data |
||
| 46 | if (isset($data['e'])) { |
||
| 47 | /* @var $Exception \Exception */ |
||
| 48 | $Exception = $data['e']; |
||
| 49 | unset($data['e']); |
||
| 50 | $message = $Exception->getMessage(); |
||
| 51 | if (!empty($message)) { |
||
| 52 | $this->_add($message); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | //# add request data |
||
| 57 | $request = (php_sapi_name() !== 'cli') ? Router::getRequest() : false; |
||
| 58 | |||
| 59 | $url = false; |
||
| 60 | if (isset($data['URL'])) { |
||
| 61 | $url = $data['URL']; |
||
| 62 | } elseif ($request) { |
||
| 63 | $url = $request->getRequestTarget(); |
||
| 64 | } |
||
| 65 | |||
| 66 | $requestMethod = $request ? $request->getMethod() : false; |
||
| 67 | if ($url && $requestMethod) { |
||
| 68 | $url .= ' ' . $requestMethod; |
||
| 69 | } |
||
| 70 | if ($url) { |
||
| 71 | $this->_add($url, 'Request URL'); |
||
| 72 | } |
||
| 73 | |||
| 74 | if (isset($_SERVER['HTTP_USER_AGENT'])) { |
||
| 75 | $this->_add($_SERVER['HTTP_USER_AGENT'], 'User-Agent'); |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->_addUser($data); |
||
| 79 | |||
| 80 | if ($request) { |
||
| 81 | $data = $request->getData(); |
||
| 82 | if (!empty($data)) { |
||
| 83 | $this->_add($this->_filterData($data), 'Data'); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | $this->_write(); |
||
| 88 | } |
||
| 195 |