Complex classes like Addresscheck 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Addresscheck, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Addresscheck |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * Response from the PAYONE addresscheck request |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $aResponse = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Determines if the request was NOT executed because the lifetime of the last was still valid |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $blIsLifetimeValid; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Saves if the address was corrected |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $addressCorrected; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * PAYONE addresscheck request model |
||
| 61 | * |
||
| 62 | * @var \Payone\Core\Model\Api\Request\Addresscheck |
||
| 63 | */ |
||
| 64 | protected $addresscheck; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * PAYONE database helper |
||
| 68 | * |
||
| 69 | * @var \Payone\Core\Helper\Database |
||
| 70 | */ |
||
| 71 | protected $databaseHelper; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * PAYONE toolkit helper |
||
| 75 | * |
||
| 76 | * @var \Payone\Core\Helper\Toolkit |
||
| 77 | */ |
||
| 78 | protected $toolkitHelper; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Checkout session |
||
| 82 | * |
||
| 83 | * @var \Magento\Checkout\Model\Session |
||
| 84 | */ |
||
| 85 | protected $checkoutSession; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Constructor |
||
| 89 | * |
||
| 90 | * @param \Payone\Core\Model\Api\Request\Addresscheck $addresscheck |
||
| 91 | * @param \Payone\Core\Helper\Database $databaseHelper |
||
| 92 | * @param \Payone\Core\Helper\Toolkit $toolkitHelper |
||
| 93 | * @param \Magento\Checkout\Model\Session $checkoutSession |
||
| 94 | */ |
||
| 95 | public function __construct( |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Return if check was not executed because the lifetime of the last check was still valid |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function isLifetimeStillValid() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Return addressCorrected property |
||
| 119 | * |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function isAddressCorrected() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get addresscheck config parameter |
||
| 129 | * |
||
| 130 | * @param string $sParam |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getConfigParam($sParam) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Correct a single address field if needed |
||
| 140 | * |
||
| 141 | * @param AddressInterface $oAddress |
||
| 142 | * @param array $aResponse |
||
| 143 | * @param string $sArrayKey |
||
| 144 | * @param string $sPropertyName |
||
| 145 | * @return AddressInterface |
||
| 146 | */ |
||
| 147 | protected function correctSingleField(AddressInterface $oAddress, $aResponse, $sArrayKey, $sPropertyName) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Change the address according to the response |
||
| 159 | * |
||
| 160 | * @param AddressInterface $oAddress |
||
| 161 | * @return AddressInterface |
||
| 162 | */ |
||
| 163 | public function correctAddress(AddressInterface $oAddress) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Check if the addresscheck needs to be executed for this quote |
||
| 193 | * |
||
| 194 | * @param bool $isBillingAddress |
||
| 195 | * @param bool $isVirtual |
||
| 196 | * @param double $dTotal |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function isCheckNeededForQuote($isBillingAddress, $isVirtual, $dTotal) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns the personstatus mapping from addresscheck admin config |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public function getPersonstatusMapping() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get formatted invalid message |
||
| 243 | * |
||
| 244 | * @param string $sCustomermessage |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getInvalidMessage($sCustomermessage) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Return error message |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getErrorMessage() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get error message by the given response |
||
| 276 | * |
||
| 277 | * @param array $aResponse |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getErrorMessageByResponse($aResponse) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Execute addresscheck and return the response |
||
| 297 | * |
||
| 298 | * @param AddressInterface $oAddress |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | |||
| 302 | /** |
||
| 303 | * |
||
| 304 | * @param AddressInterface $oAddress |
||
| 305 | * @return type |
||
| 306 | * @throws LocalizedException |
||
| 307 | */ |
||
| 308 | protected function handleAddresscheck(AddressInterface $oAddress) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get score from session or from a new addresscheck and add it to the address |
||
| 322 | * |
||
| 323 | * @param AddressInterface $oAddress |
||
| 324 | * @param Quote $oQuote |
||
| 325 | * @param bool $blIsBillingAddress |
||
| 326 | * @return AddressInterface |
||
| 327 | */ |
||
| 328 | public function handleAddressManagement(AddressInterface $oAddress, Quote $oQuote, $blIsBillingAddress = true) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get score from response or an old saved score from the database |
||
| 354 | * |
||
| 355 | * @param AddressInterface $oAddress |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function getScore(AddressInterface $oAddress) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Perform the PAYONE addresscheck request and return the response |
||
| 381 | * |
||
| 382 | * @param AddressInterface $oAddress |
||
| 383 | * @param bool $blIsBillingAddress |
||
| 384 | * @return array|bool |
||
| 385 | */ |
||
| 386 | public function getResponse(AddressInterface $oAddress, $blIsBillingAddress = false) |
||
| 396 | } |
||
| 397 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: