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 |
||
| 22 | class Name |
||
| 23 | { |
||
| 24 | use FormattingTrait, |
||
| 25 | AffixesTrait, |
||
| 26 | DelimiterTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $nameParts; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Specifies the delimiter between the second to last and last name of the names in a name variable. Allowed values |
||
| 35 | * are “text” (selects the “and” term, e.g. “Doe, Johnson and Smith”) and “symbol” (selects the ampersand, |
||
| 36 | * e.g. “Doe, Johnson & Smith”). |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $and; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Determines when the name delimiter or a space is used between a truncated name list and the “et-al” |
||
| 44 | * (or “and others”) term in case of et-al abbreviation. Allowed values: |
||
| 45 | * - “contextual” - (default), name delimiter is only used for name lists truncated to two or more names |
||
| 46 | * - 1 name: “J. Doe et al.” |
||
| 47 | * - 2 names: “J. Doe, S. Smith, et al.” |
||
| 48 | * - “after-inverted-name” - name delimiter is only used if the preceding name is inverted as a result of the |
||
| 49 | * - name-as-sort-order attribute. E.g. with name-as-sort-order set to “first”: |
||
| 50 | * - “Doe, J., et al.” |
||
| 51 | * - “Doe, J., S. Smith et al.” |
||
| 52 | * - “always” - name delimiter is always used |
||
| 53 | * - 1 name: “J. Doe, et al.” |
||
| 54 | * - 2 names: “J. Doe, S. Smith, et al.” |
||
| 55 | * - “never” - name delimiter is never used |
||
| 56 | * - 1 name: “J. Doe et al.” |
||
| 57 | * - 2 names: “J. Doe, S. Smith et al.” |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $delimiterPrecedesEtAl; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Determines when the name delimiter is used to separate the second to last and the last name in name lists (if |
||
| 65 | * and is not set, the name delimiter is always used, regardless of the value of delimiter-precedes-last). Allowed |
||
| 66 | * values: |
||
| 67 | * |
||
| 68 | * - “contextual” - (default), name delimiter is only used for name lists with three or more names |
||
| 69 | * - 2 names: “J. Doe and T. Williams” |
||
| 70 | * - 3 names: “J. Doe, S. Smith, and T. Williams” |
||
| 71 | * - “after-inverted-name” - name delimiter is only used if the preceding name is inverted as a result of the |
||
| 72 | * name-as-sort-order attribute. E.g. with name-as-sort-order set to “first”: |
||
| 73 | * - “Doe, J., and T. Williams” |
||
| 74 | * - “Doe, J., S. Smith and T. Williams” |
||
| 75 | * - “always” - name delimiter is always used |
||
| 76 | * - 2 names: “J. Doe, and T. Williams” |
||
| 77 | * - 3 names: “J. Doe, S. Smith, and T. Williams” |
||
| 78 | * - “never” - name delimiter is never used |
||
| 79 | * - 2 names: “J. Doe and T. Williams” |
||
| 80 | * - 3 names: “J. Doe, S. Smith and T. Williams” |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | private $delimiterPrecedesLast; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Use of etAlMin (et-al-min attribute) and etAlUseFirst (et-al-use-first attribute) enables et-al abbreviation. If |
||
| 88 | * the number of names in a name variable matches or exceeds the number set on etAlMin, the rendered name list is |
||
| 89 | * truncated after reaching the number of names set on etAlUseFirst. |
||
| 90 | * |
||
| 91 | * @var int |
||
| 92 | */ |
||
| 93 | private $etAlMin; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Use of etAlMin (et-al-min attribute) and etAlUseFirst (et-al-use-first attribute) enables et-al abbreviation. If |
||
| 97 | * the number of names in a name variable matches or exceeds the number set on etAlMin, the rendered name list is |
||
| 98 | * truncated after reaching the number of names set on etAlUseFirst. |
||
| 99 | * |
||
| 100 | * @var int |
||
| 101 | */ |
||
| 102 | private $etAlUseFirst; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * If used, the values of these attributes (et-al-subsequent-min and et-al-subsequent-use-first) replace those of |
||
| 106 | * respectively et-al-min and et-al-use-first for subsequent cites (cites referencing earlier cited items). |
||
| 107 | * |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | private $etAlSubsequentMin; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * If used, the values of these attributes (et-al-subsequent-min and et-al-subsequent-use-first) replace those of |
||
| 114 | * respectively et-al-min and et-al-use-first for subsequent cites (cites referencing earlier cited items). |
||
| 115 | * |
||
| 116 | * @var int |
||
| 117 | */ |
||
| 118 | private $etAlSubsequentUseFirst; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * When set to “true” (the default is “false”), name lists truncated by et-al abbreviation are followed by the name |
||
| 122 | * delimiter, the ellipsis character, and the last name of the original name list. This is only possible when the |
||
| 123 | * original name list has at least two more names than the truncated name list (for this the value of |
||
| 124 | * et-al-use-first/et-al-subsequent-min must be at least 2 less than the value of |
||
| 125 | * et-al-min/et-al-subsequent-use-first). |
||
| 126 | * A. Goffeau, B. G. Barrell, H. Bussey, R. W. Davis, B. Dujon, H. Feldmann, … S. G. Oliver |
||
| 127 | * |
||
| 128 | * @var bool |
||
| 129 | */ |
||
| 130 | private $etAlUseLast = false; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Specifies whether all the name-parts of personal names should be displayed (value “long”, the default), or only |
||
| 134 | * the family name and the non-dropping-particle (value “short”). A third value, “count”, returns the total number |
||
| 135 | * of names that would otherwise be rendered by the use of the cs:names element (taking into account the effects of |
||
| 136 | * et-al abbreviation and editor/translator collapsing), which allows for advanced sorting. |
||
| 137 | * |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | private $form = "long"; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * When set to “false” (the default is “true”), given names are no longer initialized when “initialize-with” is set. |
||
| 144 | * However, the value of “initialize-with” is still added after initials present in the full name (e.g. with |
||
| 145 | * initialize set to “false”, and initialize-with set to ”.”, “James T Kirk” becomes “James T. Kirk”). |
||
| 146 | * |
||
| 147 | * @var bool |
||
| 148 | */ |
||
| 149 | private $initialize = true; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * When set, given names are converted to initials. The attribute value is added after each initial (”.” results |
||
| 153 | * in “J.J. Doe”). For compound given names (e.g. “Jean-Luc”), hyphenation of the initials can be controlled with |
||
| 154 | * the global initialize-with-hyphen option |
||
| 155 | * |
||
| 156 | * @var string |
||
| 157 | */ |
||
| 158 | private $initializeWith = ""; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Specifies that names should be displayed with the given name following the family name (e.g. “John Doe” becomes |
||
| 162 | * “Doe, John”). The attribute has two possible values: |
||
| 163 | * - “first” - attribute only has an effect on the first name of each name variable |
||
| 164 | * - “all” - attribute has an effect on all names |
||
| 165 | * Note that even when name-as-sort-order changes the name-part order, the display order is not necessarily the same |
||
| 166 | * as the sorting order for names containing particles and suffixes (see Name-part order). Also, name-as-sort-order |
||
| 167 | * only affects names written in the latin or Cyrillic alphabets. Names written in other alphabets (e.g. Asian |
||
| 168 | * scripts) are always displayed with the family name preceding the given name. |
||
| 169 | * |
||
| 170 | * @var string |
||
| 171 | */ |
||
| 172 | private $nameAsSortOrder = ""; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Sets the delimiter for name-parts that have switched positions as a result of name-as-sort-order. The default |
||
| 176 | * value is ”, ” (“Doe, John”). As is the case for name-as-sort-order, this attribute only affects names written in |
||
| 177 | * the latin or Cyrillic alphabets. |
||
| 178 | * |
||
| 179 | * @var string |
||
| 180 | */ |
||
| 181 | private $sortSeparator = ", "; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Specifies the text string used to separate names in a name variable. Default is ”, ” (e.g. “Doe, Smith”). |
||
| 185 | * @var |
||
| 186 | */ |
||
| 187 | private $delimiter = ", "; |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * @var Names |
||
| 192 | */ |
||
| 193 | private $parent; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Name constructor. |
||
| 197 | * @param \SimpleXMLElement $node |
||
| 198 | * @param Names $parent |
||
| 199 | */ |
||
| 200 | public function __construct(\SimpleXMLElement $node, Names $parent) |
||
| 271 | |||
| 272 | public function render($data) |
||
| 361 | |||
| 362 | private function formatName($name, $rank) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param $name |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | private function getNamesString($name, $rank) |
||
| 441 | |||
| 442 | public function getOptions() |
||
| 463 | |||
| 464 | } |
It seems like you are relying on a variable being defined by an iteration: