• 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

80.56
/src/Generator/MutatorAccessorClassGeneratorAbstract.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace DoclerLabs\ApiClientGenerator\Generator;
6

7
use DoclerLabs\ApiClientException\RequestValidationException;
8
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
9
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintInterface;
10
use DoclerLabs\ApiClientGenerator\Entity\Field;
11
use DoclerLabs\ApiClientGenerator\Input\Specification;
12
use DoclerLabs\ApiClientGenerator\Naming\SchemaNaming;
13
use DoclerLabs\ApiClientGenerator\Output\Php\PhpFileCollection;
14
use PhpParser\Node\Stmt\ClassMethod;
15
use PhpParser\Node\Stmt\Property;
16
use PhpParser\NodeAbstract;
17

18
abstract class MutatorAccessorClassGeneratorAbstract extends GeneratorAbstract
19
{
20
    protected CodeBuilder $builder;
21

22
    abstract public function generate(Specification $specification, PhpFileCollection $fileRegistry): void;
23

24
    protected function generateValidationStmts(Field $field): array
25
    {
26
        return array_filter(
1✔
27
            [
28
                ...$this->generateConstraints($field),
1✔
29
            ]
30
        );
31
    }
32

33
    /**
34
     * @param array<int, NodeAbstract> $additionalStatements
35
     */
36
    protected function generateSet(Field $field, array $additionalStatements = []): ClassMethod
37
    {
38
        $statements         = $this->generateValidationStmts($field);
1✔
39
        $thrownExceptionMap = empty($statements) ? [] : ['RequestValidationException' => true];
1✔
40
        $docType            = $field->getPhpDocType($field->isNullable());
1✔
41
        $param              = $this->builder
1✔
42
            ->param($field->getPhpVariableName())
1✔
43
            ->setType($field->getPhpTypeHint(), $field->isNullable())
1✔
44
            ->setDocBlockType($docType)
1✔
45
            ->getNode();
1✔
46

47
        $statements[] = $this->builder->assign(
1✔
48
            $this->builder->localPropertyFetch($field->getPhpVariableName()),
1✔
49
            $this->builder->var($field->getPhpVariableName())
1✔
50
        );
51

52
        $statements = array_merge($statements, $additionalStatements);
1✔
53

54
        $return     = $this->builder->return($this->builder->var('this'));
1✔
55
        $returnType = 'self';
1✔
56

57
        return $this->builder
1✔
58
            ->method($this->getSetMethodName($field))
1✔
59
            ->makePublic()
1✔
60
            ->addParam($param)
1✔
61
            ->addStmts($statements)
1✔
62
            ->addStmt($return)
1✔
63
            ->setReturnType($returnType)
1✔
64
            ->composeDocBlock([$param], $returnType, array_keys($thrownExceptionMap))
1✔
65
            ->getNode();
1✔
66
    }
67

68
    protected function generateGet(Field $field): ClassMethod
69
    {
70
        $return = $this->builder->return($this->builder->localPropertyFetch($field->getPhpVariableName()));
1✔
71

72
        return $this->builder
1✔
73
            ->method($this->getGetMethodName($field))
1✔
74
            ->makePublic()
1✔
75
            ->addStmt($return)
1✔
76
            ->setReturnType($field->getPhpTypeHint(), $field->isNullable() || !$field->isRequired())
1✔
77
            ->composeDocBlock([], $field->getPhpDocType())
1✔
78
            ->getNode();
1✔
79
    }
80

81
    protected function generateProperty(Field $field): Property
82
    {
83
        return $this->builder->localProperty(
1✔
84
            $field->getPhpVariableName(),
1✔
85
            $field->getPhpTypeHint(),
1✔
86
            $field->getPhpDocType(),
1✔
87
            nullable: $field->isOptional() || $field->isNullable(),
1✔
88
            readonly: $field->isRequired()
1✔
89
        );
90
    }
91

92
    protected function getSetMethodName(Field $field): string
93
    {
94
        return sprintf('set%s', ucfirst($field->getPhpVariableName()));
1✔
95
    }
96

97
    protected function getGetMethodName(Field $field): string
98
    {
99
        return sprintf('get%s', ucfirst($field->getPhpVariableName()));
1✔
100
    }
101

102
    protected function getHasMethodName(Field $field): string
103
    {
104
        return sprintf('has%s', ucfirst($field->getPhpVariableName()));
1✔
105
    }
106

107
    protected function generateEnumStatements(Field $field): array
108
    {
109
        if ($this->phpVersion->isEnumSupported()) {
1✔
NEW
110
            return [];
×
111
        }
112

113
        $statements = [];
1✔
114
        $enumValues = $field->getEnumValues();
1✔
115
        if (!empty($enumValues)) {
1✔
116
            foreach ($enumValues as $enumValue) {
1✔
117
                if (is_string($enumValue)) {
1✔
118
                    $constName    = SchemaNaming::getEnumConstName($field, $enumValue);
1✔
119
                    $statements[] = $this->builder->constant(
1✔
120
                        $constName,
121
                        $this->builder->val($enumValue)
1✔
122
                    );
123
                }
124
            }
125
        }
126

127
        return $statements;
1✔
128
    }
129

130
    protected function generateConstraints(Field $root): array
131
    {
132
        $stmts = [];
1✔
133

134
        /** @var ConstraintInterface $constraint */
135
        foreach ($root->getConstraints() as $constraint) {
1✔
136
            if (!$constraint->exists()) {
1✔
137
                continue;
1✔
138
            }
139

140
            $propertyVar      = $this->builder->var($root->getPhpVariableName());
×
141
            $exceptionMessage = $this->builder->funcCall(
×
142
                'sprintf',
×
143
                [
144
                    'Invalid %s value. Given: `%s`. ' . $constraint->getExceptionMessage(),
×
145
                    $root->getName(),
×
146
                    $propertyVar,
×
147
                ]
148
            );
149

150
            $ifConditionExpr = $constraint->getIfCondition($propertyVar, $this->builder);
×
151

152
            if ($root->isNullable()) {
×
153
                $ifConditionExpr = $this->builder->and(
×
154
                    $this->builder->notEquals($propertyVar, $this->builder->val(null)),
×
155
                    $ifConditionExpr
156
                );
157
            }
158

159
            $stmts[] = $this->builder->if(
×
160
                $ifConditionExpr,
161
                [
162
                    $this->builder->throw('RequestValidationException', $exceptionMessage),
×
163
                ]
164
            );
165
        }
166

167
        if (!empty($stmts)) {
1✔
168
            $this->addImport(RequestValidationException::class);
×
169
        }
170

171
        return $stmts;
1✔
172
    }
173
}
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