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:28
created

Mods::getYears()   B

Complexity

Conditions 9
Paths 20

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
c 1
b 0
f 0
nc 20
nop 0
dl 0
loc 26
rs 8.0555
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
     * This extracts the essential MODS metadata from XML
27
     *
28
     * @access public
29
     *
30
     * @param \SimpleXMLElement $xml: The XML to extract the metadata from
31
     * @param array &$metadata: The metadata array to fill
32
     *
33
     * @return void
34
     */
35
    public function extractMetadata(\SimpleXMLElement $xml, array &$metadata)
36
    {
37
        $this->xml = $xml;
0 ignored issues
show
Bug Best Practice introduced by
The property xml does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
        $this->metadata = $metadata;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
40
        $this->xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
41
42
        $this->getAuthors();
43
        $this->getHolders();
44
        $this->getPlaces();
45
        $this->getYears();
46
        $this->getDescription();
47
        $this->getIdentifier();
48
        $this->getLicense();
49
        $this->getObjectNames();
50
        $this->getObjectLocationMetadata();
51
52
        $metadata = $this->metadata;
53
    }
54
55
    /**
56
     * Get "author" and "author_sorting".
57
     * 
58
     * @access private
59
     *
60
     * @return void
61
     */
62
    private function getAuthors() {
63
        $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]');
64
65
        // Get "author" and "author_sorting" again if that was too sophisticated.
66
        if (empty($authors)) {
67
            // Get all names which do not have any role term assigned and assume these are authors.
68
            $authors = $this->xml->xpath('./mods:name[not(./mods:role)]');
69
        }
70
71
        if (!empty($authors)) {
72
            for ($i = 0, $j = count($authors); $i < $j; $i++) {
73
                $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
74
75
                $identifier = $authors[$i]->xpath('./mods:nameIdentifier');
0 ignored issues
show
Unused Code introduced by
The assignment to $identifier is dead and can be removed.
Loading history...
76
                // Check if there is a display form.
77
                if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) {
78
                    $this->metadata['author'][$i] = (string) $displayForm[0];
0 ignored issues
show
Bug Best Practice introduced by
The property metadata does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
                } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) {
80
                    $name = [];
81
                    $k = 4;
82
                    foreach ($nameParts as $namePart) {
83
                        if (
84
                            isset($namePart['type'])
85
                            && (string) $namePart['type'] == 'family'
86
                        ) {
87
                            $name[0] = (string) $namePart;
88
                        } elseif (
89
                            isset($namePart['type'])
90
                            && (string) $namePart['type'] == 'given'
91
                        ) {
92
                            $name[1] = (string) $namePart;
93
                        } elseif (
94
                            isset($namePart['type'])
95
                            && (string) $namePart['type'] == 'termsOfAddress'
96
                        ) {
97
                            $name[2] = (string) $namePart;
98
                        } elseif (
99
                            isset($namePart['type'])
100
                            && (string) $namePart['type'] == 'date'
101
                        ) {
102
                            $name[3] = (string) $namePart;
103
                        } else {
104
                            $name[$k] = (string) $namePart;
105
                        }
106
                        $k++;
107
                    }
108
                    ksort($name);
109
                    $this->metadata['author'][$i] = trim(implode(', ', $name));
110
                }
111
                // Append "valueURI" to name using Unicode unit separator.
112
                if (isset($authors[$i]['valueURI'])) {
113
                    $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
114
                }
115
            }
116
        }
117
    }
118
119
    /**
120
     * Get holder.
121
     * 
122
     * @access private
123
     *
124
     * @return void
125
     */
126
    private function getHolders() {
127
        $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]');
128
129
        if (!empty($holders)) {
130
            for ($i = 0, $j = count($holders); $i < $j; $i++) {
131
                $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
132
133
                $identifier = $holders[$i]->xpath('./mods:nameIdentifier');
0 ignored issues
show
Unused Code introduced by
The assignment to $identifier is dead and can be removed.
Loading history...
134
                // Check if there is a display form.
135
                if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) {
136
                    $this->metadata['holder'][$i] = (string) $displayForm[0];
0 ignored issues
show
Bug Best Practice introduced by
The property metadata does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
137
                } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) {
138
                    $name = [];
139
                    $k = 4;
140
                    foreach ($nameParts as $namePart) {
141
                        if (
142
                            isset($namePart['type'])
143
                            && (string) $namePart['type'] == 'family'
144
                        ) {
145
                            $name[0] = (string) $namePart;
146
                        } elseif (
147
                            isset($namePart['type'])
148
                            && (string) $namePart['type'] == 'given'
149
                        ) {
150
                            $name[1] = (string) $namePart;
151
                        } elseif (
152
                            isset($namePart['type'])
153
                            && (string) $namePart['type'] == 'termsOfAddress'
154
                        ) {
155
                            $name[2] = (string) $namePart;
156
                        } elseif (
157
                            isset($namePart['type'])
158
                            && (string) $namePart['type'] == 'date'
159
                        ) {
160
                            $name[3] = (string) $namePart;
161
                        } else {
162
                            $name[$k] = (string) $namePart;
163
                        }
164
                        $k++;
165
                    }
166
                    ksort($name);
167
                    $this->metadata['holder'][$i] = trim(implode(', ', $name));
168
                }
169
                // Append "valueURI" to name using Unicode unit separator.
170
                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...
171
                    $this->metadata['holder'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
172
                }
173
            }
174
        }
175
    }
176
177
    /**
178
     * Get "place" and "place_sorting".
179
     * 
180
     * @access private
181
     *
182
     * @return void
183
     */
184
    private function getPlaces() {
185
        $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
186
        // Get "place" and "place_sorting" again if that was to sophisticated.
187
        if (empty($places)) {
188
            // Get all places and assume these are places of publication.
189
            $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
190
        }
191
        if (!empty($places)) {
192
            foreach ($places as $place) {
193
                $this->metadata['place'][] = (string) $place;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
194
                if (!$this->metadata['place_sorting'][0]) {
195
                    $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
196
                }
197
            }
198
        }
199
    }
200
201
    /**
202
     * Get "year" and "year_sorting".
203
     * 
204
     * @access private
205
     *
206
     * @return void
207
     */
208
    private function getYears() {
209
        // Get "year_sorting".
210
        if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
211
            foreach ($years_sorting as $year_sorting) {
212
                $this->metadata['year_sorting'][0] = intval($year_sorting);
0 ignored issues
show
Bug Best Practice introduced by
The property metadata does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
213
            }
214
        }
215
        // Get "year" and "year_sorting" if not specified separately.
216
        $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
217
        // Get "year" and "year_sorting" again if that was to sophisticated.
218
        if (empty($years)) {
219
            // Get all dates and assume these are dates of publication.
220
            $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued');
221
        }
222
        if (!empty($years)) {
223
            foreach ($years as $year) {
224
                $this->metadata['year'][] = (string) $year;
225
                if (!$this->metadata['year_sorting'][0]) {
226
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
227
                    if (
228
                        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

228
                        strpos(/** @scrutinizer ignore-type */ $year_sorting, '.')
Loading history...
229
                        || 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

229
                        || strlen(/** @scrutinizer ignore-type */ $year_sorting) < 3
Loading history...
230
                    ) {
231
                        $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

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