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

DoclerLabs / api-client-generator / 9254068657

27 May 2024 11:30AM UTC coverage: 86.981% (-1.4%) from 88.428%
9254068657

push

github

web-flow
Merge pull request #112 from DoclerLabs/php81

php 8.1 features

106 of 172 new or added lines in 20 files covered. (61.63%)

4 existing lines in 2 files now uncovered.

2913 of 3349 relevant lines covered (86.98%)

4.92 hits per line

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

84.21
/src/Entity/Field.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace DoclerLabs\ApiClientGenerator\Entity;
6

7
use DoclerLabs\ApiClientGenerator\Ast\PhpVersion;
8
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintCollection;
9
use DoclerLabs\ApiClientGenerator\Naming\CaseCaster;
10
use DoclerLabs\ApiClientGenerator\Naming\SchemaCollectionNaming;
11
use RuntimeException;
12

13
class Field
14
{
15
    private const FORMAT_DATE = 'date';
16

17
    private const FORMAT_DATE_TIME = 'date-time';
18

19
    private const TYPE_MIXED = 'mixed';
20

21
    private ?Field $arrayItem = null;
22

23
    private array $objectProperties = [];
24

25
    private array $enumValues = [];
26

27
    private string $format = '';
28

29
    private mixed $default = null;
30

31
    private mixed $discriminator = null;
32

33
    public function __construct(
34
        private PhpVersion $phpVersion,
35
        private string $name,
36
        private FieldType $type,
37
        private ConstraintCollection $constraints,
38
        private string $referenceName,
39
        private bool $required,
40
        private bool $nullable,
41
        private bool $additionalProperties,
42
        private bool $hasOneOf = false,
43
        private bool $hasAnyOf = false
44
    ) {
45
    }
2✔
46

47
    public function getArrayItem(): Field
48
    {
49
        if ($this->arrayItem === null) {
2✔
50
            throw new RuntimeException('Call of getArrayItem on the non-array field.');
×
51
        }
52

53
        return $this->arrayItem;
2✔
54
    }
55

56
    public function setArrayItem(Field $arrayItem): self
57
    {
58
        $this->arrayItem = $arrayItem;
2✔
59

60
        return $this;
2✔
61
    }
62

63
    /**
64
     * @return Field[]
65
     */
66
    public function getObjectProperties(): array
67
    {
68
        return $this->objectProperties;
2✔
69
    }
70

71
    /**
72
     * @param Field[] $objectProperties
73
     */
74
    public function setObjectProperties(array $objectProperties): self
75
    {
76
        $this->objectProperties = $objectProperties;
2✔
77

78
        return $this;
2✔
79
    }
80

81
    public function getEnumValues(): ?array
82
    {
83
        return $this->enumValues;
1✔
84
    }
85

86
    public function setEnumValues(array $enumValues): self
87
    {
88
        $this->enumValues = $enumValues;
1✔
89

90
        return $this;
1✔
91
    }
92

93
    public function getFormat(): ?string
94
    {
95
        return $this->format;
1✔
96
    }
97

98
    public function setFormat(string $format): self
99
    {
100
        $this->format = $format;
2✔
101

102
        return $this;
2✔
103
    }
104

105
    public function getName(): string
106
    {
107
        return $this->name;
1✔
108
    }
109

110
    public function getType(): FieldType
111
    {
112
        return $this->type;
×
113
    }
114

115
    public function getConstraints(): ConstraintCollection
116
    {
117
        return $this->constraints;
1✔
118
    }
119

120
    public function getReferenceName(): string
121
    {
122
        return $this->referenceName;
2✔
123
    }
124

125
    public function isRequired(): bool
126
    {
127
        return $this->required;
1✔
128
    }
129

130
    public function isOptional(): bool
131
    {
132
        return !$this->required;
1✔
133
    }
134

135
    public function isNullable(): bool
136
    {
137
        return $this->nullable;
1✔
138
    }
139

140
    public function hasOneOf(): bool
141
    {
142
        return $this->hasOneOf;
1✔
143
    }
144

145
    public function hasAnyOf(): bool
146
    {
147
        return $this->hasAnyOf;
1✔
148
    }
149

150
    public function isDate(): bool
151
    {
152
        $isDateFormat = $this->getFormat() === self::FORMAT_DATE || $this->getFormat() === self::FORMAT_DATE_TIME;
1✔
153

154
        return $this->type->isString() && $isDateFormat;
1✔
155
    }
156

157
    public function isEnum(): bool
158
    {
159
        return !empty($this->enumValues);
1✔
160
    }
161

162
    public function isObject(): bool
163
    {
164
        return $this->type->isObject();
2✔
165
    }
166

167
    public function isFreeFormObject(): bool
168
    {
169
        return $this->isObject() && $this->additionalProperties && count($this->getObjectProperties()) === 0;
1✔
170
    }
171

172
    public function isArray(): bool
173
    {
174
        return $this->type->isArray();
2✔
175
    }
176

177
    public function isArrayOfObjects(): bool
178
    {
179
        return $this->isArray()
2✔
180
               && $this->getArrayItem() !== null
2✔
181
               && $this->getArrayItem()->isObject();
2✔
182
    }
183

184
    public function isArrayOfEnums(): bool
185
    {
NEW
186
        return $this->isArray()
×
NEW
187
               && $this->getArrayItem() !== null
×
NEW
188
               && $this->getArrayItem()->isEnum();
×
189
    }
190

191
    public function getDefault(): mixed
192
    {
193
        return $this->default;
1✔
194
    }
195

196
    public function setDefault(mixed $default): self
197
    {
198
        $this->default = $default;
1✔
199

200
        return $this;
1✔
201
    }
202

203
    public function getDiscriminator(): mixed
204
    {
205
        return $this->discriminator;
×
206
    }
207

208
    public function setDiscriminator(mixed $discriminator): self
209
    {
210
        $this->discriminator = $discriminator;
×
211

212
        return $this;
×
213
    }
214

215
    public function getPhpVariableName(): string
216
    {
217
        return CaseCaster::toCamel($this->name);
1✔
218
    }
219

220
    public function getPhpClassName(): string
221
    {
222
        if ($this->type->isObject()) {
2✔
223
            return $this->referenceName;
2✔
224
        }
225

226
        if (
227
            $this->type->isArray()
2✔
228
            && $this->getArrayItem()->isObject()
2✔
229
        ) {
230
            return SchemaCollectionNaming::getClassName($this->getArrayItem()->getReferenceName());
2✔
231
        }
232

233
        if ($this->isDate()) {
1✔
234
            return 'DateTimeInterface';
1✔
235
        }
236

NEW
237
        if ($this->isEnum() && $this->phpVersion->isEnumSupported()) {
×
NEW
238
            return $this->referenceName . (str_ends_with($this->referenceName, 'Enum') ? '' : 'Enum');
×
239
        }
240

UNCOV
241
        throw new RuntimeException('Call of getPhpClassName on the non-composite field.');
×
242
    }
243

244
    public function getPhpTypeHint(): string
245
    {
246
        if (
247
            $this->isComposite()
1✔
248
            || $this->isDate()
1✔
249
            || (
250
                $this->isEnum()
1✔
251
                && $this->phpVersion->isEnumSupported()
1✔
252
            )
253
        ) {
254
            return $this->getPhpClassName();
1✔
255
        }
256

257
        return $this->type->toPhpType();
1✔
258
    }
259

260
    public function getPhpDocType(bool $allowNullable = true): string
261
    {
262
        if ($this->type->isMixed()) {
1✔
263
            return self::TYPE_MIXED;
×
264
        }
265

266
        $nullableSuffix = '';
1✔
267
        $arraySuffix    = '';
1✔
268
        if (
269
            $this->isComposite()
1✔
270
            || $this->isDate()
1✔
271
            || (
272
                $this->isEnum()
1✔
273
                && $this->phpVersion->isEnumSupported()
1✔
274
            )
275
        ) {
276
            $typeHint = $this->getPhpClassName();
1✔
277
        } else {
278
            $typeHint = $this->type->toPhpType();
1✔
279
        }
280

281
        if ($allowNullable && ($this->isNullable() || $this->isOptional())) {
1✔
282
            $nullableSuffix = '|null';
1✔
283
        }
284

285
        if ($this->isArray() && !$this->isArrayOfObjects()) {
1✔
286
            $arraySuffix = '[]';
1✔
287
            $typeHint    = $this->getArrayItem()->getPhpDocType();
1✔
288
        }
289

290
        return sprintf('%s%s%s', $typeHint, $arraySuffix, $nullableSuffix);
1✔
291
    }
292

293
    public function isComposite(): bool
294
    {
295
        return $this->isObject() || $this->isArrayOfObjects();
1✔
296
    }
297
}
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