Passed
Push — develop ( 6b3bb3...b964e0 )
by Mikaël
01:53 queued 11s
created

AbstractComponent::getElementString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 3.0261
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 16
    public function addConstant(string $name, $value = null, ?PhpClassElement $class = null): self
56
    {
57 16
        return $this->addConstantElement(new PhpConstantElement($name, $value, $class));
58
    }
59
60 16
    public function addAnnotationBlockElement(PhpAnnotationBlockElement $annotationBlock): self
61
    {
62 16
        $this->mainElement->addChild($annotationBlock);
63
64 16
        return $this;
65
    }
66
67
    /**
68
     * @param array|PhpAnnotationBlockElement|string $annotations
69
     */
70 16
    public function addAnnotationBlock($annotations): self
71
    {
72 16
        return $this->addAnnotationBlockElement(new PhpAnnotationBlockElement(is_array($annotations) ? $annotations : [
73 16
            $annotations,
74
        ]));
75
    }
76
77 2
    public function addString(string $string = ''): self
78
    {
79 2
        $this->mainElement->addChild($string);
80
81 2
        return $this;
82
    }
83
}
84