Complex classes like Name 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Name, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Name |
||
| 30 | { |
||
| 31 | use FormattingTrait, |
||
| 32 | AffixesTrait, |
||
| 33 | DelimiterTrait; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $nameParts; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Specifies the delimiter between the second to last and last name of the names in a name variable. Allowed values |
||
| 42 | * are “text” (selects the “and” term, e.g. “Doe, Johnson and Smith”) and “symbol” (selects the ampersand, |
||
| 43 | * e.g. “Doe, Johnson & Smith”). |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $and; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Determines when the name delimiter or a space is used between a truncated name list and the “et-al” |
||
| 51 | * (or “and others”) term in case of et-al abbreviation. Allowed values: |
||
| 52 | * - “contextual” - (default), name delimiter is only used for name lists truncated to two or more names |
||
| 53 | * - 1 name: “J. Doe et al.” |
||
| 54 | * - 2 names: “J. Doe, S. Smith, et al.” |
||
| 55 | * - “after-inverted-name” - name delimiter is only used if the preceding name is inverted as a result of the |
||
| 56 | * - name-as-sort-order attribute. E.g. with name-as-sort-order set to “first”: |
||
| 57 | * - “Doe, J., et al.” |
||
| 58 | * - “Doe, J., S. Smith et al.” |
||
| 59 | * - “always” - name delimiter is always used |
||
| 60 | * - 1 name: “J. Doe, et al.” |
||
| 61 | * - 2 names: “J. Doe, S. Smith, et al.” |
||
| 62 | * - “never” - name delimiter is never used |
||
| 63 | * - 1 name: “J. Doe et al.” |
||
| 64 | * - 2 names: “J. Doe, S. Smith et al.” |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $delimiterPrecedesEtAl; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Determines when the name delimiter is used to separate the second to last and the last name in name lists (if |
||
| 72 | * and is not set, the name delimiter is always used, regardless of the value of delimiter-precedes-last). Allowed |
||
| 73 | * values: |
||
| 74 | * |
||
| 75 | * - “contextual” - (default), name delimiter is only used for name lists with three or more names |
||
| 76 | * - 2 names: “J. Doe and T. Williams” |
||
| 77 | * - 3 names: “J. Doe, S. Smith, and T. Williams” |
||
| 78 | * - “after-inverted-name” - name delimiter is only used if the preceding name is inverted as a result of the |
||
| 79 | * name-as-sort-order attribute. E.g. with name-as-sort-order set to “first”: |
||
| 80 | * - “Doe, J., and T. Williams” |
||
| 81 | * - “Doe, J., S. Smith and T. Williams” |
||
| 82 | * - “always” - name delimiter is always used |
||
| 83 | * - 2 names: “J. Doe, and T. Williams” |
||
| 84 | * - 3 names: “J. Doe, S. Smith, and T. Williams” |
||
| 85 | * - “never” - name delimiter is never used |
||
| 86 | * - 2 names: “J. Doe and T. Williams” |
||
| 87 | * - 3 names: “J. Doe, S. Smith and T. Williams” |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | private $delimiterPrecedesLast; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Use of etAlMin (et-al-min attribute) and etAlUseFirst (et-al-use-first attribute) enables et-al abbreviation. If |
||
| 95 | * the number of names in a name variable matches or exceeds the number set on etAlMin, the rendered name list is |
||
| 96 | * truncated after reaching the number of names set on etAlUseFirst. |
||
| 97 | * |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | private $etAlMin; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Use of etAlMin (et-al-min attribute) and etAlUseFirst (et-al-use-first attribute) enables et-al abbreviation. If |
||
| 104 | * the number of names in a name variable matches or exceeds the number set on etAlMin, the rendered name list is |
||
| 105 | * truncated after reaching the number of names set on etAlUseFirst. |
||
| 106 | * |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | private $etAlUseFirst; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * If used, the values of these attributes (et-al-subsequent-min and et-al-subsequent-use-first) replace those of |
||
| 113 | * respectively et-al-min and et-al-use-first for subsequent cites (cites referencing earlier cited items). |
||
| 114 | * |
||
| 115 | * @var int |
||
| 116 | */ |
||
| 117 | private $etAlSubsequentMin; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * If used, the values of these attributes (et-al-subsequent-min and et-al-subsequent-use-first) replace those of |
||
| 121 | * respectively et-al-min and et-al-use-first for subsequent cites (cites referencing earlier cited items). |
||
| 122 | * |
||
| 123 | * @var int |
||
| 124 | */ |
||
| 125 | private $etAlSubsequentUseFirst; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * When set to “true” (the default is “false”), name lists truncated by et-al abbreviation are followed by the name |
||
| 129 | * delimiter, the ellipsis character, and the last name of the original name list. This is only possible when the |
||
| 130 | * original name list has at least two more names than the truncated name list (for this the value of |
||
| 131 | * et-al-use-first/et-al-subsequent-min must be at least 2 less than the value of |
||
| 132 | * et-al-min/et-al-subsequent-use-first). |
||
| 133 | * A. Goffeau, B. G. Barrell, H. Bussey, R. W. Davis, B. Dujon, H. Feldmann, … S. G. Oliver |
||
| 134 | * |
||
| 135 | * @var bool |
||
| 136 | */ |
||
| 137 | private $etAlUseLast = false; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Specifies whether all the name-parts of personal names should be displayed (value “long”, the default), or only |
||
| 141 | * the family name and the non-dropping-particle (value “short”). A third value, “count”, returns the total number |
||
| 142 | * of names that would otherwise be rendered by the use of the cs:names element (taking into account the effects of |
||
| 143 | * et-al abbreviation and editor/translator collapsing), which allows for advanced sorting. |
||
| 144 | * |
||
| 145 | * @var string |
||
| 146 | */ |
||
| 147 | private $form = "long"; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * When set to “false” (the default is “true”), given names are no longer initialized when “initialize-with” is set. |
||
| 151 | * However, the value of “initialize-with” is still added after initials present in the full name (e.g. with |
||
| 152 | * initialize set to “false”, and initialize-with set to ”.”, “James T Kirk” becomes “James T. Kirk”). |
||
| 153 | * |
||
| 154 | * @var bool |
||
| 155 | */ |
||
| 156 | private $initialize = true; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * When set, given names are converted to initials. The attribute value is added after each initial (”.” results |
||
| 160 | * in “J.J. Doe”). For compound given names (e.g. “Jean-Luc”), hyphenation of the initials can be controlled with |
||
| 161 | * the global initialize-with-hyphen option |
||
| 162 | * |
||
| 163 | * @var string |
||
| 164 | */ |
||
| 165 | private $initializeWith = ""; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Specifies that names should be displayed with the given name following the family name (e.g. “John Doe” becomes |
||
| 169 | * “Doe, John”). The attribute has two possible values: |
||
| 170 | * - “first” - attribute only has an effect on the first name of each name variable |
||
| 171 | * - “all” - attribute has an effect on all names |
||
| 172 | * Note that even when name-as-sort-order changes the name-part order, the display order is not necessarily the same |
||
| 173 | * as the sorting order for names containing particles and suffixes (see Name-part order). Also, name-as-sort-order |
||
| 174 | * only affects names written in the latin or Cyrillic alphabets. Names written in other alphabets (e.g. Asian |
||
| 175 | * scripts) are always displayed with the family name preceding the given name. |
||
| 176 | * |
||
| 177 | * @var string |
||
| 178 | */ |
||
| 179 | private $nameAsSortOrder = ""; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Sets the delimiter for name-parts that have switched positions as a result of name-as-sort-order. The default |
||
| 183 | * value is ”, ” (“Doe, John”). As is the case for name-as-sort-order, this attribute only affects names written in |
||
| 184 | * the latin or Cyrillic alphabets. |
||
| 185 | * |
||
| 186 | * @var string |
||
| 187 | */ |
||
| 188 | private $sortSeparator = ", "; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Specifies the text string used to separate names in a name variable. Default is ”, ” (e.g. “Doe, Smith”). |
||
| 192 | * @var |
||
| 193 | */ |
||
| 194 | private $delimiter = ", "; |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * @var Names |
||
| 199 | */ |
||
| 200 | private $parent; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Name constructor. |
||
| 204 | * @param \SimpleXMLElement $node |
||
| 205 | * @param Names $parent |
||
| 206 | */ |
||
| 207 | public function __construct(\SimpleXMLElement $node, Names $parent) |
||
| 278 | |||
| 279 | public function render($data) |
||
| 280 | { |
||
| 281 | $resultNames = []; |
||
| 282 | $etAl = false; |
||
| 283 | $count = 0; |
||
| 284 | /** |
||
| 285 | * @var string $type |
||
| 286 | * @var array $name |
||
| 287 | */ |
||
| 288 | foreach ($data as $rank => $name) { |
||
| 289 | ++$count; |
||
| 290 | $resultNames[] = $this->formatName($name, $rank); |
||
| 291 | } |
||
| 292 | |||
| 293 | /* Use of et-al-min and et-al-user-first enables et-al abbreviation. If the number of names in a name variable |
||
| 294 | matches or exceeds the number set on et-al-min, the rendered name list is truncated after reaching the number of |
||
| 295 | names set on et-al-use-first. */ |
||
| 296 | if (isset($this->etAlMin) && isset($this->etAlUseFirst)) { |
||
| 297 | $cnt = count($resultNames); |
||
| 298 | if ($this->etAlMin >= count($cnt)) { |
||
| 299 | for ($i = $this->etAlUseFirst; $i < $cnt; ++$i) { |
||
| 300 | unset($resultNames[$i]); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | if ($this->parent->hasEtAl()) { |
||
| 304 | $etAl = $this->parent->getEtAl()->render($name); |
||
| 305 | } else { |
||
| 306 | $etAl = CiteProc::getContext()->getLocale()->filter('terms', 'et-al')->single; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | |||
| 311 | /* add "and" */ |
||
| 312 | $count = count($resultNames); |
||
| 313 | if (!empty($this->and) && $count > 1 && !$etAl) { |
||
| 314 | $new = $this->and . ' ' . end($resultNames); //stick an "and" in front of the last author if "and" is defined |
||
| 315 | $resultNames[key($resultNames)] = $new; |
||
| 316 | } |
||
| 317 | |||
| 318 | $text = implode($this->delimiter, $resultNames); |
||
| 319 | |||
| 320 | if (!empty($resultNames) && $etAl) { |
||
| 321 | |||
| 322 | /* By default, when a name list is truncated to a single name, the name and the “et-al” (or “and others”) |
||
| 323 | term are separated by a space (e.g. “Doe et al.”). When a name list is truncated to two or more names, the |
||
| 324 | name delimiter is used (e.g. “Doe, Smith, et al.”). This behavior can be changed with the |
||
| 325 | delimiter-precedes-et-al attribute. */ |
||
| 326 | switch ($this->delimiterPrecedesEtAl) { |
||
| 327 | case 'never': |
||
| 328 | $text = $text . " $etAl"; |
||
| 329 | break; |
||
| 330 | case 'always': |
||
| 331 | $text = $text . "$this->delimiter$etAl"; |
||
| 332 | break; |
||
| 333 | default: |
||
| 334 | if (count($resultNames) === 1) { |
||
| 335 | $text .= " $etAl"; |
||
| 336 | } else { |
||
| 337 | $text .= $this->delimiter . $etAl; |
||
| 338 | } |
||
| 339 | |||
| 340 | } |
||
| 341 | } |
||
| 342 | if ($this->form == 'count') { |
||
| 343 | if ($etAl === false) { |
||
| 344 | return (int)count($resultNames); |
||
| 345 | } else { |
||
| 346 | return (int)(count($resultNames) - 1); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | // strip out the last delimiter if not required |
||
| 350 | if (isset($this->and) && count($resultNames) > 1) { |
||
| 351 | $lastDelimiter = strrpos($text, $this->delimiter . $this->and); |
||
| 352 | switch ($this->delimiterPrecedesLast) { |
||
| 353 | case 'always': |
||
| 354 | return $text; |
||
| 355 | break; |
||
| 356 | case 'never': |
||
| 357 | return substr_replace($text, ' ', $lastDelimiter, strlen($this->delimiter)); |
||
| 358 | break; |
||
| 359 | case 'contextual': |
||
| 360 | default: |
||
| 361 | if (count($resultNames) < 3 && $lastDelimiter !== false) { |
||
| 362 | return substr_replace($text, ' ', $lastDelimiter, strlen($this->delimiter)); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | } |
||
| 366 | return $text; |
||
| 367 | } |
||
| 368 | |||
| 369 | private function formatName($name, $rank) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param $name |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | private function getNamesString($name, $rank) |
||
| 448 | |||
| 449 | public function getOptions() |
||
| 470 | |||
| 471 | } |
It seems like you are relying on a variable being defined by an iteration: