Conditions | 20 |
Paths | 41 |
Total Lines | 57 |
Code Lines | 43 |
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 |
||
46 | public function log($level, $message, array $context = []): void |
||
47 | { |
||
48 | if (is_array($message)) { |
||
49 | $this->error('Array comme message de log...', ['message' => $message]); |
||
50 | |||
51 | return; |
||
52 | } |
||
53 | $message = trim($message); |
||
54 | $date = date('Y-m-d H:i:s'); |
||
55 | |||
56 | $this->incrementStatsFromContext($context); |
||
57 | if (isset($context['stats'])) { |
||
58 | unset($context['stats']); |
||
59 | } |
||
60 | |||
61 | switch ($level) { |
||
62 | case 'emergency': |
||
63 | case 'alert': |
||
64 | case 'critical': |
||
65 | $this->echoColor("[$level] " . $date . ' : ' . $message . "\n", Color::BG_RED . Color::WHITE); |
||
66 | if ($context !== []) { |
||
67 | dump($context); |
||
68 | } |
||
69 | $this->logInFile($level, $message); |
||
70 | break; |
||
71 | case 'error': |
||
72 | case 'warning': |
||
73 | $this->echoColor("[$level] " . $date . ' : ' . $message . "\n", Color::BG_YELLOW . Color::BLACK); |
||
74 | if ($context !== []) { |
||
75 | dump($context); |
||
76 | } |
||
77 | break; |
||
78 | case 'notice': |
||
79 | $this->echoColor("[$level] " . $message . "\n"); |
||
80 | if ($context !== []) { |
||
81 | dump($context); |
||
82 | } |
||
83 | break; |
||
84 | case 'info': |
||
85 | if ($this->verbose || $this->debug) { |
||
86 | $this->echoColor("[$level] " . $message . "\n", Color::GRAY); |
||
87 | if ($context !== []) { |
||
88 | dump($context); |
||
89 | } |
||
90 | } |
||
91 | break; |
||
92 | case 'debug': |
||
93 | if ($this->debug) { |
||
94 | $this->echoColor("[$level] " . $message . "\n", Color::GRAY); |
||
95 | if ($context !== []) { |
||
96 | dump($context); |
||
97 | } |
||
98 | } |
||
99 | break; |
||
100 | case 'echo': |
||
101 | $this->echoColor($message . "\n"); |
||
102 | break; |
||
103 | } |
||
140 |