• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jojo1981 / json-ast-builder / 17578687754

09 Sep 2025 09:48AM UTC coverage: 35.019%. Remained the same
17578687754

push

github

jojo1981
Fix: Make compatible with PHP versions: ^8.0|^8.1|^8.2|^8.3|^8.4. Closes #1
https://github.com/jojo1981/json-ast-builder/issues/1

161 of 407 new or added lines in 17 files covered. (39.56%)

290 existing lines in 25 files now uncovered.

374 of 1068 relevant lines covered (35.02%)

3.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Visitor/DataGeneratorVisitor.php
1
<?php
2
/*
3
 * This file is part of the jojo1981/json-ast-builder package
4
 *
5
 * Copyright (c) 2019 Joost Nijhuis <jnijhuis81@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed in the root of the source code
9
 */
10
declare(strict_types=1);
11

12
namespace Jojo1981\JsonAstBuilder\Visitor;
13

14
use Jojo1981\JsonAstBuilder\Ast\ArrayNode;
15
use Jojo1981\JsonAstBuilder\Ast\BooleanNode;
16
use Jojo1981\JsonAstBuilder\Ast\ElementNode;
17
use Jojo1981\JsonAstBuilder\Ast\IntegerNode;
18
use Jojo1981\JsonAstBuilder\Ast\JsonNode;
19
use Jojo1981\JsonAstBuilder\Ast\KeyNode;
20
use Jojo1981\JsonAstBuilder\Ast\MemberNode;
21
use Jojo1981\JsonAstBuilder\Ast\NullNode;
22
use Jojo1981\JsonAstBuilder\Ast\NumberNode;
23
use Jojo1981\JsonAstBuilder\Ast\ObjectNode;
24
use Jojo1981\JsonAstBuilder\Ast\StringNode;
25
use Jojo1981\JsonAstBuilder\Ast\ValueNode;
26
use stdClass;
27
use function array_merge;
28

29
/**
30
 * @package Jojo1981\JsonAstBuilder\Visitor
31
 */
32
final class DataGeneratorVisitor implements VisitorInterface
33
{
34
    /** @var array */
35
    private array $options;
36

37
    /**
38
     * @param array $options
39
     */
40
    public function __construct(array $options = [])
41
    {
NEW
42
        $this->options = array_merge(['assoc' => false], $options);
×
43
    }
44

45
    /**
46
     * @param JsonNode $jsonNode
47
     * @return mixed
48
     */
49
    public function visitJsonNode(JsonNode $jsonNode): mixed
50
    {
UNCOV
51
        return $jsonNode->getElement()->accept($this);
×
52
    }
53

54
    /**
55
     * @param ElementNode $elementNode
56
     * @return mixed
57
     */
58
    public function visitElementNode(ElementNode $elementNode): mixed
59
    {
UNCOV
60
        return $elementNode->getValue()->accept($this);
×
61
    }
62

63
    /**
64
     * @param ValueNode $valueNode
65
     * @return mixed
66
     */
67
    public function visitValueNode(ValueNode $valueNode): mixed
68
    {
UNCOV
69
        return $valueNode->getType()->accept($this);
×
70
    }
71

72
    /**
73
     * @param ObjectNode $objectNode
74
     * @return array|stdClass
75
     */
76
    public function visitObjectNode(ObjectNode $objectNode): array|stdClass
77
    {
NEW
78
        $result = $this->isAssoc() ? [] : new stdClass();
×
79

UNCOV
80
        foreach ($objectNode->getMembers() as $member) {
×
UNCOV
81
            [$key, $value] = $member->accept($this);
×
82
            if ($this->isAssoc()) {
×
UNCOV
83
                $result[$key] = $value;
×
84
            } else {
85
                $result->{$key} = $value;
×
86
            }
87
        }
88

89
        return $result;
×
90
    }
91

92
    /**
93
     * @return bool
94
     */
95
    private function isAssoc(): bool
96
    {
NEW
97
        return $this->options['assoc'];
×
98
    }
99

100
    /**
101
     * @param ArrayNode $arrayNode
102
     * @return array
103
     */
104
    public function visitArrayNode(ArrayNode $arrayNode): array
105
    {
UNCOV
106
        $result = [];
×
UNCOV
107
        foreach ($arrayNode->getElements() as $element) {
×
UNCOV
108
            $result[] = $element->accept($this);
×
109
        }
110

UNCOV
111
        return $result;
×
112
    }
113

114
    /**
115
     * @param StringNode $stringNode
116
     * @return string
117
     */
118
    public function visitStringNode(StringNode $stringNode): string
119
    {
120
        return $stringNode->getValue();
×
121
    }
122

123
    /**
124
     * @param KeyNode $keyNode
125
     * @return string
126
     */
127
    public function visitKeyNode(KeyNode $keyNode): string
128
    {
UNCOV
129
        return $keyNode->getValue();
×
130
    }
131

132
    /**
133
     * @param NumberNode $numberNode
134
     * @return float
135
     */
136
    public function visitNumberNode(NumberNode $numberNode): float
137
    {
UNCOV
138
        return $numberNode->getValue();
×
139
    }
140

141
    /**
142
     * @param IntegerNode $integerNode
143
     * @return int
144
     */
145
    public function visitIntegerNode(IntegerNode $integerNode): int
146
    {
UNCOV
147
        return $integerNode->getValue();
×
148
    }
149

150
    /**
151
     * @param BooleanNode $booleanNode
152
     * @return bool
153
     */
154
    public function visitBooleanNode(BooleanNode $booleanNode): bool
155
    {
UNCOV
156
        return $booleanNode->getValue();
×
157
    }
158

159
    /**
160
     * @param NullNode $nullNode
161
     * @return null
162
     */
163
    public function visitNullNode(NullNode $nullNode): mixed
164
    {
UNCOV
165
        return null;
×
166
    }
167

168
    /**
169
     * @param MemberNode $memberNode
170
     * @return array
171
     */
172
    public function visitMemberNode(MemberNode $memberNode): array
173
    {
UNCOV
174
        $key = $memberNode->getKey()->accept($this);
×
UNCOV
175
        $value = $memberNode->getValue()->accept($this);
×
176

177
        return [$key, $value];
×
178
    }
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc