Passed
Push — develop ( 32a57e...589086 )
by Mikaël
03:14 queued 36s
created

AbstractComponent::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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\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
    public function __toString(): string
22
    {
23
        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 20
        return implode('', $content);
33
    }
34
35
    /**
36
     * @return AbstractElement[]|string[]
37
     */
38
    abstract public function getElements(): array;
39
40 22
    public function setMainElement(AbstractElement $element): self
41
    {
42 22
        if ($element instanceof PhpFileElement || $element instanceof PhpClassElement) {
43 22
            $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 22
        return $this;
48
    }
49
50
    /**
51
     * @return PhpFileElement|PhpClassElement
52
     */
53 20
    public function getMainElement(): AbstractElement
54
    {
55 20
        return $this->mainElement;
56
    }
57
58
    /**
59
     * @throws InvalidArgumentException
60
     * @param string|AbstractElement $element
61
     * @return string
62
     */
63 20
    protected function getElementString($element): string
64
    {
65 20
        $string = '';
66 20
        if (is_scalar($element)) {
67
            $string = $element;
68 20
        } elseif ($element instanceof AbstractElement) {
0 ignored issues
show
introduced by
$element is always a sub-type of WsdlToPhp\PhpGenerator\Element\AbstractElement.
Loading history...
69 20
            $string = $element->toString();
70
        }
71 20
        return $string;
72
    }
73
74 16
    public function addConstantElement(PhpConstantElement $constant): self
75
    {
76 16
        if (!$constant->getClass() instanceof PhpClassElement && $this->mainElement instanceof PhpClassElement) {
77 16
            $constant->setClass($this->mainElement);
78
        }
79 16
        $this->mainElement->addChild($constant);
80 16
        return $this;
81
    }
82
83
    /**
84
     * @param string $name
85
     * @param mixed $value
86
     * @param PhpClassElement|null $class
87
     * @return AbstractComponent
88
     */
89 16
    public function addConstant(string $name, $value = null, ?PhpClassElement $class = null): self
90
    {
91 16
        return $this->addConstantElement(new PhpConstantElement($name, $value, $class));
92
    }
93
94 16
    public function addAnnotationBlockElement(PhpAnnotationBlockElement $annotationBlock): self
95
    {
96 16
        $this->mainElement->addChild($annotationBlock);
97 16
        return $this;
98
    }
99
100
    /**
101
     * @param array|string|PhpAnnotationElement $annotations
0 ignored issues
show
Bug introduced by
The type WsdlToPhp\PhpGenerator\C...nt\PhpAnnotationElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
102
     * @return AbstractComponent
103
     */
104 16
    public function addAnnotationBlock($annotations): self
105
    {
106 16
        return $this->addAnnotationBlockElement(new PhpAnnotationBlockElement(is_array($annotations) ? $annotations : [
107 16
            $annotations,
108
        ]));
109
    }
110
}
111