PhpDeclare   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 97
ccs 29
cts 29
cp 1
rs 10
c 1
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A nameIsValid() 0 3 2
A setValue() 0 9 3
A getValue() 0 3 1
A addChild() 0 8 3
A getChildContent() 0 3 1
A getChildrenTypes() 0 4 1
A getPhpDeclaration() 0 16 4
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
    public const STATEMENT = 'declare(%s);';
12
13
    public const DIRECTIVE_ENCODING = 'encoding';
14
15
    public const DIRECTIVE_STRICT_TYPES = 'strict_types';
16
17
    public const DIRECTIVE_TICKS = 'ticks';
18
19
    public const ALLOWED_DIRECTIVES = [
20
        self::DIRECTIVE_ENCODING,
21
        self::DIRECTIVE_STRICT_TYPES,
22
        self::DIRECTIVE_TICKS,
23
    ];
24
25
    /**
26
     * @var int|string
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
     */
39 26
    public function setValue($value): self
40
    {
41 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...
42 2
            throw new InvalidArgumentException(sprintf('Value must be a scalar value, %s given', var_export($value, true)));
43
        }
44
45 24
        $this->value = $value;
46
47 24
        return $this;
48
    }
49
50
    /**
51
     * @return int|string
52
     */
53 16
    public function getValue()
54
    {
55 16
        return $this->value;
56
    }
57
58 28
    public static function nameIsValid(string $name, bool $allowBackslash = false): bool
59
    {
60 28
        return parent::nameIsValid($name, $allowBackslash) && in_array($name, self::ALLOWED_DIRECTIVES, true);
61
    }
62
63 16
    public function getPhpDeclaration(): string
64
    {
65 16
        $directives = array_merge([$this], $this->getChildren());
66
67 16
        $strings = [];
68
69 16
        /** @var PhpDeclare $directive */
70 16
        foreach ($directives as $directive) {
71 2
            if (is_null($directive->getValue())) {
72
                continue;
73
            }
74 14
75
            $strings[] = sprintf('%s=%s', $directive->getName(), var_export($directive->getValue(), true));
76
        }
77 16
78
        return 0 === count($strings) ? '' : sprintf(self::STATEMENT, implode(', ', $strings));
79
    }
80 14
81
    public function addChild($child): AbstractElement
82
    {
83 14
        /** @var AbstractElement $child */
84 2
        if ($this->childIsValid($child) && $child->getName() === $this->getName()) {
85
            throw new InvalidArgumentException(sprintf('The current directive named %s can\'t contain a child of same directive name', $this->getName()));
86
        }
87 12
88
        return parent::addChild($child);
89
    }
90 14
91
    public function getChildrenTypes(): array
92
    {
93 14
        return [
94
            PhpDeclare::class,
95
        ];
96
    }
97
98
    /**
99
     * Children are handled before in getPhpDeclaration in order to have one line per declare directive.
100
     *
101
     * @param mixed $child
102 4
     */
103
    protected function getChildContent($child, int $indentation = null): string
104 4
    {
105
        return '';
106
    }
107
}
108