Complex classes like PassFactory 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 PassFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class PassFactory |
||
32 | { |
||
33 | /** |
||
34 | * Output path for generated pass files |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $outputPath = ''; |
||
39 | |||
40 | /** |
||
41 | * Overwrite if pass exists |
||
42 | * |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $overwrite = false; |
||
46 | |||
47 | /** |
||
48 | * Pass type identifier |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $passTypeIdentifier; |
||
53 | |||
54 | /** |
||
55 | * Team identifier |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $teamIdentifier; |
||
60 | |||
61 | /** |
||
62 | * Organization name |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $organizationName; |
||
67 | |||
68 | /** |
||
69 | * P12 file |
||
70 | * |
||
71 | * @var \Passbook\Certificate\P12Interface |
||
72 | */ |
||
73 | protected $p12; |
||
74 | |||
75 | /** |
||
76 | * WWDR file |
||
77 | * |
||
78 | * @var \Passbook\Certificate\WWDRInterface |
||
79 | */ |
||
80 | protected $wwdr; |
||
81 | |||
82 | /** |
||
83 | * @var bool - skip signing the pass; should only be used for testing |
||
84 | */ |
||
85 | protected $skipSignature; |
||
86 | |||
87 | /** |
||
88 | * @var PassValidatorInterface |
||
89 | */ |
||
90 | private $passValidator; |
||
91 | |||
92 | /** |
||
93 | * Pass file extension |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | const PASS_EXTENSION = '.pkpass'; |
||
98 | |||
99 | public function __construct($passTypeIdentifier, $teamIdentifier, $organizationName, $p12File, $p12Pass, $wwdrFile) |
||
113 | |||
114 | /** |
||
115 | * Set outputPath |
||
116 | * |
||
117 | * @param string |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function setOutputPath($outputPath) |
||
127 | |||
128 | /** |
||
129 | * Get outputPath |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getOutputPath() |
||
137 | |||
138 | /** |
||
139 | * The output path with a directory separator on the end. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getNormalizedOutputPath() |
||
147 | |||
148 | /** |
||
149 | * Set overwrite |
||
150 | * |
||
151 | * @param boolean |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setOverwrite($overwrite) |
||
161 | |||
162 | /** |
||
163 | * Get overwrite |
||
164 | * |
||
165 | * @return boolean |
||
166 | */ |
||
167 | public function isOverwrite() |
||
171 | |||
172 | /** |
||
173 | * Set skip signature |
||
174 | * |
||
175 | * When set, the pass will not be signed when packaged. This should only |
||
176 | * be used for testing. |
||
177 | * |
||
178 | * @param boolean |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function setSkipSignature($skipSignature) |
||
188 | |||
189 | /** |
||
190 | * Get skip signature |
||
191 | * |
||
192 | * @return boolean |
||
193 | */ |
||
194 | public function getSkipSignature() |
||
198 | |||
199 | /** |
||
200 | * Set an implementation of PassValidatorInterface to validate the pass |
||
201 | * before packaging. When set to null, no validation is performed when |
||
202 | * packaging the pass. |
||
203 | * |
||
204 | * @param PassValidatorInterface|null $passValidator |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setPassValidator(PassValidatorInterface $passValidator = null) |
||
214 | |||
215 | /** |
||
216 | * @return PassValidatorInterface |
||
217 | */ |
||
218 | public function getPassValidator() |
||
222 | |||
223 | /** |
||
224 | * Serialize pass |
||
225 | * |
||
226 | * @param PassInterface $pass |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | public static function serialize(PassInterface $pass) |
||
234 | |||
235 | /** |
||
236 | * Creates a pkpass file |
||
237 | * |
||
238 | * @param PassInterface $pass - the pass to be packaged into a .pkpass file |
||
239 | * @param string $passName - filename to be used for the pass; if blank the serial number will be used |
||
240 | * |
||
241 | * @return SplFileObject If an IO error occurred |
||
242 | * @throws Exception |
||
243 | */ |
||
244 | public function package(PassInterface $pass, $passName = '') |
||
284 | |||
285 | /** |
||
286 | * @param $passDir |
||
287 | * @param $manifestJSONFile |
||
288 | */ |
||
289 | private function sign($passDir, $manifestJSONFile) |
||
331 | |||
332 | /** |
||
333 | * Creates a zip of a directory including all sub directories (recursive) |
||
334 | * |
||
335 | * @param $source - path to the source directory |
||
336 | * @param $destination - output directory |
||
337 | * |
||
338 | * @return bool |
||
339 | * @throws Exception |
||
340 | */ |
||
341 | private function zip($source, $destination) |
||
372 | |||
373 | /** |
||
374 | * Recursive folder remove |
||
375 | * |
||
376 | * @param string $dir |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | private function rrmdir($dir) |
||
389 | |||
390 | /** |
||
391 | * @param PassInterface $pass |
||
392 | */ |
||
393 | private function populateRequiredInformation(PassInterface $pass) |
||
407 | |||
408 | /** |
||
409 | * @param $array |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | private static function jsonEncode($array) |
||
419 | |||
420 | /** |
||
421 | * @param $passName |
||
422 | * @param PassInterface $pass |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | public function getPassName($passName, PassInterface $pass) |
||
431 | |||
432 | /** |
||
433 | * @param $passDir |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | private function prepareManifest($passDir) |
||
461 | |||
462 | /** |
||
463 | * @param PassInterface $pass |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | private function preparePassDirectory(PassInterface $pass) |
||
479 | |||
480 | /** |
||
481 | * @param PassInterface $pass |
||
482 | * @param $passDir |
||
483 | */ |
||
484 | private function prepareImages(PassInterface $pass, $passDir) |
||
496 | |||
497 | /** |
||
498 | * @param PassInterface $pass |
||
499 | * @param $passDir |
||
500 | */ |
||
501 | private function prepareLocalizations(PassInterface $pass, $passDir) |
||
523 | |||
524 | |||
525 | } |
||
526 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.