Passed
Push — develop ( 393819...8447c9 )
by Mikaël
74:52 queued 24:50
created

StructEnum::fillClassConstants()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
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 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 126
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 126
    }
30 126
31 63
    protected function fillClassConstants(ConstantContainer $constants): void
32 126
    {
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 126
38
    protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock
39 126
    {
40 126
        $block = new PhpAnnotationBlock([
41 63
            sprintf('Constant for value \'%s\'', $constant->getValue()),
42 126
        ]);
43 126
        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

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