ValueFormatter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B format() 0 23 6
A canFormat() 0 7 1
1
<?php
2
namespace Workana\AsyncJobs\Formatter;
3
4
/**
5
 * @author Carlos Frutos <[email protected]>
6
 */
7
class ValueFormatter implements FormatterInterface
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    public function format($target)
13
    {
14
        if (is_null($target)) {
15
            return '(null)';
16
        } elseif (is_array($target)) {
17
            return '(Array) {...}';
18
        } elseif (is_scalar($target)) {
19
            $type = gettype($target);
20
21
            if (is_bool($target)) {
22
                $value = $target ? 'true' : 'false';
23
            } else {
24
                $value = (string) $target;
25
            }
26
27
            return strtr('(:type) :value', [
28
                ':type' => $type,
29
                ':value' => $value,
30
            ]);
31
        } else {
32
            return strtr('(:class) {...}', [':class' => get_class($target)]);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function canFormat($target)
40
    {
41
        /**
42
         * Can format always
43
         */
44
        return true;
45
    }
46
}