Complex classes like ContentTypeAttribute 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 ContentTypeAttribute, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class ContentTypeAttribute |
||
20 | { |
||
21 | use AttributeTrait; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private const PREFIX_UNREGISTERED = 'x'; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private const PREFIX_VENDOR = 'vnd'; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private const PREFIX_PERSONAL = 'prs'; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $type; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $subType; |
||
47 | |||
48 | /** |
||
49 | * @var string|null |
||
50 | */ |
||
51 | private $prefix; |
||
52 | |||
53 | /** |
||
54 | * @var string|null |
||
55 | */ |
||
56 | private $deliminator; |
||
57 | |||
58 | /** |
||
59 | * @var string|null |
||
60 | */ |
||
61 | private $suffix; |
||
62 | |||
63 | /** |
||
64 | * @param string|null $type |
||
65 | * @param string|null $subType |
||
66 | * @param string|null $prefix |
||
67 | * @param string|null $suffix |
||
68 | * @param string|null $deliminator |
||
69 | */ |
||
70 | public function __construct( |
||
83 | |||
84 | /** |
||
85 | * @return string |
||
86 | */ |
||
87 | public function stringify(): string |
||
97 | |||
98 | /** |
||
99 | * @return null|string |
||
100 | */ |
||
101 | public function getType(): ?string |
||
105 | |||
106 | /** |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function hasType(): bool |
||
113 | |||
114 | /** |
||
115 | * @param string|null $type |
||
116 | * |
||
117 | * @return bool |
||
118 | */ |
||
119 | public function isTypeMatch(string $type = null): bool |
||
123 | |||
124 | /** |
||
125 | * @return null|string |
||
126 | */ |
||
127 | public function getSubType(): ?string |
||
131 | |||
132 | /** |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function hasSubType(): bool |
||
139 | |||
140 | /** |
||
141 | * @param string|null $subType |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function isSubTypeMatch(string $subType = null): bool |
||
149 | |||
150 | /** |
||
151 | * @return null|string |
||
152 | */ |
||
153 | public function getPrefix(): ?string |
||
157 | |||
158 | /** |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function hasPrefix(): bool |
||
165 | |||
166 | /** |
||
167 | * @param string|null $prefix |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function isPrefixMatch(string $prefix = null): bool |
||
175 | |||
176 | /** |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function isPrefixUnregistered(): bool |
||
183 | |||
184 | /** |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function isPrefixVendor(): bool |
||
191 | |||
192 | /** |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function isPrefixPersonal(): bool |
||
199 | |||
200 | /** |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function isPrefixStandard(): bool |
||
209 | |||
210 | /** |
||
211 | * @return null|string |
||
212 | */ |
||
213 | public function getDeliminator(): ?string |
||
217 | |||
218 | /** |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function hasDeliminator(): bool |
||
225 | |||
226 | /** |
||
227 | * @param string|null $deliminator |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function isDeliminatorMatch(string $deliminator = null): bool |
||
235 | |||
236 | /** |
||
237 | * @return null|string |
||
238 | */ |
||
239 | public function getSuffix(): ?string |
||
243 | |||
244 | /** |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function hasSuffix(): bool |
||
251 | |||
252 | /** |
||
253 | * @param string|null $suffix |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function isSuffixMatch(string $suffix = null): bool |
||
261 | |||
262 | /** |
||
263 | * @param string|null $type |
||
264 | * @param string|null $subType |
||
265 | * @param string|null $prefix |
||
266 | * @param string|null $suffix |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | public function isMatch(string $type = null, string $subType = null, string $prefix = null, string $suffix = null): bool |
||
277 | |||
278 | /** |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function isValid(): bool |
||
287 | |||
288 | /** |
||
289 | * @param string $string |
||
290 | * |
||
291 | * @return array|null |
||
292 | */ |
||
293 | public static function explodeParsable(string $string = null): ?array |
||
312 | |||
313 | /** |
||
314 | * @param string|null $prefix |
||
315 | * |
||
316 | * @return null|string |
||
317 | */ |
||
318 | private static function sanitizePrefix(string $prefix = null): ?string |
||
329 | |||
330 | /** |
||
331 | * @param string|null $deliminator |
||
332 | * |
||
333 | * @return null|string |
||
334 | */ |
||
335 | private static function sanitizeDeliminator(string $deliminator = null): ?string |
||
345 | } |
||
346 |