Completed
Push — master ( b00023...d35b53 )
by Francis
04:02 queued 11s
created

MethodGenerator   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 348
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 45
eloc 81
c 0
b 0
f 0
dl 0
loc 348
rs 8.8

32 Methods

Rating   Name   Duplication   Size   Complexity  
A getLines() 0 3 1
A addParameter() 0 4 2
A addReturnType() 0 5 2
A getScope() 0 3 1
A getParameters() 0 3 1
A addLine() 0 3 1
A generate() 0 4 1
A configure() 0 18 1
A getUsesGenerator() 0 3 1
A getName() 0 3 1
A setParameters() 0 3 1
A setLines() 0 3 1
A setUsesGenerator() 0 3 1
A setMethodParameterGeneratorSkel() 0 3 1
A getReturnTypes() 0 3 1
A getMethodParameterGeneratorSkel() 0 3 1
A setStatic() 0 3 1
A hasParameter() 0 3 1
A setPhpDocGenerator() 0 3 1
A hasReturnType() 0 3 1
A getPhpReturnType() 0 22 6
A getReturnType() 0 3 1
A __construct() 0 11 1
A configurePhpDocGenerator() 0 17 6
A getPhpDocGenerator() 0 3 1
A setReturnTypes() 0 5 2
A setParameter() 0 3 1
A setName() 0 3 1
A getDescription() 0 3 1
A isStatic() 0 3 1
A setScope() 0 3 1
A setDescription() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like MethodGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MethodGenerator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prometee\SwaggerClientGenerator\Base\Generator\Method;
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\Method\MethodViewInterface;
11
12
class MethodGenerator extends AbstractGenerator implements MethodGeneratorInterface
13
{
14
    /** @var PhpDocGeneratorInterface */
15
    protected $phpDocGenerator;
16
    /** @var UsesGeneratorInterface */
17
    protected $usesGenerator;
18
    /** @var MethodParameterGeneratorInterface */
19
    protected $methodParameterGeneratorSkel;
20
21
    /** @var string */
22
    protected $scope = '';
23
    /** @var string */
24
    protected $name = '';
25
    /** @var string[] */
26
    protected $returnTypes = [];
27
    /** @var bool */
28
    protected $static = false;
29
    /** @var string */
30
    protected $description = '';
31
    /** @var MethodParameterGeneratorInterface[] */
32
    protected $parameters = [];
33
    /** @var string[] */
34
    protected $lines = [];
35
    /** @var bool */
36
    protected $hasAlreadyBeenGenerated = false;
37
38
    /**
39
     * @param MethodViewInterface $methodView
40
     * @param UsesGeneratorInterface $usesGenerator
41
     * @param PhpDocGeneratorInterface $phpDocGenerator
42
     * @param MethodParameterGeneratorInterface $methodParameterGeneratorSkel
43
     */
44
    public function __construct(
45
        MethodViewInterface $methodView,
46
        UsesGeneratorInterface $usesGenerator,
47
        PhpDocGeneratorInterface $phpDocGenerator,
48
        MethodParameterGeneratorInterface $methodParameterGeneratorSkel
49
    )
50
    {
51
        $this->setView($methodView);
52
        $this->usesGenerator = $usesGenerator;
53
        $this->phpDocGenerator = $phpDocGenerator;
54
        $this->methodParameterGeneratorSkel = $methodParameterGeneratorSkel;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function configure(
61
        string $scope,
62
        string $name,
63
        array $returnTypes = [],
64
        bool $static = false,
65
        string $description = ''
66
    )
67
    {
68
        $this->setScope($scope);
69
        $this->setName($name);
70
        $this->setReturnTypes($returnTypes);
71
        $this->setStatic($static);
72
        $this->setDescription($description);
73
        $this->setParameters([]);
74
        $this->setLines([]);
75
76
        $this->phpDocGenerator->configure();
77
        $this->hasAlreadyBeenGenerated = false;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function generate(string $indent = null, string $eol = null): ?string
84
    {
85
        $this->configurePhpDocGenerator();
86
        return parent::generate($indent, $eol);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function configurePhpDocGenerator(): void
93
    {
94
        if ($this->hasAlreadyBeenGenerated) {
95
            return;
96
        }
97
98
        if (!empty($this->getDescription())) {
99
            $this->phpDocGenerator->addDescriptionLine($this->getDescription());
100
        }
101
        foreach ($this->parameters as $parameter) {
102
            $this->phpDocGenerator->addParamLine($parameter->getPhpName(), $parameter->getType(), $parameter->getDescription());
103
        }
104
        if (!empty($this->returnTypes) && !in_array('void', $this->returnTypes)) {
105
            $this->phpDocGenerator->addReturnLine($this->getReturnType());
106
        }
107
108
        $this->hasAlreadyBeenGenerated = true;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function getReturnType(): string
115
    {
116
        return implode('|', $this->returnTypes);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getReturnTypes(): array
123
    {
124
        return $this->returnTypes;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function getPhpReturnType(): ?string
131
    {
132
        if (empty($this->returnTypes)) {
133
            return null;
134
        }
135
136
        $phpReturnType = '';
137
        if (in_array('null', $this->returnTypes)) {
138
            $phpReturnType = '?';
139
        }
140
        foreach ($this->returnTypes as $type) {
141
            if (preg_match('#\[\]$#', $type)) {
142
                $phpReturnType .= 'array';
143
                break;
144
            }
145
            if ($type !== 'null') {
146
                $phpReturnType .= $type;
147
                break;
148
            }
149
        }
150
151
        return $phpReturnType;
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157
    public function setReturnTypes(array $returnTypes): void
158
    {
159
        $this->returnTypes = [];
160
        foreach ($returnTypes as $returnType) {
161
            $this->addReturnType($returnType);
162
        }
163
    }
164
165
    /**
166
     * {@inheritDoc}
167
     */
168
    public function addReturnType(string $returnType): void
169
    {
170
        $returnType = $this->usesGenerator->guessUseOrReturnType($returnType);
171
        if (false === $this->hasReturnType($returnType)) {
172
            $this->returnTypes[] = $returnType;
173
        }
174
    }
175
176
    /**
177
     * {@inheritDoc}
178
     */
179
    public function hasReturnType(string $returnType): bool
180
    {
181
        return false !== array_search($returnType, $this->returnTypes);
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function addParameter(MethodParameterGeneratorInterface $methodParameterGenerator): void
188
    {
189
        if (!$this->hasParameter($methodParameterGenerator)) {
190
            $this->setParameter($methodParameterGenerator);
191
        }
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function hasParameter(MethodParameterGeneratorInterface $methodParameterGenerator): bool
198
    {
199
        return isset($this->parameters[$methodParameterGenerator->getName()]);
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function setParameter(MethodParameterGeneratorInterface $methodParameterGenerator): void
206
    {
207
        $this->parameters[$methodParameterGenerator->getName()] = $methodParameterGenerator;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function addLine(string $line): void
214
    {
215
        $this->lines[] = $line;
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221
    public function getScope(): string
222
    {
223
        return $this->scope;
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     */
229
    public function setScope(string $scope): void
230
    {
231
        $this->scope = $scope;
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function isStatic(): bool
238
    {
239
        return $this->static;
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public function setStatic(bool $static): void
246
    {
247
        $this->static = $static;
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253
    public function getName(): string
254
    {
255
        return $this->name;
256
    }
257
258
    /**
259
     * {@inheritdoc}
260
     */
261
    public function setName(string $name): void
262
    {
263
        $this->name = $name;
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function getDescription(): string
270
    {
271
        return $this->description;
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277
    public function setDescription(string $description): void
278
    {
279
        $this->description = $description;
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285
    public function getParameters(): array
286
    {
287
        return $this->parameters;
288
    }
289
290
    /**
291
     * {@inheritdoc}
292
     */
293
    public function setParameters(array $parameters): void
294
    {
295
        $this->parameters = $parameters;
296
    }
297
298
    /**
299
     * {@inheritdoc}
300
     */
301
    public function getLines(): array
302
    {
303
        return $this->lines;
304
    }
305
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function setLines(array $lines): void
310
    {
311
        $this->lines = $lines;
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317
    public function getPhpDocGenerator(): PhpDocGeneratorInterface
318
    {
319
        return $this->phpDocGenerator;
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     */
325
    public function setPhpDocGenerator(PhpDocGeneratorInterface $phpDocGenerator): void
326
    {
327
        $this->phpDocGenerator = $phpDocGenerator;
328
    }
329
330
    /**
331
     * {@inheritdoc}
332
     */
333
    public function getUsesGenerator(): UsesGeneratorInterface
334
    {
335
        return $this->usesGenerator;
336
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341
    public function setUsesGenerator(UsesGeneratorInterface $usesGenerator): void
342
    {
343
        $this->usesGenerator = $usesGenerator;
344
    }
345
346
    /**
347
     * {@inheritdoc}
348
     */
349
    public function getMethodParameterGeneratorSkel(): MethodParameterGeneratorInterface
350
    {
351
        return $this->methodParameterGeneratorSkel;
352
    }
353
354
    /**
355
     * {@inheritdoc}
356
     */
357
    public function setMethodParameterGeneratorSkel(MethodParameterGeneratorInterface $methodParameterGeneratorSkel): void
358
    {
359
        $this->methodParameterGeneratorSkel = $methodParameterGeneratorSkel;
360
    }
361
}
362