Conditions | 14 |
Paths | 25 |
Total Lines | 103 |
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 |
||
99 | protected function process() |
||
100 | { |
||
101 | $product = null; |
||
102 | // initialize the array for the artefacts and the store view codes |
||
103 | $this->artefacts = array(); |
||
104 | $storeViewCodes = array(); |
||
105 | |||
106 | // load the SKU from the row |
||
107 | $sku = $this->getValue(ColumnKeys::SKU); |
||
108 | |||
109 | // prepare the store view code |
||
110 | $this->getSubject()->prepareStoreViewCode(); |
||
111 | |||
112 | // try to load the store view code |
||
113 | $storeViewCodeValue = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN); |
||
114 | |||
115 | // query whether or not we've a store view code |
||
116 | if ($storeViewCodeValue === StoreViewCodes::ADMIN) { |
||
117 | |||
118 | // load product to see if already exist or new |
||
119 | $product = $this->loadProduct($this->getValue(ColumnKeys::SKU)); |
||
120 | |||
121 | // Init admin row for memory overload |
||
122 | $this->adminRow = array(); |
||
123 | // remember the admin row on SKU to be safe on later process |
||
124 | $this->adminRow[$sku] = array( |
||
125 | ColumnKeys::CATEGORIES => $this->getValue(ColumnKeys::CATEGORIES), |
||
126 | ColumnKeys::PRODUCT_WEBSITES => $this->getValue(ColumnKeys::PRODUCT_WEBSITES), |
||
127 | ColumnKeys::VISIBILITY => $this->getValue(ColumnKeys::VISIBILITY), |
||
128 | ColumnKeys::URL_KEY => $this->getValue(ColumnKeys::URL_KEY) |
||
129 | ); |
||
130 | |||
131 | // if not, load the websites the product is related with |
||
132 | $websiteCodes = $this->getValue(ColumnKeys::PRODUCT_WEBSITES, array(), array($this, 'explode')); |
||
133 | |||
134 | // load the store view codes of all websites |
||
135 | foreach ($websiteCodes as $websiteCode) { |
||
136 | $storeViewCodes = array_merge($storeViewCodes, $this->getStoreViewCodesByWebsiteCode($websiteCode)); |
||
137 | } |
||
138 | } else { |
||
139 | array_push($storeViewCodes, $storeViewCodeValue); |
||
140 | } |
||
141 | |||
142 | // iterate over the available image fields |
||
143 | foreach ($storeViewCodes as $storeViewCode) { |
||
144 | // iterate over the store view codes and query if artefacts are already available |
||
145 | if ($this->hasArtefactsByTypeAndEntityId(ProductUrlRewriteObserver::ARTEFACT_TYPE, $lastEntityId = $this->getSubject()->getLastEntityId())) { |
||
|
|||
146 | // if yes, load the artefacs |
||
147 | $this->artefacts = $this->getArtefactsByTypeAndEntityId(ProductUrlRewriteObserver::ARTEFACT_TYPE, $lastEntityId); |
||
148 | |||
149 | $foundArtefactToUpdate = false; |
||
150 | // override the existing data with the store view specific one |
||
151 | for ($i = 0; $i < sizeof($this->artefacts); $i++) { |
||
152 | // query whether or not a URL key has be specfied and the store view codes are equal |
||
153 | if ($this->hasValue(ColumnKeys::URL_KEY) && $this->artefacts[$i][ColumnKeys::STORE_VIEW_CODE] === $storeViewCode) { |
||
154 | $foundArtefactToUpdate = true; |
||
155 | // update the URL key |
||
156 | $this->artefacts[$i][ColumnKeys::URL_KEY] = $this->getValue(ColumnKeys::URL_KEY); |
||
157 | // update the visibility, if available |
||
158 | if ($this->hasValue(ColumnKeys::VISIBILITY)) { |
||
159 | $this->artefacts[$i][ColumnKeys::VISIBILITY] = $this->getValue(ColumnKeys::VISIBILITY); |
||
160 | } |
||
161 | |||
162 | // also update filename and line number |
||
163 | $this->artefacts[$i][ColumnKeys::ORIGINAL_DATA][ColumnKeys::ORIGINAL_FILENAME] = $this->getSubject()->getFilename(); |
||
164 | $this->artefacts[$i][ColumnKeys::ORIGINAL_DATA][ColumnKeys::ORIGINAL_LINE_NUMBER] = $this->getSubject()->getLineNumber(); |
||
165 | } |
||
166 | } |
||
167 | if (!$foundArtefactToUpdate) { |
||
168 | // if no arefacts are available, append new data |
||
169 | $this->createArtefact($sku, $storeViewCode); |
||
170 | } |
||
171 | } else { |
||
172 | // On admin row and existing product check if url_key in database |
||
173 | if ($storeViewCodeValue === StoreViewCodes::ADMIN && $product) { |
||
174 | // initialize last entity as primary key |
||
175 | $pk = $this->getPrimaryKeyId($product); |
||
176 | // initialize the entity type ID |
||
177 | $entityType = $this->getSubject()->getEntityType(); |
||
178 | $entityTypeId = (integer) $entityType[MemberNames::ENTITY_TYPE_ID]; |
||
179 | // initialize store ID from store code |
||
180 | $storeId = $this->getSubject()->getRowStoreId($storeViewCode); |
||
181 | // take a look if url_key already exist |
||
182 | $foundExistingUrlKey = $this->getProductUrlRewriteProcessor()->loadProductVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndPK( |
||
183 | ColumnKeys::URL_KEY, |
||
184 | $entityTypeId, |
||
185 | $storeId, |
||
186 | $pk |
||
187 | ); |
||
188 | // if url_key attribute found and same store as searched |
||
189 | if ($foundExistingUrlKey && $foundExistingUrlKey[MemberNames::STORE_ID] == $storeId) { |
||
190 | // skip for artefact as default entry |
||
191 | continue; |
||
192 | } |
||
193 | } |
||
194 | // if no arefacts are available, append new data |
||
195 | $this->createArtefact($sku, $storeViewCode); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | // append the artefacts that has to be exported to the subject |
||
200 | $this->addArtefacts($this->artefacts); |
||
201 | } |
||
202 | |||
334 |
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: