Conditions | 8 |
Paths | 24 |
Total Lines | 56 |
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 |
||
87 | public function makeUnique(UrlKeyAwareSubjectInterface $subject, string $urlKey, string $urlPath = null) : string |
||
88 | { |
||
89 | |||
90 | // initialize the store view ID, use the default store view if no store view has |
||
91 | // been set, because the default url_key value has been set in default store view |
||
92 | $storeId = $subject->getRowStoreId(); |
||
93 | |||
94 | // initialize the counter |
||
95 | $counter = 0; |
||
96 | |||
97 | // initialize the counters |
||
98 | $matchingCounters = array(); |
||
99 | $notMatchingCounters = array(); |
||
100 | |||
101 | // pre-initialze the URL by concatenating path and/or key to query for |
||
102 | $url = $urlPath ? sprintf('%s/%s', $urlPath, $urlKey) : $urlKey; |
||
103 | |||
104 | do { |
||
105 | // try to load the attribute |
||
106 | $urlRewrite = $this->loadUrlRewriteByRequestPathAndStoreId($url, $storeId); |
||
107 | |||
108 | // try to load the entity's URL key |
||
109 | if ($urlRewrite) { |
||
|
|||
110 | // this IS the URL key of the passed entity |
||
111 | if ($subject->isUrlKeyOf($urlRewrite)) { |
||
112 | $matchingCounters[] = $counter; |
||
113 | } else { |
||
114 | $notMatchingCounters[] = $counter; |
||
115 | } |
||
116 | |||
117 | // prepare the next URL key to query for |
||
118 | $url = sprintf('%s-%d', $urlKey, ++$counter); |
||
119 | } |
||
120 | } while ($urlRewrite); |
||
121 | |||
122 | // sort the array ascending according to the counter |
||
123 | asort($matchingCounters); |
||
124 | asort($notMatchingCounters); |
||
125 | |||
126 | // this IS the URL key of the passed entity => we've an UPDATE |
||
127 | if (sizeof($matchingCounters) > 0) { |
||
128 | // load highest counter |
||
129 | $counter = end($matchingCounters); |
||
130 | // if the counter is > 0, we've to append it to the new URL key |
||
131 | if ($counter > 0) { |
||
132 | $urlKey = sprintf('%s-%d', $urlKey, $counter); |
||
133 | } |
||
134 | } elseif (sizeof($notMatchingCounters) > 0) { |
||
135 | // create a new URL key by raising the counter |
||
136 | $newCounter = end($notMatchingCounters); |
||
137 | $urlKey = sprintf('%s-%d', $urlKey, ++$newCounter); |
||
138 | } |
||
139 | |||
140 | // return the passed URL key, if NOT |
||
141 | return $urlKey; |
||
142 | } |
||
143 | |||
173 |
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.