Passed
Push — master ( d8ac0a...430361 )
by Mikaël
15:43 queued 14s
created

StructEnum::addClassElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File;
6
7
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
8
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
9
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
10
use WsdlToPhp\PackageGenerator\Model\StructValue as StructValueModel;
11
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
12
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
13
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
14
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
15
16
final class StructEnum extends Struct
17
{
18
    public const METHOD_VALUE_IS_VALID = 'valueIsValid';
19
    public const METHOD_GET_VALID_VALUES = 'getValidValues';
20
21
    public function setModel(AbstractModel $model): self
22 46
    {
23
        if ($model instanceof StructModel && !$model->isRestriction()) {
24 46
            throw new \InvalidArgumentException('Model must be a restriction containing values', __LINE__);
25 2
        }
26
27
        return parent::setModel($model);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setModel($model) returns the type WsdlToPhp\PackageGenerator\File\Struct which includes types incompatible with the type-hinted return WsdlToPhp\PackageGenerator\File\StructEnum.
Loading history...
28 44
    }
29
30
    protected function addClassElement(): AbstractModelFile
31 42
    {
32
        return AbstractModelFile::addClassElement();
33 42
    }
34 42
35
    protected function fillClassConstants(ConstantContainer $constants): void
36 42
    {
37
        /** @var StructModel $model */
38 42
        $model = $this->getModel();
39
        foreach ($model->getValues() as $value) {
40 42
            $constants->add(new PhpConstant($value->getCleanName(), $value->getValue()));
41
        }
42
    }
43 42
44
    protected function defineUseStatements(): self
45 42
    {
46 42
        return AbstractModelFile::defineUseStatements();
0 ignored issues
show
Bug Best Practice introduced by
The expression return WsdlToPhp\Package...::defineUseStatements() returns the type WsdlToPhp\PackageGenerator\File\AbstractModelFile which includes types incompatible with the type-hinted return WsdlToPhp\PackageGenerator\File\StructEnum.
Loading history...
47
    }
48 42
49 42
    protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock
50
    {
51 42
        $block = new PhpAnnotationBlock([
52
            sprintf('Constant for value \'%s\'', $constant->getValue()),
53 42
        ]);
54
55
        /** @var StructModel $model */
56 42
        $model = $this->getModel();
57
        if (($value = $model->getValue($constant->getValue())) instanceof StructValueModel) {
58 42
            $this->defineModelAnnotationsFromWsdl($block, $value);
59 42
        }
60
        $block->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('string \'%s\'', $constant->getValue())));
61 42
62
        return $block;
63 42
    }
64
65 42
    protected function fillClassMethods(): void
66 42
    {
67 42
        $this->methods->add($this->getEnumMethodGetValidValues());
68
    }
69 42
70
    protected function getMethodAnnotationBlock(PhpMethod $method): ?PhpAnnotationBlock
71
    {
72 42
        $block = null;
73
74
        switch ($method->getName()) {
75 42
            case self::METHOD_GET_VALID_VALUES:
76
                $block = $this->getEnumGetValidValuesAnnotationBlock();
77 42
78 42
                break;
79 42
        }
80 42
81 42
        return $block;
82
    }
83 42
84
    protected function getEnumMethodGetValidValues(): PhpMethod
85 42
    {
86
        $method = new PhpMethod(self::METHOD_GET_VALID_VALUES, [], self::TYPE_ARRAY, PhpMethod::ACCESS_PUBLIC, false, true);
87
        $validValues = $this->getEnumMethodValues();
88 42
        $method->addChild('return [');
89
        foreach ($validValues as $validValue) {
90 42
            $method->addChild(sprintf('%s,', $method->getIndentedString($validValue, 1)));
91 42
        }
92 42
        $method->addChild('];');
93
94
        return $method;
95 42
    }
96
97
    protected function getEnumMethodValues(): array
98 42
    {
99
        $values = [];
100 42
101 42
        /** @var StructModel $model */
102
        $model = $this->getModel();
103 42
        foreach ($model->getValues() as $value) {
104 42
            $values[] = sprintf('self::%s', $value->getCleanName());
105
        }
106 42
107
        return $values;
108 42
    }
109
110
    protected function getEnumGetValidValuesAnnotationBlock(): PhpAnnotationBlock
111
    {
112
        $annotationBlock = new PhpAnnotationBlock([
113
            'Return allowed values',
114
        ]);
115
        foreach ($this->getEnumMethodValues() as $value) {
116
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, $value));
117
        }
118
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, 'string[]'));
119
120
        return $annotationBlock;
121
    }
122
}
123