Completed
Push — master ( 03494f...174675 )
by Mikaël
01:38
created

AbstractComponent   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 102
ccs 31
cts 32
cp 0.9688
rs 10
c 0
b 0
f 0

9 Methods

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