RuntimeArgument::setType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Utils\ReflectionApi;
4
5
class RuntimeArgument implements CodeGenerator
6
{
7
    use DefaultValueTrait;
8
9
    private $type;
10
    private $name;
11
    private $variadic = false;
12
13 6
    public function __construct(string $name, string $type = '')
14
    {
15 6
        $this->name = $name;
16 6
        $this->type = $type;
17 6
    }
18
19 1
    public function makeVariadic(bool $variadic = true): self
20
    {
21 1
        $this->variadic = $variadic;
22
23 1
        return $this;
24
    }
25
26 1
    public function isVariadic(): bool
27
    {
28 1
        return $this->variadic;
29
    }
30
31 3
    public function getName(): string
32
    {
33 3
        return $this->name;
34
    }
35
36 2
    public function getType(): string
37
    {
38 2
        return $this->type;
39
    }
40
41 1
    public function setType(string $type): self
42
    {
43 1
        $this->type = $type;
44
45 1
        return $this;
46
    }
47
48 5
    public function generateCode(): string
49
    {
50 5
        $code = '';
51 5
        if ($this->type) {
52 3
            $code .= $this->type . ' ';
53
        }
54 5
        if ($this->variadic) {
55 1
            $code .= '...';
56
        }
57 5
        $code .= "\${$this->name}";
58
59 5
        if ($this->hasDefaultValue) {
60 1
            $defaultValue = var_export($this->defaultValue, true);
61 1
            if (is_string($this->defaultValue)) {
62
                $parts = explode('::', $this->defaultValue);
63
                if ($parts[0] === 'self' || class_exists($parts[0])) {
64
                    $defaultValue = $this->defaultValue;
65
                }
66
            }
67 1
            $code .= ' = ' . $defaultValue;
68
        }
69
70 5
        return $code;
71
    }
72
73
    public function __toString(): string
74
    {
75
        return $this->generateCode();
76
    }
77
}
78