| Conditions | 7 |
| Paths | 9 |
| Total Lines | 87 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 61 | public function parseXml(XmlObject $xmlRoot): array |
||
| 62 | { |
||
| 63 | /** @var array<XmlMandate> */ |
||
| 64 | $mandates = []; |
||
| 65 | |||
| 66 | $stringValidator = new StringValidator(); |
||
| 67 | |||
| 68 | foreach ($xmlRoot->getElements('/DocumentElement/MedgivandeViaHemsida') as $xmlSource) { |
||
| 69 | // Validate payee organisational number |
||
| 70 | $orgNr = $xmlSource->readElement('/MedgivandeViaHemsida/Organisationsnr', new IdValidator()); |
||
| 71 | if ($this->payeeOrgNr->format('S-sk') != $orgNr) { |
||
| 72 | // Hard failure, implicit rollback |
||
| 73 | throw new InvalidDataException(sprintf( |
||
| 74 | 'Invalid payee org nr %s, expecting %s', |
||
| 75 | $orgNr, |
||
| 76 | $this->payeeOrgNr->format('S-sk') |
||
| 77 | )); |
||
| 78 | } |
||
| 79 | |||
| 80 | // Validate payee bankgiro account number |
||
| 81 | $bankgiro = $xmlSource->readElement('/MedgivandeViaHemsida/Bankgironr', new AccountValidator()); |
||
| 82 | if (preg_replace('/\D/', '', $this->payeeBankgiro->getNumber()) != preg_replace('/\D/', '', $bankgiro)) { |
||
| 83 | // Hard failure, implicit rollback |
||
| 84 | throw new InvalidDataException(sprintf( |
||
| 85 | 'Invalid payee bankgiro %s, expecting %s', |
||
| 86 | $bankgiro, |
||
| 87 | $this->payeeBankgiro->getNumber() |
||
| 88 | )); |
||
| 89 | } |
||
| 90 | |||
| 91 | // require this empty element to exist |
||
| 92 | $xmlSource->readElement('/MedgivandeViaHemsida/Autogiroanmälan_x002C__x0020_medgivande', $stringValidator); |
||
| 93 | |||
| 94 | $mandate = new XmlMandate(); |
||
| 95 | |||
| 96 | $mandate->donorId = $this->idFactory->createId( |
||
| 97 | $xmlSource->readElement('/MedgivandeViaHemsida/Kontoinnehavarens_x0020_personnr', new IdValidator()) |
||
| 98 | ); |
||
| 99 | |||
| 100 | if ($xmlSource->hasElement('/MedgivandeViaHemsida/Betalarnummer')) { |
||
| 101 | try { |
||
| 102 | $mandate->payerNumber = $xmlSource->readElement( |
||
| 103 | '/MedgivandeViaHemsida/Betalarnummer', |
||
| 104 | new PayerNumberValidator() |
||
| 105 | ); |
||
| 106 | } catch (ValidatorException $e) { |
||
| 107 | // intentionally empty |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $mandate->account = $this->accountFactory->createAccount( |
||
| 112 | $xmlSource->readElement('/MedgivandeViaHemsida/Kontonr', new AccountValidator()) |
||
| 113 | ); |
||
| 114 | |||
| 115 | $mandate->name = $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_namn', $stringValidator); |
||
| 116 | |||
| 117 | $mandate->address = [ |
||
| 118 | 'line1' => |
||
| 119 | $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_adress_1', $stringValidator), |
||
| 120 | 'line2' => |
||
| 121 | $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_adress_2', $stringValidator), |
||
| 122 | 'line3' => |
||
| 123 | $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_adress_3', $stringValidator), |
||
| 124 | 'postalCode' => |
||
| 125 | $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_postnr', new PostalCodeValidator()), |
||
| 126 | 'postalCity' => |
||
| 127 | $xmlSource->readElement('/MedgivandeViaHemsida/Betalares_x0020_postort', $stringValidator) |
||
| 128 | ]; |
||
| 129 | |||
| 130 | foreach ($xmlSource->getElements('/MedgivandeViaHemsida/Övrig_x0020_info/customdata') as $custom) { |
||
| 131 | $mandate->attributes[$custom->readElement('/customdata/name', $stringValidator)] |
||
| 132 | = $custom->readElement('/customdata/value', $stringValidator); |
||
| 133 | } |
||
| 134 | |||
| 135 | $mandate->attributes['online_form_id'] |
||
| 136 | = $xmlSource->readElement('/MedgivandeViaHemsida/Formulärnamn', $stringValidator); |
||
| 137 | |||
| 138 | $mandate->attributes['online_verification_time'] |
||
| 139 | = $xmlSource->readElement('/MedgivandeViaHemsida/Verifieringstid', $stringValidator); |
||
| 140 | |||
| 141 | $mandate->attributes['online_verification_code'] |
||
| 142 | = $xmlSource->readElement('/MedgivandeViaHemsida/Verifieringsreferens', $stringValidator); |
||
| 143 | |||
| 144 | $mandates[] = $mandate; |
||
| 145 | } |
||
| 146 | |||
| 147 | return $mandates; |
||
| 148 | } |
||
| 150 |