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