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

DoclerLabs / api-client-generator / 18875374886

28 Oct 2025 12:52PM UTC coverage: 86.909% (-0.07%) from 86.981%
18875374886

push

github

web-flow
Merge pull request #127 from DoclerLabs/fix-multiline-description-and-enum-with-invalid-value

fix multiline description and enum with invalid value

4 of 9 new or added lines in 2 files covered. (44.44%)

2994 of 3445 relevant lines covered (86.91%)

6.37 hits per line

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

5.13
/src/Generator/EnumGenerator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace DoclerLabs\ApiClientGenerator\Generator;
6

7
use DoclerLabs\ApiClientGenerator\Entity\Field;
8
use DoclerLabs\ApiClientGenerator\Input\Specification;
9
use DoclerLabs\ApiClientGenerator\Output\Php\PhpFileCollection;
10

11
class EnumGenerator extends MutatorAccessorClassGeneratorAbstract
12
{
13
    public const SUBDIRECTORY = 'Schema/';
14

15
    public const NAMESPACE_SUBPATH = '\\Schema';
16

17
    public static function getCaseName(string $value): string
18
    {
NEW
19
        $sanitized = preg_replace('/[^A-Z0-9_]/', '', strtoupper(str_replace([' ', '-', '/', '.'], '_', $value)));
×
20

21
        if (is_numeric($sanitized)) {
×
22
            return 'V_' . $sanitized;
×
23
        }
24

25
        return (string)$sanitized;
×
26
    }
27

28
    public function generate(Specification $specification, PhpFileCollection $fileRegistry): void
29
    {
30
        if (!$this->phpVersion->isEnumSupported()) {
2✔
31
            return;
2✔
32
        }
33

34
        $compositeFields = $specification->getCompositeFields()->getUniqueByPhpClassName();
×
35
        foreach ($compositeFields as $field) {
×
36
            if (
37
                $field->isObject()
×
38
                && !$field->isFreeFormObject()
×
39
            ) {
40
                foreach ($field->getObjectProperties() as $propertyField) {
×
41
                    if ($propertyField->isEnum()) {
×
42
                        $this->generateEnum($propertyField, $fileRegistry);
×
43
                    }
44
                }
45
            }
46

47
            if ($field->isEnum()) {
×
48
                $this->generateEnum($field, $fileRegistry);
×
49
            }
50
        }
51
        foreach ($specification->getOperations() as $operation) {
×
52
            foreach ($operation->request->fields as $field) {
×
53
                if (!empty($field->getEnumValues())) {
×
54
                    $this->generateEnum($field, $fileRegistry);
×
55
                }
56
            }
57
        }
58
    }
×
59

60
    private function generateEnum(Field $root, PhpFileCollection $fileRegistry): void
61
    {
62
        $classBuilder = $this
×
63
            ->builder
×
64
            ->enum($root->getPhpClassName())
×
65
            ->setScalarType($root->getType()->toPhpType())
×
66
            ->addStmts($this->generateEnumConsts($root));
×
67

68
        $this->registerFile($fileRegistry, $classBuilder, self::SUBDIRECTORY, self::NAMESPACE_SUBPATH);
×
69
    }
×
70

71
    private function generateEnumConsts(Field $root): array
72
    {
73
        if ($root->getEnumValues() === null) {
×
74
            return [];
×
75
        }
76

77
        $statements = [];
×
78
        foreach ($root->getEnumValues() as $value) {
×
NEW
79
            $caseName = self::getCaseName((string)$value);
×
NEW
80
            if (empty($caseName)) {
×
NEW
81
                continue;
×
82
            }
83

84
            $statements[] = $this
×
85
                ->builder
×
NEW
86
                ->enumCase($caseName)
×
87
                ->setValue($value);
×
88
        }
89

90
        return $statements;
×
91
    }
92
}
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