Passed
Push — develop ( 0a8b55...80d7ca )
by Mikaël
06:50
created

StructEnum   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 92
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setModel() 0 7 3
A fillClassConstants() 0 4 2
A getEnumMethodValues() 0 8 2
A getEnumGetValidValuesAnnotationBlock() 0 11 2
A fillClassMethods() 0 3 1
A getEnumMethodGetValidValues() 0 11 2
A defineUseStatements() 0 3 1
A getMethodAnnotationBlock() 0 12 2
A getConstantAnnotationBlock() 0 11 2
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 defineUseStatements(): self
39
    {
40 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...
41
    }
42
43 42
    protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock
44
    {
45 42
        $block = new PhpAnnotationBlock([
46 42
            sprintf('Constant for value \'%s\'', $constant->getValue()),
47
        ]);
48 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

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