Passed
Push — master ( b817d9...04ae87 )
by 世昌
02:23
created

AttachTrait::analyse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 2
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AttachTrait::isReplacedObj() 0 4 4
1
<?php
2
3
namespace suda\framework\debug\attach;
4
5
use Throwable;
6
7
/**
8
 * 多行附加属性
9
 */
10
trait AttachTrait
11
{
12
    /**
13
     * 属性数组
14
     *
15
     * @var array
16
     */
17
    protected $attribute = [];
18
19
    public function addAttribute(string $name, $value)
20
    {
21
        $this->attribute[$name] = $value;
22
    }
23
24
    protected function isReplacedObj($val): bool
25
    {
26
        return !is_array($val) && (!is_object($val)
27
                || method_exists($val, '__toString')) && (!$val instanceof Throwable);
28
    }
29
30
    public function interpolate(string $message, array $context, array $attribute)
31
    {
32
        $replace = [];
33
        $attribute = array_merge($this->attribute, $attribute);
34
        foreach ($attribute as $key => $val) {
35
            $replace['%' . $key . '%'] = $val;
36
        }
37
        $message = strtr($message, $replace);
38
        $attachInfo = '';
39
        foreach ($context as $name => $value) {
40
            $attachInfo .= $name . ' = ';
41
            if ($value instanceof AttachValueInterface) {
42
                $attachInfo .= $value->getLogAttach() . PHP_EOL;
43
            } else {
44
                $attachInfo .= DumpTrait::parameterToString($value) . PHP_EOL;
45
            }
46
        }
47
        if (strlen($attachInfo) > 0) {
48
            return $message . PHP_EOL . $attachInfo;
49
        }
50
        return $message;
51
    }
52
53
    /**
54
     * Get 属性数组
55
     *
56
     * @return  array
57
     */
58
    public function getAttribute()
59
    {
60
        return $this->attribute;
61
    }
62
}
63