Completed
Push — master ( fc5bf0...7f5622 )
by Francis
02:50
created

MethodParameterGenerator::getValueType()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 27
rs 8.8333
cc 7
nc 7
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prometee\SwaggerClientGenerator\Base\Generator\Object\Method;
6
7
use Prometee\SwaggerClientGenerator\Base\Generator\Object\Other\UsesGeneratorInterface;
8
9
class MethodParameterGenerator implements MethodParameterGeneratorInterface
10
{
11
    /** @var UsesGeneratorInterface */
12
    protected $usesBuilder;
13
14
    /** @var string[] */
15
    protected $types = [];
16
    /** @var string */
17
    protected $name;
18
    /** @var string|null */
19
    protected $value;
20
    /** @var bool */
21
    protected $byReference = false;
22
    /** @var string */
23
    protected $description = '';
24
25
    /**
26
     * @param UsesGeneratorInterface $usesBuilder
27
     */
28
    public function __construct(UsesGeneratorInterface $usesBuilder)
29
    {
30
        $this->usesBuilder = $usesBuilder;
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public function configure(
37
        array $types,
38
        string $name,
39
        ?string $value = null,
40
        bool $byReference = false,
41
        string $description = ''
42
    ): void
43
    {
44
        $this->setTypes($types);
45
        $this->setName($name);
46
        $this->setValue($value);
47
        $this->setByReference($byReference);
48
        $this->setDescription($description);
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function generate(string $indent = null): ?string
55
    {
56
        $content = '';
57
58
        $content .= !empty($this->types) ? $this->getPhpType() . ' ' : '';
59
        $content .= $this->byReference ? '&' : '';
60
        $content .= $this->getPhpName();
61
        $content .= ($this->value !== null) ? ' = ' . $this->value : '';
62
63
        return $content;
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function getPhpName(): string
70
    {
71
        return '$' . $this->getName();
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function getTypes(): array
78
    {
79
        return $this->types;
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    public function setTypes(array $types): void
86
    {
87
        $this->types = [];
88
        foreach ($types as $type) {
89
            $this->addType($type);
90
        }
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function addType(string $type): void
97
    {
98
        $type = $this->usesBuilder->guessUseOrReturnType($type);
99
        if (false === $this->hasType($type)) {
100
            $this->types[] = $type;
101
        }
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function hasType(string $type): bool
108
    {
109
        return false !== array_search($type, $this->types);
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function getPhpType(): ?string
116
    {
117
        if (empty($this->types)) {
118
            return null;
119
        }
120
121
        $phpType = '';
122
        if (in_array('null', $this->types)) {
123
            $phpType = '?';
124
        }
125
        foreach ($this->getTypes() as $type) {
126
            if (preg_match('#\[\]$#', $type)) {
127
                $phpType .= 'array';
128
129
                break;
130
            }
131
            if ($type !== 'null') {
132
                $phpType .= $type;
133
134
                break;
135
            }
136
        }
137
138
        return $phpType;
139
    }
140
141
    /**
142
     * {@inheritDoc}
143
     */
144
    public function getValueType()
145
    {
146
        if ($this->value === null) {
147
            return null;
148
        }
149
150
        if ($this->value === '[]') {
151
            return 'array';
152
        }
153
154
        if (preg_match('#^[\'"].*[\'"]$#', $this->value)) {
155
            return 'string';
156
        }
157
158
        if (in_array($this->value, ['true', 'false'])) {
159
            return 'bool';
160
        }
161
162
        if (preg_match('#^[0-9]+$#', $this->value)) {
163
            return 'int';
164
        }
165
166
        if (preg_match('#^[0-9\.]+$#', $this->value)) {
167
            return 'float';
168
        }
169
170
        return $this->value;
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176
    public function getType(): ?string
177
    {
178
        if (empty($this->types)) {
179
            return null;
180
        }
181
182
        return implode('|', $this->types);
183
    }
184
185
    /**
186
     * {@inheritDoc}
187
     */
188
    public function getName(): string
189
    {
190
        return $this->name;
191
    }
192
193
    /**
194
     * {@inheritDoc}
195
     */
196
    public function setName(string $name): void
197
    {
198
        $this->name = $name;
199
    }
200
201
    /**
202
     * {@inheritDoc}
203
     */
204
    public function getValue(): ?string
205
    {
206
        return $this->value;
207
    }
208
209
    /**
210
     * {@inheritDoc}
211
     */
212
    public function setValue(?string $value): void
213
    {
214
        $this->value = $value;
215
    }
216
217
    /**
218
     * {@inheritDoc}
219
     */
220
    public function isByReference(): bool
221
    {
222
        return $this->byReference;
223
    }
224
225
    /**
226
     * {@inheritDoc}
227
     */
228
    public function setByReference(bool $byReference): void
229
    {
230
        $this->byReference = $byReference;
231
    }
232
233
    /**
234
     * {@inheritDoc}
235
     */
236
    public function getDescription(): string
237
    {
238
        return $this->description;
239
    }
240
241
    /**
242
     * {@inheritDoc}
243
     */
244
    public function setDescription(string $description): void
245
    {
246
        $this->description = $description;
247
    }
248
}
249