ExceptionFormatTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 27
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A format() 0 19 5
1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/exceptions
6
 * @copyright   Copyright (c) 2019 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace Webino;
12
13
/**
14
 * Trait ExceptionFormatTrait
15
 *
16
 * Used to format the exception message.
17
 *
18
 * @package exception
19
 */
20
trait ExceptionFormatTrait
21
{
22
    /**
23
     * Format the exception message via sprintf()
24
     *
25
     * @param array<int, mixed> $params Message parameters
26
     * @return $this
27
     */
28
    public function format(...$params)
29
    {
30
        if (empty($this->message)) {
31
            return $this;
32
        }
33
34
        $args = [$this->message];
35
        foreach ($params as $param) {
36
            if (is_string($param)) {
37
                $args[] = '`' . $param . '`';
38
            } elseif (is_object($param)) {
39
                $args[] = '`' . get_class($param) . '`';
40
            } else {
41
                $args[] = '`' . var_export($param, true) . '`';
42
            }
43
        }
44
45
        $this->message = call_user_func('sprintf', ...$args);
0 ignored issues
show
Bug Best Practice introduced by
The property message does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        return $this;
47
    }
48
}
49