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