Passed
Push — master ( 85428b...881489 )
by Asmir
05:57 queued 02:54
created

DefaultAccessorStrategy::setValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 3
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Accessor;
6
7
use JMS\Serializer\Exception\ExpressionLanguageRequiredException;
8
use JMS\Serializer\Exception\LogicException;
9
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
10
use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
11
use JMS\Serializer\Metadata\PropertyMetadata;
12
use JMS\Serializer\Metadata\StaticPropertyMetadata;
13
use JMS\Serializer\Metadata\VirtualPropertyMetadata;
14
15
/**
16
 * @author Asmir Mustafic <[email protected]>
17
 */
18
final class DefaultAccessorStrategy implements AccessorStrategyInterface
19
{
20
    private $readAccessors = array();
21
    private $writeAccessors = array();
22
23
    /**
24
     * @var ExpressionEvaluatorInterface
25
     */
26
    private $evaluator;
27
28 327
    public function __construct(ExpressionEvaluatorInterface $evaluator = null)
29
    {
30 327
        $this->evaluator = $evaluator;
31 327
    }
32
33 166
    public function getValue(object $object, PropertyMetadata $metadata)
34
    {
35 166
        if ($metadata instanceof StaticPropertyMetadata) {
36 15
            return $metadata->getValue(null);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\Metadata\...rtyMetadata::getValue() has too many arguments starting with null. ( Ignorable by Annotation )

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

36
            return $metadata->/** @scrutinizer ignore-call */ getValue(null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
37
        }
38
39 162
        if ($metadata instanceof ExpressionPropertyMetadata) {
40 4
            if ($this->evaluator === null) {
41 2
                throw new ExpressionLanguageRequiredException(sprintf('The property %s on %s requires the expression accessor strategy to be enabled.', $metadata->name, $metadata->class));
42
            }
43
44 2
            return $this->evaluator->evaluate($metadata->expression, ['object' => $object]);
45
        }
46
47 160
        if (null === $metadata->getter) {
48 154
            if (!isset($this->readAccessors[$metadata->class])) {
49
                $this->readAccessors[$metadata->class] = \Closure::bind(function ($o, $name) {
50 154
                    return $o->$name;
51 154
                }, null, $metadata->class);
52
            }
53
54 154
            return $this->readAccessors[$metadata->class]($object, $metadata->name);
55
        }
56
57 24
        return $object->{$metadata->getter}();
58
    }
59
60 60
    public function setValue(object $object, $value, PropertyMetadata $metadata): void
61
    {
62 60
        if ($metadata->readOnly) {
63
            throw new LogicException(sprintf('%s on %s is read only.', $metadata->name, $metadata->class));
64
        }
65
66 60
        if (null === $metadata->setter) {
67 59
            if (!isset($this->writeAccessors[$metadata->class])) {
68
                $this->writeAccessors[$metadata->class] = \Closure::bind(function ($o, $name, $value) {
69 59
                    $o->$name = $value;
70 59
                }, null, $metadata->class);
71
            }
72
73 59
            $this->writeAccessors[$metadata->class]($object, $metadata->name, $value);
74 59
            return;
75
        }
76
77 3
        $object->{$metadata->setter}($value);
78 3
    }
79
}
80