| Total Complexity | 43 |
| Total Lines | 247 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Annotation 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 Annotation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Annotation |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The complete data of the annotation |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $data; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $targetPages; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * @param array $data |
||
| 32 | */ |
||
| 33 | public function __construct($data) |
||
| 34 | { |
||
| 35 | $this->data = $data; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns the full data of the annotation |
||
| 40 | * |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | public function getRawData() |
||
| 44 | { |
||
| 45 | return $this->data; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Gets the annotation id |
||
| 50 | * |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function getId() |
||
| 54 | { |
||
| 55 | return $this->data['id'] ?? ''; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Gets the annotation title |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public function getTitle() |
||
| 64 | { |
||
| 65 | return $this->data['title'] ?? ''; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Gets the annotation body data |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function getBody() |
||
| 74 | { |
||
| 75 | $body = $this->data['body'] ?? ''; |
||
| 76 | |||
| 77 | if (is_array($body)) { |
||
| 78 | return $body; |
||
| 79 | } |
||
| 80 | |||
| 81 | return [$body]; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Gets the name of the annotation creator |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function getCreatorName() |
||
| 89 | { |
||
| 90 | return $this->data['creator']['displayName'] ?? ''; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Gets the creation date of the annotation |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getCreated() |
||
| 98 | { |
||
| 99 | return $this->data['created'] ?? ''; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Gets the modification date of the annotation |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function getModified() |
||
| 107 | { |
||
| 108 | return $this->data['modified'] ?? ''; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Gets the targets |
||
| 113 | * |
||
| 114 | * @return AnnotationTarget[] |
||
| 115 | */ |
||
| 116 | public function getTargets() |
||
| 117 | { |
||
| 118 | if (is_string($this->data['target'])) { |
||
| 119 | return [new AnnotationTarget($this->data['target'])]; |
||
| 120 | } |
||
| 121 | |||
| 122 | $annotationTargets = []; |
||
| 123 | foreach ($this->data['target'] as $target) { |
||
| 124 | $annotationTargets[] = new AnnotationTarget($target); |
||
| 125 | } |
||
| 126 | |||
| 127 | return $annotationTargets; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Sets the target pages for which the annotation is relevant |
||
| 132 | * |
||
| 133 | * @param array $targetPages |
||
| 134 | * @return void |
||
| 135 | */ |
||
| 136 | public function setTargetPages($targetPages) |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Gets the target pages for which the annotation is relevant |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function getTargetPages() |
||
| 147 | { |
||
| 148 | return $this->targetPages; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets the page numbers for which the annotation is relevant |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getPageNumbers() |
||
| 157 | { |
||
| 158 | $pages = []; |
||
| 159 | if (is_array($this->targetPages)) { |
||
|
|
|||
| 160 | foreach ($this->targetPages as $target) { |
||
| 161 | $pages = array_merge($pages, $target['pages']); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | return $pages; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Gets the annotation targets ordered by page numbers |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | public function getPageTargets() |
||
| 174 | { |
||
| 175 | $pageTargets = []; |
||
| 176 | if (is_array($this->targetPages)) { |
||
| 177 | foreach ($this->targetPages as $target) { |
||
| 178 | foreach ($target['pages'] as $page) { |
||
| 179 | $pageTargets[$page][$target['target']->getUrl()] = $target['target']; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | return $pageTargets; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Gets the audio ranges from the annotation targets ordered by page number |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | public function getPageAudioRanges() |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Gets the score ranges from the annotation targets ordered by page number |
||
| 209 | * |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | public function getPageScoreRanges() |
||
| 213 | { |
||
| 214 | $ranges = []; |
||
| 215 | if (is_array($this->getPageTargets())) { |
||
| 216 | foreach ($this->getPageTargets() as $pageNumber => $targets) { |
||
| 217 | foreach ($targets as $target) { |
||
| 218 | if ($target->isValid() && $target->isScoreRange()) { |
||
| 219 | $ranges[$pageNumber][] = $target->getRangeValue(); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | return $ranges; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Gets the facsimile ranges from the annotation targets ordered by page number |
||
| 229 | * |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public function getPageFacsimileRanges() |
||
| 233 | { |
||
| 234 | $ranges = []; |
||
| 235 | if (is_array($this->getPageTargets())) { |
||
| 236 | foreach ($this->getPageTargets() as $pageNumber => $targets) { |
||
| 237 | foreach ($targets as $target) { |
||
| 238 | if ($target->isValid() && $target->isFacsimileRange()) { |
||
| 239 | $ranges[$pageNumber][] = $target->getRangeValue(); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | return $ranges; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns if the annotation is relevant for verovio |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function isVerovioRelevant() |
||
| 262 | } |
||
| 263 | } |
||
| 264 |