Passed
Push — feature/issue-12 ( c44621 )
by Mikaël
48:26 queued 27s
created

AssignedValueElementTrait::getAnyValue()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 6
nop 1
dl 0
loc 16
rs 8.8333
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
    public function setValue($value): AbstractElement
17
    {
18
        if (!$this->getAcceptNonScalarValue() && !is_scalar($value) && !is_null($value)) {
19
            throw new InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value for %s object', gettype($value), $this->getCalledClass()));
20
        }
21
        $this->value = $value;
22
23
        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
    public function getValue()
27
    {
28
        return $this->value;
29
    }
30
31
    public function hasValue(): bool
32
    {
33
        return AssignedValueElementInterface::NO_VALUE !== $this->getValue();
34
    }
35
36
    public function getPhpValue(): ?string
37
    {
38
        if (!$this->hasValue()) {
39
            return '';
40
        }
41
42
        return $this->getFinalValue();
43
    }
44
45
    public function getPhpDeclaration(): string
46
    {
47
        return implode('', [
48
            $this->getAssignmentDeclarator(),
49
            $this->getPhpName(),
50
            $this->getAssignmentSign(),
51
            $this->getPhpValue(),
52
            $this->getAssignmentFinishing(),
53
            $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
    protected function getFinalValue(): ?string
72
    {
73
        if (is_scalar($this->getValue()) && !is_null($scalarValue = $this->getScalarValue($this->getValue()))) {
74
            return $scalarValue;
75
        }
76
        if (is_null($this->getValue())) {
77
            return 'null';
78
        }
79
80
        return $this->getAnyValue($this->getValue());
81
    }
82
83
    /**
84
     * @param mixed $value
85
     *
86
     * @return mixed
87
     */
88
    protected function getScalarValue($value)
89
    {
90
        $scalarValue = null;
91
        if (0 === stripos((string) $value, '::')) {
92
            $scalarValue = substr($value, 2);
93
        } elseif (false !== stripos((string) $value, '::') || false !== stripos((string) $value, 'new ') || false !== stripos((string) $value, '(') || false !== stripos((string) $value, ')')) {
94
            $scalarValue = $value;
95
        }
96
97
        return $scalarValue;
98
    }
99
100
    protected function getAnyValue($value): string
101
    {
102
        if (is_array($value)) {
103
            $exportedValue = empty($value) ? '[]' : implode("\n", array_map(function ($line) {
104
                return 'array (' === $line ? '[' : (')' === $line ? ']' : $line);
105
            }, explode("\n", var_export($value, true))));
106
        } else {
107
            $exportedValue = var_export($value, true);
108
        }
109
110
        // work around for known bug https://bugs.php.net/bug.php?id=66866
111
        if (is_float($value) && strlen((string) $value) !== strlen((string) $exportedValue)) {
112
            $exportedValue = (string) $value;
113
        }
114
115
        return $exportedValue;
116
    }
117
}
118