| Total Complexity | 45 | 
| Total Lines | 213 | 
| Duplicated Lines | 0 % | 
| Changes | 2 | ||
| 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  | 
            ||
| 23 | class Mods implements \Kitodo\Dlf\Common\MetadataInterface  | 
            ||
| 24 | { | 
            ||
| 25 | /**  | 
            ||
| 26 | * The metadata XML  | 
            ||
| 27 | *  | 
            ||
| 28 | * @var \SimpleXMLElement  | 
            ||
| 29 | **/  | 
            ||
| 30 | private $xml;  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * The metadata array  | 
            ||
| 34 | *  | 
            ||
| 35 | * @var array  | 
            ||
| 36 | **/  | 
            ||
| 37 | private $metadata;  | 
            ||
| 38 | |||
| 39 | /**  | 
            ||
| 40 | * This extracts the essential MODS metadata from XML  | 
            ||
| 41 | *  | 
            ||
| 42 | * @access public  | 
            ||
| 43 | *  | 
            ||
| 44 | * @param \SimpleXMLElement $xml: The XML to extract the metadata from  | 
            ||
| 45 | * @param array &$metadata: The metadata array to fill  | 
            ||
| 46 | *  | 
            ||
| 47 | * @return void  | 
            ||
| 48 | */  | 
            ||
| 49 | public function extractMetadata(\SimpleXMLElement $xml, array &$metadata)  | 
            ||
| 62 | }  | 
            ||
| 63 | |||
| 64 | /**  | 
            ||
| 65 | * Get "author" and "author_sorting".  | 
            ||
| 66 | *  | 
            ||
| 67 | * @access private  | 
            ||
| 68 | *  | 
            ||
| 69 | * @return void  | 
            ||
| 70 | */  | 
            ||
| 71 |     private function getAuthors() { | 
            ||
| 72 |         $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]'); | 
            ||
| 73 | |||
| 74 | // Get "author" and "author_sorting" again if that was too sophisticated.  | 
            ||
| 75 |         if (empty($authors)) { | 
            ||
| 76 | // Get all names which do not have any role term assigned and assume these are authors.  | 
            ||
| 77 |             $authors = $this->xml->xpath('./mods:name[not(./mods:role)]'); | 
            ||
| 78 | }  | 
            ||
| 79 |         if (!empty($authors)) { | 
            ||
| 80 |             for ($i = 0, $j = count($authors); $i < $j; $i++) { | 
            ||
| 81 |                 $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); | 
            ||
| 82 | |||
| 83 | // Check if there is a display form.  | 
            ||
| 84 |                 if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) { | 
            ||
| 85 | $this->metadata['author'][$i] = (string) $displayForm[0];  | 
            ||
| 86 |                 } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) { | 
            ||
| 87 | $name = [];  | 
            ||
| 88 | $k = 4;  | 
            ||
| 89 |                     foreach ($nameParts as $namePart) { | 
            ||
| 90 | if (  | 
            ||
| 91 | isset($namePart['type'])  | 
            ||
| 92 | && (string) $namePart['type'] == 'family'  | 
            ||
| 93 |                         ) { | 
            ||
| 94 | $name[0] = (string) $namePart;  | 
            ||
| 95 | } elseif (  | 
            ||
| 96 | isset($namePart['type'])  | 
            ||
| 97 | && (string) $namePart['type'] == 'given'  | 
            ||
| 98 |                         ) { | 
            ||
| 99 | $name[1] = (string) $namePart;  | 
            ||
| 100 | } elseif (  | 
            ||
| 101 | isset($namePart['type'])  | 
            ||
| 102 | && (string) $namePart['type'] == 'termsOfAddress'  | 
            ||
| 103 |                         ) { | 
            ||
| 104 | $name[2] = (string) $namePart;  | 
            ||
| 105 | } elseif (  | 
            ||
| 106 | isset($namePart['type'])  | 
            ||
| 107 | && (string) $namePart['type'] == 'date'  | 
            ||
| 108 |                         ) { | 
            ||
| 109 | $name[3] = (string) $namePart;  | 
            ||
| 110 |                         } else { | 
            ||
| 111 | $name[$k] = (string) $namePart;  | 
            ||
| 112 | }  | 
            ||
| 113 | $k++;  | 
            ||
| 114 | }  | 
            ||
| 115 | ksort($name);  | 
            ||
| 116 |                     $this->metadata['author'][$i] = trim(implode(', ', $name)); | 
            ||
| 117 | }  | 
            ||
| 118 | // Append "valueURI" to name using Unicode unit separator.  | 
            ||
| 119 |                 if (isset($authors[$i]['valueURI'])) { | 
            ||
| 120 | $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];  | 
            ||
| 121 | }  | 
            ||
| 122 | }  | 
            ||
| 123 | }  | 
            ||
| 124 | }  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * Get holder.  | 
            ||
| 128 | *  | 
            ||
| 129 | * @access private  | 
            ||
| 130 | *  | 
            ||
| 131 | * @return void  | 
            ||
| 132 | */  | 
            ||
| 133 |     private function getHolders() { | 
            ||
| 134 |         $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]'); | 
            ||
| 135 | |||
| 136 |         if (!empty($holders)) { | 
            ||
| 137 |             for ($i = 0, $j = count($holders); $i < $j; $i++) { | 
            ||
| 138 |                 $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); | 
            ||
| 139 | |||
| 140 | // Check if there is a display form.  | 
            ||
| 141 |                 if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) { | 
            ||
| 142 | $this->metadata['holder'][$i] = (string) $displayForm[0];  | 
            ||
| 143 |                 } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) { | 
            ||
| 144 | $name = [];  | 
            ||
| 145 | $k = 4;  | 
            ||
| 146 |                     foreach ($nameParts as $namePart) { | 
            ||
| 147 | if (  | 
            ||
| 148 | isset($namePart['type'])  | 
            ||
| 149 | && (string) $namePart['type'] == 'family'  | 
            ||
| 150 |                         ) { | 
            ||
| 151 | $name[0] = (string) $namePart;  | 
            ||
| 152 | } elseif (  | 
            ||
| 153 | isset($namePart['type'])  | 
            ||
| 154 | && (string) $namePart['type'] == 'given'  | 
            ||
| 155 |                         ) { | 
            ||
| 156 | $name[1] = (string) $namePart;  | 
            ||
| 157 | } elseif (  | 
            ||
| 158 | isset($namePart['type'])  | 
            ||
| 159 | && (string) $namePart['type'] == 'termsOfAddress'  | 
            ||
| 160 |                         ) { | 
            ||
| 161 | $name[2] = (string) $namePart;  | 
            ||
| 162 | } elseif (  | 
            ||
| 163 | isset($namePart['type'])  | 
            ||
| 164 | && (string) $namePart['type'] == 'date'  | 
            ||
| 165 |                         ) { | 
            ||
| 166 | $name[3] = (string) $namePart;  | 
            ||
| 167 |                         } else { | 
            ||
| 168 | $name[$k] = (string) $namePart;  | 
            ||
| 169 | }  | 
            ||
| 170 | $k++;  | 
            ||
| 171 | }  | 
            ||
| 172 | ksort($name);  | 
            ||
| 173 |                     $this->metadata['holder'][$i] = trim(implode(', ', $name)); | 
            ||
| 174 | }  | 
            ||
| 175 | }  | 
            ||
| 176 | }  | 
            ||
| 177 | }  | 
            ||
| 178 | |||
| 179 | /**  | 
            ||
| 180 | * Get "place" and "place_sorting".  | 
            ||
| 181 | *  | 
            ||
| 182 | * @access private  | 
            ||
| 183 | *  | 
            ||
| 184 | * @return void  | 
            ||
| 185 | */  | 
            ||
| 186 |     private function getPlaces() { | 
            ||
| 198 | }  | 
            ||
| 199 | }  | 
            ||
| 200 | }  | 
            ||
| 201 | }  | 
            ||
| 202 | |||
| 203 | /**  | 
            ||
| 204 | * Get "year" and "year_sorting".  | 
            ||
| 205 | *  | 
            ||
| 206 | * @access private  | 
            ||
| 207 | *  | 
            ||
| 208 | * @return void  | 
            ||
| 209 | */  | 
            ||
| 210 |     private function getYears() { | 
            ||
| 236 | }  | 
            ||
| 237 | }  | 
            ||
| 238 | }  | 
            ||
| 241 |