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
03:32
created

Mods::getLicense()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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
                // Check if there is a display form.
76
                if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) {
77
                    $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...
78
                } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) {
79
                    $name = [];
80
                    $k = 4;
81
                    foreach ($nameParts as $namePart) {
82
                        if (
83
                            isset($namePart['type'])
84
                            && (string) $namePart['type'] == 'family'
85
                        ) {
86
                            $name[0] = (string) $namePart;
87
                        } elseif (
88
                            isset($namePart['type'])
89
                            && (string) $namePart['type'] == 'given'
90
                        ) {
91
                            $name[1] = (string) $namePart;
92
                        } elseif (
93
                            isset($namePart['type'])
94
                            && (string) $namePart['type'] == 'termsOfAddress'
95
                        ) {
96
                            $name[2] = (string) $namePart;
97
                        } elseif (
98
                            isset($namePart['type'])
99
                            && (string) $namePart['type'] == 'date'
100
                        ) {
101
                            $name[3] = (string) $namePart;
102
                        } else {
103
                            $name[$k] = (string) $namePart;
104
                        }
105
                        $k++;
106
                    }
107
                    ksort($name);
108
                    $this->metadata['author'][$i] = trim(implode(', ', $name));
109
                }
110
                // Append "valueURI" to name using Unicode unit separator.
111
                if (isset($authors[$i]['valueURI'])) {
112
                    $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
113
                }
114
            }
115
        }
116
    }
117
118
    /**
119
     * Get holder.
120
     * 
121
     * @access private
122
     *
123
     * @return void
124
     */
125
    private function getHolders() {
126
        $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]');
127
128
        if (!empty($holders)) {
129
            for ($i = 0, $j = count($holders); $i < $j; $i++) {
130
                $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
131
132
                // Check if there is a display form.
133
                if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) {
134
                    $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...
135
                } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) {
136
                    $name = [];
137
                    $k = 4;
138
                    foreach ($nameParts as $namePart) {
139
                        if (
140
                            isset($namePart['type'])
141
                            && (string) $namePart['type'] == 'family'
142
                        ) {
143
                            $name[0] = (string) $namePart;
144
                        } elseif (
145
                            isset($namePart['type'])
146
                            && (string) $namePart['type'] == 'given'
147
                        ) {
148
                            $name[1] = (string) $namePart;
149
                        } elseif (
150
                            isset($namePart['type'])
151
                            && (string) $namePart['type'] == 'termsOfAddress'
152
                        ) {
153
                            $name[2] = (string) $namePart;
154
                        } elseif (
155
                            isset($namePart['type'])
156
                            && (string) $namePart['type'] == 'date'
157
                        ) {
158
                            $name[3] = (string) $namePart;
159
                        } else {
160
                            $name[$k] = (string) $namePart;
161
                        }
162
                        $k++;
163
                    }
164
                    ksort($name);
165
                    $this->metadata['holder'][$i] = trim(implode(', ', $name));
166
                }
167
                // Append "valueURI" to name using Unicode unit separator.
168
                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...
169
                    $this->metadata['holder'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
170
                }
171
            }
172
        }
173
    }
174
175
    /**
176
     * Get "place" and "place_sorting".
177
     * 
178
     * @access private
179
     *
180
     * @return void
181
     */
182
    private function getPlaces() {
183
        $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
184
        // Get "place" and "place_sorting" again if that was to sophisticated.
185
        if (empty($places)) {
186
            // Get all places and assume these are places of publication.
187
            $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
188
        }
189
        if (!empty($places)) {
190
            foreach ($places as $place) {
191
                $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...
192
                if (!$this->metadata['place_sorting'][0]) {
193
                    $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
194
                }
195
            }
196
        }
197
    }
198
199
    /**
200
     * Get "year" and "year_sorting".
201
     * 
202
     * @access private
203
     *
204
     * @return void
205
     */
206
    private function getYears() {
207
        // Get "year_sorting".
208
        if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
209
            foreach ($years_sorting as $year_sorting) {
210
                $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...
211
            }
212
        }
213
        // Get "year" and "year_sorting" if not specified separately.
214
        $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
215
        // Get "year" and "year_sorting" again if that was to sophisticated.
216
        if (empty($years)) {
217
            // Get all dates and assume these are dates of publication.
218
            $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued');
219
        }
220
        if (!empty($years)) {
221
            foreach ($years as $year) {
222
                $this->metadata['year'][] = (string) $year;
223
                if (!$this->metadata['year_sorting'][0]) {
224
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
225
                    if (
226
                        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

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

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

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