Completed
Push — master ( 3e05ae...99069b )
by Asmir
12s
created

DefaultAccessorStrategy   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getValue() 0 25 6
A setValue() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
namespace JMS\Serializer\Accessor;
22
23
use JMS\Serializer\Exception\ExpressionLanguageRequiredException;
24
use JMS\Serializer\Exception\LogicException;
25
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
26
use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
27
use JMS\Serializer\Metadata\PropertyMetadata;
28
use JMS\Serializer\Metadata\StaticPropertyMetadata;
29
use JMS\Serializer\Metadata\VirtualPropertyMetadata;
30
31
/**
32
 * @author Asmir Mustafic <[email protected]>
33
 */
34
final class DefaultAccessorStrategy implements AccessorStrategyInterface
35
{
36
    private $readAccessors = array();
37
    private $writeAccessors = array();
38
39
    /**
40
     * @var ExpressionEvaluatorInterface
41
     */
42
    private $evaluator;
43
44 326
    public function __construct(ExpressionEvaluatorInterface $evaluator = null)
45
    {
46 326
        $this->evaluator = $evaluator;
47 326
    }
48
49 166
    public function getValue(object $object, PropertyMetadata $metadata)
50
    {
51 166
        if ($metadata instanceof StaticPropertyMetadata) {
52 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

52
            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...
53
        }
54
55 162
        if ($metadata instanceof ExpressionPropertyMetadata) {
56 4
            if ($this->evaluator === null) {
57 2
                throw new ExpressionLanguageRequiredException(sprintf('The property %s on %s requires the expression accessor strategy to be enabled.', $metadata->name, $metadata->class));
58
            }
59
60 2
            return $this->evaluator->evaluate($metadata->expression, ['object' => $object]);
61
        }
62
63 160
        if (null === $metadata->getter) {
64 154
            if (!isset($this->readAccessors[$metadata->class])) {
65 154
                $this->readAccessors[$metadata->class] = \Closure::bind(function ($o, $name) {
66 154
                    return $o->$name;
67 154
                }, null, $metadata->class);
68
            }
69
70 154
            return $this->readAccessors[$metadata->class]($object, $metadata->name);
71
        }
72
73 24
        return $object->{$metadata->getter}();
74
    }
75
76 59
    public function setValue(object $object, $value, PropertyMetadata $metadata): void
77
    {
78 59
        if ($metadata->readOnly) {
79
            throw new LogicException(sprintf('%s on %s is read only.'), $metadata->name, $metadata->class);
0 ignored issues
show
Bug introduced by
$metadata->class of type string is incompatible with the type Throwable|null expected by parameter $previous of JMS\Serializer\Exception...xception::__construct(). ( Ignorable by Annotation )

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

79
            throw new LogicException(sprintf('%s on %s is read only.'), $metadata->name, /** @scrutinizer ignore-type */ $metadata->class);
Loading history...
Bug introduced by
$metadata->name of type string is incompatible with the type integer expected by parameter $code of JMS\Serializer\Exception...xception::__construct(). ( Ignorable by Annotation )

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

79
            throw new LogicException(sprintf('%s on %s is read only.'), /** @scrutinizer ignore-type */ $metadata->name, $metadata->class);
Loading history...
80
        }
81
82 59
        if (null === $metadata->setter) {
83 58
            if (!isset($this->writeAccessors[$metadata->class])) {
84 58
                $this->writeAccessors[$metadata->class] = \Closure::bind(function ($o, $name, $value) {
85 58
                    $o->$name = $value;
86 58
                }, null, $metadata->class);
87
            }
88
89 58
            $this->writeAccessors[$metadata->class]($object, $metadata->name, $value);
90 58
            return;
91
        }
92
93 3
        $object->{$metadata->setter}($value);
94 3
    }
95
}
96