| 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 |
||
| 31 | class EdmDirectValueAnnotationsManager implements IDirectValueAnnotationsManager |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var SplObjectStorage|array<IEdmElement, mixed> keeps track of transient annotations on elements |
||
| 35 | */ |
||
| 36 | private $annotationsDictionary; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array elements for which normal comparison failed to produce a valid result, arbitrarily ordered to enable stable comparisons |
||
| 40 | */ |
||
| 41 | private $unsortedElements = []; |
||
|
|
|||
| 42 | |||
| 43 | /** |
||
| 44 | * Initializes a new instance of the EdmDirectValueAnnotationsManager class. |
||
| 45 | */ |
||
| 46 | public function __construct() |
||
| 47 | { |
||
| 48 | $this->annotationsDictionary = new SplObjectStorage(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Gets annotations associated with an element. |
||
| 53 | * |
||
| 54 | * @param IEdmElement $element the annotated element |
||
| 55 | * @return IDirectValueAnnotation[] The direct value annotations for the element |
||
| 56 | */ |
||
| 57 | public function getDirectValueAnnotations(IEdmElement $element): iterable |
||
| 58 | { |
||
| 59 | // Fetch the annotations dictionary once and only once, because this.annotationsDictionary might get updated by another thread. |
||
| 60 | $annotationsDictionary = $this->annotationsDictionary; |
||
| 61 | |||
| 62 | $immutableAnnotations = $this->getAttachedAnnotations($element); |
||
| 63 | $transientAnnotations = self::getTransientAnnotations($element, $annotationsDictionary); |
||
| 64 | |||
| 65 | if ($immutableAnnotations != null) { |
||
| 66 | /** @var IDirectValueAnnotation $existingAnnotation */ |
||
| 67 | foreach ($immutableAnnotations as $existingAnnotation) { |
||
| 68 | if (!self::isDead($existingAnnotation->getNamespaceUri(), $existingAnnotation->getName(), $transientAnnotations)) { |
||
| 69 | yield $existingAnnotation; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | /** @var IDirectValueAnnotation $existingAnnotation */ |
||
| 74 | foreach (self::transientAnnotations($transientAnnotations) as $existingAnnotation) { |
||
| 75 | yield $existingAnnotation; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Sets an annotation value for an EDM element. If the value is null, no annotation is added and an existing |
||
| 81 | * annotation with the same name is removed. |
||
| 82 | * |
||
| 83 | * @param IEdmElement $element the annotated element |
||
| 84 | * @param string $namespaceName namespace that the annotation belongs to |
||
| 85 | * @param string $localName name of the annotation within the namespace |
||
| 86 | * @param mixed $value the value of the annotation |
||
| 87 | * @return IDirectValueAnnotationsManager self |
||
| 88 | */ |
||
| 89 | public function setAnnotationValue(IEdmElement $element, string $namespaceName, string $localName, $value): IDirectValueAnnotationsManager |
||
| 90 | { |
||
| 91 | $annotationsDictionary = $this->getAnnotationsDictionary(); |
||
| 92 | $transientAnnotations = self::getTransientAnnotations($element, $annotationsDictionary); |
||
| 93 | $transientAnnotationsBeforeSet = null !== $transientAnnotations ? clone $transientAnnotations : new SplObjectStorage(); |
||
| 94 | self::setAnnotation($this->getAttachedAnnotations($element), $transientAnnotations, $namespaceName, $localName, $value); |
||
| 95 | |||
| 96 | // There is at least one case (removing an annotation that was not present to begin with) where the transient annotations are not changed, |
||
| 97 | // so test to see if updating the dictionary is necessary. |
||
| 98 | if ($transientAnnotations != $transientAnnotationsBeforeSet) { |
||
| 99 | $annotationsDictionary->offsetSet($element, $transientAnnotations); |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->annotationsDictionary = $annotationsDictionary; |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Sets a set of annotation values. If a supplied value is null, no annotation is added and an existing annotation |
||
| 108 | * with the same name is removed. |
||
| 109 | * @param IDirectValueAnnotationBinding[] $annotations The annotations to set |
||
| 110 | * @return IDirectValueAnnotationsManager self |
||
| 111 | */ |
||
| 112 | public function setAnnotationValues(array $annotations): IDirectValueAnnotationsManager |
||
| 113 | { |
||
| 114 | /** @var IDirectValueAnnotationBinding $annotation */ |
||
| 115 | foreach ($annotations as $annotation) { |
||
| 116 | $this->SetAnnotationValue($annotation->getElement(), $annotation->getNamespaceUri(), $annotation->getName(), $annotation->getValue()); |
||
| 117 | } |
||
| 118 | return $this; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param IEdmElement $element the annotated element |
||
| 123 | * @param string $namespaceName namespace that the annotation belongs to |
||
| 124 | * @param string $localName local name of the annotation |
||
| 125 | * @return mixed Returns the annotation value that corresponds to the provided name. Returns null if no |
||
| 126 | * annotation with the given name exists for the given element. |
||
| 127 | */ |
||
| 128 | public function getAnnotationValue(IEdmElement $element, string $namespaceName, string $localName) |
||
| 129 | { |
||
| 130 | $annotationsDictionary = $this->getAnnotationsDictionary(); |
||
| 131 | $transients = self::getTransientAnnotations($element, $annotationsDictionary); |
||
| 132 | $annotation = self::findTransientAnnotation($transients, $namespaceName, $localName); |
||
| 133 | if ($annotation != null) { |
||
| 134 | return $annotation->getValue(); |
||
| 135 | } |
||
| 136 | |||
| 137 | $immutableAnnotations = $this->getAttachedAnnotations($element); |
||
| 138 | if ($immutableAnnotations != null) { |
||
| 139 | /** @var IDirectValueAnnotation $existingAnnotation */ |
||
| 140 | foreach ($immutableAnnotations as $existingAnnotation) { |
||
| 141 | if ($existingAnnotation->getNamespaceUri() == $namespaceName && $existingAnnotation->getName() == $localName) { |
||
| 142 | // No need to check that the immutable annotation isn't Dead, because if it were |
||
| 143 | // the tombstone would have been found in the transient annotations. |
||
| 144 | return $existingAnnotation->getValue(); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return null; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Retrieves a set of annotation values. For each requested value, returns null if no annotation with the given |
||
| 154 | * name exists for the given element. |
||
| 155 | * |
||
| 156 | * @param IDirectValueAnnotationBinding[] $annotations The set of requested annotations |
||
| 157 | * @return array Returns values that correspond to the provided annotations. A value is null if no annotation with |
||
| 158 | * the given name exists for the given element. |
||
| 159 | */ |
||
| 160 | public function getAnnotationValues(array $annotations): ?iterable |
||
| 161 | { |
||
| 162 | $values = []; |
||
| 163 | |||
| 164 | $index = 0; |
||
| 165 | foreach ($annotations as $annotation) { |
||
| 166 | $values[$index++] = $this->getAnnotationValue($annotation->getElement(), $annotation->getNamespaceUri(), $annotation->getName()); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $values; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Retrieves the annotations that are directly attached to an element. |
||
| 174 | * |
||
| 175 | * @param IEdmElement $element the element in question |
||
| 176 | * @return iterable|null the annotations that are directly attached to an element (outside the control of the manager) |
||
| 177 | */ |
||
| 178 | protected function getAttachedAnnotations(IEdmElement $element): ?iterable |
||
| 179 | { |
||
| 180 | return null; |
||
| 181 | } |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * Retrieves the transient annotations for an EDM element. |
||
| 186 | * |
||
| 187 | * @param IEdmElement $element the annotated element |
||
| 188 | * @param SplObjectStorage $annotationsDictionary the dictionary for looking up the element's annotations |
||
| 189 | * @return mixed|null The transient annotations for the element, in a form managed by the annotations manager. |
||
| 190 | * This method is static to guarantee that the annotations dictionary is not fetched more than once per lookup operation. |
||
| 191 | */ |
||
| 192 | private static function getTransientAnnotations(IEdmElement $element, SplObjectStorage $annotationsDictionary) |
||
| 193 | { |
||
| 194 | return $annotationsDictionary->offsetExists($element) ? $annotationsDictionary->offsetGet($element) : null; |
||
| 195 | } |
||
| 196 | |||
| 197 | private static function removeTransientAnnotation(&$transientAnnotations, $namespaceName, string $localName) |
||
| 198 | { |
||
| 199 | if (null !== $transientAnnotations) { |
||
| 200 | $singleAnnotation = $transientAnnotations; |
||
| 201 | if ($singleAnnotation instanceof IDirectValueAnnotation) { |
||
| 202 | if ($singleAnnotation->getNamespaceUri() == $namespaceName && $singleAnnotation->getName() == $localName) { |
||
| 203 | $transientAnnotations = null; |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | } else { |
||
| 207 | $annotationsList = $transientAnnotations; |
||
| 208 | assert(is_array( |
||
| 209 | $annotationsList |
||
| 210 | )); |
||
| 211 | $numAnnotations = count($annotationsList); |
||
| 212 | for ($index = 0; $index < $numAnnotations; $index++) { |
||
| 213 | $existingAnnotation = $annotationsList[$index]; |
||
| 214 | assert($existingAnnotation instanceof IDirectValueAnnotation); |
||
| 215 | if ($existingAnnotation->getNamespaceUri() == $namespaceName && $existingAnnotation->getName() == $localName) { |
||
| 216 | unset($annotationsList[$index]); |
||
| 217 | if (count($annotationsList) === 1) { |
||
| 218 | $transientAnnotations = array_pop($annotationsList); |
||
| 219 | } else { |
||
| 220 | $transientAnnotations = $annotationsList; |
||
| 221 | } |
||
| 222 | |||
| 223 | return; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | private static function transientAnnotations($transientAnnotations): iterable |
||
| 230 | { |
||
| 231 | if ($transientAnnotations == null) { |
||
| 232 | return []; |
||
| 233 | } |
||
| 234 | |||
| 235 | $singleAnnotation = $transientAnnotations; |
||
| 236 | if ($singleAnnotation instanceof IDirectValueAnnotation) { |
||
| 237 | if ($singleAnnotation->getValue() != null) { |
||
| 238 | yield $singleAnnotation; |
||
| 239 | } |
||
| 240 | |||
| 241 | return []; |
||
| 242 | } |
||
| 243 | |||
| 244 | $annotationsList = $transientAnnotations; |
||
| 245 | assert(is_iterable($annotationsList)); |
||
| 246 | /** @var IDirectValueAnnotation $existingAnnotation */ |
||
| 247 | foreach ($annotationsList as $existingAnnotation) { |
||
| 248 | if ($existingAnnotation->getValue() != null) { |
||
| 249 | yield $existingAnnotation; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | private static function isDead(string $namespaceName, string $localName, $transientAnnotations): bool |
||
| 257 | } |
||
| 258 | |||
| 259 | private static function setAnnotation(?iterable $immutableAnnotations, &$transientAnnotations, string $namespaceName, string $localName, $value): void |
||
| 322 | } |
||
| 323 | |||
| 324 | |||
| 325 | private static function findTransientAnnotation($transientAnnotations, string $namespaceName, string $localName): ?IDirectValueAnnotation |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return SplObjectStorage |
||
| 350 | */ |
||
| 351 | private function getAnnotationsDictionary(): SplObjectStorage |
||
| 354 | } |
||
| 355 | } |
||
| 356 |