• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 14049246919

24 Mar 2025 09:42PM UTC coverage: 79.353% (-0.04%) from 79.391%
14049246919

push

github

web-flow
feat(support): support array parameters in string manipulations (#1073)

48 of 48 new or added lines in 2 files covered. (100.0%)

735 existing lines in 126 files now uncovered.

10492 of 13222 relevant lines covered (79.35%)

90.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/src/Tempest/Debug/src/Debug.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Debug;
6

7
use Exception;
8
use Symfony\Component\VarDumper\Cloner\VarCloner;
9
use Symfony\Component\VarDumper\Dumper\CliDumper;
10
use Symfony\Component\VarDumper\VarDumper;
11
use Tempest\Container\GenericContainer;
12
use Tempest\EventBus\EventBus;
13
use Tempest\Highlight\Themes\TerminalStyle;
14
use Tempest\Log\LogConfig;
15

16
final readonly class Debug
17
{
18
    private function __construct(
5✔
19
        private ?LogConfig $logConfig = null,
20
        private ?EventBus $eventBus = null,
21
    ) {}
5✔
22

23
    public static function resolve(): self
5✔
24
    {
25
        try {
26
            $container = GenericContainer::instance();
5✔
27

28
            return new self(
5✔
29
                logConfig: $container?->get(LogConfig::class),
5✔
30
                eventBus: $container?->get(EventBus::class),
5✔
31
            );
5✔
UNCOV
32
        } catch (Exception) {
×
33
            return new self();
×
34
        }
35
    }
36

37
    public function log(array $items, bool $writeToLog = true, bool $writeToOut = true): void
5✔
38
    {
39
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
5✔
40
        $callPath = $trace[1]['file'] . ':' . $trace[1]['line'];
5✔
41

42
        if ($writeToLog) {
5✔
43
            $this->writeToLog($items, $callPath);
4✔
44
        }
45

46
        if ($writeToOut) {
5✔
UNCOV
47
            $this->writeToOut($items, $callPath);
×
48
        }
49

50
        $this->eventBus?->dispatch(new ItemsDebugged($items));
5✔
51
    }
52

53
    private function writeToLog(array $items, string $callPath): void
4✔
54
    {
55
        if ($this->logConfig === null) {
4✔
UNCOV
56
            return;
×
57
        }
58

59
        if (! $this->logConfig->debugLogPath) {
4✔
UNCOV
60
            return;
×
61
        }
62

63
        $directory = dirname($this->logConfig->debugLogPath);
4✔
64

65
        if (! is_dir($directory)) {
4✔
66
            mkdir(directory: $directory, recursive: true);
1✔
67
        }
68

69
        $handle = @fopen($this->logConfig->debugLogPath, 'a');
4✔
70

71
        if (! $handle) {
4✔
UNCOV
72
            return;
×
73
        }
74

75
        foreach ($items as $key => $item) {
4✔
76
            $output = $this->createDump($item) . $callPath;
4✔
77

78
            fwrite($handle, "{$key} " . $output . PHP_EOL);
4✔
79
        }
80

81
        fclose($handle);
4✔
82
    }
83

UNCOV
84
    private function writeToOut(array $items, string $callPath): void
×
85
    {
UNCOV
86
        foreach ($items as $key => $item) {
×
87
            if (defined('STDOUT')) {
×
88
                fwrite(STDOUT, TerminalStyle::BG_BLUE(" {$key} ") . ' ');
×
89

UNCOV
90
                $output = $this->createDump($item);
×
91

UNCOV
92
                fwrite(STDOUT, $output);
×
93

UNCOV
94
                fwrite(STDOUT, $callPath . PHP_EOL);
×
95
            } else {
UNCOV
96
                echo sprintf(
×
97
                        '<span style="
×
98
                    display:inline-block; 
99
                    color: #fff; 
100
                    font-family: %s;
101
                    padding: 2px 4px;
102
                    font-size: 0.8rem;
103
                    margin-bottom: -12px;
104
                    background: #0071BC;"
UNCOV
105
                >%s (%s)</span>',
×
106
                        'Source Code Pro, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
×
107
                        $key,
×
108
                        $callPath,
×
109
                    );
×
110

UNCOV
111
                VarDumper::dump($item);
×
112
            }
113
        }
114
    }
115

116
    private function createDump(mixed $input): string
4✔
117
    {
118
        $cloner = new VarCloner();
4✔
119

120
        $output = '';
4✔
121

122
        $dumper = new CliDumper(function ($line, $depth) use (&$output): void {
4✔
123
            if ($depth < 0) {
4✔
124
                return;
4✔
125
            }
126

127
            $output .= str_repeat(' ', $depth) . $line . "\n";
4✔
128
        });
4✔
129

130
        $dumper->setColors(true);
4✔
131

132
        $dumper->dump($cloner->cloneVar($input));
4✔
133

134
        return preg_replace(
4✔
135
            pattern: '/\e](.*)\e]8;;\e/',
4✔
136
            replacement: '',
4✔
137
            subject: $output,
4✔
138
        );
4✔
139
    }
140
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc