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

AssignedValueElementTrait::getFinalValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PhpGenerator\Element;
6
7
use InvalidArgumentException;
8
9
trait AssignedValueElementTrait
10
{
11
    /**
12
     * @var mixed
13
     */
14
    protected $value;
15
16 198
    public function setValue($value): AbstractElement
17
    {
18 198
        if (!$this->getAcceptNonScalarValue() && !is_scalar($value) && !is_null($value)) {
19 4
            throw new InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value for %s object', gettype($value), $this->getCalledClass()));
20
        }
21 194
        $this->value = $value;
22
23 194
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type WsdlToPhp\PhpGenerator\E...signedValueElementTrait which is incompatible with the type-hinted return WsdlToPhp\PhpGenerator\Element\AbstractElement.
Loading history...
24
    }
25
26 164
    public function getValue()
27
    {
28 164
        return $this->value;
29
    }
30
31 164
    public function hasValue(): bool
32
    {
33 164
        return AssignedValueElementInterface::NO_VALUE !== $this->getValue();
34
    }
35
36 164
    public function getPhpValue(): ?string
37
    {
38 164
        if (!$this->hasValue()) {
39 68
            return '';
40
        }
41
42 138
        return $this->getFinalValue();
43
    }
44
45 136
    public function getPhpDeclaration(): string
46
    {
47 136
        return implode('', [
48 136
            $this->getAssignmentDeclarator(),
49 136
            $this->getPhpName(),
50 136
            $this->getAssignmentSign(),
51 136
            $this->getPhpValue(),
52 136
            $this->getAssignmentFinishing(),
53 136
            $this->endsWithSemicolon() ? ';' : '',
54
        ]);
55
    }
56
57
    abstract public function endsWithSemicolon(): bool;
58
59
    abstract public function getAssignmentDeclarator(): string;
60
61
    abstract public function getAssignmentSign(): string;
62
63
    abstract public function getAssignmentFinishing(): string;
64
65
    abstract public function getAcceptNonScalarValue(): bool;
66
67
    abstract public function getCalledClass(): string;
68
69
    abstract public function getPhpName(): string;
70
71 138
    protected function getFinalValue(): ?string
72
    {
73 138
        if (is_scalar($this->getValue()) && !is_null($scalarValue = $this->getScalarValue($this->getValue()))) {
74 12
            return $scalarValue;
75
        }
76 126
        if (is_null($this->getValue())) {
77 66
            return 'null';
78
        }
79
80 100
        return $this->getAnyValue($this->getValue());
81
    }
82
83
    /**
84
     * @param mixed $value
85
     *
86
     * @return mixed
87
     */
88 84
    protected function getScalarValue($value)
89
    {
90 84
        $scalarValue = null;
91 84
        if (0 === stripos((string) $value, '::')) {
92 2
            $scalarValue = substr($value, 2);
93 82
        } elseif (false !== stripos((string) $value, '::') || false !== stripos((string) $value, 'new ') || false !== stripos((string) $value, '(') || false !== stripos((string) $value, ')')) {
94 10
            $scalarValue = $value;
95
        }
96
97 84
        return $scalarValue;
98
    }
99
100 98
    protected function getAnyValue($value): string
101
    {
102 98
        $exportedValue = var_export($value, true);
103
        // work around for known bug https://bugs.php.net/bug.php?id=66866
104 98
        if (is_float($value) && strlen((string) $value) !== strlen((string) $exportedValue)) {
105 2
            $exportedValue = (string) $value;
106
        }
107
108 98
        return $exportedValue;
109
    }
110
}
111