Passed
Push — main ( 87d9c5...5f2e15 )
by ANDREY
02:24
created

JSConsoleLogger::log()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 51
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 44
c 3
b 0
f 0
nc 6
nop 3
dl 0
loc 51
rs 8.5937
ccs 19
cts 19
cp 1
crap 6

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace VPA\Logger;
6
7
use Psr\Log\AbstractLogger;
8
use Psr\Log\LogLevel;
9
use VPA\DI\Container;
10
use VPA\DI\Injectable;
11
12
#[Injectable()]
13
class JSConsoleLogger extends BaseLogger
14
{
15
    /**
16
     * Table information.
17
     *
18
     * @param string $message
19
     * @param array $context
20
     * @return void
21
     */
22 1
    public function table(string|\Stringable $message, array $context = []): void
23
    {
24 1
        $this->log('table', $message, $context);
25
    }
26
27
    /**
28
     * Logs with an arbitrary level.
29
     *
30
     * @param mixed $level
31
     * @param string $message
32
     * @param array $context
33
     * @return void
34
     */
35 10
    public function log(mixed $level, string|\Stringable $message, array $context = []): void
36
    {
37 10
        $text = $this->interpolate($message, $context);
38 10
        switch ($level) {
39
            case LogLevel::ERROR:
40 1
                printf(
41 1
                    "<script>console.error('%s [%s] %s');</script>",
42
                    date('y-m-d H:i:s'),
43 1
                    $level,
44 1
                    $this->stringFormat($text)
45
                );
46 1
                break;
47 1
            case LogLevel::DEBUG:
48
                printf(
49 1
                    "<script>console.debug('%s [%s] %s');</script>",
50 1
                    date('y-m-d H:i:s'),
51 6
                    $level,
52 1
                    $this->stringFormat($text)
53 1
                );
54
                break;
55 5
            case LogLevel::INFO:
56
                printf(
57
                    "<script>console.info('%s [%s] %s');</script>",
58
                    date('y-m-d H:i:s'),
59 10
                    $level,
60
                    $this->stringFormat($text)
61 10
                );
62 10
                break;
63
            case LogLevel::WARNING:
64 10
                printf(
65
                    "<script>console.warn('%s [%s] %s');</script>",
66
                    date('y-m-d H:i:s'),
67
                    $level,
68
                    $this->stringFormat($text)
69
                );
70
                break;
71
            case 'table':
72
                printf(
73
                    "<script>console.debug('%s [%s] %s');console.table(%s);</script>",
74
                    date('y-m-d H:i:s'),
75
                    $level,
76
                    $this->stringFormat($message),
77
                    json_encode($context)
78
                );
79
                break;
80
            default:
81
                printf(
82
                    "<script>console.log('%s [%s] %s');</script>",
83
                    date('y-m-d H:i:s'),
84
                    $level,
85
                    $this->stringFormat($text)
86
                );
87
        }
88
    }
89
90
    private function stringFormat(string $input): string
91
    {
92
        $output = addcslashes($input, "`'\"");
93
        $output = str_replace("\n", "\\n", $output);
94
        //$output= str_replace("\\","\\\\", $output);
95
        return $output;
96
    }
97
}
98