StringHandler::handler()   A
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 13
rs 9.2222
1
<?php
2
3
namespace Tleckie\Log\Formatter\Handler;
4
5
/**
6
 * Class StringHandler
7
 *
8
 * @package Tleckie\Log\Formatter\Handler
9
 * @author  Teodoro Leckie Westberg <[email protected]>
10
 */
11
class StringHandler extends Handler
12
{
13
    /**
14
     * @param mixed $message
15
     * @param array $context
16
     * @return string
17
     */
18
    public function handler(mixed $message, array $context = []): string
19
    {
20
        if (is_string($message)) {
21
            $replace = [];
22
            foreach ($context as $key => $value) {
23
                if (is_string($value) || is_numeric($value) || method_exists($value, '__toString')) {
24
                    $replace['{' . $key . '}'] = $value;
25
                }
26
            }
27
            $message = sprintf('%s', strtr($message, $replace));
28
        }
29
30
        return parent::handler($message, $context);
31
    }
32
}
33