MessageInterpolationTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A interpolateMessage() 0 17 4
1
<?php
2
3
namespace SubjectivePHP\Psr\Log;
4
5
/**
6
 * Trait for PSR-3 Message Interpolation.
7
 */
8
trait MessageInterpolationTrait
9
{
10
    /**
11
     * Interpolates context values into the message placeholders.
12
     *
13
     * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#12-message
14
     *
15
     * @param string $message The string containing the placeholders.
16
     * @param array  $context The key/value array of replacement values.
17
     *
18
     * @return string
19
     */
20
    protected function interpolateMessage($message, array $context)//@codingStandardsIgnoreLine Ignore missing type hint
21
    {
22
        $context = array_filter(
23
            $context,
24
            function ($value) {
25
                return (is_scalar($value) || (is_object($value) && method_exists($value, '__toString')));
26
            }
27
        );
28
29
        $search = [];
30
        $replace = [];
31
        foreach ($context as $key => $value) {
32
            $search[] = "{{$key}}";
33
            $replace[] = (string)$value;
34
        }
35
36
        return str_replace($search, $replace, $message);
37
    }
38
}
39