| Total Complexity | 48 |
| Total Lines | 184 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like VolunteerShift 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.
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 VolunteerShift, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class VolunteerShift extends VolunteerObject |
||
| 17 | { |
||
| 18 | protected static $deptCache = array(); |
||
| 19 | protected static $roleCache = array(); |
||
| 20 | protected static $eventCache = array(); |
||
| 21 | protected $mod = null; |
||
| 22 | protected $myStart = null; |
||
| 23 | protected $myEnd = null; |
||
| 24 | protected $modStart = null; |
||
| 25 | protected $modEnd = null; |
||
| 26 | protected $participantObj = null; |
||
| 27 | protected $webParticipantName = null; |
||
| 28 | |||
| 29 | public function __construct($shiftID, $dbData = null) |
||
| 30 | { |
||
| 31 | parent::__construct($shiftID, $dbData, 'shifts', '_id'); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function __get($propName) |
||
| 35 | { |
||
| 36 | switch($propName) |
||
| 37 | { |
||
| 38 | case 'modTime': |
||
| 39 | if($this->mod === null) |
||
| 40 | { |
||
| 41 | $this->mod = new \DateInterval('PT'.strval($this->role->down_time).'H'); |
||
|
|
|||
| 42 | } |
||
| 43 | return $this->mod; |
||
| 44 | case 'startTime': |
||
| 45 | if($this->myStart === null) |
||
| 46 | { |
||
| 47 | $this->myStart = new \DateTime($this->dbData['startTime']); |
||
| 48 | } |
||
| 49 | return $this->myStart; |
||
| 50 | case 'startTimeWithMod': |
||
| 51 | if($this->modStart === null) |
||
| 52 | { |
||
| 53 | $this->modStart = clone $this->startTime; |
||
| 54 | $this->modStart->sub($this->modTime); |
||
| 55 | } |
||
| 56 | return $this->modStart; |
||
| 57 | case 'endTime': |
||
| 58 | if($this->myEnd === null) |
||
| 59 | { |
||
| 60 | $this->myEnd = new \DateTime($this->dbData['endTime']); |
||
| 61 | } |
||
| 62 | return $this->myEnd; |
||
| 63 | case 'endTimeWithMod': |
||
| 64 | if($this->modEnd === null) |
||
| 65 | { |
||
| 66 | $this->modEnd = clone $this->endTime; |
||
| 67 | $this->modEnd->add($this->modTime); |
||
| 68 | } |
||
| 69 | return $this->modEnd; |
||
| 70 | case 'department': |
||
| 71 | if(!isset(self::$deptCache[$this->dbData['departmentID']])) |
||
| 72 | { |
||
| 73 | self::$deptCache[$this->dbData['departmentID']] = new \VolunteerDepartment($this->dbData['departmentID']); |
||
| 74 | } |
||
| 75 | return self::$deptCache[$this->dbData['departmentID']]; |
||
| 76 | case 'role': |
||
| 77 | if(!isset(self::$roleCache[$this->dbData['roleID']])) |
||
| 78 | { |
||
| 79 | self::$roleCache[$this->dbData['roleID']] = new \VolunteerRole($this->dbData['roleID']); |
||
| 80 | } |
||
| 81 | return self::$roleCache[$this->dbData['roleID']]; |
||
| 82 | case 'event': |
||
| 83 | if(!isset(self::$eventCache[$this->dbData['eventID']])) |
||
| 84 | { |
||
| 85 | self::$eventCache[$this->dbData['eventID']] = new \VolunteerEvent($this->dbData['eventID']); |
||
| 86 | } |
||
| 87 | return self::$eventCache[$this->dbData['eventID']]; |
||
| 88 | case 'participantObj': |
||
| 89 | if($this->participantObj === null) |
||
| 90 | { |
||
| 91 | if(isset($this->dbData['participant']) && $this->dbData['participant'] !== '' && $this->dbData['participant'] !== '/dev/null') |
||
| 92 | { |
||
| 93 | $this->participantObj = new \VolunteerProfile($this->dbData['participant']); |
||
| 94 | } |
||
| 95 | else |
||
| 96 | { |
||
| 97 | $this->participantObj = false; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | return $this->participantObj; |
||
| 101 | case 'webParticipantName': |
||
| 102 | if($this->webParticipantName === null) |
||
| 103 | { |
||
| 104 | if(isset($this->dbData['participant'])) |
||
| 105 | { |
||
| 106 | $tmp = new \VolunteerProfile($this->dbData['participant']); |
||
| 107 | $this->webParticipantName = $tmp->getDisplayName(); |
||
| 108 | } |
||
| 109 | else |
||
| 110 | { |
||
| 111 | $this->webParticipantName = ""; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | return $this->webParticipantName; |
||
| 115 | case 'scheduleParticipantName': |
||
| 116 | if($this->scheduleParticipantName === null) |
||
| 117 | { |
||
| 118 | if(isset($this->dbData['participant'])) |
||
| 119 | { |
||
| 120 | $tmp = new \VolunteerProfile($this->dbData['participant']); |
||
| 121 | $this->scheduleParticipantName = $tmp->getDisplayName('paperName'); |
||
| 122 | } |
||
| 123 | else |
||
| 124 | { |
||
| 125 | $this->scheduleParticipantName = ""; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | return $this->scheduleParticipantName; |
||
| 129 | default: |
||
| 130 | return $this->dbData[$propName]; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | public function isSame($shift) |
||
| 135 | { |
||
| 136 | return $this->dbData['_id'] === $shift->dbData['_id']; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function overlaps($shift) |
||
| 154 | } |
||
| 155 | |||
| 156 | public function isFilled() |
||
| 159 | } |
||
| 160 | |||
| 161 | public function findOverlaps($uid, $shortCircuit = false) |
||
| 162 | { |
||
| 163 | static $userShifts = null; |
||
| 164 | static $count = 0; |
||
| 165 | if($userShifts === null) |
||
| 166 | { |
||
| 167 | $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
||
| 168 | $filter = new \Data\Filter("participant eq '$uid'"); |
||
| 169 | $userShifts = $dataTable->read($filter); |
||
| 170 | $count = count($userShifts); |
||
| 171 | for($i = 0; $i < $count; $i++) |
||
| 172 | { |
||
| 173 | $userShifts[$i] = new VolunteerShift(false, $userShifts[$i]); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | $res = array(); |
||
| 177 | for($i = 0; $i < $count; $i++) |
||
| 178 | { |
||
| 179 | if($this->overlaps($userShifts[$i])) |
||
| 180 | { |
||
| 181 | if($shortCircuit === true) |
||
| 182 | { |
||
| 183 | return true; |
||
| 184 | } |
||
| 185 | array_push($res, $userShifts[$i]); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | if($shortCircuit === true) |
||
| 189 | { |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | return $res; |
||
| 193 | } |
||
| 194 | |||
| 195 | public function makeCopy($dataTable) |
||
| 200 | } |
||
| 201 | } |
||
| 202 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 203 |