We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 27 |
| Paths | 320 |
| Total Lines | 79 |
| Code Lines | 53 |
| 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 |
||
| 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) { |
||
|
|
|||
| 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
|
|||
| 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) { |
||
| 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
|
|||
| 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) { |
||
| 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
|
|||
| 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 | } |
||
| 117 |
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.