Passed
Push — feature/issue-244 ( 0a8b55...80d7ca )
by Mikaël
46:58
created

StructEnum::getEnumMethodGetValidValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File;
6
7
use InvalidArgumentException;
8
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
9
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
10
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
11
use WsdlToPhp\PackageGenerator\Model\StructValue as StructValueModel;
12
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
13
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
14
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
15
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
16
17
final class StructEnum extends Struct
18
{
19
    public const METHOD_VALUE_IS_VALID = 'valueIsValid';
20
    public const METHOD_GET_VALID_VALUES = 'getValidValues';
21
22
    public function setModel(AbstractModel $model): self
23
    {
24
        if ($model instanceof StructModel && !$model->isRestriction()) {
25
            throw new InvalidArgumentException('Model must be a restriction containing values', __LINE__);
26
        }
27
28
        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...
29
    }
30
31
    protected function fillClassConstants(ConstantContainer $constants): void
32
    {
33
        foreach ($this->getModel()->getValues() as $value) {
0 ignored issues
show
Bug introduced by
The method getValues() does not exist on WsdlToPhp\PackageGenerator\Model\AbstractModel. It seems like you code against a sub-type of WsdlToPhp\PackageGenerator\Model\AbstractModel such as WsdlToPhp\PackageGenerator\Model\Struct. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        foreach ($this->getModel()->/** @scrutinizer ignore-call */ getValues() as $value) {
Loading history...
34
            $constants->add(new PhpConstant($value->getCleanName(), $value->getValue()));
35
        }
36
    }
37
38
    protected function defineUseStatements(): self
39
    {
40
        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...
41
    }
42
43
    protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock
44
    {
45
        $block = new PhpAnnotationBlock([
46
            sprintf('Constant for value \'%s\'', $constant->getValue()),
47
        ]);
48
        if (($value = $this->getModel()->getValue($constant->getValue())) instanceof StructValueModel) {
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on WsdlToPhp\PackageGenerator\Model\AbstractModel. It seems like you code against a sub-type of WsdlToPhp\PackageGenerator\Model\AbstractModel such as WsdlToPhp\PackageGenerator\Model\Struct or WsdlToPhp\PackageGenerator\Model\StructValue. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        if (($value = $this->getModel()->/** @scrutinizer ignore-call */ getValue($constant->getValue())) instanceof StructValueModel) {
Loading history...
49
            $this->defineModelAnnotationsFromWsdl($block, $value);
50
        }
51
        $block->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('string \'%s\'', $constant->getValue())));
52
53
        return $block;
54
    }
55
56
    protected function fillClassMethods(): void
57
    {
58
        $this->methods->add($this->getEnumMethodGetValidValues());
59
    }
60
61
    protected function getMethodAnnotationBlock(PhpMethod $method): ?PhpAnnotationBlock
62
    {
63
        $block = null;
64
65
        switch ($method->getName()) {
66
            case self::METHOD_GET_VALID_VALUES:
67
                $block = $this->getEnumGetValidValuesAnnotationBlock();
68
69
                break;
70
        }
71
72
        return $block;
73
    }
74
75
    protected function getEnumMethodGetValidValues(): PhpMethod
76
    {
77
        $method = new PhpMethod(self::METHOD_GET_VALID_VALUES, [], self::TYPE_ARRAY, PhpMethod::ACCESS_PUBLIC, false, true);
78
        $validValues = $this->getEnumMethodValues();
79
        $method->addChild('return [');
80
        foreach ($validValues as $validValue) {
81
            $method->addChild(sprintf('%s,', $method->getIndentedString($validValue, 1)));
82
        }
83
        $method->addChild('];');
84
85
        return $method;
86
    }
87
88
    protected function getEnumMethodValues(): array
89
    {
90
        $values = [];
91
        foreach ($this->getModel()->getValues() as $value) {
92
            $values[] = sprintf('self::%s', $value->getCleanName());
93
        }
94
95
        return $values;
96
    }
97
98
    protected function getEnumGetValidValuesAnnotationBlock(): PhpAnnotationBlock
99
    {
100
        $annotationBlock = new PhpAnnotationBlock([
101
            'Return allowed values',
102
        ]);
103
        foreach ($this->getEnumMethodValues() as $value) {
104
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, $value));
105
        }
106
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, 'string[]'));
107
108
        return $annotationBlock;
109
    }
110
}
111