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
Push — master ( 14584b...f60880 )
by Sebastian
02:42
created

Mods   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 27

1 Method

Rating   Name   Duplication   Size   Complexity  
F extractMetadata() 0 79 27
1
<?php
2
namespace Kitodo\Dlf\Format;
3
4
/**
5
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
6
 *
7
 * This file is part of the Kitodo and TYPO3 projects.
8
 *
9
 * @license GNU General Public License version 3 or later.
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 */
13
14
/**
15
 * Metadata MODS format class for the 'dlf' extension
16
 *
17
 * @author Sebastian Meyer <[email protected]>
18
 * @package TYPO3
19
 * @subpackage dlf
20
 * @access public
21
 */
22
class Mods implements \Kitodo\Dlf\Common\MetadataInterface {
23
    /**
24
     * This extracts the essential MODS metadata from XML
25
     *
26
     * @access public
27
     *
28
     * @param \SimpleXMLElement $xml: The XML to extract the metadata from
29
     * @param array &$metadata: The metadata array to fill
30
     *
31
     * @return void
32
     */
33
    public function extractMetadata(\SimpleXMLElement $xml, array &$metadata) {
34
        $xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
35
        // Get "author" and "author_sorting".
36
        $authors = $xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]');
37
        // Get "author" and "author_sorting" again if that was to sophisticated.
38
        if (!$authors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $authors of type SimpleXMLElement[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39
            // Get all names which do not have any role term assigned and assume these are authors.
40
            $authors = $xml->xpath('./mods:name[not(./mods:role)]');
41
        }
42
        if (is_array($authors)) {
1 ignored issue
show
introduced by
The condition is_array($authors) is always true.
Loading history...
43
            for ($i = 0, $j = count($authors); $i < $j; $i++) {
44
                $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
45
                // Check if there is a display form.
46
                if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) {
47
                    $metadata['author'][$i] = (string) $displayForm[0];
48
                } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) {
49
                    $name = [];
50
                    $k = 4;
51
                    foreach ($nameParts as $namePart) {
52
                        if (isset($namePart['type'])
53
                            && (string) $namePart['type'] == 'family') {
54
                            $name[0] = (string) $namePart;
55
                        } elseif (isset($namePart['type'])
56
                            && (string) $namePart['type'] == 'given') {
57
                            $name[1] = (string) $namePart;
58
                        } elseif (isset($namePart['type'])
59
                            && (string) $namePart['type'] == 'termsOfAddress') {
60
                            $name[2] = (string) $namePart;
61
                        } elseif (isset($namePart['type'])
62
                            && (string) $namePart['type'] == 'date') {
63
                            $name[3] = (string) $namePart;
64
                        } else {
65
                            $name[$k] = (string) $namePart;
66
                        }
67
                        $k++;
68
                    }
69
                    ksort($name);
70
                    $metadata['author'][$i] = trim(implode(', ', $name));
71
                }
72
            }
73
        }
74
        // Get "place" and "place_sorting".
75
        $places = $xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
76
        // Get "place" and "place_sorting" again if that was to sophisticated.
77
        if (!$places) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $places of type SimpleXMLElement[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78
            // Get all places and assume these are places of publication.
79
            $places = $xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
80
        }
81
        if (is_array($places)) {
1 ignored issue
show
introduced by
The condition is_array($places) is always true.
Loading history...
82
            foreach ($places as $place) {
83
                $metadata['place'][] = (string) $place;
84
                if (!$metadata['place_sorting'][0]) {
85
                    $metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
86
                }
87
            }
88
        }
89
        // Get "year_sorting".
90
        if (($years_sorting = $xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
91
            foreach ($years_sorting as $year_sorting) {
92
                $metadata['year_sorting'][0] = intval($year_sorting);
93
            }
94
        }
95
        // Get "year" and "year_sorting" if not specified separately.
96
        $years = $xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
97
        // Get "year" and "year_sorting" again if that was to sophisticated.
98
        if (!$years) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $years of type SimpleXMLElement[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
99
            // Get all dates and assume these are dates of publication.
100
            $years = $xml->xpath('./mods:originInfo/mods:dateIssued');
101
        }
102
        if (is_array($years)) {
1 ignored issue
show
introduced by
The condition is_array($years) is always true.
Loading history...
103
            foreach ($years as $year) {
104
                $metadata['year'][] = (string) $year;
105
                if (!$metadata['year_sorting'][0]) {
106
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
107
                    if (strpos($year_sorting, '.')
108
                        || strlen($year_sorting) < 3) {
109
                        $year_sorting = ((intval(trim($year_sorting, '.')) - 1) * 100) + 50;
110
                    }
111
                    $metadata['year_sorting'][0] = intval($year_sorting);
112
                }
113
            }
114
        }
115
    }
116
}
117