Node   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 24
dl 0
loc 82
rs 10
c 3
b 1
f 1
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getScalarName() 0 18 6
A getArrayName() 0 5 1
A __construct() 0 4 1
A toArray() 0 5 1
A getName() 0 10 3
1
<?php
2
/**
3
 * This file is part of the koriym/printo package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Koriym\Printo;
8
9
final class Node implements NodeInterface
10
{
11
    /**
12
     * @var
13
     */
14
    private $value;
15
16
    /**
17
     * @var array
18
     */
19
    private $meta = [];
20
21
    /**
22
     * @param       $value
23
     * @param array $meta
24
     */
25
    public function __construct($value, array $meta)
26
    {
27
        $this->value = $value;
28
        $this->meta = $meta;
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    public function toArray()
35
    {
36
        $array = $this->meta + ['name' => $this->getName($this->value)];
37
38
        return $array;
39
    }
40
41
    /**
42
     * @param $value
43
     *
44
     * @return string
45
     */
46
    private function getName($value)
47
    {
48
        if (is_object($value)) {
49
            return get_class($value);
50
        }
51
        if (is_array($value)) {
52
            return $this->getArrayName();
53
        }
54
55
        return $this->getScalarName($value);
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    private function getArrayName()
62
    {
63
        $name = 'array';
64
65
        return $name;
66
    }
67
68
    /**
69
     * @param $value
70
     *
71
     * @return string
72
     */
73
    private function getScalarName($value)
74
    {
75
        if (is_bool($value)) {
76
            return $value ? 'true' : 'false';
77
        }
78
        if ($value === null) {
79
            return 'null';
80
        }
81
82
        if (is_string($value)) {
83
            return "'{$value}'";
84
        }
85
        if (is_int($value)) {
86
            return $value;
87
        }
88
        $name = sprintf('(%s) %s', gettype($value), (string) $value);
89
90
        return $name;
91
    }
92
}
93