| Conditions | 27 |
| Paths | 320 |
| Total Lines | 136 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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) { |
||
|
|
|||
| 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) { |
||
| 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) { |
||
| 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 | |||
| 178 |
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.