Completed
Push — master ( ae9c65...55772a )
by Asmir
07:43 queued 05:39
created

SerializationContext::getPath()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 0
crap 3.0261
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;
20
21
use JMS\Serializer\Exception\RuntimeException;
22
use Metadata\MetadataFactoryInterface;
23
24
class SerializationContext extends Context
25
{
26
    /** @var \SplObjectStorage */
27
    private $visitingSet;
28
29
    /** @var \SplStack */
30
    private $visitingStack;
31
32
    /**
33
     * @var string
34
     */
35
    private $initialType;
36
37 78
    public static function create()
38
    {
39 78
        return new self();
40
    }
41
42
    /**
43
     * @param string $format
44
     */
45 350
    public function initialize($format, VisitorInterface $visitor, GraphNavigator $navigator, MetadataFactoryInterface $factory)
46
    {
47 350
        parent::initialize($format, $visitor, $navigator, $factory);
48
49 350
        $this->visitingSet = new \SplObjectStorage();
50 350
        $this->visitingStack = new \SplStack();
51 350
    }
52
53 244
    public function startVisiting($object)
54
    {
55 244
        if (!is_object($object)) {
56 4
            return;
57
        }
58 240
        $this->visitingSet->attach($object);
59 240
        $this->visitingStack->push($object);
60 240
    }
61
62 233
    public function stopVisiting($object)
63
    {
64 233
        if (!is_object($object)) {
65 4
            return;
66
        }
67 229
        $this->visitingSet->detach($object);
68 229
        $poppedObject = $this->visitingStack->pop();
69
70 229
        if ($object !== $poppedObject) {
71
            throw new RuntimeException('Context visitingStack not working well');
72
        }
73 229
    }
74
75 244
    public function isVisiting($object)
76
    {
77 244
        if (!is_object($object)) {
78 6
            return false;
79
        }
80
81 240
        return $this->visitingSet->contains($object);
82
    }
83
84 1
    public function getPath()
85
    {
86 1
        $path = array();
87 1
        foreach ($this->visitingStack as $obj) {
88 1
            $path[] = get_class($obj);
89
        }
90
91 1
        if (!$path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
92
            return null;
93
        }
94
95 1
        return implode(' -> ', $path);
96
    }
97
98 240
    public function getDirection()
99
    {
100 240
        return GraphNavigator::DIRECTION_SERIALIZATION;
101
    }
102
103 7
    public function getDepth()
104
    {
105 7
        return $this->visitingStack->count();
106
    }
107
108 32
    public function getObject()
109
    {
110 32
        return !$this->visitingStack->isEmpty() ? $this->visitingStack->top() : null;
111
    }
112
113
    public function getVisitingStack()
114
    {
115
        return $this->visitingStack;
116
    }
117
118
    public function getVisitingSet()
119
    {
120
        return $this->visitingSet;
121
    }
122
123
    /**
124
     * @param string $type
125
     * @return $this
126
     */
127 2
    public function setInitialType($type)
128
    {
129 2
        $this->initialType = $type;
130 2
        $this->attributes->set('initial_type', $type);
131 2
        return $this;
132
    }
133
134
    /**
135
     * @return string|null
136
     */
137 351
    public function getInitialType()
138
    {
139 351
        return $this->initialType
140 38
            ? $this->initialType
141 351
            : $this->attributes->containsKey('initial_type') ? $this->attributes->get('initial_type')->get() : null;
142
    }
143
}
144