| Total Complexity | 45 |
| Total Lines | 240 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Tree 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 Tree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Tree |
||
| 22 | { |
||
| 23 | private $role; |
||
| 24 | private $username; |
||
| 25 | private $studyObject; |
||
| 26 | private $linkpdo; |
||
| 27 | |||
| 28 | public function __construct(string $role, string $username, string $study, PDO $linkpdo) |
||
| 29 | { |
||
| 30 | $this->linkpdo=$linkpdo; |
||
| 31 | $this->role=$role; |
||
| 32 | $this->username=$username; |
||
| 33 | $this->studyObject=new Study($study, $linkpdo); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Determine class value of Investigator and Controller visit item |
||
| 38 | * to make specific color decoration depending on status of visit |
||
| 39 | */ |
||
| 40 | private function determineClassOfVisit(Visit $visitObject): String |
||
| 41 | { |
||
| 42 | |||
| 43 | if ($this->role == User::INVESTIGATOR) { |
||
| 44 | |||
| 45 | //Add upload status / user form in class |
||
| 46 | if ($visitObject->statusDone == Visit::DONE && $visitObject->uploadStatus == Visit::NOT_DONE && $visitObject->stateInvestigatorForm != Visit::DONE) { |
||
| 47 | $class="NotBoth"; |
||
| 48 | }else if ($visitObject->statusDone == Visit::DONE && $visitObject->stateInvestigatorForm != Visit::DONE) { |
||
| 49 | $class="NotForm"; |
||
| 50 | }else if ($visitObject->statusDone == Visit::DONE && $visitObject->uploadStatus == Visit::NOT_DONE) { |
||
| 51 | $class="NotUpload"; |
||
| 52 | }else { |
||
| 53 | $class="OK"; |
||
| 54 | } |
||
| 55 | }else if ($this->role == User::CONTROLLER) { |
||
| 56 | if ($visitObject->stateQualityControl == Visit::QC_ACCEPTED || $visitObject->stateQualityControl == Visit::QC_REFUSED) { |
||
| 57 | $class="OK"; |
||
| 58 | }else if ($visitObject->stateQualityControl == Visit::QC_NOT_DONE || $visitObject->stateQualityControl == Visit::QC_WAIT_DEFINITVE_CONCLUSION) { |
||
| 59 | $class="NotBoth"; |
||
| 60 | } |
||
| 61 | }else if ($this->role == User::REVIEWER) { |
||
| 62 | //Add status of review process (need to remove space from status string) |
||
| 63 | $class=str_replace(" ", "", $visitObject->reviewStatus); ; |
||
| 64 | } |
||
| 65 | |||
| 66 | return $class; |
||
|
|
|||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Create visit entry in Tree from a visit Object |
||
| 71 | */ |
||
| 72 | private function visitObjectToTreeObject(Visit $visitObject) |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Create a patient entry in Tree |
||
| 93 | */ |
||
| 94 | private function patientObjectToTreeObject(String $patientCode) |
||
| 95 | { |
||
| 96 | |||
| 97 | $jsonPatientLevel['id']=$patientCode; |
||
| 98 | $jsonPatientLevel['parent']='#'; |
||
| 99 | $jsonPatientLevel['icon']='/assets/images/person-icon.png'; |
||
| 100 | $jsonPatientLevel['text']=$patientCode; |
||
| 101 | $jsonPatientLevel['level']='patient'; |
||
| 102 | $jsonPatientLevel['state']['opened']=false; |
||
| 103 | |||
| 104 | return $jsonPatientLevel; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Create a Visit group entry in tree |
||
| 109 | */ |
||
| 110 | private function visitGroupToTreeObject($patientCode, $groupModality) |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * sort Visits in key by modality |
||
| 126 | */ |
||
| 127 | private function processVisitsArray($visitsArray) |
||
| 128 | { |
||
| 129 | |||
| 130 | $sortedModalities=[]; |
||
| 131 | |||
| 132 | foreach ($visitsArray as $visitObject) { |
||
| 133 | $sortedModalities[$visitObject->visitGroupObject->groupModality][]=$visitObject; |
||
| 134 | } |
||
| 135 | |||
| 136 | return $sortedModalities; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Select visit from patients for some roles |
||
| 141 | */ |
||
| 142 | private function makeTreeFromPatients($patientsArray) |
||
| 143 | { |
||
| 144 | |||
| 145 | $resultTree=[]; |
||
| 146 | foreach ($patientsArray as $patientObject) { |
||
| 147 | //If investigator display all created visits |
||
| 148 | if ($this->role == User::INVESTIGATOR) $visitsArray=$patientObject->getAllCreatedPatientsVisits(); |
||
| 149 | //if Reviewer display all QC accepted visits |
||
| 150 | if ($this->role == User::REVIEWER) $visitsArray=$patientObject->getAllQcDonePatientsVisits(); |
||
| 151 | $stortedVisits=$this->processVisitsArray($visitsArray); |
||
| 152 | $resultTree[$patientObject->patientCode]['patientObject']=$patientObject; |
||
| 153 | $resultTree[$patientObject->patientCode]['modalities']=$stortedVisits; |
||
| 154 | } |
||
| 155 | |||
| 156 | return $resultTree; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Sort an array of Visits by patient code |
||
| 161 | */ |
||
| 162 | private function makeTreeFromVisits($visitsArray) |
||
| 163 | { |
||
| 164 | |||
| 165 | $resultTree=[]; |
||
| 166 | |||
| 167 | foreach ($visitsArray as $visitObject) { |
||
| 168 | $resultTree[$visitObject->patientCode]['patientObject']=$visitObject->getPatient(); |
||
| 169 | $resultTree[$visitObject->patientCode]['modalities'][$visitObject->visitGroupObject->groupModality][]=$visitObject; |
||
| 170 | } |
||
| 171 | |||
| 172 | return $resultTree; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Generate the final JSON tree by adding patient, modality and visit items |
||
| 177 | */ |
||
| 178 | private function treeStructuretoJsonTree($treeStructure) |
||
| 179 | { |
||
| 180 | $jsonTree=[]; |
||
| 181 | foreach ($treeStructure as $patientCode => $patientData) { |
||
| 182 | $jsonTree[]=$this->patientObjectToTreeObject($patientCode); |
||
| 183 | foreach ($patientData['modalities'] as $modality => $visitObjects) { |
||
| 184 | $jsonTree[]=$this->visitGroupToTreeObject($patientCode, $modality); |
||
| 185 | foreach ($visitObjects as $visitObject) { |
||
| 186 | $jsonTree[]=$this->visitObjectToTreeObject($visitObject); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | return $jsonTree; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return JSON for JSTree according to role (patient + Visit) |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public function buildTree() |
||
| 261 | } |
||
| 262 | } |
||
| 263 |