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) |
||
368 | |||
369 | private function formatName($name, $rank) |
||
396 | |||
397 | /** |
||
398 | * @param $name |
||
399 | * @return string |
||
400 | */ |
||
401 | private function getNamesString($name, $rank) |
||
460 | |||
461 | public function getOptions() |
||
482 | |||
483 | /** |
||
484 | * @param $name |
||
485 | * @return \stdClass |
||
486 | */ |
||
487 | private function cloneNamePOSC($name) |
||
507 | |||
508 | } |
||
509 |
It seems like you are relying on a variable being defined by an iteration: