Passed
Push — master ( a51a38...e61121 )
by 世昌
01:50
created

AttachTrait::analyse()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 9.6111
1
<?php
2
namespace nebula\component\debug\attach;
3
4
/**
5
 * 多行附加属性
6
 */
7
trait AttachTrait
8
{
9
    /**
10
     * 属性数组
11
     *
12
     * @var array
13
     */
14
    protected $attribute;
15
    /**
16
     * 附加数组
17
     *
18
     * @var array
19
     */
20
    protected $attach;
21
22
    
23
    public function mergeAttribute(array $value)
24
    {
25
        $this->attribute = array_merge($this->attribute, $value);
26
    }
27
28
    protected function analyse(array $context)
29
    {
30
        $replace = [];
31
        foreach ($context as $key => $val) {
32
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
33
                $replace['{' . $key . '}'] = $val;
34
            } else {
35
                $this->attach[$key] = $val;
36
            }
37
        }
38
        return $replace;
39
    }
40
41
    public function interpolate(string $message, array $context)
42
    {
43
        $replace =  $this->analyse($context);
44
        foreach ($this->attribute as $key => $val) {
45
            $replace['%' . $key . '%'] = $val;
46
        }
47
        $message = strtr($message, $replace);
48
        foreach ($this->attach as $name => $value) {
49
            $message.=PHP_EOL.$name.'<' . get_class($val).'>'.PHP_EOL;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $val seems to be defined by a foreach iteration on line 44. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
50
            if ($value instanceof AttachValueInterface) {
51
                $message.= $value->getLogAttach().PHP_EOL;
52
            } elseif ($value instanceof \Exception) {
53
                $message.= DumpTrait::dumpException($value).PHP_EOL;
54
            } else {
55
                $message.= DumpTrait::parameterToString($value).PHP_EOL;
56
            }
57
        }
58
        return $message;
59
    }
60
}
61