tx_dlf_mods::extractMetadata()   F
last analyzed

Complexity

Conditions 27
Paths 320

Size

Total Lines 136
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 27
eloc 48
nc 320
nop 2
dl 0
loc 136
rs 3.5143
c 0
b 0
f 0

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
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
/**
13
 * Metadata format class 'tx_dlf_mods' for the 'dlf' extension.
14
 *
15
 * @author	Sebastian Meyer <[email protected]>
16
 * @package	TYPO3
17
 * @subpackage	tx_dlf
18
 * @access	public
19
 */
20
class tx_dlf_mods implements tx_dlf_format {
21
22
    /**
23
     * This extracts the essential MODS metadata from XML
24
     *
25
     * @access	public
26
     *
27
     * @param	SimpleXMLElement		$xml: The XML to extract the metadata from
28
     * @param	array		&$metadata: The metadata array to fill
29
     *
30
     * @return	void
31
     */
32
    public function extractMetadata(SimpleXMLElement $xml, array &$metadata) {
33
34
        $xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
35
36
        // Get "author" and "author_sorting".
37
        $authors = $xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]');
38
39
        // Get "author" and "author_sorting" again if that was to sophisticated.
40
        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...
41
42
            // Get all names which do not have any role term assigned and assume these are authors.
43
            $authors = $xml->xpath('./mods:name[not(./mods:role)]');
44
45
        }
46
47
        if (is_array($authors)) {
48
49
            for ($i = 0, $j = count($authors); $i < $j; $i++) {
50
51
                $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
52
53
                // Check if there is a display form.
54
                if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) {
55
56
                    $metadata['author'][$i] = (string) $displayForm[0];
57
58
                } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) {
59
60
                    $name = array ();
61
62
                    $k = 4;
63
64
                    foreach ($nameParts as $namePart) {
65
66
                        if (isset($namePart['type']) && (string) $namePart['type'] == 'family') {
67
68
                            $name[0] = (string) $namePart;
69
70
                        } elseif (isset($namePart['type']) && (string) $namePart['type'] == 'given') {
71
72
                            $name[1] = (string) $namePart;
73
74
                        } elseif (isset($namePart['type']) && (string) $namePart['type'] == 'termsOfAddress') {
75
76
                            $name[2] = (string) $namePart;
77
78
                        } elseif (isset($namePart['type']) && (string) $namePart['type'] == 'date') {
79
80
                            $name[3] = (string) $namePart;
81
82
                        } else {
83
84
                            $name[$k] = (string) $namePart;
85
86
                        }
87
88
                        $k++;
89
90
                    }
91
92
                    ksort($name);
93
94
                    $metadata['author'][$i] = trim(implode(', ', $name));
95
96
                }
97
98
            }
99
100
        }
101
102
        // Get "place" and "place_sorting".
103
        $places = $xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
104
105
        // Get "place" and "place_sorting" again if that was to sophisticated.
106
        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...
107
108
            // Get all places and assume these are places of publication.
109
            $places = $xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
110
111
        }
112
113
        if (is_array($places)) {
114
115
            foreach ($places as $place) {
116
117
                $metadata['place'][] = (string) $place;
118
119
                if (!$metadata['place_sorting'][0]) {
120
121
                    $metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
122
123
                }
124
125
            }
126
127
        }
128
129
        // Get "year_sorting".
130
        if (($years_sorting = $xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
131
132
            foreach ($years_sorting as $year_sorting) {
133
134
                $metadata['year_sorting'][0] = intval($year_sorting);
135
136
            }
137
138
        }
139
140
        // Get "year" and "year_sorting" if not specified separately.
141
        $years = $xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
142
143
        // Get "year" and "year_sorting" again if that was to sophisticated.
144
        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...
145
146
            // Get all dates and assume these are dates of publication.
147
            $years = $xml->xpath('./mods:originInfo/mods:dateIssued');
148
149
        }
150
151
        if (is_array($years)) {
152
153
            foreach ($years as $year) {
154
155
                $metadata['year'][] = (string) $year;
156
157
                if (!$metadata['year_sorting'][0]) {
158
159
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
160
161
                    if (strpos($year_sorting, '.') || strlen($year_sorting) < 3) {
162
163
                        $year_sorting = ((intval(trim($year_sorting, '.')) - 1) * 100) + 50;
164
165
                    }
166
167
                    $metadata['year_sorting'][0] = intval($year_sorting);
168
169
                }
170
171
            }
172
173
        }
174
175
    }
176
177
}
178