| Conditions | 16 |
| Paths | 278 |
| 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 |
||
| 91 | protected function process() |
||
| 92 | { |
||
| 93 | |||
| 94 | // initialize the URL key and array for the categories |
||
| 95 | $urlKey = null; |
||
| 96 | $category = array(); |
||
| 97 | |||
| 98 | // set the entity ID for the category with the passed path |
||
| 99 | try { |
||
| 100 | $this->setIds($category = $this->getCategoryByPath($path = $this->getValue(ColumnKeys::PATH))); |
||
| 101 | } catch (\Exception $e) { |
||
| 102 | $this->setIds(array()); |
||
| 103 | } |
||
| 104 | |||
| 105 | // query whether or not the URL key column has a value |
||
| 106 | if ($this->hasValue(ColumnKeys::URL_KEY)) { |
||
| 107 | $urlKey = $this->makeUnique($this->getSubject(), $this->getValue(ColumnKeys::URL_KEY)); |
||
|
|
|||
| 108 | } else { |
||
| 109 | // query whether or not the existing category `url_key` should be re-created from the category name |
||
| 110 | if ($category && !$this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::UPDATE_URL_KEY_FROM_NAME, true)) { |
||
| 111 | // if the category already exists and NO re-creation from the category name has to |
||
| 112 | // be done, load the original `url_key`from the category and use that to proceed |
||
| 113 | $urlKey = $this->loadUrlKey($this->getSubject(), $this->getPrimaryKey()); |
||
| 114 | } |
||
| 115 | |||
| 116 | // query whether or not the an URL key for the exisitng category |
||
| 117 | // has been available, if not but the column `name` contains a |
||
| 118 | // value try to create an URL key from the category's name |
||
| 119 | if ($urlKey === '' || $urlKey === null && $this->hasValue(ColumnKeys::NAME)) { |
||
| 120 | $urlKey = $this->makeUnique($this->getSubject(), $this->convertNameToUrlKey($this->getValue(ColumnKeys::NAME))); |
||
| 121 | } |
||
| 122 | |||
| 123 | // if no URL key has benn available nor the column `name` |
||
| 124 | // contains a value, we've to throw an exception |
||
| 125 | if ($urlKey === '' || $urlKey === null) { |
||
| 126 | // throw an exception, that the URL key can not be initialized and we're in default store view |
||
| 127 | if ($this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN) === StoreViewCodes::ADMIN) { |
||
| 128 | throw new \Exception('Can\'t initialize the URL key for category "%s" because columns "url_key" or "name" have a value set for default store view', $path); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // otherwise set the URL key as value for column`url_key`, |
||
| 133 | // because we need one to process the rewrites later |
||
| 134 | $this->setValue(ColumnKeys::URL_KEY, $urlKey); |
||
| 135 | } |
||
| 136 | |||
| 137 | // explode the path into the category names |
||
| 138 | if ($categories = $this->explode($this->getValue(ColumnKeys::PATH), '/')) { |
||
| 139 | // initialize the category with the actual category's URL key |
||
| 140 | $categoryPaths = array($urlKey); |
||
| 141 | // prepare the store view code |
||
| 142 | $this->prepareStoreViewCode(); |
||
| 143 | // load ID of the actual store view |
||
| 144 | $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
||
| 145 | |||
| 146 | // iterate over the category names and try to load the category therefore |
||
| 147 | for ($i = sizeof($categories) - 1; $i > 1; $i--) { |
||
| 148 | try { |
||
| 149 | // prepare the expected category name |
||
| 150 | $categoryPath = implode('/', array_slice($categories, 0, $i)); |
||
| 151 | // load the existing category and prepend the URL key the array with the category URL keys |
||
| 152 | $existingCategory = $this->getCategoryByPkAndStoreId($this->mapPath($categoryPath), $storeId); |
||
| 153 | // query whether or not an URL key is available or not |
||
| 154 | if (isset($existingCategory[MemberNames::URL_KEY])) { |
||
| 155 | array_unshift($categoryPaths, $existingCategory[MemberNames::URL_KEY]); |
||
| 156 | } else { |
||
| 157 | $this->getSystemLogger()->debug(sprintf('Can\'t find URL key for category %s', $categoryPath)); |
||
| 158 | } |
||
| 159 | } catch (\Exception $e) { |
||
| 160 | $this->getSystemLogger()->debug(sprintf('Can\'t load parent category %s', $categoryPath)); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | // create the header for the URL path |
||
| 165 | if (!$this->hasHeader(ColumnKeys::URL_PATH)) { |
||
| 166 | $this->addHeader(ColumnKeys::URL_PATH); |
||
| 167 | } |
||
| 168 | |||
| 169 | // set the URL path |
||
| 170 | $this->setValue(ColumnKeys::URL_PATH, implode('/', $categoryPaths)); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 290 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: