Total Complexity | 57 |
Total Lines | 346 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Complex classes like Mods 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 Mods, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Mods implements MetadataInterface |
||
30 | { |
||
31 | /** |
||
32 | * @access private |
||
33 | * @var \SimpleXMLElement The metadata XML |
||
34 | **/ |
||
35 | private $xml; |
||
36 | |||
37 | /** |
||
38 | * @access private |
||
39 | * @var ModsReader The metadata XML |
||
40 | **/ |
||
41 | private $modsReader; |
||
42 | |||
43 | /** |
||
44 | * @access private |
||
45 | * @var array The metadata array |
||
46 | **/ |
||
47 | private $metadata; |
||
48 | |||
49 | /** |
||
50 | * @access private |
||
51 | * @var bool The metadata array |
||
52 | **/ |
||
53 | private $useExternalApis; |
||
54 | |||
55 | /** |
||
56 | * This extracts the essential MODS metadata from XML |
||
57 | * |
||
58 | * @access public |
||
59 | * |
||
60 | * @param \SimpleXMLElement $xml The XML to extract the metadata from |
||
61 | * @param array &$metadata The metadata array to fill |
||
62 | * @param bool $useExternalApis true if external APIs should be called, false otherwise |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function extractMetadata(\SimpleXMLElement $xml, array &$metadata, bool $useExternalApis): void |
||
67 | { |
||
68 | $this->xml = $xml; |
||
69 | $this->metadata = $metadata; |
||
70 | $this->useExternalApis = $useExternalApis; |
||
71 | |||
72 | $this->modsReader = new ModsReader($this->xml); |
||
73 | |||
74 | $this->getAuthors(); |
||
75 | $this->getHolders(); |
||
76 | $this->getPlaces(); |
||
77 | $this->getYears(); |
||
78 | |||
79 | $metadata = $this->metadata; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get "author" and "author_sorting". |
||
84 | * |
||
85 | * @access private |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | private function getAuthors(): void |
||
90 | { |
||
91 | $authors = $this->modsReader->getNames('[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]'); |
||
92 | // Get "author" and "author_sorting" again if that was too sophisticated. |
||
93 | if (empty($authors)) { |
||
94 | // Get all names which do not have any role term assigned and assume these are authors. |
||
95 | $authors = $this->modsReader->getNames('[not(./mods:role)]'); |
||
96 | } |
||
97 | if (!empty($authors)) { |
||
98 | for ($i = 0, $j = count($authors); $i < $j; $i++) { |
||
99 | $identifiers = $authors[$i]->getNameIdentifiers('[@type="orcid"]'); |
||
100 | if ($this->useExternalApis && !empty($identifiers)) { |
||
101 | $this->getAuthorFromOrcidApi($identifiers[0]->getValue(), $authors, $i); |
||
102 | } else { |
||
103 | $this->getAuthorFromXml($authors, $i); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get author from ORCID API. |
||
111 | * |
||
112 | * @access private |
||
113 | * |
||
114 | * @param string $orcidId |
||
115 | * @param array $authors |
||
116 | * @param int $i |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | private function getAuthorFromOrcidApi(string $orcidId, array $authors, int $i): void |
||
121 | { |
||
122 | $profile = new OrcidProfile($orcidId); |
||
123 | $name = $profile->getFullName(); |
||
124 | if (!empty($name)) { |
||
125 | $this->metadata['author'][$i] = [ |
||
126 | 'name' => $name, |
||
127 | 'url' => 'https://orcid.org/' . $orcidId |
||
128 | ]; |
||
129 | } else { |
||
130 | //fallback into display form |
||
131 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Get author from XML. |
||
137 | * |
||
138 | * @access private |
||
139 | * |
||
140 | * @param array $authors |
||
141 | * @param int $i |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | private function getAuthorFromXml(array $authors, int $i): void |
||
146 | { |
||
147 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
148 | |||
149 | $nameParts = $authors[$i]->getNameParts(); |
||
150 | if (empty($this->metadata['author'][$i]) && $nameParts) { |
||
151 | $name = []; |
||
152 | $k = 4; |
||
153 | foreach ($nameParts as $namePart) { |
||
154 | if ( |
||
155 | !empty($namePart->getType()) |
||
156 | && $namePart->getType() == 'family' |
||
157 | ) { |
||
158 | $name[0] = $namePart->getValue(); |
||
159 | } elseif ( |
||
160 | !empty($namePart->getType()) |
||
161 | && $namePart->getType() == 'given' |
||
162 | ) { |
||
163 | $name[1] = $namePart->getValue(); |
||
164 | } elseif ( |
||
165 | !empty($namePart->getType()) |
||
166 | && $namePart->getType() == 'termsOfAddress' |
||
167 | ) { |
||
168 | $name[2] = $namePart->getValue(); |
||
169 | } elseif ( |
||
170 | !empty($namePart->getType()) |
||
171 | && $namePart->getType() == 'date' |
||
172 | ) { |
||
173 | $name[3] = $namePart->getValue(); |
||
174 | } else { |
||
175 | $name[$k] = $namePart->getValue(); |
||
176 | } |
||
177 | $k++; |
||
178 | } |
||
179 | ksort($name); |
||
180 | $this->metadata['author'][$i] = trim(implode(', ', $name)); |
||
181 | } |
||
182 | // Append "valueURI" to name using Unicode unit separator. |
||
183 | if (!empty($authors[$i]->getValueURI())) { |
||
184 | $this->metadata['author'][$i] .= pack('C', 31) . $authors[$i]->getValueURI(); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get author from XML display form. |
||
190 | * |
||
191 | * @access private |
||
192 | * |
||
193 | * @param Name[] $authors |
||
194 | * @param int $i |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | private function getAuthorFromXmlDisplayForm(array $authors, int $i): void |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Get holder. |
||
208 | * |
||
209 | * @access private |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | private function getHolders(): void |
||
214 | { |
||
215 | $holders = $this->modsReader->getNames('[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]'); |
||
216 | |||
217 | if (!empty($holders)) { |
||
218 | for ($i = 0, $j = count($holders); $i < $j; $i++) { |
||
219 | $identifiers = $holders[$i]->getNameIdentifiers('[@type="viaf"]'); |
||
220 | if ($this->useExternalApis && !empty($identifiers)) { |
||
221 | $this->getHolderFromViafApi($identifiers[0]->getValue(), $holders, $i); |
||
222 | } else { |
||
223 | $this->getHolderFromXml($holders, $i); |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get holder from VIAF API. |
||
231 | * |
||
232 | * @access private |
||
233 | * |
||
234 | * @param string $viafId |
||
235 | * @param array $holders |
||
236 | * @param int $i |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | private function getHolderFromViafApi(string $viafId, array $holders, int $i): void |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Get holder from XML. |
||
257 | * |
||
258 | * @access private |
||
259 | * |
||
260 | * @param array $holders |
||
261 | * @param int $i |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | private function getHolderFromXml(array $holders, int $i): void |
||
266 | { |
||
267 | $this->getHolderFromXmlDisplayForm($holders, $i); |
||
268 | // Append "valueURI" to name using Unicode unit separator. |
||
269 | if (!empty($holders[$i]->getValueURI())) { |
||
270 | $this->metadata['holder'][$i] .= pack('C', 31) . $holders[$i]->getValueURI(); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Get holder from XML display form. |
||
276 | * |
||
277 | * @access private |
||
278 | * |
||
279 | * @param array $holders |
||
280 | * @param int $i |
||
281 | * |
||
282 | * @return void |
||
283 | */ |
||
284 | private function getHolderFromXmlDisplayForm(array $holders, int $i): void |
||
285 | { |
||
286 | // Check if there is a display form. |
||
287 | $displayForms = $holders[$i]->getDisplayForm(); |
||
288 | if ($displayForms) { |
||
289 | $this->metadata['holder'][$i] = $displayForms[0]->getValue(); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Get "place" and "place_sorting". |
||
295 | * |
||
296 | * @access private |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | private function getPlaces(): void |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Get "year" and "year_sorting". |
||
337 | * |
||
338 | * @access private |
||
339 | * |
||
340 | * @return void |
||
341 | */ |
||
342 | private function getYears(): void |
||
375 | } |
||
376 | } |
||
377 | } |
||
381 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths