Total Complexity | 65 |
Total Lines | 328 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like EdmDirectValueAnnotationsManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EdmDirectValueAnnotationsManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class EdmDirectValueAnnotationsManager implements IDirectValueAnnotationsManager |
||
31 | { |
||
32 | /** |
||
33 | * @var SplObjectStorage|array<IEdmElement, mixed> keeps track of transient annotations on elements |
||
34 | */ |
||
35 | private $annotationsDictionary; |
||
36 | |||
37 | /** |
||
38 | * @var array elements for which normal comparison failed to produce a valid result, arbitrarily ordered to enable stable comparisons |
||
39 | */ |
||
40 | private $unsortedElements = []; |
||
|
|||
41 | |||
42 | /** |
||
43 | * Initializes a new instance of the EdmDirectValueAnnotationsManager class. |
||
44 | */ |
||
45 | public function __construct() |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Gets annotations associated with an element. |
||
52 | * |
||
53 | * @param IEdmElement $element the annotated element |
||
54 | * @return IDirectValueAnnotation[] The direct value annotations for the element |
||
55 | */ |
||
56 | public function getDirectValueAnnotations(IEdmElement $element): iterable |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Sets an annotation value for an EDM element. If the value is null, no annotation is added and an existing |
||
80 | * annotation with the same name is removed. |
||
81 | * |
||
82 | * @param IEdmElement $element the annotated element |
||
83 | * @param string $namespaceName namespace that the annotation belongs to |
||
84 | * @param string $localName name of the annotation within the namespace |
||
85 | * @param mixed $value the value of the annotation |
||
86 | * @return IDirectValueAnnotationsManager self |
||
87 | */ |
||
88 | public function setAnnotationValue(IEdmElement $element, string $namespaceName, string $localName, $value): IDirectValueAnnotationsManager |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Sets a set of annotation values. If a supplied value is null, no annotation is added and an existing annotation |
||
107 | * with the same name is removed. |
||
108 | * @param IDirectValueAnnotationBinding[] $annotations The annotations to set |
||
109 | * @return IDirectValueAnnotationsManager self |
||
110 | */ |
||
111 | public function setAnnotationValues(array $annotations): IDirectValueAnnotationsManager |
||
112 | { |
||
113 | /** @var IDirectValueAnnotationBinding $annotation */ |
||
114 | foreach ($annotations as $annotation) { |
||
115 | $this->SetAnnotationValue( |
||
116 | $annotation->getElement(), |
||
117 | $annotation->getNamespaceUri(), |
||
118 | $annotation->getName(), |
||
119 | $annotation->getValue() |
||
120 | ); |
||
121 | } |
||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param IEdmElement $element the annotated element |
||
127 | * @param string $namespaceName namespace that the annotation belongs to |
||
128 | * @param string $localName local name of the annotation |
||
129 | * @return mixed Returns the annotation value that corresponds to the provided name. Returns null if no |
||
130 | * annotation with the given name exists for the given element. |
||
131 | */ |
||
132 | public function getAnnotationValue(IEdmElement $element, string $namespaceName, string $localName) |
||
133 | { |
||
134 | $annotationsDictionary = $this->getAnnotationsDictionary(); |
||
135 | $transients = self::getTransientAnnotations($element, $annotationsDictionary); |
||
136 | $annotation = self::findTransientAnnotation($transients, $namespaceName, $localName); |
||
137 | if ($annotation != null) { |
||
138 | return $annotation->getValue(); |
||
139 | } |
||
140 | |||
141 | $immutableAnnotations = $this->getAttachedAnnotations($element); |
||
142 | if ($immutableAnnotations != null) { |
||
143 | /** @var IDirectValueAnnotation $existingAnnotation */ |
||
144 | foreach ($immutableAnnotations as $existingAnnotation) { |
||
145 | if ($existingAnnotation->getNamespaceUri() == $namespaceName && $existingAnnotation->getName() == $localName) { |
||
146 | // No need to check that the immutable annotation isn't Dead, because if it were |
||
147 | // the tombstone would have been found in the transient annotations. |
||
148 | return $existingAnnotation->getValue(); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | return null; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Retrieves a set of annotation values. For each requested value, returns null if no annotation with the given |
||
158 | * name exists for the given element. |
||
159 | * |
||
160 | * @param IDirectValueAnnotationBinding[] $annotations The set of requested annotations |
||
161 | * @return array Returns values that correspond to the provided annotations. A value is null if no annotation with |
||
162 | * the given name exists for the given element. |
||
163 | */ |
||
164 | public function getAnnotationValues(array $annotations): ?iterable |
||
165 | { |
||
166 | $values = []; |
||
167 | |||
168 | $index = 0; |
||
169 | foreach ($annotations as $annotation) { |
||
170 | $values[$index++] = $this->getAnnotationValue($annotation->getElement(), $annotation->getNamespaceUri(), $annotation->getName()); |
||
171 | } |
||
172 | |||
173 | return $values; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Retrieves the annotations that are directly attached to an element. |
||
178 | * |
||
179 | * @param IEdmElement $element the element in question |
||
180 | * @return iterable|null the annotations that are directly attached to an element (outside the control of the manager) |
||
181 | */ |
||
182 | protected function getAttachedAnnotations(IEdmElement $element): ?iterable |
||
183 | { |
||
184 | return null; |
||
185 | } |
||
186 | |||
187 | |||
188 | /** |
||
189 | * Retrieves the transient annotations for an EDM element. |
||
190 | * |
||
191 | * @param IEdmElement $element the annotated element |
||
192 | * @param SplObjectStorage $annotationsDictionary the dictionary for looking up the element's annotations |
||
193 | * @return mixed|null The transient annotations for the element, in a form managed by the annotations manager. |
||
194 | * This method is static to guarantee that the annotations dictionary is not fetched more than once per lookup operation. |
||
195 | */ |
||
196 | private static function getTransientAnnotations(IEdmElement $element, SplObjectStorage $annotationsDictionary) |
||
197 | { |
||
198 | return $annotationsDictionary->offsetExists($element) ? $annotationsDictionary->offsetGet($element) : null; |
||
199 | } |
||
200 | |||
201 | private static function removeTransientAnnotation(&$transientAnnotations, $namespaceName, string $localName) |
||
202 | { |
||
203 | if (null !== $transientAnnotations) { |
||
204 | $singleAnnotation = $transientAnnotations; |
||
205 | if ($singleAnnotation instanceof IDirectValueAnnotation) { |
||
206 | if ($singleAnnotation->getNamespaceUri() == $namespaceName && $singleAnnotation->getName() == $localName) { |
||
207 | $transientAnnotations = null; |
||
208 | return; |
||
209 | } |
||
210 | } else { |
||
211 | $annotationsList = $transientAnnotations; |
||
212 | assert(is_array( |
||
213 | $annotationsList |
||
214 | )); |
||
215 | $numAnnotations = count($annotationsList); |
||
216 | for ($index = 0; $index < $numAnnotations; $index++) { |
||
217 | $existingAnnotation = $annotationsList[$index]; |
||
218 | assert($existingAnnotation instanceof IDirectValueAnnotation); |
||
219 | if ($existingAnnotation->getNamespaceUri() == $namespaceName && $existingAnnotation->getName() == $localName) { |
||
220 | unset($annotationsList[$index]); |
||
221 | if (count($annotationsList) === 1) { |
||
222 | $transientAnnotations = array_pop($annotationsList); |
||
223 | } else { |
||
224 | $transientAnnotations = $annotationsList; |
||
225 | } |
||
226 | |||
227 | return; |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | private static function transientAnnotations($transientAnnotations): iterable |
||
234 | { |
||
235 | if ($transientAnnotations == null) { |
||
236 | return []; |
||
237 | } |
||
238 | |||
239 | $singleAnnotation = $transientAnnotations; |
||
240 | if ($singleAnnotation instanceof IDirectValueAnnotation) { |
||
241 | if (null !== $singleAnnotation->getValue()) { |
||
242 | yield $singleAnnotation; |
||
243 | } |
||
244 | |||
245 | return []; |
||
246 | } |
||
247 | |||
248 | $annotationsList = $transientAnnotations; |
||
249 | assert(is_iterable($annotationsList)); |
||
250 | /** @var IDirectValueAnnotation $existingAnnotation */ |
||
251 | foreach ($annotationsList as $existingAnnotation) { |
||
252 | if (null !== $existingAnnotation->getValue()) { |
||
253 | yield $existingAnnotation; |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | private static function isDead(string $namespaceName, string $localName, $transientAnnotations): bool |
||
261 | } |
||
262 | |||
263 | private static function setAnnotation(?iterable $immutableAnnotations, &$transientAnnotations, string $namespaceName, string $localName, $value): void |
||
326 | } |
||
327 | |||
328 | |||
329 | private static function findTransientAnnotation($transientAnnotations, string $namespaceName, string $localName): ?IDirectValueAnnotation |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @return SplObjectStorage |
||
354 | */ |
||
355 | private function getAnnotationsDictionary(): SplObjectStorage |
||
358 | } |
||
359 | } |
||
360 |