| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 164 |
| Code Lines | 108 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 60 | public function getconsent(Request $request, string $id): Template |
||
|
|
|||
| 61 | { |
||
| 62 | session_cache_limiter('nocache'); |
||
| 63 | |||
| 64 | Logger::info('Consent - getconsent: Accessing consent interface'); |
||
| 65 | |||
| 66 | $state = Auth\State::loadState($id, 'consent:request'); |
||
| 67 | |||
| 68 | if (is_null($state)) { |
||
| 69 | throw new Error\NoState(); |
||
| 70 | } elseif (array_key_exists('core:SP', $state)) { |
||
| 71 | $spentityid = $state['core:SP']; |
||
| 72 | } elseif (array_key_exists('saml:sp:State', $state)) { |
||
| 73 | $spentityid = $state['saml:sp:State']['core:SP']; |
||
| 74 | } else { |
||
| 75 | $spentityid = 'UNKNOWN'; |
||
| 76 | } |
||
| 77 | |||
| 78 | // The user has pressed the yes-button |
||
| 79 | if (array_key_exists('yes', $_REQUEST)) { |
||
| 80 | if (array_key_exists('saveconsent', $_REQUEST)) { |
||
| 81 | Logger::stats('consentResponse remember'); |
||
| 82 | } else { |
||
| 83 | Logger::stats('consentResponse rememberNot'); |
||
| 84 | } |
||
| 85 | |||
| 86 | $statsInfo = [ |
||
| 87 | 'remember' => array_key_exists('saveconsent', $_REQUEST), |
||
| 88 | ]; |
||
| 89 | if (isset($state['Destination']['entityid'])) { |
||
| 90 | $statsInfo['spEntityID'] = $state['Destination']['entityid']; |
||
| 91 | } |
||
| 92 | Stats::log('consent:accept', $statsInfo); |
||
| 93 | |||
| 94 | if ( |
||
| 95 | array_key_exists('consent:store', $state) |
||
| 96 | && array_key_exists('saveconsent', $_REQUEST) |
||
| 97 | && $_REQUEST['saveconsent'] === '1' |
||
| 98 | ) { |
||
| 99 | // Save consent |
||
| 100 | $store = $state['consent:store']; |
||
| 101 | $userId = $state['consent:store.userId']; |
||
| 102 | $targetedId = $state['consent:store.destination']; |
||
| 103 | $attributeSet = $state['consent:store.attributeSet']; |
||
| 104 | |||
| 105 | Logger::debug( |
||
| 106 | 'Consent - saveConsent() : [' . $userId . '|' . $targetedId . '|' . $attributeSet . ']' |
||
| 107 | ); |
||
| 108 | try { |
||
| 109 | $store->saveConsent($userId, $targetedId, $attributeSet); |
||
| 110 | } catch (Exception $e) { |
||
| 111 | Logger::error('Consent: Error writing to storage: ' . $e->getMessage()); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | Auth\ProcessingChain::resumeProcessing($state); |
||
| 116 | } |
||
| 117 | |||
| 118 | // Prepare attributes for presentation |
||
| 119 | $attributes = $state['Attributes']; |
||
| 120 | $noconsentattributes = $state['consent:noconsentattributes']; |
||
| 121 | |||
| 122 | // Remove attributes that do not require consent |
||
| 123 | foreach ($attributes as $attrkey => $attrval) { |
||
| 124 | if (in_array($attrkey, $noconsentattributes, true)) { |
||
| 125 | unset($attributes[$attrkey]); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | $para = [ |
||
| 129 | 'attributes' => &$attributes |
||
| 130 | ]; |
||
| 131 | |||
| 132 | // Reorder attributes according to attributepresentation hooks |
||
| 133 | Module::callHooks('attributepresentation', $para); |
||
| 134 | |||
| 135 | // Parse parameters |
||
| 136 | if (array_key_exists('name', $state['Source'])) { |
||
| 137 | $srcName = $state['Source']['name']; |
||
| 138 | } elseif (array_key_exists('OrganizationDisplayName', $state['Source'])) { |
||
| 139 | $srcName = $state['Source']['OrganizationDisplayName']; |
||
| 140 | } else { |
||
| 141 | $srcName = $state['Source']['entityid']; |
||
| 142 | } |
||
| 143 | |||
| 144 | if (array_key_exists('name', $state['Destination'])) { |
||
| 145 | $dstName = $state['Destination']['name']; |
||
| 146 | } elseif (array_key_exists('OrganizationDisplayName', $state['Destination'])) { |
||
| 147 | $dstName = $state['Destination']['OrganizationDisplayName']; |
||
| 148 | } else { |
||
| 149 | $dstName = $state['Destination']['entityid']; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Make, populate and layout consent form |
||
| 153 | $t = new Template($this->config, 'consent:consentform.twig'); |
||
| 154 | $translator = $t->getTranslator(); |
||
| 155 | $t->data['srcMetadata'] = $state['Source']; |
||
| 156 | $t->data['dstMetadata'] = $state['Destination']; |
||
| 157 | $t->data['yesTarget'] = Module::getModuleURL('consent/getconsent'); |
||
| 158 | $t->data['yesData'] = ['StateId' => $id]; |
||
| 159 | $t->data['noTarget'] = Module::getModuleURL('consent/noconsent'); |
||
| 160 | $t->data['noData'] = ['StateId' => $id]; |
||
| 161 | $t->data['attributes'] = $attributes; |
||
| 162 | $t->data['checked'] = $state['consent:checked']; |
||
| 163 | $t->data['stateId'] = $id; |
||
| 164 | |||
| 165 | $t->data['srcName'] = htmlspecialchars(is_array($srcName) ? $translator->getPreferredTranslation($srcName) : $srcName); |
||
| 166 | $t->data['dstName'] = htmlspecialchars(is_array($dstName) ? $translator->getPreferredTranslation($dstName) : $dstName); |
||
| 167 | |||
| 168 | if (array_key_exists('descr_purpose', $state['Destination'])) { |
||
| 169 | $t->data['dstDesc'] = $translator->getPreferredTranslation( |
||
| 170 | Utils\Arrays::arrayize( |
||
| 171 | $state['Destination']['descr_purpose'], |
||
| 172 | 'en' |
||
| 173 | ) |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | // Fetch privacypolicy |
||
| 178 | if ( |
||
| 179 | array_key_exists('UIInfo', $state['Destination']) && |
||
| 180 | array_key_exists('PrivacyStatementURL', $state['Destination']['UIInfo']) && |
||
| 181 | (!empty($state['Destination']['UIInfo']['PrivacyStatementURL'])) |
||
| 182 | ) { |
||
| 183 | $privacypolicy = reset($state['Destination']['UIInfo']['PrivacyStatementURL']); |
||
| 184 | } elseif ( |
||
| 185 | array_key_exists('UIInfo', $state['Source']) && |
||
| 186 | array_key_exists('PrivacyStatementURL', $state['Source']['UIInfo']) && |
||
| 187 | (!empty($state['Source']['UIInfo']['PrivacyStatementURL'])) |
||
| 188 | ) { |
||
| 189 | $privacypolicy = reset($state['Source']['UIInfo']['PrivacyStatementURL']); |
||
| 190 | } else { |
||
| 191 | $privacypolicy = false; |
||
| 192 | } |
||
| 193 | if ($privacypolicy !== false) { |
||
| 194 | $privacypolicy = str_replace( |
||
| 195 | '%SPENTITYID%', |
||
| 196 | urlencode($spentityid), |
||
| 197 | $privacypolicy |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | $t->data['sppp'] = $privacypolicy; |
||
| 201 | |||
| 202 | // Set focus element |
||
| 203 | switch ($state['consent:focus']) { |
||
| 204 | case 'yes': |
||
| 205 | $t->data['autofocus'] = 'yesbutton'; |
||
| 206 | break; |
||
| 207 | case 'no': |
||
| 208 | $t->data['autofocus'] = 'nobutton'; |
||
| 209 | break; |
||
| 210 | case null: |
||
| 211 | default: |
||
| 212 | break; |
||
| 213 | } |
||
| 214 | |||
| 215 | $t->data['usestorage'] = array_key_exists('consent:store', $state); |
||
| 216 | |||
| 217 | if (array_key_exists('consent:hiddenAttributes', $state)) { |
||
| 218 | $t->data['hiddenAttributes'] = $state['consent:hiddenAttributes']; |
||
| 219 | } else { |
||
| 220 | $t->data['hiddenAttributes'] = []; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $t; |
||
| 224 | } |
||
| 307 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.