Total Complexity | 65 |
Total Lines | 323 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 |
||
89 | { |
||
90 | $annotationsDictionary = $this->getAnnotationsDictionary(); |
||
91 | $transientAnnotations = self::getTransientAnnotations($element, $annotationsDictionary); |
||
92 | $transientAnnotationsBeforeSet = null !== $transientAnnotations ? clone $transientAnnotations : new SplObjectStorage(); |
||
93 | self::setAnnotation($this->getAttachedAnnotations($element), $transientAnnotations, $namespaceName, $localName, $value); |
||
94 | |||
95 | // There is at least one case (removing an annotation that was not present to begin with) where the transient annotations are not changed, |
||
96 | // so test to see if updating the dictionary is necessary. |
||
97 | if ($transientAnnotations != $transientAnnotationsBeforeSet) { |
||
98 | $annotationsDictionary->offsetSet($element, $transientAnnotations); |
||
99 | } |
||
100 | |||
101 | $this->annotationsDictionary = $annotationsDictionary; |
||
102 | return $this; |
||
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($annotation->getElement(), $annotation->getNamespaceUri(), $annotation->getName(), $annotation->getValue()); |
||
116 | } |
||
117 | return $this; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param IEdmElement $element the annotated element |
||
122 | * @param string $namespaceName namespace that the annotation belongs to |
||
123 | * @param string $localName local name of the annotation |
||
124 | * @return mixed Returns the annotation value that corresponds to the provided name. Returns null if no |
||
125 | * annotation with the given name exists for the given element. |
||
126 | */ |
||
127 | public function getAnnotationValue(IEdmElement $element, string $namespaceName, string $localName) |
||
128 | { |
||
129 | $annotationsDictionary = $this->getAnnotationsDictionary(); |
||
130 | $transients = self::getTransientAnnotations($element, $annotationsDictionary); |
||
131 | $annotation = self::findTransientAnnotation($transients, $namespaceName, $localName); |
||
132 | if ($annotation != null) { |
||
133 | return $annotation->getValue(); |
||
134 | } |
||
135 | |||
136 | $immutableAnnotations = $this->getAttachedAnnotations($element); |
||
137 | if ($immutableAnnotations != null) { |
||
138 | /** @var IDirectValueAnnotation $existingAnnotation */ |
||
139 | foreach ($immutableAnnotations as $existingAnnotation) { |
||
140 | if ($existingAnnotation->getNamespaceUri() == $namespaceName && $existingAnnotation->getName() == $localName) { |
||
141 | // No need to check that the immutable annotation isn't Dead, because if it were |
||
142 | // the tombstone would have been found in the transient annotations. |
||
143 | return $existingAnnotation->getValue(); |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | |||
148 | return null; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Retrieves a set of annotation values. For each requested value, returns null if no annotation with the given |
||
153 | * name exists for the given element. |
||
154 | * |
||
155 | * @param IDirectValueAnnotationBinding[] $annotations The set of requested annotations |
||
156 | * @return array Returns values that correspond to the provided annotations. A value is null if no annotation with |
||
157 | * the given name exists for the given element. |
||
158 | */ |
||
159 | public function getAnnotationValues(array $annotations): ?iterable |
||
160 | { |
||
161 | $values = []; |
||
162 | |||
163 | $index = 0; |
||
164 | foreach ($annotations as $annotation) { |
||
165 | $values[$index++] = $this->getAnnotationValue($annotation->getElement(), $annotation->getNamespaceUri(), $annotation->getName()); |
||
166 | } |
||
167 | |||
168 | return $values; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Retrieves the annotations that are directly attached to an element. |
||
173 | * |
||
174 | * @param IEdmElement $element the element in question |
||
175 | * @return iterable|null the annotations that are directly attached to an element (outside the control of the manager) |
||
176 | */ |
||
177 | protected function getAttachedAnnotations(IEdmElement $element): ?iterable |
||
178 | { |
||
179 | return null; |
||
180 | } |
||
181 | |||
182 | |||
183 | /** |
||
184 | * Retrieves the transient annotations for an EDM element. |
||
185 | * |
||
186 | * @param IEdmElement $element the annotated element |
||
187 | * @param SplObjectStorage $annotationsDictionary the dictionary for looking up the element's annotations |
||
188 | * @return mixed|null The transient annotations for the element, in a form managed by the annotations manager. |
||
189 | * This method is static to guarantee that the annotations dictionary is not fetched more than once per lookup operation. |
||
190 | */ |
||
191 | private static function getTransientAnnotations(IEdmElement $element, SplObjectStorage $annotationsDictionary) |
||
192 | { |
||
193 | return $annotationsDictionary->offsetExists($element) ? $annotationsDictionary->offsetGet($element) : null; |
||
194 | } |
||
195 | |||
196 | private static function removeTransientAnnotation(&$transientAnnotations, $namespaceName, string $localName) |
||
197 | { |
||
198 | if (null !== $transientAnnotations) { |
||
199 | $singleAnnotation = $transientAnnotations; |
||
200 | if ($singleAnnotation instanceof IDirectValueAnnotation) { |
||
201 | if ($singleAnnotation->getNamespaceUri() == $namespaceName && $singleAnnotation->getName() == $localName) { |
||
202 | $transientAnnotations = null; |
||
203 | return; |
||
204 | } |
||
205 | } else { |
||
206 | $annotationsList = $transientAnnotations; |
||
207 | assert(is_array( |
||
208 | $annotationsList |
||
209 | )); |
||
210 | $numAnnotations = count($annotationsList); |
||
211 | for ($index = 0; $index < $numAnnotations; $index++) { |
||
212 | $existingAnnotation = $annotationsList[$index]; |
||
213 | assert($existingAnnotation instanceof IDirectValueAnnotation); |
||
214 | if ($existingAnnotation->getNamespaceUri() == $namespaceName && $existingAnnotation->getName() == $localName) { |
||
215 | unset($annotationsList[$index]); |
||
216 | if (count($annotationsList) === 1) { |
||
217 | $transientAnnotations = array_pop($annotationsList); |
||
218 | } else { |
||
219 | $transientAnnotations = $annotationsList; |
||
220 | } |
||
221 | |||
222 | return; |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | private static function transientAnnotations($transientAnnotations): iterable |
||
229 | { |
||
230 | if ($transientAnnotations == null) { |
||
231 | return []; |
||
232 | } |
||
233 | |||
234 | $singleAnnotation = $transientAnnotations; |
||
235 | if ($singleAnnotation instanceof IDirectValueAnnotation) { |
||
236 | if (null !== $singleAnnotation->getValue()) { |
||
237 | yield $singleAnnotation; |
||
238 | } |
||
239 | |||
240 | return []; |
||
241 | } |
||
242 | |||
243 | $annotationsList = $transientAnnotations; |
||
244 | assert(is_iterable($annotationsList)); |
||
245 | /** @var IDirectValueAnnotation $existingAnnotation */ |
||
246 | foreach ($annotationsList as $existingAnnotation) { |
||
247 | if (null !== $existingAnnotation->getValue()) { |
||
248 | yield $existingAnnotation; |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | |||
253 | private static function isDead(string $namespaceName, string $localName, $transientAnnotations): bool |
||
256 | } |
||
257 | |||
258 | private static function setAnnotation(?iterable $immutableAnnotations, &$transientAnnotations, string $namespaceName, string $localName, $value): void |
||
321 | } |
||
322 | |||
323 | |||
324 | private static function findTransientAnnotation($transientAnnotations, string $namespaceName, string $localName): ?IDirectValueAnnotation |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @return SplObjectStorage |
||
349 | */ |
||
350 | private function getAnnotationsDictionary(): SplObjectStorage |
||
353 | } |
||
354 | } |
||
355 |