| Conditions | 8 |
| Paths | 7 |
| Total Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 58 | protected function process() |
||
| 59 | { |
||
| 60 | |||
| 61 | // initialize the array for the artefacts and the store view codes |
||
| 62 | $this->artefacts = array(); |
||
| 63 | $storeViewCodes = array(); |
||
| 64 | |||
| 65 | // load the SKU from the row |
||
| 66 | $sku = $this->getValue(ColumnKeys::SKU); |
||
| 67 | |||
| 68 | // prepare the store view code |
||
| 69 | $this->getSubject()->prepareStoreViewCode(); |
||
| 70 | |||
| 71 | // try to load the store view code |
||
| 72 | $storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN); |
||
| 73 | |||
| 74 | // query whether or not we've a store view code |
||
| 75 | if ($storeViewCode === StoreViewCodes::ADMIN) { |
||
| 76 | // if not, load the websites the product is related with |
||
| 77 | $websiteCodes = $this->getValue(ColumnKeys::PRODUCT_WEBSITES, array(), array($this, 'explode')); |
||
| 78 | |||
| 79 | // load the store view codes of all websites |
||
| 80 | foreach ($websiteCodes as $websiteCode) { |
||
| 81 | $storeViewCodes = array_merge($storeViewCodes, $this->getStoreViewCodesByWebsiteCode($websiteCode)); |
||
| 82 | } |
||
| 83 | } else { |
||
| 84 | array_push($storeViewCodes, $storeViewCode); |
||
| 85 | } |
||
| 86 | |||
| 87 | // iterate over the available image fields |
||
| 88 | foreach ($storeViewCodes as $storeViewCode) { |
||
| 89 | // iterate over the store view codes and query if artefacts are already available |
||
| 90 | if ($this->hasArtefactsByTypeAndEntityId(ProductUrlRewriteObserver::ARTEFACT_TYPE, $lastEntityId = $this->getSubject()->getLastEntityId())) { |
||
|
|
|||
| 91 | // if yes, load the artefacs |
||
| 92 | $this->artefacts = $this->getArtefactsByTypeAndEntityId(ProductUrlRewriteObserver::ARTEFACT_TYPE, $lastEntityId); |
||
| 93 | |||
| 94 | // override the existing data with the store view specific one |
||
| 95 | for ($i = 0; $i < sizeof($this->artefacts); $i++) { |
||
| 96 | // query whether or not a URL key has be specfied and the store view codes are equal |
||
| 97 | if ($this->hasValue(ColumnKeys::URL_KEY) && $this->artefacts[$i][ColumnKeys::STORE_VIEW_CODE] === $storeViewCode) { |
||
| 98 | // update the URL key |
||
| 99 | $this->artefacts[$i][ColumnKeys::URL_KEY] = $this->getValue(ColumnKeys::URL_KEY); |
||
| 100 | |||
| 101 | // also update filename and line number |
||
| 102 | $this->artefacts[$i][ColumnKeys::ORIGINAL_DATA][ColumnKeys::ORIGINAL_FILENAME] = $this->getSubject()->getFilename(); |
||
| 103 | $this->artefacts[$i][ColumnKeys::ORIGINAL_DATA][ColumnKeys::ORIGINAL_LINE_NUMBER] = $this->getSubject()->getLineNumber(); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } else { |
||
| 107 | // if no arefacts are available, append new data |
||
| 108 | $artefact = $this->newArtefact( |
||
| 109 | array( |
||
| 110 | ColumnKeys::SKU => $sku, |
||
| 111 | ColumnKeys::STORE_VIEW_CODE => $storeViewCode, |
||
| 112 | ColumnKeys::CATEGORIES => $this->getValue(ColumnKeys::CATEGORIES), |
||
| 113 | ColumnKeys::PRODUCT_WEBSITES => $this->getValue(ColumnKeys::PRODUCT_WEBSITES), |
||
| 114 | ColumnKeys::VISIBILITY => $this->getValue(ColumnKeys::VISIBILITY), |
||
| 115 | ColumnKeys::URL_KEY => $this->getValue(ColumnKeys::URL_KEY) |
||
| 116 | ), |
||
| 117 | array( |
||
| 118 | ColumnKeys::SKU => ColumnKeys::SKU, |
||
| 119 | ColumnKeys::STORE_VIEW_CODE => ColumnKeys::STORE_VIEW_CODE, |
||
| 120 | ColumnKeys::CATEGORIES => ColumnKeys::CATEGORIES, |
||
| 121 | ColumnKeys::PRODUCT_WEBSITES => ColumnKeys::PRODUCT_WEBSITES, |
||
| 122 | ColumnKeys::VISIBILITY => ColumnKeys::VISIBILITY, |
||
| 123 | ColumnKeys::URL_KEY => ColumnKeys::URL_KEY, |
||
| 124 | ) |
||
| 125 | ); |
||
| 126 | |||
| 127 | // append the base image to the artefacts |
||
| 128 | $this->artefacts[] = $artefact; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // append the artefacts that has to be exported to the subject |
||
| 133 | $this->addArtefacts($this->artefacts); |
||
| 134 | } |
||
| 135 | |||
| 202 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: