We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 49 | 
| Total Lines | 230 | 
| Duplicated Lines | 0 % | 
| Changes | 6 | ||
| Bugs | 0 | Features | 0 | 
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  | 
            ||
| 26 | class Mods implements \Kitodo\Dlf\Common\MetadataInterface  | 
            ||
| 27 | { | 
            ||
| 28 | /**  | 
            ||
| 29 | * The metadata XML  | 
            ||
| 30 | *  | 
            ||
| 31 | * @var \SimpleXMLElement  | 
            ||
| 32 | **/  | 
            ||
| 33 | private $xml;  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * The metadata array  | 
            ||
| 37 | *  | 
            ||
| 38 | * @var array  | 
            ||
| 39 | **/  | 
            ||
| 40 | private $metadata;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * This extracts the essential MODS metadata from XML  | 
            ||
| 44 | *  | 
            ||
| 45 | * @access public  | 
            ||
| 46 | *  | 
            ||
| 47 | * @param \SimpleXMLElement $xml: The XML to extract the metadata from  | 
            ||
| 48 | * @param array &$metadata: The metadata array to fill  | 
            ||
| 49 | *  | 
            ||
| 50 | * @return void  | 
            ||
| 51 | */  | 
            ||
| 52 | public function extractMetadata(\SimpleXMLElement $xml, array &$metadata)  | 
            ||
| 53 |     { | 
            ||
| 54 | $this->xml = $xml;  | 
            ||
| 55 | $this->metadata = $metadata;  | 
            ||
| 56 | |||
| 57 |         $this->xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); | 
            ||
| 58 | |||
| 59 | $this->getAuthors();  | 
            ||
| 60 | $this->getHolders();  | 
            ||
| 61 | $this->getPlaces();  | 
            ||
| 62 | $this->getYears();  | 
            ||
| 63 | |||
| 64 | $metadata = $this->metadata;  | 
            ||
| 65 | }  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * Get "author" and "author_sorting".  | 
            ||
| 69 | *  | 
            ||
| 70 | * @access private  | 
            ||
| 71 | *  | 
            ||
| 72 | * @return void  | 
            ||
| 73 | */  | 
            ||
| 74 |     private function getAuthors() { | 
            ||
| 75 |         $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]'); | 
            ||
| 76 | |||
| 77 | // Get "author" and "author_sorting" again if that was too sophisticated.  | 
            ||
| 78 |         if (empty($authors)) { | 
            ||
| 79 | // Get all names which do not have any role term assigned and assume these are authors.  | 
            ||
| 80 |             $authors = $this->xml->xpath('./mods:name[not(./mods:role)]'); | 
            ||
| 81 | }  | 
            ||
| 82 |         if (!empty($authors)) { | 
            ||
| 83 |             for ($i = 0, $j = count($authors); $i < $j; $i++) { | 
            ||
| 84 |                 $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); | 
            ||
| 85 | |||
| 86 |                 $identifier = $authors[$i]->xpath('./mods:nameIdentifier'); | 
            ||
| 87 |                 if ($identifier[0]['type'] == 'orcid' && !empty((string) $identifier[0])) { | 
            ||
| 88 |                     $orcidIdParts = explode('/', (string) $identifier[0]); | 
            ||
| 89 | $orcidId = trim(end($orcidIdParts));  | 
            ||
| 90 |                     if (!str_contains($orcidId, 'orcid=')) { | 
            ||
| 91 | $profile = new OrcidProfile($orcidId);  | 
            ||
| 92 | $this->metadata['author'][$i] = $profile->getFullName();  | 
            ||
| 93 | }  | 
            ||
| 94 |                 } else { | 
            ||
| 95 | // Check if there is a display form.  | 
            ||
| 96 |                     if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) { | 
            ||
| 97 | $this->metadata['author'][$i] = (string) $displayForm[0];  | 
            ||
| 98 |                     } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) { | 
            ||
| 99 | $name = [];  | 
            ||
| 100 | $k = 4;  | 
            ||
| 101 |                         foreach ($nameParts as $namePart) { | 
            ||
| 102 | if (  | 
            ||
| 103 | isset($namePart['type'])  | 
            ||
| 104 | && (string) $namePart['type'] == 'family'  | 
            ||
| 105 |                             ) { | 
            ||
| 106 | $name[0] = (string) $namePart;  | 
            ||
| 107 | } elseif (  | 
            ||
| 108 | isset($namePart['type'])  | 
            ||
| 109 | && (string) $namePart['type'] == 'given'  | 
            ||
| 110 |                             ) { | 
            ||
| 111 | $name[1] = (string) $namePart;  | 
            ||
| 112 | } elseif (  | 
            ||
| 113 | isset($namePart['type'])  | 
            ||
| 114 | && (string) $namePart['type'] == 'termsOfAddress'  | 
            ||
| 115 |                             ) { | 
            ||
| 116 | $name[2] = (string) $namePart;  | 
            ||
| 117 | } elseif (  | 
            ||
| 118 | isset($namePart['type'])  | 
            ||
| 119 | && (string) $namePart['type'] == 'date'  | 
            ||
| 120 |                             ) { | 
            ||
| 121 | $name[3] = (string) $namePart;  | 
            ||
| 122 |                             } else { | 
            ||
| 123 | $name[$k] = (string) $namePart;  | 
            ||
| 124 | }  | 
            ||
| 125 | $k++;  | 
            ||
| 126 | }  | 
            ||
| 127 | ksort($name);  | 
            ||
| 128 |                         $this->metadata['author'][$i] = trim(implode(', ', $name)); | 
            ||
| 129 | }  | 
            ||
