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 (#829)
by Beatrycze
03:18
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
use Kitodo\Dlf\Api\Orcid\Profile;
16
17
/**
18
 * Metadata MODS format class for the 'dlf' extension
19
 *
20
 * @author Sebastian Meyer <[email protected]>
21
 * @package TYPO3
22
 * @subpackage dlf
23
 * @access public
24
 */
25
class Mods implements \Kitodo\Dlf\Common\MetadataInterface
26
{
27
    /**
28
     * The metadata XML
29
     *
30
     * @var \SimpleXMLElement
31
     **/
32
    private $xml;
33
34
    /**
35
     * The metadata array
36
     *
37
     * @var array
38
     **/
39
    private $metadata;
40
41
    /**
42
     * This extracts the essential MODS metadata from XML
43
     *
44
     * @access public
45
     *
46
     * @param \SimpleXMLElement $xml: The XML to extract the metadata from
47
     * @param array &$metadata: The metadata array to fill
48
     *
49
     * @return void
50
     */
51
    public function extractMetadata(\SimpleXMLElement $xml, array &$metadata)
52
    {
53
        $this->xml = $xml;
54
        $this->metadata = $metadata;
55
56
        $this->xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
57
58
        $this->getAuthors();
59
        $this->getHolders();
60
        $this->getPlaces();
61
        $this->getYears();
62
63
        $metadata = $this->metadata;
64
    }
65
66
    /**
67
     * Get "author" and "author_sorting".
68
     *
69
     * @access private
70
     *
71
     * @return void
72
     */
73
    private function getAuthors() {
74
        $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]');
75
76
        // Get "author" and "author_sorting" again if that was too sophisticated.
77
        if (empty($authors)) {
78
            // Get all names which do not have any role term assigned and assume these are authors.
79
            $authors = $this->xml->xpath('./mods:name[not(./mods:role)]');
80
        }
81
        if (!empty($authors)) {
82
            for ($i = 0, $j = count($authors); $i < $j; $i++) {
83
                $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
84
85
                $identifier = $authors[$i]->xpath('./mods:name/mods:nameIdentifier[@type="orcid"]');
86
                if (!empty((string) $identifier[0])) {
87
                    $this->metadata['author'][$i] = $profile->getFullName();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $profile seems to be never defined.
Loading history...
88
                } else {
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
    /**
134
     * Get holder.
135
     *
136
     * @access private
137
     *
138
     * @return void
139
     */
140
    private function getHolders() {
141
        $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]');
142
143
        if (!empty($holders)) {
144
            for ($i = 0, $j = count($holders); $i < $j; $i++) {
145
                $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
146
147
                //$identifier = $holders[$i]->xpath('./mods:nameIdentifier');
148
                if (1 != 1) {
149
                    //TODO: reading VIAF API
150
                } else {
151
                    // Check if there is a display form.
152
                    if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) {
153
                        $this->metadata['holder'][$i] = (string) $displayForm[0];
154
                    } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) {
155
                        $name = [];
156
                        $k = 4;
157
                        foreach ($nameParts as $namePart) {
158
                            if (
159
                                isset($namePart['type'])
160
                                && (string) $namePart['type'] == 'family'
161
                            ) {
162
                                $name[0] = (string) $namePart;
163
                            } elseif (
164
                                isset($namePart['type'])
165
                                && (string) $namePart['type'] == 'given'
166
                            ) {
167
                                $name[1] = (string) $namePart;
168
                            } elseif (
169
                                isset($namePart['type'])
170
                                && (string) $namePart['type'] == 'termsOfAddress'
171
                            ) {
172
                                $name[2] = (string) $namePart;
173
                            } elseif (
174
                                isset($namePart['type'])
175
                                && (string) $namePart['type'] == 'date'
176
                            ) {
177
                                $name[3] = (string) $namePart;
178
                            } else {
179
                                $name[$k] = (string) $namePart;
180
                            }
181
                            $k++;
182
                        }
183
                        ksort($name);
184
                        $this->metadata['holder'][$i] = trim(implode(', ', $name));
185
                    }
186
                }
187
            }
188
        }
189
    }
190
191
    /**
192
     * Get "place" and "place_sorting".
193
     *
194
     * @access private
195
     *
196
     * @return void
197
     */
198
    private function getPlaces() {
199
        $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
200
        // Get "place" and "place_sorting" again if that was to sophisticated.
201
        if (empty($places)) {
202
            // Get all places and assume these are places of publication.
203
            $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
204
        }
205
        if (!empty($places)) {
206
            foreach ($places as $place) {
207
                $this->metadata['place'][] = (string) $place;
208
                if (!$this->metadata['place_sorting'][0]) {
209
                    $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
210
                }
211
            }
212
        }
213
    }
214
215
    /**
216
     * Get "year" and "year_sorting".
217
     *
218
     * @access private
219
     *
220
     * @return void
221
     */
222
    private function getYears() {
223
        // Get "year_sorting".
224
        if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
225
            foreach ($years_sorting as $year_sorting) {
226
                $this->metadata['year_sorting'][0] = intval($year_sorting);
227
            }
228
        }
229
        // Get "year" and "year_sorting" if not specified separately.
230
        $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
231
        // Get "year" and "year_sorting" again if that was to sophisticated.
232
        if (empty($years)) {
233
            // Get all dates and assume these are dates of publication.
234
            $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued');
235
        }
236
        if (!empty($years)) {
237
            foreach ($years as $year) {
238
                $this->metadata['year'][] = (string) $year;
239
                if (!$this->metadata['year_sorting'][0]) {
240
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
241
                    if (
242
                        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

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

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

245
                        $year_sorting = ((intval(trim(/** @scrutinizer ignore-type */ $year_sorting, '.')) - 1) * 100) + 50;
Loading history...
246
                    }
247
                    $this->metadata['year_sorting'][0] = intval($year_sorting);
248
                }
249
            }
250
        }
251
    }
252
}
253