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

PhpDeclare::getChildContent()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PhpGenerator\Element;
6
7
use InvalidArgumentException;
8
9
class PhpDeclare extends AbstractElement
10
{
11
    const STATEMENT = 'declare(%s);';
12
13
    const DIRECTIVE_ENCODING = 'encoding';
14
15
    const DIRECTIVE_STRICT_TYPES = 'strict_types';
16
17
    const DIRECTIVE_TICKS = 'ticks';
18
19
    const ALLOWED_DIRECTIVES = [
20
        self::DIRECTIVE_ENCODING,
21
        self::DIRECTIVE_STRICT_TYPES,
22
        self::DIRECTIVE_TICKS,
23
    ];
24
25
    /**
26
     * @var string|int
27
     */
28
    private $value;
29
30 28
    public function __construct(string $name, $value = null)
31
    {
32 28
        parent::__construct($name);
33 26
        $this->setValue($value);
34 24
    }
35
36
    /**
37
     * @param int|string $value
38
     * @return PhpDeclare
39
     */
40 26
    public function setValue($value): self
41
    {
42 26
        if (!is_null($value) && !is_scalar($value)) {
0 ignored issues
show
introduced by
The condition is_null($value) is always false.
Loading history...
introduced by
The condition is_scalar($value) is always true.
Loading history...
43 2
            throw new InvalidArgumentException(sprintf('Value must be a scalar value, %s given', var_export($value, true)));
44
        }
45
46 24
        $this->value = $value;
47
48 24
        return $this;
49
    }
50
51
    /**
52
     * @return int|string
53
     */
54 16
    public function getValue()
55
    {
56 16
        return $this->value;
57
    }
58
59 28
    public static function nameIsValid(string $name, bool $allowBackslash = false): bool
60
    {
61 28
        return parent::nameIsValid($name, $allowBackslash) && in_array($name, self::ALLOWED_DIRECTIVES, true);
62
    }
63
64 16
    public function getPhpDeclaration(): string
65
    {
66 16
        $directives = array_merge([$this], $this->getChildren());
67
68 16
        $strings = [];
69
        /** @var PhpDeclare $directive */
70 16
        foreach ($directives as $directive) {
71 16
            if (is_null($directive->getValue())) {
72 2
                continue;
73
            }
74
75 14
            $strings[] = sprintf('%s=%s', $directive->getName(), var_export($directive->getValue(), true));
76
        }
77
78 16
        return empty($strings) ? '' : sprintf(self::STATEMENT, implode(', ', $strings));
79
    }
80
81
    /**
82
     * Children are handled before in getPhpDeclaration in order to have one line per declare directive
83
     * @param $child
84
     * @param int|null $indentation
85
     * @return string
86
     */
87 4
    protected function getChildContent($child, int $indentation = null): string
88
    {
89 4
        return '';
90
    }
91
92 14
    public function addChild($child): AbstractElement
93
    {
94
        /** @var AbstractElement $child */
95 14
        if (static::childIsValid($child) && $child->getName() === $this->getName()) {
0 ignored issues
show
Bug Best Practice introduced by
The method WsdlToPhp\PhpGenerator\E...Element::childIsValid() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        if (static::/** @scrutinizer ignore-call */ childIsValid($child) && $child->getName() === $this->getName()) {
Loading history...
96 2
            throw new InvalidArgumentException(sprintf('The current directive named %s can\'t contain a child of same directive name', $this->getName()));
97
        }
98
99 12
        return parent::addChild($child);
100
    }
101
102 14
    public function getChildrenTypes(): array
103
    {
104
        return [
105 14
            PhpDeclare::class,
106
        ];
107
    }
108
}
109