Conditions | 9 |
Paths | 8 |
Total Lines | 82 |
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 | // do nothing if the column `url_key` is empty |
||
62 | if ($this->hasValue(ColumnKeys::URL_KEY) === false) { |
||
63 | return; |
||
64 | } |
||
65 | |||
66 | // initialize the array for the artefacts and the store view codes |
||
67 | $this->artefacts = array(); |
||
68 | $storeViewCodes = array(); |
||
69 | |||
70 | // load the SKU from the row |
||
71 | $sku = $this->getValue(ColumnKeys::SKU); |
||
72 | |||
73 | // prepare the store view code |
||
74 | $this->getSubject()->prepareStoreViewCode(); |
||
75 | |||
76 | // try to load the store view code |
||
77 | $storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN); |
||
78 | |||
79 | // query whether or not we've a store view code |
||
80 | if ($storeViewCode === StoreViewCodes::ADMIN) { |
||
81 | // if not, load the websites the product is related with |
||
82 | $websiteCodes = $this->getValue(ColumnKeys::PRODUCT_WEBSITES, array(), array($this, 'explode')); |
||
83 | |||
84 | // load the store view codes of all websites |
||
85 | foreach ($websiteCodes as $websiteCode) { |
||
86 | $storeViewCodes = array_merge($storeViewCodes, $this->getStoreViewCodesByWebsiteCode($websiteCode)); |
||
87 | } |
||
88 | } else { |
||
89 | array_push($storeViewCodes, $storeViewCode); |
||
90 | } |
||
91 | |||
92 | // iterate over the available image fields |
||
93 | foreach ($storeViewCodes as $storeViewCode) { |
||
94 | // iterate over the store view codes and query if artefacts are already available |
||
95 | if ($this->hasArtefactsByTypeAndEntityId(ProductUrlRewriteObserver::ARTEFACT_TYPE, $lastEntityId = $this->getSubject()->getLastEntityId())) { |
||
|
|||
96 | // if yes, load the artefacs |
||
97 | $this->artefacts = $this->getArtefactsByTypeAndEntityId(ProductUrlRewriteObserver::ARTEFACT_TYPE, $lastEntityId); |
||
98 | |||
99 | // override the existing data with the store view specific one |
||
100 | for ($i = 0; $i < sizeof($this->artefacts); $i++) { |
||
101 | // query whether or not a URL key has be specfied and the store view codes are equal |
||
102 | if ($this->hasValue(ColumnKeys::URL_KEY) && $this->artefacts[$i][ColumnKeys::STORE_VIEW_CODE] === $storeViewCode) { |
||
103 | // update the URL key |
||
104 | $this->artefacts[$i][ColumnKeys::URL_KEY] = $this->getValue(ColumnKeys::URL_KEY); |
||
105 | |||
106 | // also update filename and line number |
||
107 | $this->artefacts[$i][ColumnKeys::ORIGINAL_DATA][ColumnKeys::ORIGINAL_FILENAME] = $this->getSubject()->getFilename(); |
||
108 | $this->artefacts[$i][ColumnKeys::ORIGINAL_DATA][ColumnKeys::ORIGINAL_LINE_NUMBER] = $this->getSubject()->getLineNumber(); |
||
109 | } |
||
110 | } |
||
111 | } else { |
||
112 | // if no arefacts are available, append new data |
||
113 | $artefact = $this->newArtefact( |
||
114 | array( |
||
115 | ColumnKeys::SKU => $sku, |
||
116 | ColumnKeys::STORE_VIEW_CODE => $storeViewCode, |
||
117 | ColumnKeys::CATEGORIES => $this->getValue(ColumnKeys::CATEGORIES), |
||
118 | ColumnKeys::PRODUCT_WEBSITES => $this->getValue(ColumnKeys::PRODUCT_WEBSITES), |
||
119 | ColumnKeys::VISIBILITY => $this->getValue(ColumnKeys::VISIBILITY), |
||
120 | ColumnKeys::URL_KEY => $this->getValue(ColumnKeys::URL_KEY) |
||
121 | ), |
||
122 | array( |
||
123 | ColumnKeys::SKU => ColumnKeys::SKU, |
||
124 | ColumnKeys::STORE_VIEW_CODE => ColumnKeys::STORE_VIEW_CODE, |
||
125 | ColumnKeys::CATEGORIES => ColumnKeys::CATEGORIES, |
||
126 | ColumnKeys::PRODUCT_WEBSITES => ColumnKeys::PRODUCT_WEBSITES, |
||
127 | ColumnKeys::VISIBILITY => ColumnKeys::VISIBILITY, |
||
128 | ColumnKeys::URL_KEY => ColumnKeys::URL_KEY, |
||
129 | ) |
||
130 | ); |
||
131 | |||
132 | // append the base image to the artefacts |
||
133 | $this->artefacts[] = $artefact; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | // append the artefacts that has to be exported to the subject |
||
138 | $this->addArtefacts($this->artefacts); |
||
139 | } |
||
140 | |||
207 |
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: