| Conditions | 33 |
| Paths | > 20000 |
| Total Lines | 153 |
| 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 |
||
| 68 | public function __construct(QuiteSimpleXmlElement $data = null) |
||
| 69 | { |
||
| 70 | if (is_null($data)) { |
||
| 71 | return; |
||
| 72 | } |
||
| 73 | |||
| 74 | $altLabels = array(); |
||
| 75 | |||
| 76 | // 001: Control number |
||
| 77 | $this->id = $data->text('marc:controlfield[@tag="001"]'); |
||
| 78 | |||
| 79 | // 003: MARC code for the agency whose system control number is |
||
| 80 | // contained in field 001 (Control Number) |
||
| 81 | // See http://www.loc.gov/marc/authority/ecadorg.html |
||
| 82 | $this->agency = $data->text('marc:controlfield[@tag="003"]'); |
||
| 83 | |||
| 84 | // 005: Modified |
||
| 85 | $this->modified = $this->parseDateTime($data->text('marc:controlfield[@tag="005"]')); |
||
| 86 | |||
| 87 | // 008: Extract *some* information |
||
| 88 | $f008 = $data->text('marc:controlfield[@tag="008"]'); |
||
| 89 | $r = substr($f008, 10, 1); |
||
| 90 | $this->cataloging = isset(self::$cat_rules[$r]) ? self::$cat_rules[$r] : null; |
||
| 91 | $r = substr($f008, 11, 1); |
||
| 92 | $this->vocabulary = isset(self::$vocabularies[$r]) ? self::$vocabularies[$r] : null; |
||
| 93 | |||
| 94 | // 040: |
||
| 95 | $source = $data->first('marc:datafield[@tag="040"]'); |
||
| 96 | if ($source) { |
||
| 97 | $this->catalogingAgency = $source->text('marc:subfield[@code="a"]') ?: null; |
||
| 98 | $this->language = $source->text('marc:subfield[@code="b"]') ?: null; |
||
| 99 | $this->transcribingAgency = $source->text('marc:subfield[@code="c"]') ?: null; |
||
| 100 | $this->modifyingAgency = $source->text('marc:subfield[@code="d"]') ?: null; |
||
| 101 | $this->vocabulary = $source->text('marc:subfield[@code="f"]') ?: $this->vocabulary; |
||
| 102 | } |
||
| 103 | |||
| 104 | // 100: Personal name (NR) |
||
| 105 | foreach ($data->all('marc:datafield[@tag="100"]') as $field) { |
||
| 106 | $this->class = 'person'; |
||
| 107 | $this->name = $field->text('marc:subfield[@code="a"]'); |
||
| 108 | $this->label = $this->normalize_name($this->name); |
||
| 109 | $enum = $field->text('marc:subfield[@code="b"]'); |
||
| 110 | if (!empty($enum)) { |
||
| 111 | $this->name = "{$this->name} {$enum}"; |
||
| 112 | $this->label = "{$this->label} {$enum}"; |
||
| 113 | } |
||
| 114 | $bd = $field->text('marc:subfield[@code="d"]'); |
||
| 115 | $bd = explode('-', $bd); |
||
| 116 | $this->birth = $bd[0] ?: null; |
||
| 117 | $this->death = (count($bd) > 1 && $bd[1]) ? $bd[1] : null; |
||
| 118 | } |
||
| 119 | |||
| 120 | // 110: Corporate Name (NR) |
||
| 121 | foreach ($data->all('marc:datafield[@tag="110"]') as $field) { |
||
| 122 | $this->class = 'corporation'; |
||
| 123 | $this->name = $field->text('marc:subfield[@code="a"]'); |
||
| 124 | $subdiv = $field->text('marc:subfield[@code="b"]'); |
||
| 125 | |||
| 126 | $this->label = ($field->attr('ind1') == '0') // Inverted name |
||
| 127 | ? $this->normalize_name($this->name) |
||
| 128 | : $this->name; |
||
| 129 | |||
| 130 | if (!empty($subdiv)) { |
||
| 131 | $this->name = "{$this->name} : {$subdiv}"; |
||
| 132 | $this->label = "{$this->label} : {$subdiv}"; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | // 111: Meeting Name (NR) |
||
| 137 | foreach ($data->all('marc:datafield[@tag="111"]') as $field) { |
||
| 138 | $this->class = 'meeting'; |
||
| 139 | $this->name = $field->text('marc:subfield[@code="a"]'); |
||
| 140 | $this->label = ($field->attr('ind1') == '0') // Inverted name |
||
| 141 | ? $this->normalize_name($this->name) |
||
| 142 | : $this->name; |
||
| 143 | } |
||
| 144 | |||
| 145 | // 130: Uniform title |
||
| 146 | foreach ($data->all('marc:datafield[@tag="130"]') as $field) { |
||
| 147 | $this->class = 'uniform_title'; |
||
| 148 | $label = $field->text('marc:subfield[@code="a"]'); |
||
| 149 | foreach ($field->all('marc:subfield[@code="d"]') as $s) { |
||
| 150 | $label .= ' (' . $s . ')'; |
||
| 151 | } |
||
| 152 | $this->name = $label; |
||
| 153 | } |
||
| 154 | |||
| 155 | // 150: Topical Term (NR) |
||
| 156 | foreach ($data->all('marc:datafield[@tag="150"]') as $field) { |
||
| 157 | $this->class = 'topicalTerm'; |
||
| 158 | $this->term = $field->text('marc:subfield[@code="a"]'); |
||
| 159 | $label = $field->text('marc:subfield[@code="a"]'); |
||
| 160 | foreach ($field->all('marc:subfield[@code="x"]') as $s) { |
||
| 161 | $label .= ' : ' . $s; |
||
| 162 | } |
||
| 163 | foreach ($field->all('marc:subfield[@code="v"]') as $s) { |
||
| 164 | $label .= ' : ' . $s; |
||
| 165 | } |
||
| 166 | foreach ($field->all('marc:subfield[@code="y"]') as $s) { |
||
| 167 | $label .= ' : ' . $s; |
||
| 168 | } |
||
| 169 | foreach ($field->all('marc:subfield[@code="z"]') as $s) { |
||
| 170 | $label .= ' : ' . $s; |
||
| 171 | } |
||
| 172 | $this->label = $label; |
||
| 173 | // TODO: ... |
||
| 174 | } |
||
| 175 | |||
| 176 | // 151: Geographic Term (NR) |
||
| 177 | // 155: Genre/form Term (NR) |
||
| 178 | |||
| 179 | // 375: Gender (R) |
||
| 180 | $genders = array(); |
||
| 181 | foreach ($data->all('marc:datafield[@tag="375"]') as $field) { |
||
| 182 | $gender = $field->text('marc:subfield[@code="a"]'); |
||
| 183 | $start = $field->text('marc:subfield[@code="s"]'); |
||
| 184 | $end = $field->text('marc:subfield[@code="e"]'); |
||
| 185 | $genders[] = array( |
||
| 186 | 'value' => $gender, |
||
| 187 | 'from' => $start, |
||
| 188 | 'until' => $end, |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | $this->genders = $genders; |
||
| 192 | |||
| 193 | // Alias gender to the last value to make utilizing easier |
||
| 194 | $this->gender = (count($this->genders) > 0) |
||
| 195 | ? $this->genders[count($this->genders) - 1]['value'] // assume sane ordering for now |
||
| 196 | : null; |
||
| 197 | |||
| 198 | // 400: See From Tracing-Personal Name (R) |
||
| 199 | foreach ($data->all('marc:datafield[@tag="400"]') as $field) { |
||
| 200 | $altLabels[] = $field->text('marc:subfield[@code="a"]'); |
||
| 201 | } |
||
| 202 | |||
| 203 | // 410: See From Tracing-Corporate Name (R) |
||
| 204 | foreach ($data->all('marc:datafield[@tag="410"]') as $field) { |
||
| 205 | $s = $field->text('marc:subfield[@code="a"]'); |
||
| 206 | if ($field->has('marc:subfield[@code="b"]')) { |
||
| 207 | $s .= ' : ' . $field->text('marc:subfield[@code="b"]'); |
||
| 208 | } |
||
| 209 | $altLabels[] = $s; |
||
| 210 | } |
||
| 211 | |||
| 212 | // 411: See From Tracing-Meeting Name (R) |
||
| 213 | foreach ($data->all('marc:datafield[@tag="411"]') as $field) { |
||
| 214 | $altLabels[] = $field->text('marc:subfield[@code="a"]'); |
||
| 215 | } |
||
| 216 | |||
| 217 | // TODO: rest |
||
| 218 | |||
| 219 | $this->altLabels = $altLabels; |
||
| 220 | } |
||
| 221 | } |
||
| 255 |