Passed
Push — feature/issue-11 ( b7a9d0 )
by Mikaël
03:18
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 22
    public function setMainElement(AbstractElement $element): self
32
    {
33 22
        if ($element instanceof PhpFileElement || $element instanceof PhpClassElement) {
34 22
            $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 22
        return $this;
40
    }
41
42 16
    public function addConstantElement(PhpConstantElement $constant): self
43
    {
44 16
        if (!$constant->getClass() instanceof PhpClassElement && $this->mainElement instanceof PhpClassElement) {
45 16
            $constant->setClass($this->mainElement);
46
        }
47 16
        $this->mainElement->addChild($constant);
48
49 16
        return $this;
50
    }
51
52
    /**
53
     * @param mixed $value
54
     *
55
     * @return AbstractComponent
56
     */
57 16
    public function addConstant(string $name, $value = null, ?PhpClassElement $class = null): self
58
    {
59 16
        return $this->addConstantElement(new PhpConstantElement($name, $value, $class));
60
    }
61
62 16
    public function addAnnotationBlockElement(PhpAnnotationBlockElement $annotationBlock): self
63
    {
64 16
        $this->mainElement->addChild($annotationBlock);
65
66 16
        return $this;
67
    }
68
69
    /**
70
     * @param array|PhpAnnotationBlockElement|string $annotations
71
     */
72 16
    public function addAnnotationBlock($annotations): self
73
    {
74 16
        return $this->addAnnotationBlockElement(new PhpAnnotationBlockElement(is_array($annotations) ? $annotations : [
75 16
            $annotations,
76
        ]));
77
    }
78
79 2
    public function addString(string $string = ''): self
80
    {
81 2
        $this->mainElement->addChild($string);
82
83 2
        return $this;
84
    }
85
}
86