Passed
Pull Request — 0.6.x (#223)
by Shinji
01:41
created

CompatCallTraceFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 24
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 6 2
A format() 0 7 2
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the reliforp/reli-prof package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Reli\Inspector\Output\TraceFormatter\Compat;
15
16
use Reli\Inspector\Output\TraceFormatter\CallTraceFormatter;
17
use Reli\Lib\PhpProcessReader\CallTrace;
18
19
use function join;
20
21
final class CompatCallTraceFormatter implements CallTraceFormatter
22
{
23
    private static ?self $cache;
24
25
    public function __construct(
26
        private CompatCallFrameFormatter $call_frame_formatter,
27
    ) {
28
    }
29
30
    public static function getInstance(): self
31
    {
32
        if (!isset(self::$cache)) {
33
            self::$cache = new self(new CompatCallFrameFormatter());
34
        }
35
        return self::$cache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::cache could return the type null which is incompatible with the type-hinted return Reli\Inspector\Output\Tr...ompatCallTraceFormatter. Consider adding an additional type-check to rule them out.
Loading history...
36
    }
37
38
    public function format(CallTrace $call_trace): string
39
    {
40
        $frames = [];
41
        foreach ($call_trace->call_frames as $call_frame) {
42
            $frames[] = $this->call_frame_formatter->format($call_frame);
43
        }
44
        return join(PHP_EOL, $frames);
45
    }
46
}
47