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