Logger::writeDebugLog()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.9256

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
ccs 8
cts 12
cp 0.6667
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5.9256
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine;
4
5
class Logger
6
{
7
    /**
8
     * Flag to determine whether a debug log should be generated by the calculation engine
9
     *        If true, then a debug log will be generated
10
     *        If false, then a debug log will not be generated.
11
     */
12
    private bool $writeDebugLog = false;
13
14
    /**
15
     * Flag to determine whether a debug log should be echoed by the calculation engine
16
     *        If true, then a debug log will be echoed
17
     *        If false, then a debug log will not be echoed
18
     * A debug log can only be echoed if it is generated.
19
     */
20
    private bool $echoDebugLog = false;
21
22
    /**
23
     * The debug log generated by the calculation engine.
24
     *
25
     * @var string[]
26
     */
27
    private array $debugLog = [];
28
29
    /**
30
     * The calculation engine cell reference stack.
31
     */
32
    private CyclicReferenceStack $cellStack;
33
34
    /**
35
     * Instantiate a Calculation engine logger.
36
     */
37 10544
    public function __construct(CyclicReferenceStack $stack)
38
    {
39 10544
        $this->cellStack = $stack;
40
    }
41
42
    /**
43
     * Enable/Disable Calculation engine logging.
44
     */
45 1115
    public function setWriteDebugLog(bool $writeDebugLog): void
46
    {
47 1115
        $this->writeDebugLog = $writeDebugLog;
48
    }
49
50
    /**
51
     * Return whether calculation engine logging is enabled or disabled.
52
     */
53 12511
    public function getWriteDebugLog(): bool
54
    {
55 12511
        return $this->writeDebugLog;
56
    }
57
58
    /**
59
     * Enable/Disable echoing of debug log information.
60
     */
61 129
    public function setEchoDebugLog(bool $echoDebugLog): void
62
    {
63 129
        $this->echoDebugLog = $echoDebugLog;
64
    }
65
66
    /**
67
     * Return whether echoing of debug log information is enabled or disabled.
68
     */
69 129
    public function getEchoDebugLog(): bool
70
    {
71 129
        return $this->echoDebugLog;
72
    }
73
74
    /**
75
     * Write an entry to the calculation engine debug log.
76
     */
77 11971
    public function writeDebugLog(string $message, mixed ...$args): void
78
    {
79
        //    Only write the debug log if logging is enabled
80 11971
        if ($this->writeDebugLog) {
81 3
            $message = sprintf($message, ...$args); //* @phpstan-ignore-line
82 3
            $cellReference = implode(' -> ', $this->cellStack->showStack());
83 3
            if ($this->echoDebugLog) {
84
                echo $cellReference,
85
                ($this->cellStack->count() > 0 ? ' => ' : ''),
86
                $message,
87
                PHP_EOL;
88
            }
89 3
            $this->debugLog[] = $cellReference
90 3
                . ($this->cellStack->count() > 0 ? ' => ' : '')
91 3
                . $message;
92
        }
93
    }
94
95
    /**
96
     * Write a series of entries to the calculation engine debug log.
97
     *
98
     * @param string[] $args
99
     */
100
    public function mergeDebugLog(array $args): void
101
    {
102
        if ($this->writeDebugLog) {
103
            foreach ($args as $entry) {
104
                $this->writeDebugLog($entry);
105
            }
106
        }
107
    }
108
109
    /**
110
     * Clear the calculation engine debug log.
111
     */
112 8319
    public function clearLog(): void
113
    {
114 8319
        $this->debugLog = [];
115
    }
116
117
    /**
118
     * Return the calculation engine debug log.
119
     *
120
     * @return string[]
121
     */
122 3
    public function getLog(): array
123
    {
124 3
        return $this->debugLog;
125
    }
126
}
127