Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#828)
by Beatrycze
04:55 queued 01:27
created

Mods::getHolders()   C

Complexity

Conditions 15
Paths 8

Size

Total Lines 45
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 33
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 45
rs 5.9166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Format;
14
15
/**
16
 * Metadata MODS format class for the 'dlf' extension
17
 *
18
 * @author Sebastian Meyer <[email protected]>
19
 * @package TYPO3
20
 * @subpackage dlf
21
 * @access public
22
 */
23
class Mods implements \Kitodo\Dlf\Common\MetadataInterface
24
{
25
        /**
26
     * The metadata XML
27
     *
28
     * @var \SimpleXMLElement
29
     **/
30
    private $xml;
31
32
    /**
33
     * The metadata array
34
     *
35
     * @var array
36
     **/
37
    private $metadata;
38
39
    /**
40
     * This extracts the essential MODS metadata from XML
41
     *
42
     * @access public
43
     *
44
     * @param \SimpleXMLElement $xml: The XML to extract the metadata from
45
     * @param array &$metadata: The metadata array to fill
46
     *
47
     * @return void
48
     */
49
    public function extractMetadata(\SimpleXMLElement $xml, array &$metadata)
50
    {
51
        $this->xml = $xml;
52
        $this->metadata = $metadata;
53
54
        $this->xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
55
56
        $this->getAuthors();
57
        $this->getHolders();
58
        $this->getPlaces();
59
        $this->getYears();
60
        $this->getDescription();
61
        $this->getIdentifier();
62
        $this->getLicense();
63
        $this->getObjectNames();
64
        $this->getObjectLocationMetadata();
65
66
        $metadata = $this->metadata;
67
    }
68
69
    /**
70
     * Get "author" and "author_sorting".
71
     *
72
     * @access private
73
     *
74
     * @return void
75
     */
76
    private function getAuthors() {
77
        $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]');
78
79
        // Get "author" and "author_sorting" again if that was too sophisticated.
80
        if (empty($authors)) {
81
            // Get all names which do not have any role term assigned and assume these are authors.
82
            $authors = $this->xml->xpath('./mods:name[not(./mods:role)]');
83
        }
84
85
        if (!empty($authors)) {
86
            for ($i = 0, $j = count($authors); $i < $j; $i++) {
87
                $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
88
89
                // Check if there is a display form.
90
                if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) {
91
                    $this->metadata['author'][$i] = (string) $displayForm[0];
92
                } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) {
93
                    $name = [];
94
                    $k = 4;
95
                    foreach ($nameParts as $namePart) {
96
                        if (
97
                            isset($namePart['type'])
98
                            && (string) $namePart['type'] == 'family'
99
                        ) {
100
                            $name[0] = (string) $namePart;
101
                        } elseif (
102
                            isset($namePart['type'])
103
                            && (string) $namePart['type'] == 'given'
104
                        ) {
105
                            $name[1] = (string) $namePart;
106
                        } elseif (
107
                            isset($namePart['type'])
108
                            && (string) $namePart['type'] == 'termsOfAddress'
109
                        ) {
110
                            $name[2] = (string) $namePart;
111
                        } elseif (
112
                            isset($namePart['type'])
113
                            && (string) $namePart['type'] == 'date'
114
                        ) {
115
                            $name[3] = (string) $namePart;
116
                        } else {
117
                            $name[$k] = (string) $namePart;
118
                        }
119
                        $k++;
120
                    }
121
                    ksort($name);
122
                    $this->metadata['author'][$i] = trim(implode(', ', $name));
123
                }
124
                // Append "valueURI" to name using Unicode unit separator.
125
                if (isset($authors[$i]['valueURI'])) {
126
                    $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
127
                }
128
            }
129
        }
130
    }
131
132
    /**
133
     * Get holder.
134
     * 
135
     * @access private
136
     *
137
     * @return void
138
     */
139
    private function getHolders() {
140
        $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]');
141
142
        if (!empty($holders)) {
143
            for ($i = 0, $j = count($holders); $i < $j; $i++) {
144
                $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
145
146
                // Check if there is a display form.
147
                if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) {
148
                    $this->metadata['holder'][$i] = (string) $displayForm[0];
149
                } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) {
150
                    $name = [];
151
                    $k = 4;
152
                    foreach ($nameParts as $namePart) {
153
                        if (
154
                            isset($namePart['type'])
155
                            && (string) $namePart['type'] == 'family'
156
                        ) {
157
                            $name[0] = (string) $namePart;
158
                        } elseif (
159
                            isset($namePart['type'])
160
                            && (string) $namePart['type'] == 'given'
161
                        ) {
162
                            $name[1] = (string) $namePart;
163
                        } elseif (
164
                            isset($namePart['type'])
165
                            && (string) $namePart['type'] == 'termsOfAddress'
166
                        ) {
167
                            $name[2] = (string) $namePart;
168
                        } elseif (
169
                            isset($namePart['type'])
170
                            && (string) $namePart['type'] == 'date'
171
                        ) {
172
                            $name[3] = (string) $namePart;
173
                        } else {
174
                            $name[$k] = (string) $namePart;
175
                        }
176
                        $k++;
177
                    }
178
                    ksort($name);
179
                    $this->metadata['holder'][$i] = trim(implode(', ', $name));
180
                }
181
                // Append "valueURI" to name using Unicode unit separator.
182
                if (isset($authors[$i]['valueURI'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $authors seems to never exist and therefore isset should always be false.
Loading history...
183
                    $this->metadata['holder'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
184
                }
185
            }
186
        }
187
    }
