Passed
Push — master ( d35b53...e0ffea )
by Francis
02:44
created

PropertyGenerator::getPhpTypeFromTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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