| 130 | // Append "valueURI" to name using Unicode unit separator.  | 
            ||
| 131 |                     if (isset($authors[$i]['valueURI'])) { | 
            ||
| 132 | $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];  | 
            ||
| 133 | }  | 
            ||
| 134 | }  | 
            ||
| 135 | }  | 
            ||
| 136 | }  | 
            ||
| 137 | }  | 
            ||
| 138 | |||
| 139 | /**  | 
            ||
| 140 | * Get holder.  | 
            ||
| 141 | *  | 
            ||
| 142 | * @access private  | 
            ||
| 143 | *  | 
            ||
| 144 | * @return void  | 
            ||
| 145 | */  | 
            ||
| 146 |     private function getHolders() { | 
            ||
| 147 |         $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]'); | 
            ||
| 148 | |||
| 149 |         if (!empty($holders)) { | 
            ||
| 150 |             for ($i = 0, $j = count($holders); $i < $j; $i++) { | 
            ||
| 151 |                 $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); | 
            ||
| 152 | |||
| 153 |                 $identifier = $holders[$i]->xpath('./mods:nameIdentifier'); | 
            ||
| 154 |                 if ($identifier[0]['type'] == 'viaf') { | 
            ||
| 155 | $viafUrl = (string) $identifier[0];  | 
            ||
| 156 | $profile = new ViafProfile($viafUrl);  | 
            ||
| 157 | $this->metadata['holder'][$i] = $profile->getFullName();  | 
            ||
| 158 |                 } else { | 
            ||
| 159 | // Check if there is a display form.  | 
            ||
| 160 |                     if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) { | 
            ||
| 161 | $this->metadata['holder'][$i] = (string) $displayForm[0];  | 
            ||
| 162 |                     } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) { | 
            ||
| 163 | $name = [];  | 
            ||
| 164 | $k = 4;  | 
            ||
| 165 |                         foreach ($nameParts as $namePart) { | 
            ||
| 166 | if (  | 
            ||
| 167 | isset($namePart['type'])  | 
            ||
| 168 | && (string) $namePart['type'] == 'family'  | 
            ||
| 169 |                             ) { | 
            ||
| 170 | $name[0] = (string) $namePart;  | 
            ||
| 171 | } elseif (  | 
            ||
| 172 | isset($namePart['type'])  | 
            ||
| 173 | && (string) $namePart['type'] == 'given'  | 
            ||
| 174 |                             ) { | 
            ||
| 175 | $name[1] = (string) $namePart;  | 
            ||
| 176 | } elseif (  | 
            ||
| 177 | isset($namePart['type'])  | 
            ||
| 178 | && (string) $namePart['type'] == 'termsOfAddress'  | 
            ||
| 179 |                             ) { | 
            ||
| 180 | $name[2] = (string) $namePart;  | 
            ||
| 181 | } elseif (  | 
            ||
| 182 | isset($namePart['type'])  | 
            ||
| 183 | && (string) $namePart['type'] == 'date'  | 
            ||
| 184 |                             ) { | 
            ||
| 185 | $name[3] = (string) $namePart;  | 
            ||
| 186 |                             } else { | 
            ||
| 187 | $name[$k] = (string) $namePart;  | 
            ||
| 188 | }  | 
            ||
| 189 | $k++;  | 
            ||
| 190 | }  | 
            ||
| 191 | ksort($name);  | 
            ||
| 192 |                         $this->metadata['holder'][$i] = trim(implode(', ', $name)); | 
            ||
| 193 | }  | 
            ||
| 194 | }  | 
            ||
| 195 | }  | 
            ||
| 196 | }  | 
            ||
| 197 | }  | 
            ||
| 198 | |||
| 199 | /**  | 
            ||
| 200 | * Get "place" and "place_sorting".  | 
            ||
| 201 | *  | 
            ||
| 202 | * @access private  | 
            ||
| 203 | *  | 
            ||
| 204 | * @return void  | 
            ||
| 205 | */  | 
            ||
| 206 |     private function getPlaces() { | 
            ||
| 218 | }  | 
            ||
| 219 | }  | 
            ||
| 220 | }  | 
            ||
| 221 | }  | 
            ||
| 222 | |||
| 223 | /**  | 
            ||
| 224 | * Get "year" and "year_sorting".  | 
            ||
| 225 | *  | 
            ||
| 226 | * @access private  | 
            ||
| 227 | *  | 
            ||
| 228 | * @return void  | 
            ||
| 229 | */  | 
            ||
| 230 |     private function getYears() { | 
            ||
| 256 | }  | 
            ||
| 257 | }  | 
            ||
| 258 | }  | 
            ||
| 259 | }  | 
            ||
| 261 |