Conditions | 22 |
Paths | 142 |
Total Lines | 62 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
52 | public function export(array $messages): void |
||
53 | { |
||
54 | if (!is_callable($this->getServer)) { |
||
55 | return; |
||
56 | } |
||
57 | /** @var Server $server */ |
||
58 | $swooleServer = call_user_func($this->getServer); |
||
59 | if (!$swooleServer || !$swooleServer instanceof Server) { |
||
60 | return; |
||
61 | } |
||
62 | foreach ($swooleServer->connections as $fd) { |
||
63 | if ($swooleServer->isEstablished($fd)) { |
||
|
|||
64 | foreach ($messages as $message) { |
||
65 | foreach ($message as $msg) { |
||
66 | if (is_string($msg)) { |
||
67 | switch (ini_get('seaslog.appender')) { |
||
68 | case '2': |
||
69 | case '3': |
||
70 | $msg = trim(substr($msg, $this->str_n_pos($msg, ' ', 6))); |
||
71 | break; |
||
72 | } |
||
73 | $msg = explode($this->split, trim($msg)); |
||
74 | $ranColor = $this->default; |
||
75 | } else { |
||
76 | $ranColor = ArrayHelper::remove($msg, '%c'); |
||
77 | } |
||
78 | if (!empty($this->levelList) && !in_array(strtolower($msg[$this->levelIndex]), |
||
79 | $this->levelList)) { |
||
80 | continue; |
||
81 | } |
||
82 | if (empty($ranColor)) { |
||
83 | $ranColor = $this->default; |
||
84 | } elseif (is_array($ranColor) && isset($ranColor['websocket'])) { |
||
85 | $ranColor = $ranColor['websocket']; |
||
86 | } else { |
||
87 | $ranColor = $this->default; |
||
88 | } |
||
89 | foreach ($msg as $index => $msgValue) { |
||
90 | $msg[$index] = is_string($msgValue) ? trim($msgValue) : (string)$msgValue; |
||
91 | $level = $this->getLevelColor(trim($msg[$this->levelIndex])); |
||
92 | if (isset($this->colorTemplate[$index])) { |
||
93 | $color = $this->colorTemplate[$index]; |
||
94 | switch ($color) { |
||
95 | case self::COLOR_LEVEL: |
||
96 | $colors[] = HtmlColor::getColor($level); |
||
97 | break; |
||
98 | case self::COLOR_RANDOM: |
||
99 | $colors[] = HtmlColor::getColor($ranColor); |
||
100 | break; |
||
101 | case self::COLOR_DEFAULT: |
||
102 | $colors[] = $this->default; |
||
103 | break; |
||
104 | default: |
||
105 | $colors[] = HtmlColor::getColor($color); |
||
106 | } |
||
107 | } else { |
||
108 | $colors[] = $level; |
||
109 | } |
||
110 | } |
||
111 | $msg = json_encode([$msg, $colors], JSON_UNESCAPED_UNICODE); |
||
112 | rgo(function () use ($swooleServer, $fd, $msg) { |
||
113 | $swooleServer->push($fd, $msg); |
||
114 | }); |
||
141 | } |