188
189
    /**
190
     * Get "place" and "place_sorting".
191
     * 
192
     * @access private
193
     *
194
     * @return void
195
     */
196
    private function getPlaces() {
197
        $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
198
        // Get "place" and "place_sorting" again if that was to sophisticated.
199
        if (empty($places)) {
200
            // Get all places and assume these are places of publication.
201
            $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
202
        }
203
        if (!empty($places)) {
204
            foreach ($places as $place) {
205
                $this->metadata['place'][] = (string) $place;
206
                if (!$this->metadata['place_sorting'][0]) {
207
                    $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
208
                }
209
            }
210
        }
211
    }
212
213
    /**
214
     * Get "year" and "year_sorting".
215
     * 
216
     * @access private
217
     *
218
     * @return void
219
     */
220
    private function getYears() {
221
        // Get "year_sorting".
222
        if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
223
            foreach ($years_sorting as $year_sorting) {
224
                $this->metadata['year_sorting'][0] = intval($year_sorting);
225
            }
226
        }
227
        // Get "year" and "year_sorting" if not specified separately.
228
        $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
229
        // Get "year" and "year_sorting" again if that was to sophisticated.
230
        if (empty($years)) {
231
            // Get all dates and assume these are dates of publication.
232
            $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued');
233
        }
234
        if (!empty($years)) {
235
            foreach ($years as $year) {
236
                $this->metadata['year'][] = (string) $year;
237
                if (!$this->metadata['year_sorting'][0]) {
238
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
239
                    if (
240
                        strpos($year_sorting, '.')
0 ignored issues
show
Bug introduced by
It seems like $year_sorting can also be of type array; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

240
                        strpos(/** @scrutinizer ignore-type */ $year_sorting, '.')
Loading history...
241
                        || strlen($year_sorting) < 3
0 ignored issues
show
Bug introduced by
It seems like $year_sorting can also be of type array; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

241
                        || strlen(/** @scrutinizer ignore-type */ $year_sorting) < 3
Loading history...
242
                    ) {
243
                        $year_sorting = ((intval(trim($year_sorting, '.')) - 1) * 100) + 50;
0 ignored issues
show
Bug introduced by
It seems like $year_sorting can also be of type array; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

243
                        $year_sorting = ((intval(trim(/** @scrutinizer ignore-type */ $year_sorting, '.')) - 1) * 100) + 50;
Loading history...
244
                    }
245
                    $this->metadata['year_sorting'][0] = intval($year_sorting);
246
                }
247
            }
248
        }
249
    }
250
251
    /**
252
     * Get "description" stored in recordInfoNote.
253
     * 
254
     * @access private
255
     *
256
     * @return void
257
     */
258
    private function getDescription() {
259
        $this->getSingleMetadata('description', './mods:recordInfo/mods:recordInfoNote/text()');
260
    }
261
262
    /**
263
     * Get "identifier" - example: hotels (built public accommodations), AAT ID: 300007166.
264
     * 
265
     * @access private
266
     *
267
     * @return void
268
     */
269
    private function getIdentifier() {
270
        $this->getSingleMetadata('identifier', './mods:identifier/text()');
271
    }
272
273
    /**
274
     * Get "license" - license on which object is published.
275
     * 
276
     * @access private
277
     *
278
     * @return void
279
     */
280
    private function getLicense() {
281
        $this->getSingleMetadata('license', './mods:accessCondition/text()');
282
    }
283
284
    /**
285
     * Get names of the original object:
286
     *   - main name
287
     *   - alternative names
288
     * 
289
     * @access private
290
     *
291
     * @return void
292
     */
293
    private function getObjectNames() {
294
        $this->getSingleMetadata('object_name', './mods:relatedItem/mods:titleInfo[not(@displayLabel="alternative")]/mods:title/text()');
295
        $this->getSingleMetadata('object_alternative_names', './mods:relatedItem/mods:titleInfo[@displayLabel="alternative"]/mods:title/text()');
296
    }
297
298
    /**
299
     * Get location information about the original object:
300
     *  - city (object_location)
301
     *  - geonames
302
     *  - wikidata
303
     *  - wikipedia
304
     * 
305
     * @access private
306
     *
307
     * @return void
308
     */
309
    private function getObjectLocationMetadata() {
310
        $this->getSingleMetadata('object_location', './mods:relatedItem/mods:location/mods:physicalLocation/text()');
311
        $this->getSingleMetadata('geonames', './mods:relatedItem/mods:location/mods:url[@displayLabel="geonames"]/text()');
312
        $this->getSingleMetadata('wikidata', './mods:relatedItem/mods:location/mods:url[@displayLabel="wikidata"]/text()');
313
        $this->getSingleMetadata('wikipedia', './mods:relatedItem/mods:location/mods:url[@displayLabel="wikipedia"]/text()');
314
    }
315
316
    /**
317
     * Save to the metadata array the last found matching metadata.
318
     * 
319
     * @access private
320
     *
321
     * @param string $metadataIndex: The index in the array by which metadata is going to be accessible
322
     * @param string $xpath: The xpath for searching metadata in MODS
323
     *
324
     * @return void
325
     */
326
    private function getSingleMetadata($metadataIndex, $xpath) {
327
        $results = $this->xml->xpath($xpath);
328
        if (!empty($results)) {
329
            foreach ($results as $result) {
330
                $this->metadata[$metadataIndex][0] = (string) $result;
331
            }
332
        }
333
    }
334
}
335