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

Mods   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 111
c 5
b 0
f 0
dl 0
loc 228
rs 8.48
wmc 49

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPlaces() 0 12 5
B getYears() 0 26 9
D getAuthors() 0 59 19
C getHolders() 0 45 15
A extractMetadata() 0 13 1

How to fix   Complexity   

Complex Class

Complex classes like Mods often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Mods, and based on these observations, apply Extract Interface, too.

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

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

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

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