Complex classes like Job 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 Job, and based on these observations, apply Extract Interface, too.
1 | <?php namespace JobApis\Jobs\Client; |
||
12 | class Job extends JobPosting implements JsonSerializable |
||
13 | { |
||
14 | use AttributeTrait, JsonLinkedDataTrait; |
||
15 | |||
16 | const SERIALIZE_STANDARD = "serialize-standard"; |
||
17 | |||
18 | const SERIALIZE_STANDARD_LD = "serialize-standard-ld"; |
||
19 | |||
20 | const SERIALIZE_CORE_SCHEMA_LD = "serialize-core-schema-ld"; |
||
21 | |||
22 | /** |
||
23 | * Job Company |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $company; |
||
28 | |||
29 | /** |
||
30 | * Job End Date |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $endDate; |
||
35 | |||
36 | /** |
||
37 | * Job Industry |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $industry; |
||
42 | |||
43 | /** |
||
44 | * Javascript Action |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $javascriptAction; |
||
49 | |||
50 | /** |
||
51 | * Javascript Function |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $javascriptFunction; |
||
56 | |||
57 | /** |
||
58 | * Job Location |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $location; |
||
63 | |||
64 | /** |
||
65 | * Job Maximum Salary |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $maximumSalary; |
||
70 | |||
71 | /** |
||
72 | * Job Minimum Salary |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $minimumSalary; |
||
77 | |||
78 | /** |
||
79 | * Job Query |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $query; |
||
84 | |||
85 | /** |
||
86 | * Job Source |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $source; |
||
91 | |||
92 | /** |
||
93 | * Job Id |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $sourceId; |
||
98 | |||
99 | /** |
||
100 | * Job Start Date |
||
101 | * |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $startDate; |
||
105 | |||
106 | /** |
||
107 | * Create new job |
||
108 | * |
||
109 | * @param array $attributes |
||
110 | */ |
||
111 | public function __construct($attributes = []) |
||
117 | |||
118 | /** |
||
119 | * Magic method to get protected property, if exists |
||
120 | * |
||
121 | * @param string $name |
||
122 | * |
||
123 | * @return mixed |
||
124 | * @throws \OutOfRangeException |
||
125 | */ |
||
126 | 48 | public function __get($name) |
|
127 | { |
||
128 | 48 | if (!property_exists($this, $name)) { |
|
129 | 2 | throw new \OutOfRangeException(sprintf( |
|
130 | 2 | '%s does not contain a property by the name of "%s"', |
|
131 | 2 | __CLASS__, |
|
132 | $name |
||
133 | 2 | )); |
|
134 | } |
||
135 | |||
136 | 46 | return $this->{$name}; |
|
137 | } |
||
138 | |||
139 | // Getters |
||
140 | |||
141 | /** |
||
142 | * Get city |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | 4 | public function getCity() |
|
153 | |||
154 | /** |
||
155 | * Get hiring organization description |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | 4 | public function getCompanyDescription() |
|
165 | |||
166 | /** |
||
167 | * Get hiring organization email |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 4 | public function getCompanyEmail() |
|
177 | |||
178 | /** |
||
179 | * Get hiring organization logo |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | 4 | public function getCompanyLogo() |
|
189 | |||
190 | /** |
||
191 | * Get hiring organization name |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | 6 | public function getCompanyName() |
|
201 | |||
202 | /** |
||
203 | * Get hiring organization url |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 4 | public function getCompanyUrl() |
|
213 | |||
214 | /** |
||
215 | * Get core schema types |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | 8 | public function getCoreSchemaTypes() |
|
223 | |||
224 | /** |
||
225 | * Get country |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 4 | public function getCountry() |
|
236 | |||
237 | /** |
||
238 | * Gets hiringOrganization. |
||
239 | * |
||
240 | * @return Organization |
||
241 | */ |
||
242 | 34 | public function getHiringOrganization($parent = false) |
|
250 | |||
251 | /** |
||
252 | * Gets jobLocation. |
||
253 | * |
||
254 | * @return Place |
||
255 | */ |
||
256 | 18 | public function getJobLocation($parent = false) |
|
264 | |||
265 | /** |
||
266 | * Gets latitude. |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | 2 | public function getLatitude() |
|
274 | |||
275 | /** |
||
276 | * Get linked data schema types |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | 8 | public function getLinkedDataSchemaTypes() |
|
284 | |||
285 | /** |
||
286 | * Get location |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | 2 | public function getLocation() |
|
294 | |||
295 | /** |
||
296 | * Gets longitude. |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | 2 | public function getLongitude() |
|
304 | |||
305 | /** |
||
306 | * Get minimum salary |
||
307 | * |
||
308 | * @return float |
||
309 | */ |
||
310 | 2 | public function getMinimumSalary() |
|
314 | |||
315 | /** |
||
316 | * Get postal code |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | 4 | public function getPostalCode() |
|
327 | |||
328 | /** |
||
329 | * Gets query. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | 2 | public function getQuery() |
|
337 | |||
338 | /** |
||
339 | * Gets source. |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | 2 | public function getSource() |
|
347 | |||
348 | /** |
||
349 | * Gets sourceId. |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | 4 | public function getSourceId() |
|
357 | |||
358 | /** |
||
359 | * Get state |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | 4 | public function getState() |
|
370 | |||
371 | /** |
||
372 | * Get street address |
||
373 | * |
||
374 | * @return string |
||
375 | */ |
||
376 | 6 | public function getStreetAddress() |
|
383 | |||
384 | /** |
||
385 | * Get telephone |
||
386 | * |
||
387 | * @return string |
||
388 | */ |
||
389 | 4 | public function getTelephone() |
|
395 | |||
396 | /** |
||
397 | * Allow class to be serialized with json_encode |
||
398 | * |
||
399 | * @param string $serializeSetting |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | 8 | public function jsonSerialize($serializeSetting = self::SERIALIZE_STANDARD) |
|
407 | |||
408 | // Setters |
||
409 | |||
410 | /** |
||
411 | * Sets baseSalary. |
||
412 | * |
||
413 | * @param float $baseSalary |
||
414 | * |
||
415 | * @return $this |
||
416 | */ |
||
417 | 14 | public function setBaseSalary($baseSalary) |
|
427 | |||
428 | /** |
||
429 | * Set city |
||
430 | * |
||
431 | * @param string $city |
||
432 | * |
||
433 | * @return $this |
||
434 | */ |
||
435 | 2 | public function setCity($city) |
|
439 | |||
440 | /** |
||
441 | * Set company (simple) |
||
442 | * |
||
443 | * @param string $company |
||
444 | * |
||
445 | * @return $this |
||
446 | */ |
||
447 | 6 | public function setCompany($company) |
|
453 | |||
454 | /** |
||
455 | * Set hiring organization description |
||
456 | * |
||
457 | * @param string $description |
||
458 | * |
||
459 | * @return $this |
||
460 | */ |
||
461 | 2 | public function setCompanyDescription($description) |
|
469 | |||
470 | /** |
||
471 | * Set hiring organization email |
||
472 | * |
||
473 | * @param string $email |
||
474 | * |
||
475 | * @return $this |
||
476 | */ |
||
477 | 2 | public function setCompanyEmail($email) |
|
485 | |||
486 | /** |
||
487 | * Set hiring organization logo url |
||
488 | * |
||
489 | * @param string $logo (image url) |
||
490 | * |
||
491 | * @return $this |
||
492 | */ |
||
493 | 2 | public function setCompanyLogo($logo) |
|
501 | |||
502 | /** |
||
503 | * Set hiring organization name |
||
504 | * |
||
505 | * @param string $companyName |
||
506 | * |
||
507 | * @return $this |
||
508 | */ |
||
509 | 10 | public function setCompanyName($companyName) |
|
517 | |||
518 | /** |
||
519 | * Set hiring organization url |
||
520 | * |
||
521 | * @param string $url |
||
522 | * |
||
523 | * @return $this |
||
524 | */ |
||
525 | 2 | public function setCompanyUrl($url) |
|
533 | |||
534 | /** |
||
535 | * Set country |
||
536 | * |
||
537 | * @param string $country |
||
538 | * |
||
539 | * @return $this |
||
540 | */ |
||
541 | 2 | public function setCountry($country) |
|
545 | |||
546 | /** |
||
547 | * Sets datePosted. |
||
548 | * |
||
549 | * @param string $datePosted |
||
550 | * |
||
551 | * @return $this |
||
552 | */ |
||
553 | 8 | public function setDatePostedAsString($datePosted) |
|
565 | |||
566 | /** |
||
567 | * Set latitude. |
||
568 | * |
||
569 | * @param string $latitude |
||
570 | * |
||
571 | * @return $this |
||
572 | */ |
||
573 | 2 | public function setLatitude($latitude) |
|
583 | |||
584 | /** |
||
585 | * Set location |
||
586 | * |
||
587 | * @param string $location |
||
588 | * |
||
589 | * @return $this |
||
590 | */ |
||
591 | 2 | public function setLocation($location) |
|
597 | |||
598 | /** |
||
599 | * Set longitude. |
||
600 | * |
||
601 | * @param string $longitude |
||
602 | * |
||
603 | * @return $this |
||
604 | */ |
||
605 | 2 | public function setLongitude($longitude) |
|
615 | |||
616 | /** |
||
617 | * Set minimum salary |
||
618 | * |
||
619 | * @param mixed $salary |
||
620 | * |
||
621 | * @return $this |
||
622 | */ |
||
623 | 2 | public function setMinimumSalary($salary) |
|
629 | |||
630 | /** |
||
631 | * Sets occupationalCategory with code and title as input |
||
632 | * |
||
633 | * @param string $code |
||
634 | * @param string $title |
||
635 | * |
||
636 | * @return $this |
||
637 | */ |
||
638 | 2 | public function setOccupationalCategoryWithCodeAndTitle($code, $title) |
|
646 | |||
647 | /** |
||
648 | * Set postal code |
||
649 | * |
||
650 | * @param string $postalCode |
||
651 | * |
||
652 | * @return $this |
||
653 | */ |
||
654 | 2 | public function setPostalCode($postalCode) |
|
658 | |||
659 | /** |
||
660 | * Sets query. |
||
661 | * |
||
662 | * @param string $query |
||
663 | * |
||
664 | * @return $this |
||
665 | */ |
||
666 | 6 | public function setQuery($query) |
|
672 | |||
673 | /** |
||
674 | * Sets source. |
||
675 | * |
||
676 | * @param string $source |
||
677 | * |
||
678 | * @return $this |
||
679 | */ |
||
680 | 6 | public function setSource($source) |
|
686 | |||
687 | /** |
||
688 | * Sets sourceId. |
||
689 | * |
||
690 | * @param string $sourceId |
||
691 | * |
||
692 | * @return $this |
||
693 | */ |
||
694 | 8 | public function setSourceId($sourceId) |
|
700 | |||
701 | /** |
||
702 | * Set state |
||
703 | * |
||
704 | * @param string $state |
||
705 | * |
||
706 | * @return $this |
||
707 | */ |
||
708 | 2 | public function setState($state) |
|
712 | |||
713 | /** |
||
714 | * Set street address |
||
715 | * |
||
716 | * @param string $streetAddress |
||
717 | * |
||
718 | * @return $this |
||
719 | */ |
||
720 | 4 | public function setStreetAddress($streetAddress) |
|
728 | |||
729 | /** |
||
730 | * Set telephone |
||
731 | * |
||
732 | * @param string $telephone |
||
733 | * |
||
734 | * @return $this |
||
735 | */ |
||
736 | 2 | public function setTelephone($telephone) |
|
746 | |||
747 | /** |
||
748 | * Serialize class as json |
||
749 | * |
||
750 | * @param string $serializeSetting |
||
751 | * |
||
752 | * @return string |
||
753 | */ |
||
754 | 8 | public function toJson($serializeSetting = self::SERIALIZE_STANDARD) |
|
758 | |||
759 | // Private Methods |
||
760 | |||
761 | /** |
||
762 | * Attempt to convert currency to float |
||
763 | * |
||
764 | * @param mixed $amount |
||
765 | * |
||
766 | * @return float|null |
||
767 | */ |
||
768 | 14 | private function convertCurrency($amount) |
|
778 | |||
779 | /** |
||
780 | * Gets hiring org if it exists or creates if it doesn't |
||
781 | * |
||
782 | * @return Organization |
||
783 | */ |
||
784 | 34 | private function getOrCreateHiringOrganization() |
|
794 | |||
795 | /** |
||
796 | * Gets jobLocation if it exists or creates if it doesn't |
||
797 | * |
||
798 | * @return Place |
||
799 | */ |
||
800 | 18 | private function getOrCreateJobLocation() |
|
812 | |||
813 | /** |
||
814 | * Gets address from location if it exists or creates if it doesn't |
||
815 | * |
||
816 | * @param Place $location |
||
817 | * |
||
818 | * @return PostalAddress |
||
819 | */ |
||
820 | 14 | private function getOrCreatePostalAddress(Place $location) |
|
830 | |||
831 | /** |
||
832 | * Attempt to update commonly shared address information |
||
833 | * |
||
834 | * @param string $attribute |
||
835 | * @param string $value |
||
836 | * |
||
837 | * @return $this |
||
838 | */ |
||
839 | 12 | private function updateAddressAttribute($attribute, $value) |
|
859 | } |
||
860 |