Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
19 / 19 |
|
88.00% |
22 / 25 |
|
31.82% |
7 / 22 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| TextFormatter | |
100.00% |
19 / 19 |
|
88.00% |
22 / 25 |
|
31.82% |
7 / 22 |
|
100.00% |
4 / 4 |
34.67 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| format | |
100.00% |
6 / 6 |
|
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| interpolate | |
100.00% |
8 / 8 |
|
83.33% |
10 / 12 |
|
16.67% |
2 / 12 |
|
100.00% |
1 / 1 |
8.21 | |||
| transform | |
100.00% |
4 / 4 |
|
85.71% |
6 / 7 |
|
16.67% |
1 / 6 |
|
100.00% |
1 / 1 |
4.31 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celemas\Log\Formatter; |
| 6 | |
| 7 | use Celemas\Log\Formatter; |
| 8 | use Override; |
| 9 | |
| 10 | /** @api */ |
| 11 | final class TextFormatter implements Formatter |
| 12 | { |
| 13 | use PreparesValue; |
| 14 | |
| 15 | public function __construct( |
| 16 | private readonly bool $includeTraceback = true, |
| 17 | ) {} |
| 18 | |
| 19 | #[Override] |
| 20 | public function format(string $message, array $context = []): string |
| 21 | { |
| 22 | if ($context === []) { |
| 23 | return $message; |
| 24 | } |
| 25 | |
| 26 | [$message, $context] = $this->interpolate($message, $context); |
| 27 | |
| 28 | if ($context === []) { |
| 29 | return $message; |
| 30 | } |
| 31 | |
| 32 | return $message . ":\n" . $this->transform($context); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param array<array-key, mixed> $context |
| 37 | * @return array{string, array<array-key, mixed>} |
| 38 | */ |
| 39 | private function interpolate(string $message, array $context): array |
| 40 | { |
| 41 | $substitutes = []; |
| 42 | |
| 43 | foreach (array_keys($context) as $key) { |
| 44 | $placeholder = '{' . $key . '}'; |
| 45 | |
| 46 | if (!str_contains($message, $placeholder)) { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | $substitutes[$placeholder] = $this->prepare($context[$key], $this->includeTraceback); |
| 51 | unset($context[$key]); |
| 52 | } |
| 53 | |
| 54 | return [strtr($message, $substitutes), $context]; |
| 55 | } |
| 56 | |
| 57 | /** @param array<array-key, mixed> $context */ |
| 58 | private function transform(array $context): string |
| 59 | { |
| 60 | $lines = []; |
| 61 | |
| 62 | foreach (array_keys($context) as $key) { |
| 63 | $lines[] = " [{$key}] => " . $this->prepare($context[$key], $this->includeTraceback, ' '); |
| 64 | } |
| 65 | |
| 66 | return implode("\n", $lines); |
| 67 | } |
| 68 | } |
Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not
necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once.
Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement
always has an else as part of its logical flow even if you didn't write one.
| 16 | private readonly bool $includeTraceback = true, |
| 17 | ) {} |
| 20 | public function format(string $message, array $context = []): string |
| 21 | { |
| 22 | if ($context === []) { |
| 23 | return $message; |
| 26 | [$message, $context] = $this->interpolate($message, $context); |
| 27 | |
| 28 | if ($context === []) { |
| 29 | return $message; |
| 32 | return $message . ":\n" . $this->transform($context); |
| 33 | } |
| 39 | private function interpolate(string $message, array $context): array |
| 40 | { |
| 41 | $substitutes = []; |
| 42 | |
| 43 | foreach (array_keys($context) as $key) { |
| 43 | foreach (array_keys($context) as $key) { |
| 44 | $placeholder = '{' . $key . '}'; |
| 45 | |
| 46 | if (!str_contains($message, $placeholder)) { |
| 46 | if (!str_contains($message, $placeholder)) { |
| 46 | if (!str_contains($message, $placeholder)) { |
| 46 | if (!str_contains($message, $placeholder)) { |
| 47 | continue; |
| 43 | foreach (array_keys($context) as $key) { |
| 44 | $placeholder = '{' . $key . '}'; |
| 45 | |
| 46 | if (!str_contains($message, $placeholder)) { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | $substitutes[$placeholder] = $this->prepare($context[$key], $this->includeTraceback); |
| 43 | foreach (array_keys($context) as $key) { |
| 44 | $placeholder = '{' . $key . '}'; |
| 45 | |
| 46 | if (!str_contains($message, $placeholder)) { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | $substitutes[$placeholder] = $this->prepare($context[$key], $this->includeTraceback); |
| 51 | unset($context[$key]); |
| 52 | } |
| 53 | |
| 54 | return [strtr($message, $substitutes), $context]; |
| 54 | return [strtr($message, $substitutes), $context]; |
| 54 | return [strtr($message, $substitutes), $context]; |
| 54 | return [strtr($message, $substitutes), $context]; |
| 55 | } |
| 58 | private function transform(array $context): string |
| 59 | { |
| 60 | $lines = []; |
| 61 | |
| 62 | foreach (array_keys($context) as $key) { |
| 62 | foreach (array_keys($context) as $key) { |
| 62 | foreach (array_keys($context) as $key) { |
| 63 | $lines[] = " [{$key}] => " . $this->prepare($context[$key], $this->includeTraceback, ' '); |
| 62 | foreach (array_keys($context) as $key) { |
| 63 | $lines[] = " [{$key}] => " . $this->prepare($context[$key], $this->includeTraceback, ' '); |
| 64 | } |
| 65 | |
| 66 | return implode("\n", $lines); |
| 66 | return implode("\n", $lines); |
| 66 | return implode("\n", $lines); |
| 66 | return implode("\n", $lines); |
| 67 | } |