Test Failed
Push — master ( e081e8...a6938b )
by Kirill
02:31
created

Renderer   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 27 8
A node() 0 4 1
A opcode() 0 4 1
A value() 0 4 1
A scalar() 0 17 5
A iterable() 0 12 3
A object() 0 4 1
A file() 0 4 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL;
11
12
use Railt\Io\Readable;
13
use Railt\Parser\Ast\NodeInterface;
14
use Railt\SDL\Frontend\IR\OpcodeInterface;
15
use Railt\SDL\Frontend\IR\Value\ValueInterface;
16
17
/**
18
 * Class Renderer
19
 */
20
class Renderer
21
{
22
    /**
23
     * @param mixed $value
24
     * @return string
25
     */
26
    public static function render($value): string
27
    {
28
        switch (true) {
29
            case $value instanceof NodeInterface:
30
                return self::node($value);
31
32
            case $value instanceof OpcodeInterface:
33
                return self::opcode($value);
34
35
            case $value instanceof ValueInterface:
36
                return self::value($value);
37
38
            case $value instanceof Readable:
39
                return self::file($value);
40
41
            case \is_scalar($value):
42
                return self::scalar($value);
43
44
            case \is_iterable($value):
45
                return self::iterable($value);
0 ignored issues
show
Documentation introduced by
$value is of type object|array|null, but the function expects a object<Railt\SDL\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
47
            case \is_object($value):
48
                return self::object($value);
49
        }
50
51
        return \gettype($value);
52
    }
53
54
    /**
55
     * @param NodeInterface $ast
56
     * @return string
57
     */
58
    private static function node(NodeInterface $ast): string
59
    {
60
        return '<' . $ast->getName() . '>';
61
    }
62
63
    /**
64
     * @param OpcodeInterface $opcode
65
     * @return string
66
     */
67
    private static function opcode(OpcodeInterface $opcode): string
68
    {
69
        return '#' . $opcode->getId() . ' ' . $opcode->getName();
70
    }
71
72
    /**
73
     * @param ValueInterface $value
74
     * @return string
75
     */
76
    private static function value(ValueInterface $value): string
77
    {
78
        return (string)$value;
79
    }
80
81
    /**
82
     * @param mixed $value
83
     * @return string
84
     */
85
    private static function scalar($value): string
86
    {
87
        switch (true) {
88
            case $value === null:
89
                return 'null';
90
91
            case \is_bool($value):
92
                return $value ? 'true' : 'false';
93
94
            case \is_string($value):
95
                $minified = \preg_replace('/\s+/', ' ', (string)$value);
96
                return '"' . \addcslashes($minified, '"') . '"';
97
98
            default:
99
                return (string)$value;
100
        }
101
    }
102
103
    /**
104
     * @param iterable $values
105
     * @return string
106
     */
107
    private static function iterable(iterable $values): string
108
    {
109
        $arguments = [];
110
111
        foreach ($values as $value) {
112
            $arguments[] = self::render($value);
113
        }
114
115
        $name = \is_array($values) ? 'array' : \get_class($values);
116
117
        return \sprintf('%s(%s)', $name, \implode(', ', $arguments));
118
    }
119
120
    /**
121
     * @param mixed $value
122
     * @return string
123
     */
124
    private static function object($value): string
125
    {
126
        return \get_class($value) . '#' . \spl_object_hash($value);
127
    }
128
129
    /**
130
     * @param Readable $readable
131
     * @return string
132
     */
133
    private static function file(Readable $readable): string
134
    {
135
        return '(file)' . $readable->getPathname();
136
    }
137
}
138