Passed
Pull Request — develop (#234)
by Mikaël
49:21 queued 46:19
created

StructEnum::fillClassConstants()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
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 46
    public function setModel(AbstractModel $model): self
23
    {
24 46
        if ($model instanceof StructModel && !$model->isRestriction()) {
25 2
            throw new InvalidArgumentException('Model must be a restriction containing values', __LINE__);
26
        }
27
28 44
        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 42
    protected function fillClassConstants(ConstantContainer $constants): void
32
    {
33 42
        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 42
            $constants->add(new PhpConstant($value->getCleanName(), $value->getValue()));
35
        }
36 42
    }
37
38 42
    protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock
39
    {
40 42
        $block = new PhpAnnotationBlock([
41 42
            sprintf('Constant for value \'%s\'', $constant->getValue()),
42
        ]);
43 42
        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 42
            $this->defineModelAnnotationsFromWsdl($block, $value);
45
        }
46 42
        $block->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('string \'%s\'', $constant->getValue())));
47
48 42
        return $block;
49
    }
50
51 42
    protected function fillClassMethods(): void
52
    {
53 42
        $this->methods->add($this->getEnumMethodGetValidValues());
54 42
    }
55
56 42
    protected function getMethodAnnotationBlock(PhpMethod $method): ?PhpAnnotationBlock
57
    {
58 42
        $block = null;
59
60 42
        switch ($method->getName()) {
61 42
            case self::METHOD_GET_VALID_VALUES:
62 42
                $block = $this->getEnumGetValidValuesAnnotationBlock();
63
64 42
                break;
65
        }
66
67 42
        return $block;
68
    }
69
70 42
    protected function getEnumMethodGetValidValues(): PhpMethod
71
    {
72 42
        $method = new PhpMethod(self::METHOD_GET_VALID_VALUES, [], self::TYPE_ARRAY, PhpMethod::ACCESS_PUBLIC, false, true);
73 42
        $validValues = $this->getEnumMethodValues();
74 42
        $method->addChild('return [');
75 42
        foreach ($validValues as $validValue) {
76 42
            $method->addChild(sprintf('%s,', $method->getIndentedString($validValue, 1)));
77
        }
78 42
        $method->addChild('];');
79
80 42
        return $method;
81
    }
82
83 42
    protected function getEnumMethodValues(): array
84
    {
85 42
        $values = [];
86 42
        foreach ($this->getModel()->getValues() as $value) {
87 42
            $values[] = sprintf('self::%s', $value->getCleanName());
88
        }
89
90 42
        return $values;
91
    }
92
93 42
    protected function getEnumGetValidValuesAnnotationBlock(): PhpAnnotationBlock
94
    {
95 42
        $annotationBlock = new PhpAnnotationBlock([
96 42
            'Return allowed values',
97
        ]);
98 42
        foreach ($this->getEnumMethodValues() as $value) {
99 42
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, $value));
100
        }
101 42
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, 'string[]'));
102
103 42
        return $annotationBlock;
104
    }
105
}
106