Passed
Push — feature/issue-11 ( b7a9d0 )
by Mikaël
03:18
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 196
    public function setValue($value): AbstractElement
17
    {
18 196
        if (false === $this->getAcceptNonScalarValue() && !is_scalar($value) && null !== $value) {
0 ignored issues
show
Bug introduced by
It seems like getAcceptNonScalarValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

18
        if (false === $this->/** @scrutinizer ignore-call */ getAcceptNonScalarValue() && !is_scalar($value) && null !== $value) {
Loading history...
19 4
            throw new InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value for %s object', gettype($value), $this->getCalledClass()));
0 ignored issues
show
Bug introduced by
It seems like getCalledClass() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

19
            throw new InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value for %s object', gettype($value), $this->/** @scrutinizer ignore-call */ getCalledClass()));
Loading history...
20
        }
21 192
        $this->value = $value;
22
23 192
        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 162
    public function getValue()
27
    {
28 162
        return $this->value;
29
    }
30
31 162
    public function hasValue(): bool
32
    {
33 162
        return AssignedValueElementInterface::NO_VALUE !== $this->getValue();
34
    }
35
36 162
    public function getPhpValue(): ?string
37
    {
38 162
        if (!$this->hasValue()) {
39 66
            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(),
0 ignored issues
show
Bug introduced by
It seems like getAssignmentDeclarator() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

48
            $this->/** @scrutinizer ignore-call */ 
49
                   getAssignmentDeclarator(),
Loading history...
49 136
            $this->getPhpName(),
0 ignored issues
show
Bug introduced by
The method getPhpName() does not exist on WsdlToPhp\PhpGenerator\E...signedValueElementTrait. Did you maybe mean getPhpValue()? ( Ignorable by Annotation )

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

49
            $this->/** @scrutinizer ignore-call */ 
50
                   getPhpName(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50 136
            $this->getAssignmentSign(),
0 ignored issues
show
Bug introduced by
It seems like getAssignmentSign() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

50
            $this->/** @scrutinizer ignore-call */ 
51
                   getAssignmentSign(),
Loading history...
51 136
            $this->getPhpValue(),
52 136
            $this->getAssignmentFinishing(),
0 ignored issues
show
Bug introduced by
It seems like getAssignmentFinishing() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

52
            $this->/** @scrutinizer ignore-call */ 
53
                   getAssignmentFinishing(),
Loading history...
53 136
            true === $this->endsWithSemicolon() ? ';' : '',
0 ignored issues
show
Bug introduced by
It seems like endsWithSemicolon() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

53
            true === $this->/** @scrutinizer ignore-call */ endsWithSemicolon() ? ';' : '',
Loading history...
54
        ]);
55
    }
56
57 138
    protected function getFinalValue(): ?string
58
    {
59 138
        if (is_scalar($this->getValue()) && null !== ($scalarValue = $this->getScalarValue($this->getValue()))) {
60 12
            return $scalarValue;
61
        }
62 126
        if (is_null($this->getValue())) {
63 66
            return 'null';
64
        }
65
66 100
        return $this->getAnyValue($this->getValue());
67
    }
68
69
    /**
70
     * @param mixed $value
71
     *
72
     * @return mixed
73
     */
74 84
    protected function getScalarValue($value)
75
    {
76 84
        $scalarValue = null;
77 84
        if (0 === stripos((string) $value, '::')) {
78 2
            $scalarValue = substr($value, 2);
79 82
        } elseif (false !== stripos((string) $value, '::') || false !== stripos((string) $value, 'new ') || false !== stripos((string) $value, '(') || false !== stripos((string) $value, ')')) {
80 10
            $scalarValue = $value;
81
        }
82
83 84
        return $scalarValue;
84
    }
85
86 98
    protected function getAnyValue($value): string
87
    {
88 98
        $exportedValue = var_export($value, true);
89
        // work around for known bug https://bugs.php.net/bug.php?id=66866
90 98
        if (is_float($value) && strlen((string) $value) !== strlen((string) $exportedValue)) {
91 2
            $exportedValue = (string) $value;
92
        }
93
94 98
        return $exportedValue;
95
    }
96
}
97