Failed Conditions
Push — mixed-collections ( 23b545...27e0f6 )
by Michael
02:15
created

Dumper::visitCollectionPositionalEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Parser\Visitor;
6
7
use Doctrine\Annotations\Parser\Ast\Annotation;
8
use Doctrine\Annotations\Parser\Ast\Annotations;
9
use Doctrine\Annotations\Parser\Ast\ClassConstantFetch;
10
use Doctrine\Annotations\Parser\Ast\Collection\Collection;
11
use Doctrine\Annotations\Parser\Ast\Collection\Entry;
12
use Doctrine\Annotations\Parser\Ast\Collection\NamedEntry;
13
use Doctrine\Annotations\Parser\Ast\Collection\PositionalEntry;
14
use Doctrine\Annotations\Parser\Ast\ConstantFetch;
15
use Doctrine\Annotations\Parser\Ast\Node;
16
use Doctrine\Annotations\Parser\Ast\Pair;
17
use Doctrine\Annotations\Parser\Ast\Parameter\NamedParameter;
18
use Doctrine\Annotations\Parser\Ast\Parameter\UnnamedParameter;
19
use Doctrine\Annotations\Parser\Ast\Parameters;
20
use Doctrine\Annotations\Parser\Ast\Reference;
21
use Doctrine\Annotations\Parser\Ast\Scalar\BooleanScalar;
22
use Doctrine\Annotations\Parser\Ast\Scalar\FloatScalar;
23
use Doctrine\Annotations\Parser\Ast\Scalar\Identifier;
24
use Doctrine\Annotations\Parser\Ast\Scalar\IntegerScalar;
25
use Doctrine\Annotations\Parser\Ast\Scalar\NullScalar;
26
use Doctrine\Annotations\Parser\Ast\Scalar\StringScalar;
27
use const JSON_PRESERVE_ZERO_FRACTION;
28
use const JSON_UNESCAPED_UNICODE;
29
use function count;
30
use function json_encode;
31
use function str_repeat;
32
use function strlen;
33
use function substr;
34
35
final class Dumper implements Visitor
36
{
37
    private const INDENT = '>  ';
38
39
    /** @var int */
40
    private $depth = 0;
41
42
    public function visit(Node $node) : void
43
    {
44
        $node->dispatch($this);
45
    }
46
47
    public function visitAnnotations(Annotations $annotations) : void
48
    {
49
        $this->print(Annotations::class);
50
51
        $this->depth++;
52
        foreach ($annotations as $annotation) {
53
            $annotation->dispatch($this);
54
        }
55
        $this->depth--;
56
    }
57
58
    public function visitAnnotation(Annotation $annotation) : void
59
    {
60
        $this->print(Annotation::class);
61
62
        $this->depth++;
63
        $annotation->getName()->dispatch($this);
64
        $annotation->getParameters()->dispatch($this);
65
        $this->depth--;
66
    }
67
68
    public function visitReference(Reference $reference) : void
69
    {
70
        $this->print(
71
            Reference::class,
72
            ['identifier' => $reference->getIdentifier(), 'fully_qualified' => $reference->isFullyQualified()]
73
        );
74
    }
75
76
    public function visitParameters(Parameters $parameters) : void
77
    {
78
        $this->print(Parameters::class);
79
80
        $this->depth++;
81
        foreach ($parameters as $parameter) {
82
            $parameter->dispatch($this);
83
        }
84
        $this->depth--;
85
    }
86
87
    public function visitNamedParameter(NamedParameter $parameter) : void
88
    {
89
        $this->print(NamedParameter::class);
90
91
        $this->depth++;
92
        $parameter->getName()->dispatch($this);
93
        $parameter->getValue()->dispatch($this);
94
        $this->depth--;
95
    }
96
97
    public function visitUnnamedParameter(UnnamedParameter $parameter) : void
98
    {
99
        $this->print(UnnamedParameter::class);
100
101
        $this->depth++;
102
        $parameter->getValue()->dispatch($this);
103
        $this->depth--;
104
    }
105
106
    public function visitCollection(Collection $collection) : void
107
    {
108
        $this->print(Collection::class);
109
110
        $this->depth++;
111
        foreach ($collection as $item) {
112
            $item->dispatch($this);
113
        }
114
        $this->depth--;
115
    }
116
117
    public function visitCollectionPositionalEntry(PositionalEntry $entry) : void
118
    {
119
        $this->print(Entry::class);
120
121
        $this->depth++;
122
        $entry->getValue()->dispatch($this);
123
        $this->depth--;
124
    }
125
126
    public function visitCollectionNamedEntry(NamedEntry $entry) : void
127
    {
128
        $this->print(Entry::class);
129
130
        $this->depth++;
131
        $entry->getKey()->dispatch($this);
132
        $entry->getValue()->dispatch($this);
133
        $this->depth--;
134
    }
135
136
    public function visitPair(Pair $pair) : void
137
    {
138
        $this->print(Pair::class);
139
140
        $this->depth++;
141
        $pair->getKey()->dispatch($this);
142
        $pair->getValue()->dispatch($this);
143
        $this->depth--;
144
    }
145
146
    public function visitIdentifier(Identifier $identifier) : void
147
    {
148
        $this->print(Identifier::class, ['value' => $identifier->getValue()]);
149
    }
150
151
    public function visitConstantFetch(ConstantFetch $constantFetch) : void
152
    {
153
        $this->print(ConstantFetch::class);
154
155
        $this->depth++;
156
        $constantFetch->getName()->dispatch($this);
157
        $this->depth--;
158
    }
159
160
    public function visitClassConstantFetch(ClassConstantFetch $classConstantFetch) : void
161
    {
162
        $this->print(ClassConstantFetch::class);
163
164
        $this->depth++;
165
        $classConstantFetch->getClass()->dispatch($this);
166
        $classConstantFetch->getName()->dispatch($this);
167
        $this->depth--;
168
    }
169
170
    public function visitNullScalar(NullScalar $nullScalar) : void
171
    {
172
        $this->print(NullScalar::class, ['value' => $nullScalar->getValue()]);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $nullScalar->getValue() targeting Doctrine\Annotations\Par...\NullScalar::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
173
    }
174
175
    public function visitBooleanScalar(BooleanScalar $booleanScalar) : void
176
    {
177
        $this->print(BooleanScalar::class, ['value' => $booleanScalar->getValue()]);
178
    }
179
180
    public function visitIntegerScalar(IntegerScalar $integerScalar) : void
181
    {
182
        $this->print(IntegerScalar::class, ['value' => $integerScalar->getValue()]);
183
    }
184
185
    public function visitFloatScalar(FloatScalar $floatScalar) : void
186
    {
187
        $this->print(FloatScalar::class, ['value' => $floatScalar->getValue()]);
188
    }
189
190
    public function visitStringScalar(StringScalar $stringScalar) : void
191
    {
192
        $this->print(StringScalar::class, ['value' => $stringScalar->getValue()]);
193
    }
194
195
    /**
196
     * @param mixed[] $data
197
     */
198
    private function print(string $name, array $data = []) : void
199
    {
200
        echo str_repeat(self::INDENT, $this->depth + 1);
201
        echo substr($name, strlen(Node::class) - 4);
202
203
        if (count($data) !== 0) {
204
            echo ' ', json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION);
205
        }
206
207
        echo "\n";
208
    }
209
}
210