Passed
Push — develop ( a97e4b...d3b84d )
by Mikaël
01:37 queued 15s
created

AbstractComponent::getMainElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\PhpAnnotationBlock as PhpAnnotationBlockElement;
10
use WsdlToPhp\PhpGenerator\Element\PhpClass as PhpClassElement;
11
use WsdlToPhp\PhpGenerator\Element\PhpConstant as PhpConstantElement;
12
use WsdlToPhp\PhpGenerator\Element\PhpFile as PhpFileElement;
13
14
abstract class AbstractComponent implements GenerateableInterface
15
{
16
    /**
17
     * @var PhpClassElement|PhpFileElement
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
        return (string) $this->mainElement;
29
    }
30
31 26
    public function setMainElement(AbstractElement $element): self
32
    {
33 26
        if ($element instanceof PhpFileElement || $element instanceof PhpClassElement) {
34 26
            $this->mainElement = $element;
35
        } else {
36 2
            throw new InvalidArgumentException(sprintf('Element of type "%s" must be of type Element\PhpClass or Element\PhpFile', get_class($element)));
37
        }
38
39 26
        return $this;
40
    }
41
42
    /**
43
     * @return PhpClassElement|PhpFileElement
44
     */
45 4
    public function getMainElement()
46
    {
47 4
        return $this->mainElement;
48
    }
49
50 16
    public function addConstantElement(PhpConstantElement $constant): self
51
    {
52 16
        if (!$constant->getClass() instanceof PhpClassElement && $this->mainElement instanceof PhpClassElement) {
53 16
            $constant->setClass($this->mainElement);
54
        }
55 16
        $this->mainElement->addChild($constant);
56
57 16
        return $this;
58
    }
59
60
    /**
61
     * @param mixed $value
62
     */
63 16
    public function addConstant(string $name, $value = null, ?PhpClassElement $class = null): self
64
    {
65 16
        return $this->addConstantElement(new PhpConstantElement($name, $value, $class));
66
    }
67
68 16
    public function addAnnotationBlockElement(PhpAnnotationBlockElement $annotationBlock): self
69
    {
70 16
        $this->mainElement->addChild($annotationBlock);
71
72 16
        return $this;
73
    }
74
75
    /**
76
     * @param array|PhpAnnotationBlockElement|string $annotations
77
     */
78 16
    public function addAnnotationBlock($annotations): self
79
    {
80 16
        return $this->addAnnotationBlockElement(new PhpAnnotationBlockElement(is_array($annotations) ? $annotations : [
81 16
            $annotations,
82
        ]));
83
    }
84
85 2
    public function addString(string $string = ''): self
86
    {
87 2
        $this->mainElement->addChild($string);
88
89 2
        return $this;
90
    }
91
}
92