| Conditions | 33 |
| Paths | 47 |
| Total Lines | 96 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 101 | public function setParameter(string $key, mixed $value): void |
||
| 102 | { |
||
| 103 | switch ($key) { |
||
| 104 | // limited values |
||
| 105 | case 'action': |
||
| 106 | case 'lang': |
||
| 107 | $allowedValues = [ |
||
| 108 | 'action' => ['START', 'CONFIRM'], |
||
| 109 | 'lang' => ['NL', 'FR', 'EN', 'DE', 'Default'], |
||
| 110 | ]; |
||
| 111 | if (!in_array($value, $allowedValues[$key], true)) { |
||
| 112 | throw new BpostInvalidValueException($key, (string) $value, $allowedValues[$key]); |
||
| 113 | } |
||
| 114 | $this->parameters[$key] = $value; |
||
| 115 | break; |
||
| 116 | |||
| 117 | // maximum 2 chars |
||
| 118 | case 'customerCountry': |
||
| 119 | if (mb_strlen((string) $value) > 2) { |
||
| 120 | throw new BpostInvalidLengthException($key, mb_strlen((string) $value), 2); |
||
| 121 | } |
||
| 122 | $this->parameters[$key] = (string) $value; |
||
| 123 | break; |
||
| 124 | |||
| 125 | // maximum 8 chars |
||
| 126 | case 'customerStreetNumber': |
||
| 127 | case 'customerBox': |
||
| 128 | if (mb_strlen((string) $value) > 8) { |
||
| 129 | throw new BpostInvalidLengthException($key, mb_strlen((string) $value), 8); |
||
| 130 | } |
||
| 131 | $this->parameters[$key] = (string) $value; |
||
| 132 | break; |
||
| 133 | |||
| 134 | // maximum 20 chars |
||
| 135 | case 'customerPhoneNumber': |
||
| 136 | if (mb_strlen((string) $value) > 20) { |
||
| 137 | throw new BpostInvalidLengthException($key, mb_strlen((string) $value), 20); |
||
| 138 | } |
||
| 139 | $this->parameters[$key] = (string) $value; |
||
| 140 | break; |
||
| 141 | |||
| 142 | // maximum 32 chars |
||
| 143 | case 'customerPostalCode': |
||
| 144 | if (mb_strlen((string) $value) > 32) { |
||
| 145 | throw new BpostInvalidLengthException($key, mb_strlen((string) $value), 32); |
||
| 146 | } |
||
| 147 | $this->parameters[$key] = (string) $value; |
||
| 148 | break; |
||
| 149 | |||
| 150 | // maximum 40 chars |
||
| 151 | case 'customerFirstName': |
||
| 152 | case 'customerLastName': |
||
| 153 | case 'customerCompany': |
||
| 154 | case 'customerStreet': |
||
| 155 | case 'customerCity': |
||
| 156 | if (mb_strlen((string) $value) > 40) { |
||
| 157 | throw new BpostInvalidLengthException($key, mb_strlen((string) $value), 40); |
||
| 158 | } |
||
| 159 | $this->parameters[$key] = (string) $value; |
||
| 160 | break; |
||
| 161 | |||
| 162 | // maximum 50 chars |
||
| 163 | case 'orderReference': |
||
| 164 | case 'costCenter': |
||
| 165 | case 'customerEmail': |
||
| 166 | if (mb_strlen((string) $value) > 50) { |
||
| 167 | throw new BpostInvalidLengthException($key, mb_strlen((string) $value), 50); |
||
| 168 | } |
||
| 169 | $this->parameters[$key] = (string) $value; |
||
| 170 | break; |
||
| 171 | |||
| 172 | // integers |
||
| 173 | case 'orderTotalPrice': |
||
| 174 | case 'orderWeight': |
||
| 175 | $this->parameters[$key] = (int) $value; |
||
| 176 | break; |
||
| 177 | |||
| 178 | // array (order lines) |
||
| 179 | case 'orderLine': |
||
| 180 | $this->parameters[$key] ??= []; |
||
| 181 | $this->parameters[$key][] = $value; |
||
| 182 | break; |
||
| 183 | |||
| 184 | // unknown (free fields/URLs/flags/overrides...) |
||
| 185 | case 'deliveryMethodOverrides': |
||
| 186 | case 'extra': |
||
| 187 | case 'extraSecure': |
||
| 188 | case 'confirmUrl': |
||
| 189 | case 'cancelUrl': |
||
| 190 | case 'errorUrl': |
||
| 191 | default: |
||
| 192 | if (is_array($value)) { |
||
| 193 | // garantir un ordre stable pour le checksum |
||
| 194 | sort($value); |
||
| 195 | } |
||
| 196 | $this->parameters[$key] = $value; |
||
| 197 | } |
||
| 200 |