Passed
Push — develop ( 18ac87...d6d324 )
by Mikaël
02:10
created

AbstractComponent   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 99
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 8 2
A addConstant() 0 3 1
A setMainElement() 0 9 3
A addConstantElement() 0 8 3
A addAnnotationBlockElement() 0 5 1
A getMainElement() 0 3 1
A addAnnotationBlock() 0 4 2
A getElementString() 0 10 3
A __toString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PhpGenerator\Component;
6
7
use InvalidArgumentException;
8
use WsdlToPhp\PhpGenerator\Element\AbstractElement;
9
use WsdlToPhp\PhpGenerator\Element\PhpFile as PhpFileElement;
10
use WsdlToPhp\PhpGenerator\Element\PhpClass as PhpClassElement;
11
use WsdlToPhp\PhpGenerator\Element\PhpConstant as PhpConstantElement;
12
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock as PhpAnnotationBlockElement;
13
14
abstract class AbstractComponent implements GenerateableInterface
15
{
16
    /**
17
     * @var PhpFileElement|PhpClassElement
18
     */
19
    protected $mainElement;
20
21 16
    public function __toString(): string
22
    {
23 16
        return $this->toString();
24
    }
25
26 20
    public function toString(): string
27
    {
28 20
        $content = [];
29 20
        foreach ($this->getElements() as $element) {
30 20
            $content[] = $this->getElementString($element);
31
        }
32
33 20
        return implode('', $content);
34
    }
35
36
    /**
37
     * @return AbstractElement[]|string[]
38
     */
39
    abstract public function getElements(): array;
40
41 22
    public function setMainElement(AbstractElement $element): self
42
    {
43 22
        if ($element instanceof PhpFileElement || $element instanceof PhpClassElement) {
44 22
            $this->mainElement = $element;
45
        } else {
46 2
            throw new InvalidArgumentException(sprintf('Element of type "%s" must be of type Element\PhpClass or Element\PhpFile', get_class($element)));
47
        }
48
49 22
        return $this;
50
    }
51
52
    /**
53
     * @return PhpFileElement|PhpClassElement
54
     */
55 20
    public function getMainElement(): AbstractElement
56
    {
57 20
        return $this->mainElement;
58
    }
59
60
    /**
61
     * @throws InvalidArgumentException
62
     * @param string|AbstractElement $element
63
     * @return string
64
     */
65 20
    protected function getElementString($element): string
66
    {
67 20
        $string = '';
68 20
        if (is_scalar($element)) {
69
            $string = $element;
70 20
        } elseif ($element instanceof AbstractElement) {
0 ignored issues
show
introduced by
$element is always a sub-type of WsdlToPhp\PhpGenerator\Element\AbstractElement.
Loading history...
71 20
            $string = $element->toString();
72
        }
73
74 20
        return $string;
75
    }
76
77 16
    public function addConstantElement(PhpConstantElement $constant): self
78
    {
79 16
        if (!$constant->getClass() instanceof PhpClassElement && $this->mainElement instanceof PhpClassElement) {
80 16
            $constant->setClass($this->mainElement);
81
        }
82 16
        $this->mainElement->addChild($constant);
83
84 16
        return $this;
85
    }
86
87
    /**
88
     * @param string $name
89
     * @param mixed $value
90
     * @param PhpClassElement|null $class
91
     * @return AbstractComponent
92
     */
93 16
    public function addConstant(string $name, $value = null, ?PhpClassElement $class = null): self
94
    {
95 16
        return $this->addConstantElement(new PhpConstantElement($name, $value, $class));
96
    }
97
98 16
    public function addAnnotationBlockElement(PhpAnnotationBlockElement $annotationBlock): self
99
    {
100 16
        $this->mainElement->addChild($annotationBlock);
101
102 16
        return $this;
103
    }
104
105
    /**
106
     * @param array|string|PhpAnnotationBlockElement $annotations
107
     * @return AbstractComponent
108
     */
109 16
    public function addAnnotationBlock($annotations): self
110
    {
111 16
        return $this->addAnnotationBlockElement(new PhpAnnotationBlockElement(is_array($annotations) ? $annotations : [
112 16
            $annotations,
113
        ]));
114
    }
115
}
116