Complex classes like AdvancedTrait 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 AdvancedTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | trait AdvancedTrait |
||
20 | { |
||
21 | /** |
||
22 | * Get argument value |
||
23 | * |
||
24 | * @param string $name |
||
25 | * |
||
26 | * @return null|string |
||
27 | */ |
||
28 | abstract protected function getArgumentValue($name); |
||
29 | |||
30 | /** |
||
31 | * Set argument |
||
32 | * |
||
33 | * @param string $argument |
||
34 | * |
||
35 | * @return $this |
||
36 | */ |
||
37 | abstract protected function setArgument($argument); |
||
38 | |||
39 | /** |
||
40 | * Get PDF settings |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | abstract public function getPdfSettings(); |
||
45 | |||
46 | /** |
||
47 | * Whether ASCII85 encode pages |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | 2 | public function isAscii85EncodePages() |
|
60 | |||
61 | /** |
||
62 | * Set ASCII85 encode pages flag |
||
63 | * |
||
64 | * @param bool $ascii85EncodePages |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | 2 | public function setAscii85EncodePages($ascii85EncodePages) |
|
74 | |||
75 | /** |
||
76 | * Whether to auto position EPS files |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | 2 | public function isAutoPositionEpsFiles() |
|
89 | |||
90 | /** |
||
91 | * Set auto position EPS files flag |
||
92 | * |
||
93 | * @param bool $autoPositionEpsFiles |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | 2 | public function setAutoPositionEpsFiles($autoPositionEpsFiles) |
|
103 | |||
104 | /** |
||
105 | * Whether to create job ticket |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | 10 | public function isCreateJobTicket() |
|
110 | { |
||
111 | 10 | $value = $this->getArgumentValue('-dCreateJobTicket'); |
|
112 | 10 | if (null === $value) { |
|
113 | 10 | switch ($this->getPdfSettings()) { |
|
114 | 10 | case PdfSettings::PRINTER: |
|
115 | 9 | case PdfSettings::PREPRESS: |
|
116 | 4 | return true; |
|
117 | 3 | default: |
|
118 | 6 | return false; |
|
119 | 3 | } |
|
120 | } |
||
121 | |||
122 | 10 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Set create job ticket flag |
||
127 | * |
||
128 | * @param bool $createJobTicket |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | 10 | public function setCreateJobTicket($createJobTicket) |
|
138 | |||
139 | /** |
||
140 | * Whether to detect blends |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | 2 | public function isDetectBlends() |
|
153 | |||
154 | /** |
||
155 | * Set detect blends flag |
||
156 | * |
||
157 | * @param bool $detectBlends |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | 2 | public function setDetectBlends($detectBlends) |
|
167 | |||
168 | /** |
||
169 | * Whether to emit DSC warnings |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | 2 | public function isEmitDscWarnings() |
|
182 | |||
183 | /** |
||
184 | * Set emit DSC warnings flag |
||
185 | * |
||
186 | * @param bool $emitDscWarnings |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | 2 | public function setEmitDscWarnings($emitDscWarnings) |
|
196 | |||
197 | /** |
||
198 | * Whether to lock distiller params |
||
199 | * |
||
200 | * @return bool |
||
201 | */ |
||
202 | 2 | public function isLockDistillerParams() |
|
211 | |||
212 | /** |
||
213 | * Set lock distiller params flag |
||
214 | * |
||
215 | * @param bool $lockDistillerParams |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | 2 | public function setLockDistillerParams($lockDistillerParams) |
|
225 | |||
226 | /** |
||
227 | * Get OPM |
||
228 | * |
||
229 | * @return int |
||
230 | */ |
||
231 | 2 | public function getOpm() |
|
240 | |||
241 | /** |
||
242 | * Set OPM |
||
243 | * |
||
244 | * @param int $opm |
||
245 | * |
||
246 | * @return $this |
||
247 | */ |
||
248 | 2 | public function setOpm($opm) |
|
254 | |||
255 | /** |
||
256 | * Whether to parse DSC comments |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | 2 | public function isParseDscComments() |
|
269 | |||
270 | /** |
||
271 | * Set parse DSC comments flag |
||
272 | * |
||
273 | * @param bool $parseDscComments |
||
274 | * |
||
275 | * @return $this |
||
276 | */ |
||
277 | 2 | public function setParseDscComments($parseDscComments) |
|
283 | |||
284 | /** |
||
285 | * Whether to parse DSC comments for doc info |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | 2 | public function isParseDscCommentsForDocInfo() |
|
298 | |||
299 | /** |
||
300 | * Set parse DSC comments for doc info flag |
||
301 | * |
||
302 | * @param bool $parseDscCommentsForDocInfo |
||
303 | * |
||
304 | * @return $this |
||
305 | */ |
||
306 | 2 | public function setParseDscCommentsForDocInfo($parseDscCommentsForDocInfo) |
|
312 | |||
313 | /** |
||
314 | * Whether to preserve copy page |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | 2 | public function isPreserveCopyPage() |
|
327 | |||
328 | /** |
||
329 | * Set preserve copy page flag |
||
330 | * |
||
331 | * @param bool $preserveCopyPage |
||
332 | * |
||
333 | * @return $this |
||
334 | */ |
||
335 | 2 | public function setPreserveCopyPage($preserveCopyPage) |
|
341 | |||
342 | /** |
||
343 | * Whether to preserve EPS info |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | 2 | public function isPreserveEpsInfo() |
|
356 | |||
357 | /** |
||
358 | * Set preserve EPS info flag |
||
359 | * |
||
360 | * @param bool $preserveEpsInfo |
||
361 | * |
||
362 | * @return $this |
||
363 | */ |
||
364 | 2 | public function setPreserveEpsInfo($preserveEpsInfo) |
|
370 | |||
371 | /** |
||
372 | * Whether to preserve OPI comments |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | 10 | public function isPreserveOpiComments() |
|
377 | { |
||
378 | 10 | $value = $this->getArgumentValue('-dPreserveOPIComments'); |
|
379 | 10 | if (null === $value) { |
|
380 | 10 | switch ($this->getPdfSettings()) { |
|
381 | 10 | case PdfSettings::PRINTER: |
|
382 | 9 | case PdfSettings::PREPRESS: |
|
383 | 4 | return true; |
|
384 | 3 | default: |
|
385 | 6 | return false; |
|
386 | 3 | } |
|
387 | } |
||
388 | |||
389 | 10 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
390 | } |
||
391 | |||
392 | /** |
||
393 | * Set preserve OPI comments flag |
||
394 | * |
||
395 | * @param bool $preserveOpiComments |
||
396 | * |
||
397 | * @return $this |
||
398 | */ |
||
399 | 10 | public function setPreserveOpiComments($preserveOpiComments) |
|
405 | |||
406 | /** |
||
407 | * Whether to use prologue |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | 2 | public function isUsePrologue() |
|
420 | |||
421 | /** |
||
422 | * Set use prologue flag |
||
423 | * |
||
424 | * @param bool $usePrologue |
||
425 | * |
||
426 | * @return $this |
||
427 | */ |
||
428 | 2 | public function setUsePrologue($usePrologue) |
|
434 | } |
||
435 |