| Conditions | 7 |
| Paths | 12 |
| Total Lines | 66 |
| 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 |
||
| 106 | public function makeUnique(UrlKeyAwareSubjectInterface $subject, $urlKey) |
||
| 107 | { |
||
| 108 | |||
| 109 | // initialize the entity type ID |
||
| 110 | $entityType = $subject->getEntityType(); |
||
| 111 | $entityTypeId = (integer) $entityType[MemberNames::ENTITY_TYPE_ID]; |
||
| 112 | |||
| 113 | // initialize the store view ID, use the admin store view if no store view has |
||
| 114 | // been set, because the default url_key value has been set in admin store view |
||
| 115 | $storeId = $subject->getRowStoreId(StoreViewCodes::ADMIN); |
||
| 116 | |||
| 117 | // initialize the counter |
||
| 118 | $counter = 0; |
||
| 119 | |||
| 120 | // initialize the counters |
||
| 121 | $matchingCounters = array(); |
||
| 122 | $notMatchingCounters = array(); |
||
| 123 | |||
| 124 | // pre-initialze the URL key to query for |
||
| 125 | $value = $urlKey; |
||
| 126 | |||
| 127 | do { |
||
| 128 | // try to load the attribute |
||
| 129 | $attribute = |
||
| 130 | $this->loadVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndValue( |
||
| 131 | MemberNames::URL_KEY, |
||
| 132 | $entityTypeId, |
||
| 133 | $storeId, |
||
| 134 | $value |
||
| 135 | ); |
||
| 136 | |||
| 137 | // try to load the entity's URL key |
||
| 138 | if ($attribute) { |
||
|
|
|||
| 139 | // this IS the URL key of the passed entity |
||
| 140 | if ($subject->isUrlKeyOf($attribute)) { |
||
| 141 | $matchingCounters[] = $counter; |
||
| 142 | } else { |
||
| 143 | $notMatchingCounters[] = $counter; |
||
| 144 | } |
||
| 145 | |||
| 146 | // prepare the next URL key to query for |
||
| 147 | $value = sprintf('%s-%d', $urlKey, ++$counter); |
||
| 148 | } |
||
| 149 | } while ($attribute); |
||
| 150 | |||
| 151 | // sort the array ascending according to the counter |
||
| 152 | asort($matchingCounters); |
||
| 153 | asort($notMatchingCounters); |
||
| 154 | |||
| 155 | // this IS the URL key of the passed entity => we've an UPDATE |
||
| 156 | if (sizeof($matchingCounters) > 0) { |
||
| 157 | // load highest counter |
||
| 158 | $counter = end($matchingCounters); |
||
| 159 | // if the counter is > 0, we've to append it to the new URL key |
||
| 160 | if ($counter > 0) { |
||
| 161 | $urlKey = sprintf('%s-%d', $urlKey, $counter); |
||
| 162 | } |
||
| 163 | } elseif (sizeof($notMatchingCounters) > 0) { |
||
| 164 | // create a new URL key by raising the counter |
||
| 165 | $newCounter = end($notMatchingCounters); |
||
| 166 | $urlKey = sprintf('%s-%d', $urlKey, ++$newCounter); |
||
| 167 | } |
||
| 168 | |||
| 169 | // return the passed URL key, if NOT |
||
| 170 | return $urlKey; |
||
| 171 | } |
||
| 172 | |||
| 202 |
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.