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

eliashaeussler / cache-warmup / 7865581400

12 Feb 2024 12:06AM UTC coverage: 97.674%. Remained the same
7865581400

push

github

web-flow
[TASK] Update all dependencies

1092 of 1118 relevant lines covered (97.67%)

8.39 hits per line

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

97.22
/src/Log/Formatter/CompactLogFormatter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/cache-warmup".
7
 *
8
 * Copyright (C) 2020-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\CacheWarmup\Log\Formatter;
25

26
use DateTimeImmutable;
27
use DateTimeInterface;
28
use JsonSerializable;
29
use Stringable;
30

31
use function get_debug_type;
32
use function is_scalar;
33
use function json_encode;
34
use function preg_replace_callback;
35
use function sprintf;
36
use function strtoupper;
37

38
/**
39
 * CompactLogFormatter.
40
 *
41
 * @author Elias Häußler <elias@haeussler.dev>
42
 * @license GPL-3.0-or-later
43
 */
44
final class CompactLogFormatter implements LogFormatter
45
{
46
    private const TEMPLATE = '[%s] %s: %s %s';
47

48
    public function format(string $level, Stringable|string $message, array $context = []): string
4✔
49
    {
50
        $date = new DateTimeImmutable();
4✔
51
        $formattedMessage = $this->formatMessage($message, $context);
4✔
52
        $context = $this->formatContext($context);
4✔
53

54
        return sprintf(
4✔
55
            self::TEMPLATE,
4✔
56
            $date->format(DateTimeInterface::ATOM),
4✔
57
            strtoupper($level),
4✔
58
            $formattedMessage,
4✔
59
            json_encode($context),
4✔
60
        );
4✔
61
    }
62

63
    /**
64
     * @param array<string, mixed> $context
65
     */
66
    private function formatMessage(Stringable|string $message, array &$context = []): string
4✔
67
    {
68
        $formattedMessage = preg_replace_callback(
4✔
69
            '/\{([^\s}]+)}/',
4✔
70
            function (array $matches) use (&$context) {
4✔
71
                return $this->replacePlaceholder($matches[0], $matches[1], $context);
4✔
72
            },
4✔
73
            (string) $message,
4✔
74
        );
4✔
75

76
        // Return original message on failures
77
        if (null === $formattedMessage) {
4✔
78
            return (string) $message;
×
79
        }
80

81
        return $formattedMessage;
4✔
82
    }
83

84
    /**
85
     * @param array<string, mixed> $context
86
     */
87
    private function replacePlaceholder(string $placeholder, string $placeholderKey, array &$context): string
4✔
88
    {
89
        // Keep placeholder if no replacement is available
90
        if (!isset($context[$placeholderKey])) {
4✔
91
            return $placeholder;
1✔
92
        }
93

94
        // Replace placeholder value
95
        $value = $context[$placeholderKey];
3✔
96

97
        if (!is_scalar($value) && !($value instanceof Stringable)) {
3✔
98
            return $placeholder;
1✔
99
        }
100

101
        // Remove processed placeholder from context
102
        unset($context[$placeholderKey]);
2✔
103

104
        return (string) $value;
2✔
105
    }
106

107
    /**
108
     * @param array<string, mixed> $context
109
     *
110
     * @return array<string, mixed>
111
     */
112
    private function formatContext(array $context): array
4✔
113
    {
114
        foreach ($context as $key => $value) {
4✔
115
            if (is_scalar($value) || $value instanceof Stringable || $value instanceof JsonSerializable) {
2✔
116
                continue;
1✔
117
            }
118

119
            $context[$key] = get_debug_type($value);
1✔
120
        }
121

122
        return $context;
4✔
123
    }
124
}
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