1 | <?php |
||
34 | abstract class Context |
||
35 | { |
||
36 | /** |
||
37 | * @var \PhpCollection\Map |
||
38 | */ |
||
39 | public $attributes; |
||
40 | |||
41 | private $format; |
||
42 | |||
43 | /** @var VisitorInterface */ |
||
44 | private $visitor; |
||
45 | |||
46 | /** @var GraphNavigator */ |
||
47 | private $navigator; |
||
48 | |||
49 | /** @var MetadataFactory */ |
||
50 | private $metadataFactory; |
||
51 | |||
52 | /** @var ExclusionStrategyInterface */ |
||
53 | private $exclusionStrategy; |
||
54 | |||
55 | /** @var boolean */ |
||
56 | private $serializeNull; |
||
57 | |||
58 | private $initialized = false; |
||
59 | |||
60 | /** @var \SplStack */ |
||
61 | private $metadataStack; |
||
62 | 270 | ||
63 | public function __construct() |
||
64 | 270 | { |
|
65 | 270 | $this->attributes = new Map(); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $format |
||
70 | 237 | */ |
|
71 | public function initialize($format, VisitorInterface $visitor, GraphNavigator $navigator, MetadataFactoryInterface $factory) |
||
72 | 237 | { |
|
73 | if ($this->initialized) { |
||
74 | throw new \LogicException('This context was already initialized, and cannot be re-used.'); |
||
75 | } |
||
76 | 237 | ||
77 | 237 | $this->initialized = true; |
|
78 | 237 | $this->format = $format; |
|
79 | 237 | $this->visitor = $visitor; |
|
80 | 237 | $this->navigator = $navigator; |
|
81 | 237 | $this->metadataFactory = $factory; |
|
|
|||
82 | 237 | $this->metadataStack = new \SplStack(); |
|
83 | } |
||
84 | |||
85 | public function accept($data, array $type = null) |
||
86 | { |
||
87 | return $this->navigator->accept($data, $type, $this); |
||
88 | } |
||
89 | |||
90 | public function getMetadataFactory() |
||
91 | { |
||
92 | return $this->metadataFactory; |
||
93 | } |
||
94 | 237 | ||
95 | public function getVisitor() |
||
96 | 237 | { |
|
97 | return $this->visitor; |
||
98 | } |
||
99 | |||
100 | public function getNavigator() |
||
101 | { |
||
102 | return $this->navigator; |
||
103 | } |
||
104 | 149 | ||
105 | public function getExclusionStrategy() |
||
106 | 149 | { |
|
107 | return $this->exclusionStrategy; |
||
108 | } |
||
109 | 3 | ||
110 | public function setAttribute($key, $value) |
||
111 | 3 | { |
|
112 | 3 | $this->assertMutable(); |
|
113 | $this->attributes->set($key, $value); |
||
114 | 3 | ||
115 | return $this; |
||
116 | } |
||
117 | 18 | ||
118 | private function assertMutable() |
||
119 | 18 | { |
|
120 | 18 | if ( ! $this->initialized) { |
|
121 | return; |
||
122 | } |
||
123 | |||
124 | throw new \LogicException('This context was already initialized and is immutable; you cannot modify it anymore.'); |
||
125 | } |
||
126 | 15 | ||
127 | public function addExclusionStrategy(ExclusionStrategyInterface $strategy) |
||
128 | 15 | { |
|
129 | $this->assertMutable(); |
||
130 | 15 | ||
131 | 15 | if (null === $this->exclusionStrategy) { |
|
132 | $this->exclusionStrategy = $strategy; |
||
133 | 15 | ||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | if ($this->exclusionStrategy instanceof DisjunctExclusionStrategy) { |
||
138 | $this->exclusionStrategy->addStrategy($strategy); |
||
139 | |||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | $this->exclusionStrategy = new DisjunctExclusionStrategy(array( |
||
144 | $this->exclusionStrategy, |
||
145 | $strategy, |
||
146 | )); |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param integer $version |
||
153 | 3 | */ |
|
154 | public function setVersion($version) |
||
155 | 3 | { |
|
156 | if (null === $version) { |
||
157 | throw new \LogicException('The version must not be null.'); |
||
158 | } |
||
159 | 3 | ||
160 | 3 | $this->attributes->set('version', $version); |
|
161 | $this->addExclusionStrategy(new VersionExclusionStrategy($version)); |
||
162 | 3 | ||
163 | return $this; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param array|string $groups |
||
168 | 7 | */ |
|
169 | public function setGroups($groups) |
||
170 | 7 | { |
|
171 | if (empty($groups)) { |
||
172 | throw new \LogicException('The groups must not be empty.'); |
||
173 | } |
||
174 | 7 | ||
175 | 7 | $this->attributes->set('groups', (array) $groups); |
|
176 | $this->addExclusionStrategy(new GroupsExclusionStrategy((array) $groups)); |
||
177 | 7 | ||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | public function enableMaxDepthChecks() |
||
182 | { |
||
183 | $this->addExclusionStrategy(new DepthExclusionStrategy()); |
||
184 | |||
185 | return $this; |
||
186 | } |
||
187 | 11 | ||
188 | public function setSerializeNull($bool) |
||
189 | 11 | { |
|
190 | $this->serializeNull = (boolean) $bool; |
||
191 | 11 | ||
192 | return $this; |
||
193 | } |
||
194 | 17 | ||
195 | public function shouldSerializeNull() |
||
196 | 17 | { |
|
197 | return $this->serializeNull; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @return string |
||
202 | 169 | */ |
|
203 | public function getFormat() |
||
204 | 169 | { |
|
205 | return $this->format; |
||
206 | } |
||
207 | 144 | ||
208 | public function pushClassMetadata(ClassMetadata $metadata) |
||
209 | 144 | { |
|
210 | 144 | $this->metadataStack->push($metadata); |
|
211 | } |
||
212 | 138 | ||
213 | public function pushPropertyMetadata(PropertyMetadata $metadata) |
||
214 | 138 | { |
|
215 | 138 | $this->metadataStack->push($metadata); |
|
216 | } |
||
217 | 137 | ||
218 | public function pushIndexMetadata(IndexMetadata $metadata) |
||
219 | 137 | { |
|
220 | $this->metadataStack->push($metadata); |
||
221 | 137 | } |
|
222 | |||
223 | public function popIndexMetadata() |
||
224 | 137 | { |
|
225 | $metadata = $this->metadataStack->pop(); |
||
226 | 142 | ||
227 | if ( ! $metadata instanceof IndexMetadata) { |
||
228 | 142 | throw new RuntimeException('Context metadataStack not working well'); |
|
229 | } |
||
230 | 142 | } |
|
231 | |||
232 | public function popPropertyMetadata() |
||
233 | 142 | { |
|
234 | $metadata = $this->metadataStack->pop(); |
||
235 | 4 | ||
236 | if ( ! $metadata instanceof PropertyMetadata) { |
||
237 | 4 | throw new RuntimeException('Context metadataStack not working well'); |
|
238 | } |
||
239 | } |
||
240 | |||
241 | public function popClassMetadata() |
||
242 | { |
||
243 | $metadata = $this->metadataStack->pop(); |
||
244 | |||
245 | if ( ! $metadata instanceof ClassMetadata) { |
||
246 | throw new RuntimeException('Context metadataStack not working well'); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | public function getMetadataStack() |
||
251 | { |
||
252 | return $this->metadataStack; |
||
253 | } |
||
254 | |||
255 | abstract public function getDepth(); |
||
256 | |||
257 | /** |
||
258 | * @return integer |
||
259 | */ |
||
260 | abstract public function getDirection(); |
||
261 | } |
||
262 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.