| Conditions | 11 |
| Paths | 18 |
| Total Lines | 79 |
| 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 |
||
| 91 | protected function process() |
||
| 92 | { |
||
| 93 | |||
| 94 | // initialize the URL key and array for the categories |
||
| 95 | $category = array(); |
||
| 96 | |||
| 97 | // set the entity ID for the category with the passed path |
||
| 98 | try { |
||
| 99 | $this->setIds($category = $this->getCategoryByPath($this->getValue(ColumnKeys::PATH))); |
||
| 100 | } catch (\Exception $e) { |
||
| 101 | $this->setIds(array()); |
||
| 102 | } |
||
| 103 | |||
| 104 | // query whether or not the URL key column has a |
||
| 105 | // value, if yes, use the value from the column |
||
| 106 | if ($this->hasValue(ColumnKeys::URL_KEY)) { |
||
| 107 | $urlKey = $this->getValue(ColumnKeys::URL_KEY); |
||
| 108 | } else { |
||
| 109 | // query whether or not the column `url_key` has a value |
||
| 110 | if ($category && |
||
|
|
|||
| 111 | $this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::UPDATE_URL_KEY_FROM_NAME) && |
||
| 112 | !$this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::UPDATE_URL_KEY_FROM_NAME, true) |
||
| 113 | ) { |
||
| 114 | // product already exists and NO new URL key |
||
| 115 | // has been specified in column `url_key`, so |
||
| 116 | // we stop processing here |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | // initialize the URL key with the converted name |
||
| 120 | $urlKey = $this->convertNameToUrlKey($this->getValue(ColumnKeys::NAME)); |
||
| 121 | } |
||
| 122 | |||
| 123 | // prepare the store view code |
||
| 124 | $this->prepareStoreViewCode(); |
||
| 125 | |||
| 126 | // load ID of the actual store view |
||
| 127 | $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
||
| 128 | |||
| 129 | // explode the path into the category names |
||
| 130 | if ($categories = $this->explode($this->getValue(ColumnKeys::PATH), '/')) { |
||
| 131 | // initialize the array for the category paths |
||
| 132 | $categoryPaths = array(); |
||
| 133 | // iterate over the parent category names and try |
||
| 134 | // to load the categories to build the URL path |
||
| 135 | for ($i = sizeof($categories) - 1; $i > 1; $i--) { |
||
| 136 | try { |
||
| 137 | // prepare the expected category name |
||
| 138 | $categoryPath = implode('/', array_slice($categories, 0, $i)); |
||
| 139 | // load the existing category and prepend the URL key the array with the category URL keys |
||
| 140 | $existingCategory = $this->getCategoryByPkAndStoreId($this->mapPath($categoryPath), $storeId); |
||
| 141 | // query whether or not an URL key is available or not |
||
| 142 | if (isset($existingCategory[MemberNames::URL_KEY])) { |
||
| 143 | array_unshift($categoryPaths, $existingCategory[MemberNames::URL_KEY]); |
||
| 144 | } else { |
||
| 145 | $this->getSystemLogger()->debug(sprintf('Can\'t find URL key for category "%s"', $categoryPath)); |
||
| 146 | } |
||
| 147 | } catch (\Exception $e) { |
||
| 148 | $this->getSystemLogger()->debug(sprintf('Can\'t load parent category "%s"', $categoryPath)); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | // update the URL key with the unique value |
||
| 154 | $this->setValue( |
||
| 155 | ColumnKeys::URL_KEY, |
||
| 156 | $urlKey = $this->makeUnique($this->getSubject(), $urlKey, implode('/', $categoryPaths)) |
||
| 157 | ); |
||
| 158 | |||
| 159 | // finally, append the URL key as last element to the path |
||
| 160 | array_push($categoryPaths, $urlKey); |
||
| 161 | |||
| 162 | // create the virtual column for the URL path |
||
| 163 | if ($this->hasHeader(ColumnKeys::URL_PATH) === false) { |
||
| 164 | $this->addHeader(ColumnKeys::URL_PATH); |
||
| 165 | } |
||
| 166 | |||
| 167 | // set the URL path |
||
| 168 | $this->setValue(ColumnKeys::URL_PATH, implode('/', $categoryPaths)); |
||
| 169 | } |
||
| 170 | |||
| 264 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.