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 (#834)
by Beatrycze
03:12
created

Mods::getAuthors()   D

Complexity

Conditions 19
Paths 20

Size

Total Lines 59
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 19
eloc 43
c 4
b 0
f 0
nc 20
nop 0
dl 0
loc 59
rs 4.5166

How to fix   Long Method    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
use Kitodo\Dlf\Api\Orcid\OrcidProfile;
16
use Kitodo\Dlf\Api\Viaf\ViafProfile;
17
18
/**
19
 * Metadata MODS format class for the 'dlf' extension
20
 *
21
 * @author Sebastian Meyer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class Mods implements \Kitodo\Dlf\Common\MetadataInterface
27
{
28
    /**
29
     * The metadata XML
30
     *
31
     * @var \SimpleXMLElement
32
     **/
33
    private $xml;
34
35
    /**
36
     * The metadata array
37
     *
38
     * @var array
39
     **/
40
    private $metadata;
41
42
    /**
43
     * This extracts the essential MODS metadata from XML
44
     *
45
     * @access public
46
     *
47
     * @param \SimpleXMLElement $xml: The XML to extract the metadata from
48
     * @param array &$metadata: The metadata array to fill
49
     *
50
     * @return void
51
     */
52
    public function extractMetadata(\SimpleXMLElement $xml, array &$metadata)
53
    {
54
        $this->xml = $xml;
55
        $this->metadata = $metadata;
56
57
        $this->xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
58
59
        $this->getAuthors();
60
        $this->getHolders();
61
        $this->getPlaces();
62
        $this->getYears();
63
64
        $metadata = $this->metadata;
65
    }
66
67
    /**
68
     * Get "author" and "author_sorting".
69
     *
70
     * @access private
71
     *
72
     * @return void
73
     */
74
    private function getAuthors() {
75
        $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]');
76
77
        // Get "author" and "author_sorting" again if that was too sophisticated.
78
        if (empty($authors)) {
79
            // Get all names which do not have any role term assigned and assume these are authors.
80
            $authors = $this->xml->xpath('./mods:name[not(./mods:role)]');
81
        }
82
        if (!empty($authors)) {
83
            for ($i = 0, $j = count($authors); $i < $j; $i++) {
84
                $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
85
86
                $identifier = $authors[$i]->xpath('./mods:nameIdentifier');
87
                if ($identifier[0]['type'] == 'orcid' && !empty((string) $identifier[0])) {
88
                    $orcidIdParts = explode('/', (string) $identifier[0]);
89
                    $orcidId = trim(end($orcidIdParts));
90
                    if (!str_contains($orcidId, 'orcid=')) {
91
                        $profile = new OrcidProfile($orcidId);
92
                        $this->metadata['author'][$i] = $profile->getFullName();
93
                    }
94
                } else {
95
                    // Check if there is a display form.
96
                    if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) {
97
                        $this->metadata['author'][$i] = (string) $displayForm[0];
98
                    } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) {
99
                        $name = [];
100
                        $k = 4;
101
                        foreach ($nameParts as $namePart) {
102
                            if (
103
                                isset($namePart['type'])
104
                                && (string) $namePart['type'] == 'family'
105
                            ) {
106
                                $name[0] = (string) $namePart;
107
                            } elseif (
108
                                isset($namePart['type'])
109
                                && (string) $namePart['type'] == 'given'
110
                            ) {
111
                                $name[1] = (string) $namePart;
112
                            } elseif (
113
                                isset($namePart['type'])
114
                                && (string) $namePart['type'] == 'termsOfAddress'
115
                            ) {
116
                                $name[2] = (string) $namePart;
117
                            } elseif (
118
                                isset($namePart['type'])
119
                                && (string) $namePart['type'] == 'date'
120
                            ) {
121
                                $name[3] = (string) $namePart;
122
                            } else {
123
                                $name[$k] = (string) $namePart;
124
                            }
125
                            $k++;
126
                        }
127
                        ksort($name);
128
                        $this->metadata['author'][$i] = trim(implode(', ', $name));
129
                    }
130
                    // Append "valueURI" to name using Unicode unit separator.
131
                    if (isset($authors[$i]['valueURI'])) {
132
                        $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
133
                    }
134
                }
135
            }
136
        }
137
    }
138
139
    /**
140
     * Get holder.
141
     *
142
     * @access private
143
     *
144
     * @return void
145
     */
146
    private function getHolders() {
147
        $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]');
148
149
        if (!empty($holders)) {
150
            for ($i = 0, $j = count($holders); $i < $j; $i++) {
151
                $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
152
153
                $identifier = $holders[$i]->xpath('./mods:name/mods:nameIdentifier[@type="viaf"]');
154
                if (!empty((string) $identifier[0])) {
155
                    $viaf = (string) $identifier[0];
156
                    $profile = new ViafProfile($viaf);
157
                    $this->metadata['holder'][$i] = $profile->getFullName();
158
                } else {
159
                    // Check if there is a display form.
160
                    if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) {
161
                        $this->metadata['holder'][$i] = (string) $displayForm[0];
162
                    } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) {
163
                        $name = [];
164
                        $k = 4;
165
                        foreach ($nameParts as $namePart) {
166
                            if (
167
                                isset($namePart['type'])
168
                                && (string) $namePart['type'] == 'family'
169
                            ) {
170
                                $name[0] = (string) $namePart;
171
                            } elseif (
172
                                isset($namePart['type'])
173
                                && (string) $namePart['type'] == 'given'
174
                            ) {
175
                                $name[1] = (string) $namePart;
176
                            } elseif (
177
                                isset($namePart['type'])
178
                                && (string) $namePart['type'] == 'termsOfAddress'
179
                            ) {
180
                                $name[2] = (string) $namePart;
181
                            } elseif (
182
                                isset($namePart['type'])
183
                                && (string) $namePart['type'] == 'date'
184
                            ) {
185
                                $name[3] = (string) $namePart;
186
                            } else {
187
                                $name[$k] = (string) $namePart;
188
                            }
189
                            $k++;
190
                        }
191
                        ksort($name);
192
                        $this->metadata['holder'][$i] = trim(implode(', ', $name));
193
                    }
194
                }
195
            }
196
        }
197
    }
198
199
    /**
200
     * Get "place" and "place_sorting".
201
     *
202
     * @access private
203
     *
204
     * @return void
205
     */
206
    private function getPlaces() {
207
        $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
208
        // Get "place" and "place_sorting" again if that was to sophisticated.
209
        if (empty($places)) {
210
            // Get all places and assume these are places of publication.
211
            $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
212
        }
213
        if (!empty($places)) {
214
            foreach ($places as $place) {
215
                $this->metadata['place'][] = (string) $place;
216
                if (!$this->metadata['place_sorting'][0]) {
217
                    $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
218
                }
219
            }
220
        }
221
    }
222
223
    /**
224
     * Get "year" and "year_sorting".
225
     *
226
     * @access private
227
     *
228
     * @return void
229
     */
230
    private function getYears() {
231
        // Get "year_sorting".
232
        if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
233
            foreach ($years_sorting as $year_sorting) {
234
                $this->metadata['year_sorting'][0] = intval($year_sorting);
235
            }
236
        }
237
        // Get "year" and "year_sorting" if not specified separately.
238
        $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
239
        // Get "year" and "year_sorting" again if that was to sophisticated.
240
        if (empty($years)) {
241
            // Get all dates and assume these are dates of publication.
242
            $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued');
243
        }
244
        if (!empty($years)) {
245
            foreach ($years as $year) {
246
                $this->metadata['year'][] = (string) $year;
247
                if (!$this->metadata['year_sorting'][0]) {
248
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
249
                    if (
250
                        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

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

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

253
                        $year_sorting = ((intval(trim(/** @scrutinizer ignore-type */ $year_sorting, '.')) - 1) * 100) + 50;
Loading history...
254
                    }
255
                    $this->metadata['year_sorting'][0] = intval($year_sorting);
256
                }
257
            }
258
        }
259
    }
260
}
261