Completed
Pull Request — master (#884)
by Robert
10:46 queued 04:46
created

PropertyMetadata::setAccessor()   C

Complexity

Conditions 13
Paths 14

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 13

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 18
cts 18
cp 1
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 19
nc 14
nop 3
crap 13

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Serializer\Metadata;
20
21
use JMS\Serializer\Exception\RuntimeException;
22
use JMS\Serializer\TypeParser;
23
use Metadata\PropertyMetadata as BasePropertyMetadata;
24
25
class PropertyMetadata extends BasePropertyMetadata
26
{
27
    const ACCESS_TYPE_PROPERTY = 'property';
28
    const ACCESS_TYPE_PUBLIC_METHOD = 'public_method';
29
30
    public $sinceVersion;
31
    public $untilVersion;
32
    public $groups;
33
    public $serializedName;
34
    public $type;
35
    public $xmlCollection = false;
36
    public $xmlCollectionInline = false;
37
    public $xmlCollectionSkipWhenEmpty = true;
38
    public $xmlEntryName;
39
    public $xmlEntryNamespace;
40
    public $xmlKeyAttribute;
41
    public $xmlAttribute = false;
42
    public $xmlValue = false;
43
    public $xmlNamespace;
44
    public $xmlKeyValuePairs = false;
45
    public $xmlElementCData = true;
46
    public $getter;
47
    public $setter;
48
    public $inline = false;
49
    public $skipWhenEmpty = false;
50
    public $readOnly = false;
51
    public $xmlAttributeMap = false;
52
    public $maxDepth = null;
53
    public $excludeIf = null;
54
55
    private $closureAccessor;
56
57
    private static $typeParser;
58
59 327
    public function __construct($class, $name)
60
    {
61 327
        parent::__construct($class, $name);
62 327
        $this->closureAccessor = \Closure::bind(function ($o, $name) {
63 191
            return $o->$name;
64 327
        }, null, $class);
65 327
    }
66 295
    public function setAccessor($type, $getter = null, $setter = null)
67
    {
68 295
        if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
69 15
            $class = $this->reflection->getDeclaringClass();
70
71 15
            if (empty($getter)) {
72 11
                if ($class->hasMethod('get' . $this->name) && $class->getMethod('get' . $this->name)->isPublic()) {
73 7
                    $getter = 'get' . $this->name;
74 4
                } elseif ($class->hasMethod('is' . $this->name) && $class->getMethod('is' . $this->name)->isPublic()) {
75 1
                    $getter = 'is' . $this->name;
76 3
                } elseif ($class->hasMethod('has' . $this->name) && $class->getMethod('has' . $this->name)->isPublic()) {
77 1
                    $getter = 'has' . $this->name;
78
                } else {
79 2
                    throw new RuntimeException(sprintf('There is neither a public %s method, nor a public %s method, nor a public %s method in class %s. Please specify which public method should be used for retrieving the value of the property %s.', 'get' . ucfirst($this->name), 'is' . ucfirst($this->name), 'has' . ucfirst($this->name), $this->class, $this->name));
80
                }
81
            }
82
83 13
            if (empty($setter) && !$this->readOnly) {
84 7
                if ($class->hasMethod('set' . $this->name) && $class->getMethod('set' . $this->name)->isPublic()) {
85 6
                    $setter = 'set' . $this->name;
86
                } else {
87 1
                    throw new RuntimeException(sprintf('There is no public %s method in class %s. Please specify which public method should be used for setting the value of the property %s.', 'set' . ucfirst($this->name), $this->class, $this->name));
88
                }
89
            }
90
        }
91
92 292
        $this->getter = $getter;
93 292
        $this->setter = $setter;
94 292
    }
95
96 205
    public function getValue($obj)
97
    {
98 205
        if (null === $this->getter) {
99 191
            if (null !== $this->closureAccessor) {
100 191
                $accessor = $this->closureAccessor;
101 191
                return $accessor($obj, $this->name);
102
            }
103
104
            return parent::getValue($obj);
105
        }
106
107 41
        return $obj->{$this->getter}();
108
    }
109
110 63
    public function setValue($obj, $value)
111
    {
112 63
        if (null === $this->setter) {
113 57
            parent::setValue($obj, $value);
114 57
            return;
115
        }
116
117 8
        $obj->{$this->setter}($value);
118 8
    }
119
120 253
    public function setType($type)
121
    {
122 253
        if (null === self::$typeParser) {
123 1
            self::$typeParser = new TypeParser();
124
        }
125
126 253
        $this->type = self::$typeParser->parse($type);
127 253
    }
128
129 1
    public function serialize()
130
    {
131 1
        return serialize(array(
132 1
            $this->sinceVersion,
133 1
            $this->untilVersion,
134 1
            $this->groups,
135 1
            $this->serializedName,
136 1
            $this->type,
137 1
            $this->xmlCollection,
138 1
            $this->xmlCollectionInline,
139 1
            $this->xmlEntryName,
140 1
            $this->xmlKeyAttribute,
141 1
            $this->xmlAttribute,
142 1
            $this->xmlValue,
143 1
            $this->xmlNamespace,
144 1
            $this->xmlKeyValuePairs,
145 1
            $this->xmlElementCData,
146 1
            $this->getter,
147 1
            $this->setter,
148 1
            $this->inline,
149 1
            $this->readOnly,
150 1
            $this->xmlAttributeMap,
151 1
            $this->maxDepth,
152 1
            parent::serialize(),
153 1
            'xmlEntryNamespace' => $this->xmlEntryNamespace,
154 1
            'xmlCollectionSkipWhenEmpty' => $this->xmlCollectionSkipWhenEmpty,
155 1
            'excludeIf' => $this->excludeIf,
156 1
            'skipWhenEmpty' => $this->skipWhenEmpty,
157
        ));
158
    }
159
160 1
    public function unserialize($str)
161
    {
162 1
        $unserialized = unserialize($str);
163
        list(
164 1
            $this->sinceVersion,
165 1
            $this->untilVersion,
166 1
            $this->groups,
167 1
            $this->serializedName,
168 1
            $this->type,
169 1
            $this->xmlCollection,
170 1
            $this->xmlCollectionInline,
171 1
            $this->xmlEntryName,
172 1
            $this->xmlKeyAttribute,
173 1
            $this->xmlAttribute,
174 1
            $this->xmlValue,
175 1
            $this->xmlNamespace,
176 1
            $this->xmlKeyValuePairs,
177 1
            $this->xmlElementCData,
178 1
            $this->getter,
179 1
            $this->setter,
180 1
            $this->inline,
181 1
            $this->readOnly,
182 1
            $this->xmlAttributeMap,
183 1
            $this->maxDepth,
184
            $parentStr
185 1
            ) = $unserialized;
186
187 1
        if (isset($unserialized['xmlEntryNamespace'])) {
188
            $this->xmlEntryNamespace = $unserialized['xmlEntryNamespace'];
189
        }
190 1
        if (isset($unserialized['xmlCollectionSkipWhenEmpty'])) {
191 1
            $this->xmlCollectionSkipWhenEmpty = $unserialized['xmlCollectionSkipWhenEmpty'];
192
        }
193 1
        if (isset($unserialized['excludeIf'])) {
194
            $this->excludeIf = $unserialized['excludeIf'];
195
        }
196 1
        if (isset($unserialized['skipWhenEmpty'])) {
197 1
            $this->skipWhenEmpty = $unserialized['skipWhenEmpty'];
198
        }
199
200 1
        parent::unserialize($parentStr);
201
202 1
        $this->closureAccessor = \Closure::bind(function ($o, $name) {
203
            return $o->$name;
204 1
        }, null, $this->class);
205
    }
206
}
207